Skip to content

Commit

Permalink
Breaking: Upgrade to glob 7, implement ignore & remove RegExp as nega…
Browse files Browse the repository at this point in the history
…tive matchers (closes #24, closes #57)
  • Loading branch information
thirdcreed authored and phated committed Feb 21, 2017
1 parent 59f75c7 commit a961eb2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 56 deletions.
50 changes: 14 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);

Expand All @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -180,7 +155,6 @@ function toGlob(obj) {

function globIsSingular(glob) {
var globSet = glob.minimatch.set;

if (globSet.length !== 1) {
return false;
}
Expand All @@ -206,4 +180,8 @@ function getBasePath(ourGlob, opt) {
return basePath;
}

function stripExclamationMark(glob) {
return glob.slice(1);
}

module.exports = gs;
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 0 additions & 18 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
Expand Down

0 comments on commit a961eb2

Please sign in to comment.