Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing node-glob's ignore option, fixes #24 #40

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 19 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 */){
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 0 additions & 18 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
Expand Down