Skip to content

Commit cbdf255

Browse files
MhMadHamsteres128
authored andcommitted
fix for handling braces in path (#622)
* fix for handling braces in path * fix entry parts checking * fix braces test * fix braces test * fix paths in test for windows
1 parent 528826f commit cbdf255

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var globParent = require('glob-parent');
88
var isGlob = require('is-glob');
99
var isAbsolute = require('path-is-absolute');
1010
var inherits = require('inherits');
11+
var braces = require('braces');
1112

1213
var NodeFsHandler = require('./lib/nodefs-handler');
1314
var FsEventsHandler = require('./lib/fsevents-handler');
@@ -423,21 +424,31 @@ FSWatcher.prototype._getWatchHelpers = function(path, depth) {
423424

424425
var getDirParts = function(path) {
425426
if (!hasGlob) return false;
426-
var parts = sysPath.relative(watchPath, path).split(/[\/\\]/);
427+
var parts = [];
428+
var expandedPath = braces.expand(path);
429+
expandedPath.forEach(function(path) {
430+
parts.push(sysPath.relative(watchPath, path).split(/[\/\\]/));
431+
});
427432
return parts;
428433
};
429434

430435
var dirParts = getDirParts(path);
431-
if (dirParts && dirParts.length > 1) dirParts.pop();
436+
if (dirParts) {
437+
dirParts.forEach(function(parts) {
438+
if (parts.length > 1) parts.pop();
439+
});
440+
}
432441
var unmatchedGlob;
433442

434443
var filterDir = function(entry) {
435444
if (hasGlob) {
436445
var entryParts = getDirParts(checkGlobSymlink(entry));
437446
var globstar = false;
438-
unmatchedGlob = !dirParts.every(function(part, i) {
439-
if (part === '**') globstar = true;
440-
return globstar || !entryParts[i] || anymatch(part, entryParts[i]);
447+
unmatchedGlob = !dirParts.some(function(parts) {
448+
return parts.every(function(part, i) {
449+
if (part === '**') globstar = true;
450+
return globstar || !entryParts[0][i] || anymatch(part, entryParts[0][i]);
451+
});
441452
});
442453
}
443454
return !unmatchedGlob && this._isntIgnored(entryPath(entry), entry.stat);

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@
3737
"mocha": "^3.0.0",
3838
"rimraf": "^2.4.3",
3939
"sinon": "^1.10.3",
40-
"sinon-chai": "^2.6.0"
40+
"sinon-chai": "^2.6.0",
41+
"upath": "1.0.0"
4142
},
4243
"optionalDependencies": {
4344
"fsevents": "^1.0.0"
4445
},
4546
"dependencies": {
4647
"anymatch": "^1.3.0",
4748
"async-each": "^1.0.0",
49+
"braces": "2.2.2",
4850
"glob-parent": "^2.0.0",
4951
"inherits": "^2.0.1",
5052
"is-binary-path": "^1.0.0",

test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var sinon = require('sinon');
88
var rimraf = require('rimraf');
99
var fs = require('graceful-fs');
1010
var sysPath = require('path');
11+
var upath = require("upath");
1112
var cp = require('child_process');
1213
chai.use(require('sinon-chai'));
1314
var os = process.platform;
@@ -767,6 +768,33 @@ function runTests(baseopts) {
767768
});
768769
});
769770
});
771+
it('should correctly handle glob with braces', function(done) {
772+
var spy = sinon.spy();
773+
var watchPath = upath.normalizeSafe(getFixturePath('{subdir/*,subdir1/subsub1}/subsubsub/*.txt'));
774+
var deepFileA = getFixturePath('subdir/subsub/subsubsub/a.txt');
775+
var deepFileB = getFixturePath('subdir1/subsub1/subsubsub/a.txt');
776+
fs.mkdirSync(getFixturePath('subdir'), 0x1ed);
777+
fs.mkdirSync(getFixturePath('subdir/subsub'), 0x1ed);
778+
fs.mkdirSync(getFixturePath('subdir/subsub/subsubsub'), 0x1ed);
779+
fs.mkdirSync(getFixturePath('subdir1'), 0x1ed);
780+
fs.mkdirSync(getFixturePath('subdir1/subsub1'), 0x1ed);
781+
fs.mkdirSync(getFixturePath('subdir1/subsub1/subsubsub'), 0x1ed);
782+
fs.writeFileSync(deepFileA, Date.now());
783+
fs.writeFileSync(deepFileB, Date.now());
784+
watcher = chokidar.watch(watchPath, options)
785+
.on('all', spy)
786+
.on('ready', function() {
787+
spy.should.have.been.calledWith('add', deepFileA);
788+
spy.should.have.been.calledWith('add', deepFileB);
789+
fs.appendFileSync(deepFileA, Date.now());
790+
fs.appendFileSync(deepFileB, Date.now());
791+
waitFor([[spy, 4]], function() {
792+
spy.should.have.been.calledWith('change', deepFileA);
793+
spy.should.have.been.calledWith('change', deepFileB);
794+
done();
795+
});
796+
});
797+
});
770798
});
771799
describe('watch symlinks', function() {
772800
if (os === 'win32') return;
@@ -1118,7 +1146,7 @@ function runTests(baseopts) {
11181146
describe('ignored', function() {
11191147
it('should check ignore after stating', function(done) {
11201148
options.ignored = function(path, stats) {
1121-
if (path === testDir || !stats) return false;
1149+
if (upath.normalizeSafe(path) === upath.normalizeSafe(testDir) || !stats) return false;
11221150
return stats.isDirectory();
11231151
};
11241152
var spy = sinon.spy();

0 commit comments

Comments
 (0)