Skip to content

Commit 372e6b2

Browse files
authored
fix: watch count regression (#1287)
* chore: revert package * fix: move to array of watched dirs Instead of individually listing them. Fixes #1283
1 parent 8637e52 commit 372e6b2

File tree

1 file changed

+67
-61
lines changed

1 file changed

+67
-61
lines changed

lib/monitor/watch.js

+67-61
Original file line numberDiff line numberDiff line change
@@ -39,85 +39,91 @@ function watch() {
3939
var promises = [];
4040
var watchedFiles = [];
4141

42-
dirs.forEach(function (dir) {
43-
var promise = new Promise(function (resolve) {
44-
var dotFilePattern = /[/\\]\./;
45-
var ignored = Array.from(rootIgnored);
46-
47-
// don't ignore dotfiles if explicitly watched.
48-
if (!dir.match(dotFilePattern)) {
49-
ignored.push(dotFilePattern);
50-
}
42+
const promise = new Promise(function (resolve) {
43+
const dotFilePattern = /[/\\]\./;
44+
var ignored = Array.from(rootIgnored);
45+
const addDotFile = dirs.filter(dir => dir.match(dotFilePattern));
46+
47+
// don't ignore dotfiles if explicitly watched.
48+
if (addDotFile.length === 0) {
49+
ignored.push(dotFilePattern);
50+
}
5151

52+
dirs = dirs.map(dir => {
5253
// if the directory is a file, it somehow causes
5354
// windows to lose the filename upon change
5455
if (fs.statSync(dir).isFile()) {
5556
dir = path.dirname(dir);
5657
}
5758

58-
var watchOptions = {
59-
ignorePermissionErrors: true,
60-
cwd: dir,
61-
ignored: ignored,
62-
persistent: true,
63-
usePolling: config.options.legacyWatch || false,
64-
interval: config.options.pollingInterval,
65-
};
66-
67-
if (utils.isWindows) {
68-
watchOptions.disableGlobbing = true;
69-
}
59+
return dir;
60+
});
7061

71-
if (process.env.TEST) {
72-
watchOptions.useFsEvents = false;
73-
}
62+
var watchOptions = {
63+
ignorePermissionErrors: true,
64+
cwd: process.cwd(), // dir,
65+
ignored: ignored,
66+
persistent: true,
67+
usePolling: config.options.legacyWatch || false,
68+
interval: config.options.pollingInterval,
69+
};
7470

75-
var watcher = chokidar.watch(
76-
dir,
77-
Object.assign({}, watchOptions, config.watchOptions || {})
78-
);
71+
if (utils.isWindows) {
72+
watchOptions.disableGlobbing = true;
73+
}
7974

80-
watcher.ready = false;
75+
if (process.env.TEST) {
76+
watchOptions.useFsEvents = false;
77+
}
8178

82-
var total = 0;
79+
var watcher = chokidar.watch(
80+
dirs,
81+
Object.assign({}, watchOptions, config.watchOptions || {})
82+
);
8383

84-
watcher.on('change', filterAndRestart);
85-
watcher.on('add', function (file) {
86-
if (watcher.ready) {
87-
return filterAndRestart(file);
88-
}
84+
watcher.ready = false;
8985

90-
watchedFiles.push(file);
91-
watchedFiles = Array.from(new Set(watchedFiles)); // ensure no dupes
92-
total = watchedFiles.length;
93-
bus.emit('watching', file);
94-
debug('watching dir: %s', file);
95-
});
96-
watcher.on('ready', function () {
97-
watcher.ready = true;
98-
resolve(total);
99-
debugRoot('watch is complete');
100-
});
86+
var total = 0;
10187

102-
watcher.on('error', function (error) {
103-
if (error.code === 'EINVAL') {
104-
utils.log.error(
105-
'Internal watch failed. Likely cause: too many ' +
106-
'files being watched (perhaps from the root of a drive?\n' +
107-
'See https://github.com/paulmillr/chokidar/issues/229 for details'
108-
);
109-
} else {
110-
utils.log.error('Internal watch failed: ' + error.message);
111-
process.exit(1);
112-
}
113-
});
88+
watcher.on('change', filterAndRestart);
89+
watcher.on('add', function (file) {
90+
if (watcher.ready) {
91+
return filterAndRestart(file);
92+
}
11493

115-
watchers.push(watcher);
94+
watchedFiles.push(file);
95+
watchedFiles = Array.from(new Set(watchedFiles)); // ensure no dupes
96+
total = watchedFiles.length;
97+
bus.emit('watching', file);
98+
debug('watching dir: %s', file);
99+
});
100+
watcher.on('ready', function () {
101+
watcher.ready = true;
102+
resolve(total);
103+
debugRoot('watch is complete');
116104
});
117-
promises.push(promise);
105+
106+
watcher.on('error', function (error) {
107+
if (error.code === 'EINVAL') {
108+
utils.log.error(
109+
'Internal watch failed. Likely cause: too many ' +
110+
'files being watched (perhaps from the root of a drive?\n' +
111+
'See https://github.com/paulmillr/chokidar/issues/229 for details'
112+
);
113+
} else {
114+
utils.log.error('Internal watch failed: ' + error.message);
115+
process.exit(1);
116+
}
117+
});
118+
119+
watchers.push(watcher);
118120
});
119121

120-
return Promise.all(promises).then(function (res) {
122+
return promise.catch(e => {
123+
setTimeout(() => {
124+
throw e;
125+
});
126+
}).then(function (res) {
121127
utils.log.detail(`watching ${watchedFiles.length} file${
122128
watchedFiles.length === 1 ? '' : 's'}`);
123129
return watchedFiles;

0 commit comments

Comments
 (0)