Skip to content

Commit

Permalink
Fix: Improve negative globbing with cwd option (fixes #46)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jul 23, 2020
1 parent 2076154 commit 5062520
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
33 changes: 22 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var asyncDone = require('async-done');
var defaults = require('object.defaults/immutable');
var isNegatedGlob = require('is-negated-glob');
var anymatch = require('anymatch');
var normalize = require('normalize-path');

var defaultOpts = {
delay: 200,
Expand Down Expand Up @@ -71,24 +72,34 @@ function watch(glob, options, cb) {
}
}

function shouldBeIgnored(path) {
var positiveMatch = anymatch(positives, path, true);
var negativeMatch = anymatch(negatives, path, true);
// If negativeMatch is -1, that means it was never negated
if (negativeMatch === -1) {
return false;
var toWatch = positives.filter(exists);

function joinCwd(glob) {
if (glob && opt.cwd) {
return normalize(opt.cwd + '/' + glob);
}

// If the negative is "less than" the positive, that means
// it came later in the glob array before we reversed them
return negativeMatch < positiveMatch;
return glob;
}

var toWatch = positives.filter(exists);

// We only do add our custom `ignored` if there are some negative globs
// TODO: I'm not sure how to test this
if (negatives.some(exists)) {
var normalizedPositives = positives.map(joinCwd);
var normalizedNegatives = negatives.map(joinCwd);
var shouldBeIgnored = function(path) {
var positiveMatch = anymatch(normalizedPositives, path, true);
var negativeMatch = anymatch(normalizedNegatives, path, true);
// If negativeMatch is -1, that means it was never negated
if (negativeMatch === -1) {
return false;
}

// If the negative is "less than" the positive, that means
// it came later in the glob array before we reversed them
return negativeMatch < positiveMatch;
};

opt.ignored = [].concat(opt.ignored, shouldBeIgnored);
}
var watcher = chokidar.watch(toWatch, opt);
Expand Down
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,20 @@ describe('glob-watcher', function() {

setTimeout(done, 1500);
});

// https://github.com/gulpjs/glob-watcher/issues/46
it('ignoring globs also works with `cwd` option', function(done) {
watcher = watch(['fixtures/**', '!fixtures/*.js'], { cwd: 'test' });

watcher.once('change', function(filepath) {
// It should never reach here
expect(filepath).toNotExist();
done();
});

// We default `ignoreInitial` to true, so always wait for `on('ready')`
watcher.on('ready', changeFile);

setTimeout(done, 1500);
});
});

0 comments on commit 5062520

Please sign in to comment.