diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5d12634 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/README.md b/README.md index 3dc0faf..b36d7fe 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,9 @@ You can pass any combination of globs. One caveat is that you can not only pass - cwdbase - Default is `false` - When true it is the same as saying opt.base = opt.cwd +- allowEmpty + - Default is `false` + - If true, won't emit an error when a glob pointing at a single file fails to match This argument is passed directly to [node-glob](https://github.com/isaacs/node-glob) so check there for more options diff --git a/index.js b/index.js index 60dd858..e351051 100644 --- a/index.js +++ b/index.js @@ -26,11 +26,19 @@ var gs = { // create stream and map events from globber to it var stream = through2.obj(negatives.length ? filterNegatives : undefined); + var found = false; + globber.on('error', stream.emit.bind(stream, 'error')); - globber.on('end', function(/* some args here so can't use bind directly */){ + globber.on('end', function(){ + if (opt.allowEmpty !== true && !found && globIsSingular(globber)) { + stream.emit('error', new Error('File not found with singular glob')); + } + stream.end(); }); globber.on('match', function(filename) { + found = true; + stream.write({ cwd: opt.cwd, base: basePath, @@ -95,8 +103,13 @@ var gs = { // then just pipe them to a single unique stream and return it var aggregate = new Combine(streams); var uniqueStream = unique('path'); + var returnStream = aggregate.pipe(uniqueStream); + + aggregate.on('error', function (err) { + returnStream.emit('error', err); + }); - return aggregate.pipe(uniqueStream); + return returnStream; function streamFromPositive(positive) { var negativeGlobs = negatives.filter(indexGreaterThan(positive.index)).map(toGlob); @@ -134,4 +147,16 @@ function toGlob(obj) { return obj.glob; } +function globIsSingular(glob) { + var globSet = glob.minimatch.set; + + if (globSet.length !== 1) { + return false; + } + + return globSet[0].every(function isString(value) { + return typeof value === 'string'; + }); +} + module.exports = gs; diff --git a/test/main.js b/test/main.js index 4e3df25..bca757f 100644 --- a/test/main.js +++ b/test/main.js @@ -75,7 +75,7 @@ describe('glob-stream', function() { var baseDir = join(__dirname, './fixtures'); var globArray = [ - './whatsgoingon/key/isaidhey/whatsgoingon/test.txt', + './whatsgoingon/hey/isaidhey/whatsgoingon/test.txt', './test.coffee', './whatsgoingon/test.js' ]; @@ -97,7 +97,7 @@ describe('glob-stream', function() { var baseDir = join(__dirname, './fixtures'); var globArray = [ - './whatsgoingon/key/isaidhey/whatsgoingon/test.txt', + './whatsgoingon/hey/isaidhey/whatsgoingon/test.txt', './test.coffee', './whatsgoingon/test.js' ]; @@ -307,7 +307,7 @@ describe('glob-stream', function() { var baseDir = join(__dirname, './fixtures'); var globArray = [ - './whatsgoingon/key/isaidhey/whatsgoingon/test.txt', + './whatsgoingon/hey/isaidhey/whatsgoingon/test.txt', './test.coffee', './whatsgoingon/test.js' ]; @@ -484,5 +484,45 @@ describe('glob-stream', function() { gs.create.bind(gs, ['!a', '!b'], {cwd: __dirname}).should.throw(/Missing positive glob/); }); + it('should emit error on singular glob when file not found', function(done) { + var stream = gs.create('notfound'); + should.exist(stream); + stream.on('error', function(err) { + err.should.match(/File not found with singular glob/); + done(); + }); + }); + + it('should emit error when a glob in multiple globs not found', function(done) { + var stream = gs.create(['notfound', './fixtures/whatsgoingon'], {cwd: __dirname}); + should.exist(stream); + stream.on('error', function(err) { + err.should.match(/File not found with singular glob/); + done(); + }); + }); + + it('should not emit error on glob containing {} when not found', function(done) { + var stream = gs.create('notfound{a,b}'); + should.exist(stream); + stream.on('error', function() { + throw new Error('Error was emitted'); + }); + + stream.resume(); + stream.once('end', done); + }); + + it('should not emit error on singular glob when allowEmpty is true', function(done) { + var stream = gs.create('notfound', { allowEmpty: true }); + should.exist(stream); + stream.on('error', function() { + throw new Error('Error was emitted'); + }); + + stream.resume(); + stream.once('end', done); + }); + }); });