Skip to content

Commit

Permalink
Passing a directory path implicitly watches directory contents, undoe…
Browse files Browse the repository at this point in the history
…s accidental breaking change and fixes #229

Also refactor event filtering and add logging for unexpected watched files.
  • Loading branch information
UltCombo committed Jun 12, 2016
1 parent 19e16e8 commit 11b1667
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
23 changes: 15 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ module.exports = function (globs, opts, cb) {

outputStream._read = function _read() { };

var watcher = chokidar.watch(globs, opts)
.on('all', processEvent);
var watcher = chokidar.watch(globs, opts);

opts.events.forEach(function (ev) {
watcher.on(ev, processEvent.bind(undefined, ev));
});

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

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

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

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

// React only on opts.events
if (opts.events.indexOf(event) === -1) {
return;
}

// Do not stat deleted files
if (event === 'unlink' || event === 'unlinkDir') {
opts.path = pathIsAbsolute(filepath) ? filepath : path.join(opts.cwd || process.cwd(), filepath);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
},
"dependencies": {
"anymatch": "^1.3.0",
"chokidar": "^1.0.3",
"glob": "^5.0.13",
"chokidar": "^1.5.2",
"glob": "^7.0.3",
"glob2base": "0.0.12",
"gulp-util": "^3.0.6",
"path-is-absolute": "^1.0.0",
Expand Down
8 changes: 4 additions & 4 deletions test/test-dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ describe('dir', function () {
w.close();
});

it('should not watch files inside directory', function (done) {
it('should watch files inside directory', function (done) {
fs.mkdirSync(fixtures('newDir'));
touch(fixtures('newDir/index.js'))();
w = watch(fixtures('newDir'), function () {
done('Watched unexpected file.');
w = watch(fixtures('newDir'), function (file) {
file.relative.should.eql(path.normalize('newDir/index.js'));
done();
}).on('ready', function () {
touch(fixtures('newDir/index.js'))('new content');
setTimeout(done, 200);
});
});

Expand Down

0 comments on commit 11b1667

Please sign in to comment.