From 4db4033aa12fa66a1c0be0acb509df10b2e867f2 Mon Sep 17 00:00:00 2001 From: UltCombo Date: Tue, 24 Feb 2015 01:18:03 -0300 Subject: [PATCH 1/5] Implementing node-glob's `ignore` option Quite a few tests are broken on my machine, not sure if I'm missing something or the `ignore` option is buggy in regard to the `dot` option and absolute paths (on Windows). --- index.js | 40 ++++++++++++++++++++++++++-------------- package.json | 10 +++++----- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index 60dd858..6b2aaad 100644 --- a/index.js +++ b/index.js @@ -5,9 +5,9 @@ var through2 = require('through2'); var Combine = require('ordered-read-streams'); var unique = require('unique-stream'); +var assign = require('object-assign'); var glob = require('glob'); -var Minimatch = require('minimatch').Minimatch; var glob2base = require('glob2base'); var path = require('path'); @@ -17,6 +17,23 @@ var gs = { // remove path relativity to make globs make sense ourGlob = unrelative(opt.cwd, ourGlob); + var negativeGlobs = []; + var negativeRegExps = []; + + negatives.forEach(function(negative) { + // negatives can only be String or RegExp, otherwise an error would have been + // thrown in the `gs.create()` entry point. + var negativeArray = typeof negative === 'string' ? negativeGlobs : negativeRegExps; + negativeArray.push(negative); + }); + + if (negativeGlobs.length) { + // Do not mutate the options object which may be used + // in multiple `gs.createStream()` calls. + opt = assign({}, opt); + opt.ignore = (opt.ignore || []).concat(negativeGlobs); + } + // create globbing stuff var globber = new glob.Glob(ourGlob, opt); @@ -24,7 +41,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(negativeRegExps.length ? filterNegatives : undefined); globber.on('error', stream.emit.bind(stream, 'error')); globber.on('end', function(/* some args here so can't use bind directly */){ @@ -41,8 +58,8 @@ var gs = { return stream; function filterNegatives(filename, enc, cb) { - var matcha = isMatch.bind(null, filename); - if (negatives.every(matcha)) { + var matcha = isRegExpMatch.bind(null, filename); + if (negativeRegExps.every(matcha)) { cb(null, filename); // pass } else { cb(); // ignore @@ -67,20 +84,16 @@ var gs = { var negatives = []; globs.forEach(function(glob, index) { - if (typeof glob !== 'string' && !(glob instanceof RegExp)) { + var isGlobString = typeof glob === 'string'; + if (!isGlobString && !(glob instanceof RegExp)) { 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') { - glob = new Minimatch(unrelative(opt.cwd, glob), opt); - } - globArray.push({ index: index, - glob: glob + glob: globArray === negatives && isGlobString ? glob.slice(1) : glob }); }); @@ -105,9 +118,8 @@ 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 isRegExpMatch(file, pattern) { + return pattern.test(file.path); } function isNegative(pattern) { diff --git a/package.json b/package.json index d602794..42a9e52 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": "^4.4.0", "glob2base": "^0.0.12", - "unique-stream": "^2.0.2", - "through2": "^0.6.1" + "object-assign": "^2.0.0", + "ordered-read-streams": "^0.1.0", + "through2": "^0.6.1", + "unique-stream": "^2.0.2" }, "devDependencies": { "coveralls": "^2.11.2", From fff4f8e3cc59fcfa31af92f6ccfdc39c74822e2e Mon Sep 17 00:00:00 2001 From: UltCombo Date: Tue, 24 Feb 2015 01:28:08 -0300 Subject: [PATCH 2/5] remove RegExp support --- index.js | 34 +++++----------------------------- test/main.js | 18 ------------------ 2 files changed, 5 insertions(+), 47 deletions(-) diff --git a/index.js b/index.js index 6b2aaad..b42660f 100644 --- a/index.js +++ b/index.js @@ -17,21 +17,11 @@ var gs = { // remove path relativity to make globs make sense ourGlob = unrelative(opt.cwd, ourGlob); - var negativeGlobs = []; - var negativeRegExps = []; - - negatives.forEach(function(negative) { - // negatives can only be String or RegExp, otherwise an error would have been - // thrown in the `gs.create()` entry point. - var negativeArray = typeof negative === 'string' ? negativeGlobs : negativeRegExps; - negativeArray.push(negative); - }); - - if (negativeGlobs.length) { + if (negatives.length) { // Do not mutate the options object which may be used // in multiple `gs.createStream()` calls. opt = assign({}, opt); - opt.ignore = (opt.ignore || []).concat(negativeGlobs); + opt.ignore = (opt.ignore || []).concat(negatives); } // create globbing stuff @@ -41,7 +31,7 @@ var gs = { var basePath = opt.base || glob2base(globber); // create stream and map events from globber to it - var stream = through2.obj(negativeRegExps.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 */){ @@ -56,15 +46,6 @@ var gs = { }); return stream; - - function filterNegatives(filename, enc, cb) { - var matcha = isRegExpMatch.bind(null, filename); - if (negativeRegExps.every(matcha)) { - cb(null, filename); // pass - } else { - cb(); // ignore - } - } }, // creates a stream for multiple globs or filters @@ -84,8 +65,7 @@ var gs = { var negatives = []; globs.forEach(function(glob, index) { - var isGlobString = typeof glob === 'string'; - if (!isGlobString && !(glob instanceof RegExp)) { + if (typeof glob !== 'string') { throw new Error('Invalid glob at index ' + index); } @@ -93,7 +73,7 @@ var gs = { globArray.push({ index: index, - glob: globArray === negatives && isGlobString ? glob.slice(1) : glob + glob: globArray === negatives ? glob.slice(1) : glob }); }); @@ -118,10 +98,6 @@ var gs = { } }; -function isRegExpMatch(file, pattern) { - return pattern.test(file.path); -} - function isNegative(pattern) { if (typeof pattern === 'string') return pattern[0] === '!'; if (pattern instanceof RegExp) return true; 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/); From 34cd57519ba9ad13f9ddb47aca83a3d04e6c53fe Mon Sep 17 00:00:00 2001 From: UltCombo Date: Tue, 24 Feb 2015 01:51:42 -0300 Subject: [PATCH 3/5] clean up, respond to review --- index.js | 27 +++++++++++++-------------- package.json | 2 +- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index b42660f..d9a864a 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ var through2 = require('through2'); var Combine = require('ordered-read-streams'); var unique = require('unique-stream'); -var assign = require('object-assign'); +var clone = require('lodash.clone'); var glob = require('glob'); var glob2base = require('glob2base'); @@ -14,13 +14,10 @@ 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 = assign({}, opt); + opt = clone(opt); opt.ignore = (opt.ignore || []).concat(negatives); } @@ -71,9 +68,17 @@ var gs = { var globArray = isNegative(glob) ? negatives : positives; + // remove leading "!" from negative globs to pass them to glob's `ignore` option + if (globArray === negatives) { + glob = glob.slice(1); + } + + // remove path relativity to make globs make sense + glob = unrelative(opt.cwd, glob); + globArray.push({ index: index, - glob: globArray === negatives ? glob.slice(1) : glob + glob: glob }); }); @@ -99,17 +104,11 @@ var gs = { }; 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 42a9e52..83f11e0 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "dependencies": { "glob": "^4.4.0", "glob2base": "^0.0.12", - "object-assign": "^2.0.0", + "lodash.clone": "^3.0.1", "ordered-read-streams": "^0.1.0", "through2": "^0.6.1", "unique-stream": "^2.0.2" From 6ea6ad3635eb522cd2369f1c05f258d92cb33041 Mon Sep 17 00:00:00 2001 From: UltCombo Date: Tue, 24 Feb 2015 20:08:57 -0300 Subject: [PATCH 4/5] clean up --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d9a864a..96d2138 100644 --- a/index.js +++ b/index.js @@ -66,16 +66,17 @@ var gs = { throw new Error('Invalid glob at index ' + index); } - var globArray = isNegative(glob) ? negatives : positives; + var isNegativeGlob = isNegative(glob); // remove leading "!" from negative globs to pass them to glob's `ignore` option - if (globArray === negatives) { + 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 From 90f6a36ebfb8df7389f6c4e809fd27e059c4fb14 Mon Sep 17 00:00:00 2001 From: UltCombo Date: Tue, 24 Feb 2015 23:30:40 -0300 Subject: [PATCH 5/5] try out fix branch --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 83f11e0..15a2ab7 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "index.js" ], "dependencies": { - "glob": "^4.4.0", + "glob": "UltCombo/node-glob#fix-166", "glob2base": "^0.0.12", "lodash.clone": "^3.0.1", "ordered-read-streams": "^0.1.0",