Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 11b1667

Browse files
committed
Passing a directory path implicitly watches directory contents, undoes accidental breaking change and fixes #229
Also refactor event filtering and add logging for unexpected watched files.
1 parent 19e16e8 commit 11b1667

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

index.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ module.exports = function (globs, opts, cb) {
7474

7575
outputStream._read = function _read() { };
7676

77-
var watcher = chokidar.watch(globs, opts)
78-
.on('all', processEvent);
77+
var watcher = chokidar.watch(globs, opts);
78+
79+
opts.events.forEach(function (ev) {
80+
watcher.on(ev, processEvent.bind(undefined, ev));
81+
});
7982

8083
['add', 'change', 'unlink', 'addDir', 'unlinkDir', 'error', 'ready', 'raw']
8184
.forEach(function (ev) {
@@ -95,21 +98,25 @@ module.exports = function (globs, opts, cb) {
9598
};
9699

97100
function processEvent(event, filepath) {
98-
var glob = globs[anymatch(globs, filepath, true)];
101+
var glob;
102+
var currentFilepath = filepath;
103+
while (!(glob = globs[anymatch(globs, currentFilepath, true)]) && currentFilepath !== (currentFilepath = path.resolve(currentFilepath, '..'))) {} // eslint-disable-line no-empty-blocks/no-empty-blocks
99104

100105
if (!glob) {
106+
util.log(
107+
util.colors.cyan('[gulp-watch]'),
108+
util.colors.yellow('wut? This shouldn\'t happen. Please open this link to report the issue:\n') +
109+
'https://github.com/floatdrop/gulp-watch/issues/new?title=' +
110+
encodeURIComponent('Watched unexpected filepath') + '&body=' +
111+
encodeURIComponent('Globs: `' + JSON.stringify(globs) + '`\nFilepath: `' + filepath + '`\nOptions:\n```js\n' + JSON.stringify(opts, null, 2) + '\n```')
112+
);
101113
return;
102114
}
103115

104116
if (!baseForced) {
105117
opts.base = glob2base(new Glob(glob));
106118
}
107119

108-
// React only on opts.events
109-
if (opts.events.indexOf(event) === -1) {
110-
return;
111-
}
112-
113120
// Do not stat deleted files
114121
if (event === 'unlink' || event === 'unlinkDir') {
115122
opts.path = pathIsAbsolute(filepath) ? filepath : path.join(opts.cwd || process.cwd(), filepath);

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
},
4141
"dependencies": {
4242
"anymatch": "^1.3.0",
43-
"chokidar": "^1.0.3",
44-
"glob": "^5.0.13",
43+
"chokidar": "^1.5.2",
44+
"glob": "^7.0.3",
4545
"glob2base": "0.0.12",
4646
"gulp-util": "^3.0.6",
4747
"path-is-absolute": "^1.0.0",

test/test-dir.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ describe('dir', function () {
2020
w.close();
2121
});
2222

23-
it('should not watch files inside directory', function (done) {
23+
it('should watch files inside directory', function (done) {
2424
fs.mkdirSync(fixtures('newDir'));
2525
touch(fixtures('newDir/index.js'))();
26-
w = watch(fixtures('newDir'), function () {
27-
done('Watched unexpected file.');
26+
w = watch(fixtures('newDir'), function (file) {
27+
file.relative.should.eql(path.normalize('newDir/index.js'));
28+
done();
2829
}).on('ready', function () {
2930
touch(fixtures('newDir/index.js'))('new content');
30-
setTimeout(done, 200);
3131
});
3232
});
3333

0 commit comments

Comments
 (0)