diff --git a/index.js b/index.js index 60dd858..96d2138 100644 --- a/index.js +++ b/index.js @@ -5,17 +5,21 @@ var through2 = require('through2'); var Combine = require('ordered-read-streams'); var unique = require('unique-stream'); +var clone = require('lodash.clone'); var glob = require('glob'); -var Minimatch = require('minimatch').Minimatch; var glob2base = require('glob2base'); var path = require('path'); var gs = { // creates a stream for a single glob or filter createStream: function(ourGlob, negatives, opt) { - // remove path relativity to make globs make sense - ourGlob = unrelative(opt.cwd, ourGlob); + if (negatives.length) { + // Do not mutate the options object which may be used + // in multiple `gs.createStream()` calls. + opt = clone(opt); + opt.ignore = (opt.ignore || []).concat(negatives); + } // create globbing stuff var globber = new glob.Glob(ourGlob, opt); @@ -24,7 +28,7 @@ var gs = { var basePath = opt.base || glob2base(globber); // create stream and map events from globber to it - var stream = through2.obj(negatives.length ? filterNegatives : undefined); + var stream = through2.obj(); globber.on('error', stream.emit.bind(stream, 'error')); globber.on('end', function(/* some args here so can't use bind directly */){ @@ -39,15 +43,6 @@ var gs = { }); return stream; - - function filterNegatives(filename, enc, cb) { - var matcha = isMatch.bind(null, filename); - if (negatives.every(matcha)) { - cb(null, filename); // pass - } else { - cb(); // ignore - } - } }, // creates a stream for multiple globs or filters @@ -67,17 +62,21 @@ var gs = { var negatives = []; globs.forEach(function(glob, index) { - if (typeof glob !== 'string' && !(glob instanceof RegExp)) { + if (typeof glob !== 'string') { throw new Error('Invalid glob at index ' + index); } - var globArray = isNegative(glob) ? negatives : positives; + var isNegativeGlob = isNegative(glob); - // create Minimatch instances for negative glob patterns - if (globArray === negatives && typeof glob === 'string') { - glob = new Minimatch(unrelative(opt.cwd, glob), opt); + // remove leading "!" from negative globs to pass them to glob's `ignore` option + if (isNegativeGlob) { + glob = glob.slice(1); } + // remove path relativity to make globs make sense + glob = unrelative(opt.cwd, glob); + + var globArray = isNegativeGlob ? negatives : positives; globArray.push({ index: index, glob: glob @@ -105,23 +104,12 @@ var gs = { } }; -function isMatch(file, matcher) { - if (matcher instanceof Minimatch) return matcher.match(file.path); - if (matcher instanceof RegExp) return matcher.test(file.path); -} - function isNegative(pattern) { - if (typeof pattern === 'string') return pattern[0] === '!'; - if (pattern instanceof RegExp) return true; + return pattern[0] === '!'; } function unrelative(cwd, glob) { - var mod = ''; - if (glob[0] === '!') { - mod = glob[0]; - glob = glob.slice(1); - } - return mod+path.resolve(cwd, glob); + return path.resolve(cwd, glob); } function indexGreaterThan(index) { diff --git a/package.json b/package.json index d602794..15a2ab7 100644 --- a/package.json +++ b/package.json @@ -8,12 +8,12 @@ "index.js" ], "dependencies": { - "glob": "^4.3.1", - "minimatch": "^2.0.1", - "ordered-read-streams": "^0.1.0", + "glob": "UltCombo/node-glob#fix-166", "glob2base": "^0.0.12", - "unique-stream": "^2.0.2", - "through2": "^0.6.1" + "lodash.clone": "^3.0.1", + "ordered-read-streams": "^0.1.0", + "through2": "^0.6.1", + "unique-stream": "^2.0.2" }, "devDependencies": { "coveralls": "^2.11.2", diff --git a/test/main.js b/test/main.js index 4e3df25..38cbad0 100644 --- a/test/main.js +++ b/test/main.js @@ -456,24 +456,6 @@ describe('glob-stream', function() { }); }); - it('should handle RegExps as negative matchers', function(done) { - var stream = gs.create(['./fixtures/stuff/*.dmc', /run/], {cwd: __dirname}); - should.exist(stream); - stream.on('error', function(err) { - throw err; - }); - stream.on('data', function(file) { - should.exist(file); - should.exist(file.path); - should.exist(file.base); - should.exist(file.cwd); - String(file.cwd).should.equal(__dirname); - String(file.base).should.equal(join(__dirname, 'fixtures', 'stuff'+sep)); - String(join(file.path,'')).should.equal(join(__dirname, './fixtures/stuff/run.dmc')); - done(); - }); - }); - it('should throw on invalid glob argument', function() { gs.create.bind(gs, 42, {cwd: __dirname}).should.throw(/Invalid glob .* 0/); gs.create.bind(gs, ['.', 42], {cwd: __dirname}).should.throw(/Invalid glob .* 1/);