Skip to content

Commit

Permalink
feat: throw error if nothing to inject
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuzuld committed Dec 9, 2018
1 parent 59bf9c6 commit eb4a618
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!***
Expand Down
3 changes: 3 additions & 0 deletions src/inject/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) + '.');
}
Expand Down
44 changes: 44 additions & 0 deletions src/inject/inject_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit eb4a618

Please sign in to comment.