Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 519cd7f

Browse files
committed
fix(watch): fallback for when chokidar watch ready/error don't fire (happens on windows when file is
fallback for when chokidar watch ready/error don't fire (happens on windows when file is missing) closes #282
1 parent ca6c889 commit 519cd7f

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/watch.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ function startWatchers(context: BuildContext, configFile: string) {
4343

4444
const promises = watchConfig
4545
.watchers
46-
.map((w, i) => startWatcher(i, w, context, watchConfig));
46+
.map((w, i) => {
47+
return startWatcher(i, w, context, watchConfig);
48+
});
4749

4850
return Promise.all(promises);
4951
}
@@ -52,6 +54,20 @@ function startWatchers(context: BuildContext, configFile: string) {
5254
function startWatcher(index: number, watcher: Watcher, context: BuildContext, watchConfig: WatchConfig) {
5355
return new Promise((resolve, reject) => {
5456

57+
// If a file isn't found (probably other scenarios too),
58+
// Chokidar watches don't always trigger the ready or error events
59+
// so set a timeout, and clear it if they do fire
60+
// otherwise, just reject the promise and log an error
61+
const timeoutId = setTimeout(() => {
62+
let filesWatchedString: string = null;
63+
if (typeof watcher.paths === 'string') {
64+
filesWatchedString = watcher.paths;
65+
} else if (Array.isArray(watcher.paths)) {
66+
filesWatchedString = watcher.paths.join(', ');
67+
}
68+
//Logger.error(`A watch configured to watch the following paths failed to start. It likely that a file referenced does not exist: ${filesWatchedString}`);
69+
reject(new BuildError(`A watch configured to watch the following paths failed to start. It likely that a file referenced does not exist: ${filesWatchedString}`));
70+
}, 3000);
5571
prepareWatcher(context, watcher);
5672

5773
if (!watcher.paths) {
@@ -105,13 +121,17 @@ function startWatcher(index: number, watcher: Watcher, context: BuildContext, wa
105121
});
106122

107123
chokidarWatcher.on('ready', () => {
124+
clearTimeout(timeoutId);
108125
Logger.debug(`watcher ready: ${watcher.options.cwd}${watcher.paths}`);
109126
resolve();
110127
});
111128

112129
chokidarWatcher.on('error', (err: any) => {
130+
clearTimeout(timeoutId);
113131
reject(new BuildError(`watcher error: ${watcher.options.cwd}${watcher.paths}: ${err}`));
114132
});
133+
134+
115135
});
116136
}
117137

0 commit comments

Comments
 (0)