Skip to content

Commit 0faec86

Browse files
octachromees128
authored andcommitted
disableGlobbing option: treat glob-like paths as literal paths (#598)
* Added ignoreGlobs option, which causes glob-like paths to be treated as literal paths, not glob patterns * Renamed ignoreGlobs option to disableGlobbing * More test cases for disableGlobbing * More test cases for disableGlobbing * Added disableGlobbing to readme
1 parent d90d112 commit 0faec86

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ chokidar.watch('file', {
120120
ignoreInitial: false,
121121
followSymlinks: true,
122122
cwd: '.',
123+
disableGlobbing: false,
123124

124125
usePolling: true,
125126
interval: 100,
@@ -168,6 +169,8 @@ symlinks themselves will be watched for changes instead of following
168169
the link references and bubbling events through the link's path.
169170
* `cwd` (no default). The base directory from which watch `paths` are to be
170171
derived. Paths emitted with events will be relative to this.
172+
* `disableGlobbing` (default: `false`). If set to `true` then the strings passed to `.watch()` and `.add()` are treated as
173+
literal path names, even if they look like globs.
171174

172175
#### Performance
173176

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function FSWatcher(_opts) {
7676
if (undef('ignorePermissionErrors')) opts.ignorePermissionErrors = false;
7777
if (undef('interval')) opts.interval = 100;
7878
if (undef('binaryInterval')) opts.binaryInterval = 300;
79+
if (undef('disableGlobbing')) opts.disableGlobbing = false;
7980
this.enableBinaryInterval = opts.binaryInterval !== opts.interval;
8081

8182
// Enable fsevents on OS X when polling isn't explicitly enabled.
@@ -377,7 +378,7 @@ FSWatcher.prototype._isIgnored = function(path, stats) {
377378
var replacerRe = /^\.[\/\\]/;
378379
FSWatcher.prototype._getWatchHelpers = function(path, depth) {
379380
path = path.replace(replacerRe, '');
380-
var watchPath = depth || !isGlob(path) ? path : globParent(path);
381+
var watchPath = depth || this.options.disableGlobbing || !isGlob(path) ? path : globParent(path);
381382
var fullWatchPath = sysPath.resolve(watchPath);
382383
var hasGlob = watchPath !== path;
383384
var globFilter = hasGlob ? anymatch(path) : false;

test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,59 @@ function runTests(baseopts) {
667667
});
668668
}));
669669
});
670+
it('should treat glob-like directory names as literal directory names when globbing is disabled', function(done) {
671+
options.disableGlobbing = true;
672+
var spy = sinon.spy();
673+
var filePath = getFixturePath('nota[glob]/a.txt');
674+
var watchPath = getFixturePath('nota[glob]');
675+
var matchingDir = getFixturePath('notag');
676+
var matchingFile = getFixturePath('notag/b.txt');
677+
var matchingFile2 = getFixturePath('notal');
678+
fs.mkdirSync(watchPath, 0x1ed);
679+
fs.writeFileSync(filePath, 'b');
680+
fs.mkdirSync(matchingDir, 0x1ed);
681+
fs.writeFileSync(matchingFile, 'c');
682+
fs.writeFileSync(matchingFile2, 'd');
683+
watcher = chokidar.watch(watchPath, options)
684+
.on('all', spy)
685+
.on('ready', function() {
686+
spy.should.have.been.calledWith('add', filePath);
687+
spy.should.not.have.been.calledWith('addDir', matchingDir);
688+
spy.should.not.have.been.calledWith('add', matchingFile);
689+
spy.should.not.have.been.calledWith('add', matchingFile2);
690+
w(fs.writeFile.bind(fs, filePath, Date.now(), simpleCb))();
691+
waitFor([spy.withArgs('change', filePath)], function() {
692+
spy.should.have.been.calledWith('change', filePath);
693+
done();
694+
});
695+
});
696+
});
697+
it('should treat glob-like filenames as literal filenames when globbing is disabled', function(done) {
698+
options.disableGlobbing = true;
699+
var spy = sinon.spy();
700+
var filePath = getFixturePath('nota[glob]');
701+
var watchPath = getFixturePath('nota[glob]');
702+
var matchingDir = getFixturePath('notag');
703+
var matchingFile = getFixturePath('notag/a.txt');
704+
var matchingFile2 = getFixturePath('notal');
705+
fs.writeFileSync(filePath, 'b');
706+
fs.mkdirSync(matchingDir, 0x1ed);
707+
fs.writeFileSync(matchingFile, 'c');
708+
fs.writeFileSync(matchingFile2, 'd');
709+
watcher = chokidar.watch(watchPath, options)
710+
.on('all', spy)
711+
.on('ready', function() {
712+
spy.should.have.been.calledWith('add', filePath);
713+
spy.should.not.have.been.calledWith('addDir', matchingDir);
714+
spy.should.not.have.been.calledWith('add', matchingFile);
715+
spy.should.not.have.been.calledWith('add', matchingFile2);
716+
w(fs.writeFile.bind(fs, filePath, Date.now(), simpleCb))();
717+
waitFor([spy.withArgs('change', filePath)], function() {
718+
spy.should.have.been.calledWith('change', filePath);
719+
done();
720+
});
721+
});
722+
});
670723
it('should not prematurely filter dirs against complex globstar patterns', function(done) {
671724
var spy = sinon.spy();
672725
var deepFile = getFixturePath('subdir/subsub/subsubsub/a.txt');

0 commit comments

Comments
 (0)