|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -import chokidar from 'chokidar'; |
18 | | -import type { FSWatcher as ChokidarWatcher } from 'chokidar'; |
| 17 | +import fs from 'fs'; |
| 18 | +import path from 'path'; |
19 | 19 |
|
20 | | -export type FSEvent = { event: 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir', file: string }; |
| 20 | +export type FSWatcherParams = { |
| 21 | + // Directories are watched recursively, files individually. |
| 22 | + paths: string[]; |
| 23 | + // Never reported, never entered. |
| 24 | + ignoredFolders: string[]; |
| 25 | +}; |
| 26 | + |
| 27 | +// Node.js watches recursively with FSEvents on macOS and ReadDirectoryChangesW on Windows, |
| 28 | +// a single handle per root. On other platforms it falls back to a JavaScript implementation |
| 29 | +// that watches every single file and does not skip node_modules, so we walk the tree ourselves |
| 30 | +// and watch each directory instead. Node 26 adds an `ignore` option to that fallback, so once |
| 31 | +// it is the minimum supported version, the walker can go. |
| 32 | +const kNativeRecursiveWatch = process.platform === 'darwin' || process.platform === 'win32'; |
21 | 33 |
|
22 | 34 | export class FSWatcher { |
23 | | - private _onChange: (events: FSEvent[]) => void; |
24 | | - private _watchedPaths: string[] = []; |
25 | | - private _ignoredFolders: string[] = []; |
26 | | - private _collector: FSEvent[] = []; |
27 | | - private _fsWatcher: ChokidarWatcher | undefined; |
| 35 | + private _onChange: (files: string[]) => void; |
| 36 | + private _params: FSWatcherParams = { paths: [], ignoredFolders: [] }; |
| 37 | + private _watchedFiles = new Set<string>(); |
| 38 | + private _watchers = new Map<string, fs.FSWatcher>(); |
| 39 | + private _collector = new Set<string>(); |
28 | 40 | private _throttleTimer: NodeJS.Timeout | undefined; |
29 | 41 |
|
30 | | - constructor(onChange: (events: FSEvent[]) => void) { |
| 42 | + constructor(onChange: (files: string[]) => void) { |
31 | 43 | this._onChange = onChange; |
32 | 44 | } |
33 | 45 |
|
34 | | - async update(watchedPaths: string[], ignoredFolders: string[], reportPending: boolean) { |
35 | | - if (JSON.stringify([this._watchedPaths, this._ignoredFolders]) === JSON.stringify([watchedPaths, ignoredFolders])) |
| 46 | + async update(params: FSWatcherParams, reportPending: boolean) { |
| 47 | + if (JSON.stringify(this._params) === JSON.stringify(params)) |
36 | 48 | return; |
37 | 49 |
|
38 | 50 | if (reportPending) |
39 | 51 | this._reportEventsIfAny(); |
40 | 52 |
|
41 | | - this._watchedPaths = watchedPaths; |
42 | | - this._ignoredFolders = ignoredFolders; |
43 | | - void this._fsWatcher?.close(); |
44 | | - this._fsWatcher = undefined; |
45 | | - this._collector.length = 0; |
| 53 | + this._params = params; |
| 54 | + this._closeAll(); |
| 55 | + this._collector.clear(); |
46 | 56 | clearTimeout(this._throttleTimer); |
47 | 57 | this._throttleTimer = undefined; |
48 | 58 |
|
49 | | - if (!this._watchedPaths.length) |
50 | | - return; |
| 59 | + // The test dir may come with a trailing separator. |
| 60 | + const paths = params.paths.map(p => p.length > 1 && p.endsWith(path.sep) ? p.slice(0, -1) : p); |
| 61 | + const dirs = new Set(paths.filter(p => isDirectory(p))); |
| 62 | + // Anything inside a watched directory is covered by its recursive watch. |
| 63 | + const isCovered = (p: string) => [...dirs].some(dir => isInside(dir, p)); |
| 64 | + |
| 65 | + for (const dir of dirs) { |
| 66 | + if (isCovered(dir)) |
| 67 | + continue; |
| 68 | + if (kNativeRecursiveWatch) |
| 69 | + this._watch(dir, true, file => this._onDirEvent(file)); |
| 70 | + else |
| 71 | + this._watchTree(dir); |
| 72 | + } |
51 | 73 |
|
52 | | - // Chokidar calls the matcher with paths normalized to forward slashes. |
53 | | - const ignoredPrefixes = this._ignoredFolders.map(folder => folder.replace(/\\/g, '/') + '/'); |
54 | | - const ignored = (file: string) => { |
55 | | - if (file.split('/').includes('node_modules')) |
56 | | - return true; |
57 | | - return ignoredPrefixes.some(prefix => (file + '/').startsWith(prefix)); |
58 | | - }; |
59 | | - this._fsWatcher = chokidar.watch(watchedPaths, { ignoreInitial: true, ignored }).on('all', async (event, file) => { |
60 | | - if (event !== 'add' && event !== 'addDir' && event !== 'change' && event !== 'unlink' && event !== 'unlinkDir') |
61 | | - return; |
62 | | - if (this._throttleTimer) |
63 | | - clearTimeout(this._throttleTimer); |
64 | | - this._collector.push({ event, file }); |
65 | | - this._throttleTimer = setTimeout(() => this._reportEventsIfAny(), 250); |
66 | | - }); |
67 | | - |
68 | | - await new Promise<void>((resolve, reject) => this._fsWatcher!.once('ready', () => resolve()).once('error', reject)); |
| 74 | + // Files are watched through their parent directory, so that editors |
| 75 | + // that replace the file on save do not break the watcher. |
| 76 | + this._watchedFiles = new Set(paths.filter(p => !dirs.has(p) && !isCovered(p))); |
| 77 | + for (const dir of new Set([...this._watchedFiles].map(file => path.dirname(file)))) |
| 78 | + this._watch(dir, false, file => this._onFileEvent(file)); |
69 | 79 | } |
70 | 80 |
|
71 | 81 | async close() { |
72 | | - await this._fsWatcher?.close(); |
| 82 | + this._closeAll(); |
| 83 | + clearTimeout(this._throttleTimer); |
| 84 | + this._throttleTimer = undefined; |
| 85 | + } |
| 86 | + |
| 87 | + private _watch(dir: string, recursive: boolean, onEvent: (file: string) => void) { |
| 88 | + if (this._watchers.has(dir)) |
| 89 | + return; |
| 90 | + try { |
| 91 | + const watcher = fs.watch(dir, { persistent: true, recursive }, (eventType, filename) => { |
| 92 | + onEvent(filename ? path.join(dir, filename) : dir); |
| 93 | + }); |
| 94 | + watcher.on('error', () => this._close(dir)); |
| 95 | + this._watchers.set(dir, watcher); |
| 96 | + } catch { |
| 97 | + // The directory does not exist, is not accessible, or we have run out of watches. |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private _watchTree(dir: string) { |
| 102 | + if (this._isIgnored(dir) || this._watchers.has(dir)) |
| 103 | + return; |
| 104 | + this._watch(dir, false, file => this._onTreeEvent(file)); |
| 105 | + if (!this._watchers.has(dir)) |
| 106 | + return; |
| 107 | + let entries: fs.Dirent[] = []; |
| 108 | + try { |
| 109 | + entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 110 | + } catch { |
| 111 | + } |
| 112 | + for (const entry of entries) { |
| 113 | + // Dirent.isDirectory() is false for symlinks, so they are not followed. |
| 114 | + if (entry.isDirectory()) |
| 115 | + this._watchTree(path.join(dir, entry.name)); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private _onDirEvent(file: string) { |
| 120 | + if (!this._isIgnored(file)) |
| 121 | + this._collect(file); |
| 122 | + } |
| 123 | + |
| 124 | + private _onTreeEvent(file: string) { |
| 125 | + if (this._isIgnored(file)) |
| 126 | + return; |
| 127 | + this._collect(file); |
| 128 | + // A directory may have been added, removed or replaced. |
| 129 | + if (this._watchers.has(file)) { |
| 130 | + if (!isDirectory(file)) |
| 131 | + this._closeTree(file); |
| 132 | + } else if (isDirectory(file)) { |
| 133 | + this._watchTree(file); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private _onFileEvent(file: string) { |
| 138 | + if (this._watchedFiles.has(file)) |
| 139 | + this._collect(file); |
| 140 | + } |
| 141 | + |
| 142 | + private _isIgnored(file: string) { |
| 143 | + if (file.split(path.sep).includes('node_modules')) |
| 144 | + return true; |
| 145 | + return this._params.ignoredFolders.some(folder => file === folder || isInside(folder, file)); |
| 146 | + } |
| 147 | + |
| 148 | + private _collect(file: string) { |
| 149 | + this._collector.add(file); |
| 150 | + clearTimeout(this._throttleTimer); |
| 151 | + this._throttleTimer = setTimeout(() => this._reportEventsIfAny(), 250); |
73 | 152 | } |
74 | 153 |
|
75 | 154 | private _reportEventsIfAny() { |
76 | | - if (this._collector.length) |
77 | | - this._onChange(this._collector.slice()); |
78 | | - this._collector.length = 0; |
| 155 | + if (this._collector.size) |
| 156 | + this._onChange([...this._collector]); |
| 157 | + this._collector.clear(); |
| 158 | + } |
| 159 | + |
| 160 | + private _close(dir: string) { |
| 161 | + this._watchers.get(dir)?.close(); |
| 162 | + this._watchers.delete(dir); |
| 163 | + } |
| 164 | + |
| 165 | + private _closeTree(dir: string) { |
| 166 | + for (const watched of [...this._watchers.keys()]) { |
| 167 | + if (watched === dir || isInside(dir, watched)) |
| 168 | + this._close(watched); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private _closeAll() { |
| 173 | + for (const watcher of this._watchers.values()) |
| 174 | + watcher.close(); |
| 175 | + this._watchers.clear(); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +function isInside(dir: string, file: string) { |
| 180 | + return file.startsWith(dir + path.sep); |
| 181 | +} |
| 182 | + |
| 183 | +function isDirectory(file: string) { |
| 184 | + try { |
| 185 | + return fs.lstatSync(file).isDirectory(); |
| 186 | + } catch { |
| 187 | + return false; |
79 | 188 | } |
80 | 189 | } |
0 commit comments