diff --git a/lib/monitor/watch.js b/lib/monitor/watch.js index 60e4c712..e0664764 100644 --- a/lib/monitor/watch.js +++ b/lib/monitor/watch.js @@ -39,85 +39,91 @@ function watch() { var promises = []; var watchedFiles = []; - dirs.forEach(function (dir) { - var promise = new Promise(function (resolve) { - var dotFilePattern = /[/\\]\./; - var ignored = Array.from(rootIgnored); - - // don't ignore dotfiles if explicitly watched. - if (!dir.match(dotFilePattern)) { - ignored.push(dotFilePattern); - } + const promise = new Promise(function (resolve) { + const dotFilePattern = /[/\\]\./; + var ignored = Array.from(rootIgnored); + const addDotFile = dirs.filter(dir => dir.match(dotFilePattern)); + + // don't ignore dotfiles if explicitly watched. + if (addDotFile.length === 0) { + ignored.push(dotFilePattern); + } + dirs = dirs.map(dir => { // if the directory is a file, it somehow causes // windows to lose the filename upon change if (fs.statSync(dir).isFile()) { dir = path.dirname(dir); } - var watchOptions = { - ignorePermissionErrors: true, - cwd: dir, - ignored: ignored, - persistent: true, - usePolling: config.options.legacyWatch || false, - interval: config.options.pollingInterval, - }; - - if (utils.isWindows) { - watchOptions.disableGlobbing = true; - } + return dir; + }); - if (process.env.TEST) { - watchOptions.useFsEvents = false; - } + var watchOptions = { + ignorePermissionErrors: true, + cwd: process.cwd(), // dir, + ignored: ignored, + persistent: true, + usePolling: config.options.legacyWatch || false, + interval: config.options.pollingInterval, + }; - var watcher = chokidar.watch( - dir, - Object.assign({}, watchOptions, config.watchOptions || {}) - ); + if (utils.isWindows) { + watchOptions.disableGlobbing = true; + } - watcher.ready = false; + if (process.env.TEST) { + watchOptions.useFsEvents = false; + } - var total = 0; + var watcher = chokidar.watch( + dirs, + Object.assign({}, watchOptions, config.watchOptions || {}) + ); - watcher.on('change', filterAndRestart); - watcher.on('add', function (file) { - if (watcher.ready) { - return filterAndRestart(file); - } + watcher.ready = false; - watchedFiles.push(file); - watchedFiles = Array.from(new Set(watchedFiles)); // ensure no dupes - total = watchedFiles.length; - bus.emit('watching', file); - debug('watching dir: %s', file); - }); - watcher.on('ready', function () { - watcher.ready = true; - resolve(total); - debugRoot('watch is complete'); - }); + var total = 0; - watcher.on('error', function (error) { - if (error.code === 'EINVAL') { - utils.log.error( - 'Internal watch failed. Likely cause: too many ' + - 'files being watched (perhaps from the root of a drive?\n' + - 'See https://github.com/paulmillr/chokidar/issues/229 for details' - ); - } else { - utils.log.error('Internal watch failed: ' + error.message); - process.exit(1); - } - }); + watcher.on('change', filterAndRestart); + watcher.on('add', function (file) { + if (watcher.ready) { + return filterAndRestart(file); + } - watchers.push(watcher); + watchedFiles.push(file); + watchedFiles = Array.from(new Set(watchedFiles)); // ensure no dupes + total = watchedFiles.length; + bus.emit('watching', file); + debug('watching dir: %s', file); + }); + watcher.on('ready', function () { + watcher.ready = true; + resolve(total); + debugRoot('watch is complete'); }); - promises.push(promise); + + watcher.on('error', function (error) { + if (error.code === 'EINVAL') { + utils.log.error( + 'Internal watch failed. Likely cause: too many ' + + 'files being watched (perhaps from the root of a drive?\n' + + 'See https://github.com/paulmillr/chokidar/issues/229 for details' + ); + } else { + utils.log.error('Internal watch failed: ' + error.message); + process.exit(1); + } + }); + + watchers.push(watcher); }); - return Promise.all(promises).then(function (res) { + return promise.catch(e => { + setTimeout(() => { + throw e; + }); + }).then(function (res) { utils.log.detail(`watching ${watchedFiles.length} file${ watchedFiles.length === 1 ? '' : 's'}`); return watchedFiles;