Skip to content

Commit

Permalink
Merge pull request #27 from UltCombo/globs-order
Browse files Browse the repository at this point in the history
Respect globs array order, fixes #25
  • Loading branch information
Eric Schoffstall committed Dec 28, 2014
2 parents fd2bb92 + d6f3860 commit 16daaee
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
35 changes: 26 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,29 @@ var gs = {
// only one glob no need to aggregate
if (!Array.isArray(globs)) return gs.createStream(globs, null, opt);

var positives = globs.filter(isPositive);
var negatives = globs.filter(isNegative);
var positives = [];
var negatives = [];

globs.forEach(function(glob, index) {
var globArray = isNegative(glob) ? negatives : positives;
globArray.push({
index: index,
glob: glob
});
});

if (positives.length === 0) throw new Error("Missing positive glob");

// only one positive glob no need to aggregate
if (positives.length === 1) return gs.createStream(positives[0], negatives, opt);
if (positives.length === 1) {
var negativeGlobs = negatives.filter(indexGreaterThan(positives[0].index)).map(toGlob);
return gs.createStream(positives[0].glob, negativeGlobs, opt);
}

// create all individual streams
var streams = positives.map(function(glob){
return gs.createStream(glob, negatives, opt);
var streams = positives.map(function(positive) {
var negativeGlobs = negatives.filter(indexGreaterThan(positive.index)).map(toGlob);
return gs.createStream(positive.glob, negativeGlobs, opt);
});

// then just pipe them to a single unique stream and return it
Expand All @@ -100,10 +112,6 @@ function isNegative(pattern) {
return false;
}

function isPositive(pattern) {
return !isNegative(pattern);
}

function unrelative(cwd, glob) {
var mod = '';
if (glob[0] === '!') {
Expand All @@ -113,5 +121,14 @@ function unrelative(cwd, glob) {
return mod+path.resolve(cwd, glob);
}

function indexGreaterThan(index) {
return function(obj) {
return obj.index > index;
};
}

function toGlob(obj) {
return obj.glob;
}

module.exports = gs;
37 changes: 36 additions & 1 deletion test/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var gs = require('../');
var through2 = require('through2');
var should = require('should');
require('mocha');
var path = require('path');
var join = path.join;
var sep = path.sep;
Expand Down Expand Up @@ -421,5 +420,41 @@ describe('glob-stream', function() {
});
});

it('should respect the globs array order', function(done) {
var stream = gs.create(['./fixtures/stuff/*', '!./fixtures/stuff/*.dmc', './fixtures/stuff/run.dmc'], {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 ignore leading negative globs', function(done) {
var stream = gs.create(['!./fixtures/stuff/*.dmc', './fixtures/stuff/run.dmc'], {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();
});
});

});
});

0 comments on commit 16daaee

Please sign in to comment.