fix: move to array of watched dirs

Instead of individually listing them.

Fixes #1283
This commit is contained in:
Remy Sharp
2018-03-07 09:45:20 +00:00
parent e16f2fecf6
commit b1ff637be9

View File

@@ -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);
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 (!dir.match(dotFilePattern)) {
ignored.push(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;
}
if (process.env.TEST) {
watchOptions.useFsEvents = false;
}
var watcher = chokidar.watch(
dir,
Object.assign({}, watchOptions, config.watchOptions || {})
);
watcher.ready = false;
var total = 0;
watcher.on('change', filterAndRestart);
watcher.on('add', function (file) {
if (watcher.ready) {
return filterAndRestart(file);
}
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');
});
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 dir;
});
promises.push(promise);
var watchOptions = {
ignorePermissionErrors: true,
cwd: process.cwd(), // dir,
ignored: ignored,
persistent: true,
usePolling: config.options.legacyWatch || false,
interval: config.options.pollingInterval,
};
if (utils.isWindows) {
watchOptions.disableGlobbing = true;
}
if (process.env.TEST) {
watchOptions.useFsEvents = false;
}
var watcher = chokidar.watch(
dirs,
Object.assign({}, watchOptions, config.watchOptions || {})
);
watcher.ready = false;
var total = 0;
watcher.on('change', filterAndRestart);
watcher.on('add', function (file) {
if (watcher.ready) {
return filterAndRestart(file);
}
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');
});
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;