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

Commit

Permalink
fix(watch): fallback for when chokidar watch ready/error don't fire (…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
danbucholtz committed Nov 11, 2016
1 parent ca6c889 commit 519cd7f
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ function startWatchers(context: BuildContext, configFile: string) {

const promises = watchConfig
.watchers
.map((w, i) => startWatcher(i, w, context, watchConfig));
.map((w, i) => {
return startWatcher(i, w, context, watchConfig);
});

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

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

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

chokidarWatcher.on('ready', () => {
clearTimeout(timeoutId);
Logger.debug(`watcher ready: ${watcher.options.cwd}${watcher.paths}`);
resolve();
});

chokidarWatcher.on('error', (err: any) => {
clearTimeout(timeoutId);
reject(new BuildError(`watcher error: ${watcher.options.cwd}${watcher.paths}: ${err}`));
});


});
}

Expand Down

0 comments on commit 519cd7f

Please sign in to comment.