Skip to content

Commit

Permalink
fix: watch count regression (#1287)
Browse files Browse the repository at this point in the history
* chore: revert package
* fix: move to array of watched dirs

Instead of individually listing them.

Fixes #1283
  • Loading branch information
remy authored Mar 13, 2018
1 parent 8637e52 commit 372e6b2
Showing 1 changed file with 67 additions and 61 deletions.
128 changes: 67 additions & 61 deletions lib/monitor/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 372e6b2

Please sign in to comment.