-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathwatcher.ts
135 lines (116 loc) · 3.19 KB
/
watcher.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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import WatchPack from 'watchpack';
export class ChangedFiles {
readonly added = new Set<string>();
readonly modified = new Set<string>();
readonly removed = new Set<string>();
get all(): string[] {
return [...this.added, ...this.modified, ...this.removed];
}
toDebugString(): string {
const content = {
added: Array.from(this.added),
modified: Array.from(this.modified),
removed: Array.from(this.removed),
};
return JSON.stringify(content, null, 2);
}
}
export interface BuildWatcher extends AsyncIterableIterator<ChangedFiles> {
add(paths: string | readonly string[]): void;
remove(paths: string | readonly string[]): void;
close(): Promise<void>;
}
export function createWatcher(options?: {
polling?: boolean;
interval?: number;
ignored?: string[];
followSymlinks?: boolean;
}): BuildWatcher {
const watcher = new WatchPack({
poll: options?.polling ? options?.interval ?? true : false,
ignored: options?.ignored,
followSymlinks: options?.followSymlinks,
aggregateTimeout: 250,
});
const watchedFiles = new Set<string>();
const nextQueue: ((value?: ChangedFiles) => void)[] = [];
let currentChangedFiles: ChangedFiles | undefined;
watcher.on('aggregated', (changes, removals) => {
const changedFiles = currentChangedFiles ?? new ChangedFiles();
for (const file of changes) {
changedFiles.modified.add(file);
}
for (const file of removals) {
changedFiles.removed.add(file);
}
const next = nextQueue.shift();
if (next) {
currentChangedFiles = undefined;
next(changedFiles);
} else {
currentChangedFiles = changedFiles;
}
});
return {
[Symbol.asyncIterator]() {
return this;
},
async next() {
if (currentChangedFiles && nextQueue.length === 0) {
const result = { value: currentChangedFiles };
currentChangedFiles = undefined;
return result;
}
return new Promise((resolve) => {
nextQueue.push((value) => resolve(value ? { value } : { done: true, value }));
});
},
add(paths) {
const previousSize = watchedFiles.size;
if (typeof paths === 'string') {
watchedFiles.add(paths);
} else {
for (const file of paths) {
watchedFiles.add(file);
}
}
if (previousSize !== watchedFiles.size) {
watcher.watch({
files: watchedFiles,
});
}
},
remove(paths) {
const previousSize = watchedFiles.size;
if (typeof paths === 'string') {
watchedFiles.delete(paths);
} else {
for (const file of paths) {
watchedFiles.delete(file);
}
}
if (previousSize !== watchedFiles.size) {
watcher.watch({
files: watchedFiles,
});
}
},
async close() {
try {
watcher.close();
} finally {
let next;
while ((next = nextQueue.shift()) !== undefined) {
next();
}
}
},
};
}