From eb4a618d4fff559b90d4a9884502bedaf35e60a6 Mon Sep 17 00:00:00 2001 From: Tiberiu Zuld Date: Sun, 9 Dec 2018 18:03:52 +0200 Subject: [PATCH] feat: throw error if nothing to inject --- README.md | 8 +++++++ src/inject/index.js | 3 +++ src/inject/inject_test.js | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/README.md b/README.md index 4f34776..61b38d7 100644 --- a/README.md +++ b/README.md @@ -876,6 +876,14 @@ Default: `false` Lower the verbosity by setting this to true, suppressing the logging of successful injections. +#### options.throwErrorIfNoInject +Type: `Boolean` + +Default: `false` + +If there is nothing to inject throw an error. + + #### ~~options.templateString~~ ***DEPRECATED!*** diff --git a/src/inject/index.js b/src/inject/index.js index 5900d1d..c33b46b 100644 --- a/src/inject/index.js +++ b/src/inject/index.js @@ -46,6 +46,7 @@ module.exports = exports = function (sources, opt) { // Defaults: opt.quiet = bool(opt, 'quiet', false); + opt.throwErrorIfNoInject = bool(opt, 'throwErrorIfNoInject', false); opt.relative = bool(opt, 'relative', false); opt.addRootSlash = bool(opt, 'addRootSlash', !opt.relative); opt.transform = defaults(opt, 'transform', transform); @@ -109,6 +110,8 @@ function getNewContent(target, collection, opt) { if (filesCount) { var pluralState = filesCount > 1 ? 's' : ''; log(cyan(filesCount) + ' file' + pluralState + ' into ' + magenta(target.relative) + '.'); + } else if (opt.throwErrorIfNoInject === true) { + throw error('Nothing to inject into ' + magenta(target.relative) + '.'); } else { log('Nothing to inject into ' + magenta(target.relative) + '.'); } diff --git a/src/inject/inject_test.js b/src/inject/inject_test.js index c602a71..e3c6baf 100644 --- a/src/inject/inject_test.js +++ b/src/inject/inject_test.js @@ -701,6 +701,50 @@ describe('gulp-inject', function () { streamShouldContain(stream, ['emptyTags3.html'], done); }); + + it('should throw an error when there is nothing to inject', function (done) { + var logOutput = []; + fancyLog.info = function (a, b) { + logOutput.push(a + ' ' + b); + }; + + var target = src(['templateWithExistingData2.html'], {read: true}); + + var sources = src([]); + + var stream = target.pipe(inject(sources, {throwErrorIfNoInject: true})); + + // Dummy data reader to make the `end` event be triggered + stream.on('data', function () { + }); + + stream.on('error', function (error) { + error.should.not.be.undefined(); + logOutput.should.have.length(0); + done(); + }); + }); + + it('should log when there is nothing to inject', function (done) { + var logOutput = []; + fancyLog.info = function (a, b) { + logOutput.push(a + ' ' + b); + }; + var target = src(['templateWithExistingData2.html'], {read: true}); + + var sources = src([]); + + var stream = target.pipe(inject(sources)); + + // Dummy data reader to make the `end` event be triggered + stream.on('data', function () { + }); + + stream.on('end', function () { + logOutput.should.have.length(1); + done(); + }); + }); }); function src(files, opt) {