Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing a directory path implicitly watches directory contents, undoes accidental breaking change and fixes #229 #233

Merged
merged 4 commits into from
Jun 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,22 @@ module.exports = function (globs, opts, cb) {
opts = opts || {};
cb = cb || function () {};

function resolveFilepath(filepath) {
if (pathIsAbsolute(filepath)) {
return filepath;
}
return path.resolve(opts.cwd || process.cwd(), filepath);
}

function resolveGlob(glob) {
var mod = '';
var resolveFn = path.resolve;

if (glob[0] === '!') {
mod = glob[0];
glob = glob.slice(1);
}

if (opts.cwd) {
resolveFn = path.normalize;
}

return mod + resolveFn(glob);
return mod + resolveFilepath(glob);
}
globs = globs.map(resolveGlob);

Expand All @@ -74,8 +76,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,24 +100,30 @@ module.exports = function (globs, opts, cb) {
};

function processEvent(event, filepath) {
var glob = globs[anymatch(globs, filepath, true)];
filepath = resolveFilepath(filepath);

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);
opts.path = filepath;

write(event, null, new File(opts));
return;
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
18 changes: 17 additions & 1 deletion test/test-cwd.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,24 @@ describe('cwd', function () {

it('should respect opts.cwd', function (done) {
w = watch('index.js', {cwd: fixtures('')}, function (file) {
file.relative.should.eql(path.normalize('test/fixtures/index.js'));
file.relative.should.eql('index.js');
done();
}).on('ready', touch(fixtures('index.js')));
});

it('should emit file outside opts.cwd using relative glob', function (done) {
w = watch('../index.js', {cwd: fixtures('folder')}, function (file) {
file.relative.should.eql('index.js');
file.contents.toString().should.equal('fixtures index');
done();
}).on('ready', touch(fixtures('index.js'), 'fixtures index'));
});

it('should emit file outside opts.cwd using absolute glob', function (done) {
w = watch(fixtures('index.js'), {cwd: fixtures('folder')}, function (file) {
file.relative.should.eql('index.js');
file.contents.toString().should.equal('fixtures index');
done();
}).on('ready', touch(fixtures('index.js'), 'fixtures index'));
});
});
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) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: this is the correct behavior; it aligns with gulp-watch <= 4.3.5 as well as gulp 4's gulp.watch.

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