-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
ControlledTypeScriptSystem.ts
320 lines (283 loc) · 10.7 KB
/
ControlledTypeScriptSystem.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import type * as ts from 'typescript';
import { dirname, join } from 'path';
import { createPassiveFileSystem } from '../file-system/PassiveFileSystem';
import forwardSlash from '../../utils/path/forwardSlash';
import { createRealFileSystem } from '../file-system/RealFileSystem';
import { createMemFileSystem } from '../file-system/MemFileSystem';
import { FilesMatch } from '../../reporter';
interface ControlledTypeScriptSystem extends ts.System {
// control watcher
invokeFileCreated(path: string): void;
invokeFileChanged(path: string): void;
invokeFileDeleted(path: string): void;
// control cache
clearCache(): void;
// mark these methods as defined - not optional
getFileSize(path: string): number;
watchFile(
path: string,
callback: ts.FileWatcherCallback,
pollingInterval?: number,
options?: ts.WatchOptions
): ts.FileWatcher;
watchDirectory(
path: string,
callback: ts.DirectoryWatcherCallback,
recursive?: boolean,
options?: ts.WatchOptions
): ts.FileWatcher;
getModifiedTime(path: string): Date | undefined;
setModifiedTime(path: string, time: Date): void;
deleteFile(path: string): void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
clearTimeout(timeoutId: any): void;
// detect when all tasks scheduled by `setTimeout` finished
waitForQueued(): Promise<void>;
setArtifacts(artifacts: FilesMatch): void;
}
type FileSystemMode = 'readonly' | 'write-tsbuildinfo' | 'write-references';
function createControlledTypeScriptSystem(
typescript: typeof ts,
mode: FileSystemMode = 'readonly'
): ControlledTypeScriptSystem {
let artifacts: FilesMatch = {
files: [],
dirs: [],
excluded: [],
extensions: [],
};
let isInitialRun = true;
// watchers
const fileWatcherCallbacksMap = new Map<string, ts.FileWatcherCallback[]>();
const directoryWatcherCallbacksMap = new Map<string, ts.DirectoryWatcherCallback[]>();
const recursiveDirectoryWatcherCallbacksMap = new Map<string, ts.DirectoryWatcherCallback[]>();
const deletedFiles = new Map<string, boolean>();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const timeoutCallbacks = new Set<any>();
// always use case-sensitive as normalization to lower-case can be a problem for some
// third-party libraries, like fsevents
const caseSensitive = true;
const realFileSystem = createRealFileSystem(caseSensitive);
const memFileSystem = createMemFileSystem(realFileSystem);
const passiveFileSystem = createPassiveFileSystem(memFileSystem, realFileSystem);
// based on the ts.ignorePaths
const ignoredPaths = ['/node_modules/.', '/.git', '/.#'];
function createWatcher<TCallback>(
watchersMap: Map<string, TCallback[]>,
path: string,
callback: TCallback
) {
const normalizedPath = realFileSystem.normalizePath(path);
const watchers = watchersMap.get(normalizedPath) || [];
const nextWatchers = [...watchers, callback];
watchersMap.set(normalizedPath, nextWatchers);
return {
close: () => {
const watchers = watchersMap.get(normalizedPath) || [];
const nextWatchers = watchers.filter((watcher) => watcher !== callback);
if (nextWatchers.length > 0) {
watchersMap.set(normalizedPath, nextWatchers);
} else {
watchersMap.delete(normalizedPath);
}
},
};
}
function invokeFileWatchers(path: string, event: ts.FileWatcherEventKind) {
const normalizedPath = realFileSystem.normalizePath(path);
if (normalizedPath.endsWith('.js')) {
// trigger relevant .d.ts file watcher - handles the case, when we have webpack watcher
// that points to a symlinked package
invokeFileWatchers(normalizedPath.slice(0, -3) + '.d.ts', event);
}
const fileWatcherCallbacks = fileWatcherCallbacksMap.get(normalizedPath);
if (fileWatcherCallbacks) {
// typescript expects normalized paths with posix forward slash
fileWatcherCallbacks.forEach((fileWatcherCallback) =>
fileWatcherCallback(forwardSlash(normalizedPath), event)
);
}
}
function invokeDirectoryWatchers(path: string) {
const normalizedPath = realFileSystem.normalizePath(path);
const directory = dirname(normalizedPath);
if (ignoredPaths.some((ignoredPath) => forwardSlash(normalizedPath).includes(ignoredPath))) {
return;
}
const directoryWatcherCallbacks = directoryWatcherCallbacksMap.get(directory);
if (directoryWatcherCallbacks) {
directoryWatcherCallbacks.forEach((directoryWatcherCallback) =>
directoryWatcherCallback(forwardSlash(normalizedPath))
);
}
recursiveDirectoryWatcherCallbacksMap.forEach(
(recursiveDirectoryWatcherCallbacks, watchedDirectory) => {
if (
watchedDirectory === directory ||
(directory.startsWith(watchedDirectory) &&
forwardSlash(directory)[watchedDirectory.length] === '/')
) {
recursiveDirectoryWatcherCallbacks.forEach((recursiveDirectoryWatcherCallback) =>
recursiveDirectoryWatcherCallback(forwardSlash(normalizedPath))
);
}
}
);
}
function isArtifact(path: string) {
return (
(artifacts.dirs.some((dir) => path.includes(dir)) ||
artifacts.files.some((file) => path === file)) &&
artifacts.extensions.some((extension) => path.endsWith(extension))
);
}
function getReadFileSystem(path: string) {
if ((mode === 'readonly' || mode === 'write-tsbuildinfo') && isArtifact(path)) {
if (isInitialRun && !memFileSystem.exists(path) && passiveFileSystem.exists(path)) {
// copy file to memory on initial run
const stats = passiveFileSystem.readStats(path);
if (stats?.isFile()) {
const content = passiveFileSystem.readFile(path);
if (content) {
memFileSystem.writeFile(path, content);
memFileSystem.updateTimes(path, stats.atime, stats.mtime);
}
}
}
return memFileSystem;
}
return passiveFileSystem;
}
function getWriteFileSystem(path: string) {
if (
mode === 'write-references' ||
(mode === 'write-tsbuildinfo' && path.endsWith('.tsbuildinfo'))
) {
return realFileSystem;
}
return passiveFileSystem;
}
const controlledSystem: ControlledTypeScriptSystem = {
...typescript.sys,
useCaseSensitiveFileNames: caseSensitive,
realpath(path: string): string {
return getReadFileSystem(path).realPath(path);
},
fileExists(path: string): boolean {
const stats = getReadFileSystem(path).readStats(path);
return !!stats && stats.isFile();
},
readFile(path: string, encoding?: string): string | undefined {
return getReadFileSystem(path).readFile(path, encoding);
},
getFileSize(path: string): number {
const stats = getReadFileSystem(path).readStats(path);
return stats ? stats.size : 0;
},
writeFile(path: string, data: string): void {
getWriteFileSystem(path).writeFile(path, data);
controlledSystem.invokeFileChanged(path);
},
deleteFile(path: string): void {
getWriteFileSystem(path).deleteFile(path);
controlledSystem.invokeFileDeleted(path);
},
directoryExists(path: string): boolean {
return Boolean(getReadFileSystem(path).readStats(path)?.isDirectory());
},
createDirectory(path: string): void {
getWriteFileSystem(path).createDir(path);
invokeDirectoryWatchers(path);
},
getDirectories(path: string): string[] {
const dirents = getReadFileSystem(path).readDir(path);
return dirents
.filter(
(dirent) =>
dirent.isDirectory() ||
(dirent.isSymbolicLink() && controlledSystem.directoryExists(join(path, dirent.name)))
)
.map((dirent) => dirent.name);
},
getModifiedTime(path: string): Date | undefined {
const stats = getReadFileSystem(path).readStats(path);
if (stats) {
return stats.mtime;
}
},
setModifiedTime(path: string, date: Date): void {
getWriteFileSystem(path).updateTimes(path, date, date);
invokeDirectoryWatchers(path);
invokeFileWatchers(path, typescript.FileWatcherEventKind.Changed);
},
watchFile(path: string, callback: ts.FileWatcherCallback): ts.FileWatcher {
return createWatcher(fileWatcherCallbacksMap, path, callback);
},
watchDirectory(
path: string,
callback: ts.DirectoryWatcherCallback,
recursive = false
): ts.FileWatcher {
return createWatcher(
recursive ? recursiveDirectoryWatcherCallbacksMap : directoryWatcherCallbacksMap,
path,
callback
);
},
// use immediate instead of timeout to avoid waiting 250ms for batching files changes
setTimeout: (callback, timeout, ...args) => {
const timeoutId = setImmediate(() => {
callback(...args);
timeoutCallbacks.delete(timeoutId);
});
timeoutCallbacks.add(timeoutId);
return timeoutId;
},
clearTimeout: (timeoutId) => {
clearImmediate(timeoutId);
timeoutCallbacks.delete(timeoutId);
},
async waitForQueued(): Promise<void> {
while (timeoutCallbacks.size > 0) {
await new Promise((resolve) => setImmediate(resolve));
}
isInitialRun = false;
},
invokeFileCreated(path: string) {
const normalizedPath = realFileSystem.normalizePath(path);
invokeFileWatchers(path, typescript.FileWatcherEventKind.Created);
invokeDirectoryWatchers(normalizedPath);
deletedFiles.set(normalizedPath, false);
},
invokeFileChanged(path: string) {
const normalizedPath = realFileSystem.normalizePath(path);
if (deletedFiles.get(normalizedPath) || !fileWatcherCallbacksMap.has(normalizedPath)) {
invokeFileWatchers(path, typescript.FileWatcherEventKind.Created);
invokeDirectoryWatchers(normalizedPath);
deletedFiles.set(normalizedPath, false);
} else {
invokeFileWatchers(path, typescript.FileWatcherEventKind.Changed);
}
},
invokeFileDeleted(path: string) {
const normalizedPath = realFileSystem.normalizePath(path);
if (!deletedFiles.get(normalizedPath)) {
invokeFileWatchers(path, typescript.FileWatcherEventKind.Deleted);
invokeDirectoryWatchers(path);
deletedFiles.set(normalizedPath, true);
}
},
clearCache() {
realFileSystem.clearCache();
memFileSystem.clearCache();
passiveFileSystem.clearCache();
},
setArtifacts(nextArtifacts: FilesMatch) {
artifacts = nextArtifacts;
},
};
return controlledSystem;
}
export { createControlledTypeScriptSystem, ControlledTypeScriptSystem };