Skip to content

Commit 5062520

Browse files
committedJul 23, 2020
Fix: Improve negative globbing with cwd option (fixes #46)
1 parent 2076154 commit 5062520

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed
 

‎index.js

+22-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var asyncDone = require('async-done');
66
var defaults = require('object.defaults/immutable');
77
var isNegatedGlob = require('is-negated-glob');
88
var anymatch = require('anymatch');
9+
var normalize = require('normalize-path');
910

1011
var defaultOpts = {
1112
delay: 200,
@@ -71,24 +72,34 @@ function watch(glob, options, cb) {
7172
}
7273
}
7374

74-
function shouldBeIgnored(path) {
75-
var positiveMatch = anymatch(positives, path, true);
76-
var negativeMatch = anymatch(negatives, path, true);
77-
// If negativeMatch is -1, that means it was never negated
78-
if (negativeMatch === -1) {
79-
return false;
75+
var toWatch = positives.filter(exists);
76+
77+
function joinCwd(glob) {
78+
if (glob && opt.cwd) {
79+
return normalize(opt.cwd + '/' + glob);
8080
}
8181

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

87-
var toWatch = positives.filter(exists);
88-
8985
// We only do add our custom `ignored` if there are some negative globs
9086
// TODO: I'm not sure how to test this
9187
if (negatives.some(exists)) {
88+
var normalizedPositives = positives.map(joinCwd);
89+
var normalizedNegatives = negatives.map(joinCwd);
90+
var shouldBeIgnored = function(path) {
91+
var positiveMatch = anymatch(normalizedPositives, path, true);
92+
var negativeMatch = anymatch(normalizedNegatives, path, true);
93+
// If negativeMatch is -1, that means it was never negated
94+
if (negativeMatch === -1) {
95+
return false;
96+
}
97+
98+
// If the negative is "less than" the positive, that means
99+
// it came later in the glob array before we reversed them
100+
return negativeMatch < positiveMatch;
101+
};
102+
92103
opt.ignored = [].concat(opt.ignored, shouldBeIgnored);
93104
}
94105
var watcher = chokidar.watch(toWatch, opt);

‎test/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,20 @@ describe('glob-watcher', function() {
340340

341341
setTimeout(done, 1500);
342342
});
343+
344+
// https://github.com/gulpjs/glob-watcher/issues/46
345+
it('ignoring globs also works with `cwd` option', function(done) {
346+
watcher = watch(['fixtures/**', '!fixtures/*.js'], { cwd: 'test' });
347+
348+
watcher.once('change', function(filepath) {
349+
// It should never reach here
350+
expect(filepath).toNotExist();
351+
done();
352+
});
353+
354+
// We default `ignoreInitial` to true, so always wait for `on('ready')`
355+
watcher.on('ready', changeFile);
356+
357+
setTimeout(done, 1500);
358+
});
343359
});

0 commit comments

Comments
 (0)
Please sign in to comment.