Skip to content

Commit 4e4c332

Browse files
thirdcreedphated
authored andcommitted
Breaking: Upgrade to glob 7, implement ignore & remove RegExp as negative matchers (closes #24, closes #57)
1 parent b88fe67 commit 4e4c332

File tree

3 files changed

+15
-56
lines changed

3 files changed

+15
-56
lines changed

Diff for: index.js

+14-36
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var Combine = require('ordered-read-streams');
55
var unique = require('unique-stream');
66

77
var glob = require('glob');
8-
var micromatch = require('micromatch');
98
var resolveGlob = require('to-absolute-glob');
109
var globParent = require('glob-parent');
1110
var path = require('path');
@@ -15,10 +14,16 @@ var sepRe = (process.platform === 'win32' ? /[\/\\]/ : /\/+/);
1514
var gs = {
1615
// Creates a stream for a single glob or filter
1716
createStream: function(ourGlob, negatives, opt) {
17+
function resolveNegatives(negative) {
18+
return resolveGlob(negative, opt);
19+
}
1820

1921
var ourOpt = extend({}, opt);
2022
delete ourOpt.root;
2123

24+
var ourNegatives = negatives.map(resolveNegatives);
25+
ourOpt.ignore = ourNegatives;
26+
2227
// Extract base path from glob
2328
var basePath = ourOpt.base || getBasePath(ourGlob, opt);
2429

@@ -29,8 +34,7 @@ var gs = {
2934
var globber = new glob.Glob(ourGlob, ourOpt);
3035

3136
// Create stream and map events from globber to it
32-
var stream = through2.obj(opt,
33-
negatives.length ? filterNegatives : undefined);
37+
var stream = through2.obj(ourOpt);
3438

3539
var found = false;
3640

@@ -52,17 +56,7 @@ var gs = {
5256
path: path.normalize(filename),
5357
});
5458
});
55-
5659
return stream;
57-
58-
function filterNegatives(filename, enc, cb) {
59-
var matcha = isMatch.bind(null, filename);
60-
if (negatives.every(matcha)) {
61-
cb(null, filename); // Pass
62-
} else {
63-
cb(); // Ignore
64-
}
65-
}
6660
},
6761

6862
// Creates a stream for multiple globs or filters
@@ -97,22 +91,13 @@ var gs = {
9791
var positives = [];
9892
var negatives = [];
9993

100-
var ourOpt = extend({}, opt);
101-
delete ourOpt.root;
102-
10394
globs.forEach(function(glob, index) {
104-
if (typeof glob !== 'string' && !(glob instanceof RegExp)) {
95+
if (typeof glob !== 'string') {
10596
throw new Error('Invalid glob at index ' + index);
10697
}
10798

10899
var globArray = isNegative(glob) ? negatives : positives;
109100

110-
// Create Minimatch instances for negative glob patterns
111-
if (globArray === negatives && typeof glob === 'string') {
112-
var ourGlob = resolveGlob(glob, opt);
113-
glob = micromatch.matcher(ourGlob, ourOpt);
114-
}
115-
116101
globArray.push({
117102
index: index,
118103
glob: glob,
@@ -144,28 +129,18 @@ var gs = {
144129

145130
function streamFromPositive(positive) {
146131
var negativeGlobs = negatives.filter(indexGreaterThan(positive.index))
147-
.map(toGlob);
132+
.map(toGlob)
133+
.map(stripExclamationMark);
148134
return gs.createStream(positive.glob, negativeGlobs, opt);
149135
}
150136
},
151137
};
152138

153-
function isMatch(file, matcher) {
154-
if (typeof matcher === 'function') {
155-
return matcher(file.path);
156-
}
157-
if (matcher instanceof RegExp) {
158-
return matcher.test(file.path);
159-
}
160-
}
161139

162140
function isNegative(pattern) {
163141
if (typeof pattern === 'string') {
164142
return pattern[0] === '!';
165143
}
166-
if (pattern instanceof RegExp) {
167-
return true;
168-
}
169144
}
170145

171146
function indexGreaterThan(index) {
@@ -180,7 +155,6 @@ function toGlob(obj) {
180155

181156
function globIsSingular(glob) {
182157
var globSet = glob.minimatch.set;
183-
184158
if (globSet.length !== 1) {
185159
return false;
186160
}
@@ -206,4 +180,8 @@ function getBasePath(ourGlob, opt) {
206180
return basePath;
207181
}
208182

183+
function stripExclamationMark(glob) {
184+
return glob.slice(1);
185+
}
186+
209187
module.exports = gs;

Diff for: package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
},
2323
"dependencies": {
2424
"extend": "^3.0.0",
25-
"glob": "^5.0.3",
25+
"glob": "^7.0.6",
2626
"glob-parent": "^3.0.0",
27-
"micromatch": "^2.3.7",
2827
"ordered-read-streams": "^0.3.0",
2928
"through2": "^0.6.0",
3029
"to-absolute-glob": "^0.1.1",

Diff for: test/main.js

-18
Original file line numberDiff line numberDiff line change
@@ -530,24 +530,6 @@ describe('glob-stream', function() {
530530
});
531531
});
532532

533-
it('should handle RegExps as negative matchers', function(done) {
534-
var stream = gs.create(['./fixtures/stuff/*.dmc', /run/], { cwd: __dirname });
535-
should.exist(stream);
536-
stream.on('error', function(err) {
537-
throw err;
538-
});
539-
stream.on('data', function(file) {
540-
should.exist(file);
541-
should.exist(file.path);
542-
should.exist(file.base);
543-
should.exist(file.cwd);
544-
String(file.cwd).should.equal(__dirname);
545-
String(file.base).should.equal(join(__dirname, 'fixtures', 'stuff' + sep));
546-
String(join(file.path,'')).should.equal(join(__dirname, './fixtures/stuff/run.dmc'));
547-
done();
548-
});
549-
});
550-
551533
it('should throw on invalid glob argument', function() {
552534
gs.create.bind(gs, 42, { cwd: __dirname }).should.throw(/Invalid glob .* 0/);
553535
gs.create.bind(gs, ['.', 42], { cwd: __dirname }).should.throw(/Invalid glob .* 1/);

0 commit comments

Comments
 (0)