Skip to content

Commit

Permalink
Respect globs array order, fixes gulpjs#25
Browse files Browse the repository at this point in the history
  • Loading branch information
UltCombo committed Dec 26, 2014
1 parent 472b98e commit ea2f8e2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
28 changes: 23 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,26 @@ 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) {
(isPositive(glob) ? positives : negatives).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) {
return gs.createStream(positives[0].glob, negatives.filter(indexGreaterThan(positives[0].index)).map(toGlob), opt);
}

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

// then just pipe them to a single unique stream and return it
Expand Down Expand Up @@ -113,5 +122,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;
19 changes: 18 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,23 @@ 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();
});
});

});
});

0 comments on commit ea2f8e2

Please sign in to comment.