Skip to content

throw error if nothing to inject #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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