-
Notifications
You must be signed in to change notification settings - Fork 343
/
Copy pathwatcher.js
90 lines (74 loc) · 2.41 KB
/
watcher.js
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
import { existsSync, lstatSync } from 'fs';
import Watchpack from 'watchpack';
import debounce from 'debounce';
import { UsageError } from './errors.js';
import { createLogger } from './util/logger.js';
const log = createLogger(import.meta.url);
// onSourceChange types and implementation
export default function onSourceChange({
sourceDir,
watchFile,
watchIgnored,
artifactsDir,
onChange,
shouldWatchFile,
debounceTime = 500,
}) {
// When running on Windows, transform the ignored paths and globs
// as Watchpack does translate the changed files path internally
// (See https://github.com/webpack/watchpack/blob/v2.1.1/lib/DirectoryWatcher.js#L99-L103).
const ignored =
watchIgnored && process.platform === 'win32'
? watchIgnored.map((it) => it.replace(/\\/g, '/'))
: watchIgnored;
// TODO: For network disks, we would need to add {poll: true}.
const watcher = ignored ? new Watchpack({ ignored }) : new Watchpack();
// Allow multiple files to be changed before reloading the extension
const executeImmediately = false;
onChange = debounce(onChange, debounceTime, executeImmediately);
watcher.on('change', (filePath) => {
proxyFileChanges({ artifactsDir, onChange, filePath, shouldWatchFile });
});
log.debug(
`Watching ${watchFile ? watchFile.join(',') : sourceDir} for changes`,
);
const watchedDirs = [];
const watchedFiles = [];
if (watchFile) {
for (const filePath of watchFile) {
if (existsSync(filePath) && !lstatSync(filePath).isFile()) {
throw new UsageError(
'Invalid --watch-file value: ' + `"${filePath}" is not a file.`,
);
}
watchedFiles.push(filePath);
}
} else {
watchedDirs.push(sourceDir);
}
watcher.watch({
files: watchedFiles,
directories: watchedDirs,
missing: [],
startTime: Date.now(),
});
// TODO: support interrupting the watcher on Windows.
// https://github.com/mozilla/web-ext/issues/225
process.on('SIGINT', () => watcher.close());
return watcher;
}
// proxyFileChanges types and implementation.
export function proxyFileChanges({
artifactsDir,
onChange,
filePath,
shouldWatchFile,
}) {
if (filePath.indexOf(artifactsDir) === 0 || !shouldWatchFile(filePath)) {
log.debug(`Ignoring change to: ${filePath}`);
} else {
log.debug(`Changed: ${filePath}`);
log.debug(`Last change detection: ${new Date().toTimeString()}`);
onChange();
}
}