diff --git a/index.js b/index.js index 0960c7c..53cdcd3 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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; diff --git a/test/main.js b/test/main.js index a08676d..6bdf8f4 100644 --- a/test/main.js +++ b/test/main.js @@ -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; @@ -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(); + }); + }); + }); });