-
Notifications
You must be signed in to change notification settings - Fork 62
/
watch-handler.ts
177 lines (143 loc) · 5.36 KB
/
watch-handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import type { IFileSystem, IWatchEvent, WatchEventListener } from '@file-services/types';
import type { Stylable, StylableResolverCache } from '@stylable/core';
import type { BuildContext } from './types';
import decache from 'decache';
import {
createWatchEvent,
DirectoryProcessService,
} from './directory-process-service/directory-process-service';
import { createDefaultLogger, levels, Log } from './logger';
import { buildMessages } from './messages';
import { DiagnosticsManager } from './diagnostics-manager';
export interface WatchHandlerOptions {
log?: Log;
resolverCache?: StylableResolverCache;
outputFiles?: BuildContext['outputFiles'];
rootDir?: string;
diagnosticsManager?: DiagnosticsManager;
}
export interface Build {
service: DirectoryProcessService;
identifier: string;
stylable: Stylable;
}
export interface RegisteredBuild extends Build {
generatedFiles: Set<string>;
}
type File = {
generated?: boolean;
} & IWatchEvent;
export class WatchHandler {
private builds: Build[] = [];
private resolverCache: StylableResolverCache;
private log: Log;
private diagnosticsManager: DiagnosticsManager;
private generatedFiles = new Set<string>();
constructor(private fileSystem: IFileSystem, private options: WatchHandlerOptions = {}) {
this.resolverCache = this.options.resolverCache ?? new Map();
this.log = this.options.log ?? createDefaultLogger();
this.diagnosticsManager =
this.options.diagnosticsManager ?? new DiagnosticsManager({ log: this.log });
}
public readonly listener = async (event: IWatchEvent) => {
this.log(buildMessages.CHANGE_EVENT_TRIGGERED(event.path));
if (this.generatedFiles.has(event.path)) {
this.log(buildMessages.SKIP_GENERATED_FILE(event.path));
return;
}
this.invalidateCache(event.path);
let foundChanges = false;
const files = new Map<string, File>();
for (const { service, identifier } of this.builds) {
for (const path of service.getAffectedFiles(event.path)) {
if (files.has(path)) {
continue;
}
files.set(path, createWatchEvent(path, this.fileSystem));
}
const { hasChanges, generatedFiles } = await service.handleWatchChange(files, event);
if (hasChanges) {
if (!foundChanges) {
foundChanges = true;
this.generatedFiles.clear();
this.log(levels.clear);
this.log(buildMessages.CHANGE_DETECTED(event.path), levels.info);
}
for (const generatedFile of generatedFiles) {
this.generatedFiles.add(generatedFile);
files.set(generatedFile, {
...(files.get(generatedFile) ||
createWatchEvent(generatedFile, this.fileSystem)),
generated: true,
});
}
this.log(buildMessages.BUILD_PROCESS_INFO(identifier), Array.from(files.keys()));
}
}
if (foundChanges) {
const { changed, deleted } = filesStats(files);
this.log(buildMessages.WATCH_SUMMARY(changed, deleted), levels.info);
const reported = this.diagnosticsManager.report();
if (!reported) {
this.log(
buildMessages.NO_DIAGNOSTICS(),
buildMessages.CONTINUE_WATCH(),
levels.info
);
} else {
this.log(buildMessages.CONTINUE_WATCH(), levels.info);
}
}
};
public register({ generatedFiles, ...build }: RegisteredBuild) {
this.builds.push(build);
for (const file of generatedFiles) {
this.generatedFiles.add(file);
}
}
public start() {
this.log(buildMessages.START_WATCHING(), levels.info);
this.fileSystem.watchService.addGlobalListener(this.listener as WatchEventListener);
}
public async stop() {
this.log(buildMessages.STOP_WATCHING(), levels.info);
this.diagnosticsManager.clear();
this.fileSystem.watchService.removeGlobalListener(this.listener as WatchEventListener);
for (const { service } of this.builds) {
await service.dispose();
}
this.builds = [];
}
private invalidateCache(filePath: string) {
for (const [key, entity] of this.resolverCache) {
if (
!entity.value ||
entity.resolvedPath === filePath ||
// deep source invalidation
this.options.outputFiles?.get(entity.resolvedPath)?.has(filePath)
) {
if (entity.kind === 'js') {
decache(filePath);
}
this.resolverCache.delete(key);
}
}
}
}
function filesStats(files: Map<string, File>) {
const filesChangesSummary = {
changed: 0,
deleted: 0,
};
for (const file of files.values()) {
if (file.generated) {
continue;
}
if (file.stats) {
filesChangesSummary.changed++;
} else {
filesChangesSummary.deleted++;
}
}
return filesChangesSummary;
}