diff --git a/index.js b/index.js index 363a29d..d55a633 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,6 @@ var Combine = require('ordered-read-streams'); var unique = require('unique-stream'); var glob = require('glob'); -var micromatch = require('micromatch'); var resolveGlob = require('to-absolute-glob'); var globParent = require('glob-parent'); var path = require('path'); @@ -15,10 +14,16 @@ var sepRe = (process.platform === 'win32' ? /[\/\\]/ : /\/+/); var gs = { // Creates a stream for a single glob or filter createStream: function(ourGlob, negatives, opt) { + function resolveNegatives(negative) { + return resolveGlob(negative, opt); + } var ourOpt = extend({}, opt); delete ourOpt.root; + var ourNegatives = negatives.map(resolveNegatives); + ourOpt.ignore = ourNegatives; + // Extract base path from glob var basePath = ourOpt.base || getBasePath(ourGlob, opt); @@ -29,8 +34,7 @@ var gs = { var globber = new glob.Glob(ourGlob, ourOpt); // Create stream and map events from globber to it - var stream = through2.obj(opt, - negatives.length ? filterNegatives : undefined); + var stream = through2.obj(ourOpt); var found = false; @@ -52,17 +56,7 @@ var gs = { path: path.normalize(filename), }); }); - 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 @@ -97,22 +91,13 @@ var gs = { var positives = []; var negatives = []; - var ourOpt = extend({}, opt); - delete ourOpt.root; - 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; - // Create Minimatch instances for negative glob patterns - if (globArray === negatives && typeof glob === 'string') { - var ourGlob = resolveGlob(glob, opt); - glob = micromatch.matcher(ourGlob, ourOpt); - } - globArray.push({ index: index, glob: glob, @@ -144,28 +129,18 @@ var gs = { function streamFromPositive(positive) { var negativeGlobs = negatives.filter(indexGreaterThan(positive.index)) - .map(toGlob); + .map(toGlob) + .map(stripExclamationMark); return gs.createStream(positive.glob, negativeGlobs, opt); } }, }; -function isMatch(file, matcher) { - if (typeof matcher === 'function') { - return matcher(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; - } } function indexGreaterThan(index) { @@ -180,7 +155,6 @@ function toGlob(obj) { function globIsSingular(glob) { var globSet = glob.minimatch.set; - if (globSet.length !== 1) { return false; } @@ -206,4 +180,8 @@ function getBasePath(ourGlob, opt) { return basePath; } +function stripExclamationMark(glob) { + return glob.slice(1); +} + module.exports = gs; diff --git a/package.json b/package.json index 2517ac2..2938d3f 100644 --- a/package.json +++ b/package.json @@ -22,9 +22,8 @@ }, "dependencies": { "extend": "^3.0.0", - "glob": "^5.0.3", + "glob": "^7.0.6", "glob-parent": "^3.0.0", - "micromatch": "^2.3.7", "ordered-read-streams": "^0.3.0", "through2": "^0.6.0", "to-absolute-glob": "^0.1.1", diff --git a/test/main.js b/test/main.js index bd7cfd4..a34f1bc 100644 --- a/test/main.js +++ b/test/main.js @@ -530,24 +530,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/);