From 519cd7ff68f504a3efb8627c311aea23bec24744 Mon Sep 17 00:00:00 2001 From: Dan Bucholtz Date: Fri, 11 Nov 2016 12:08:19 -0600 Subject: [PATCH] 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 --- src/watch.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/watch.ts b/src/watch.ts index fbd8009f..61e285d3 100644 --- a/src/watch.ts +++ b/src/watch.ts @@ -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); } @@ -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) { @@ -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}`)); }); + + }); }