Skip to content

Commit b4fd0d6

Browse files
committed
fix: only log file count for files actually injected (closes #184)
1 parent 68add8a commit b4fd0d6

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"scripts": {
2626
"lint": "xo",
2727
"pretest": "npm run -s lint",
28-
"test": "mocha -R spec src/**/*_test.js"
28+
"test": "mocha -R spec src/**/*_test.js",
29+
"release": "standard-version"
2930
},
3031
"dependencies": {
3132
"arrify": "^1.0.1",
@@ -39,6 +40,8 @@
3940
"devDependencies": {
4041
"mocha": "~2.0.1",
4142
"should": "^4.0.4",
43+
"standard-version": "^2.2.1",
44+
"strip-color": "^0.1.0",
4245
"xo": "^0.13.0"
4346
},
4447
"engines": {
@@ -51,7 +54,10 @@
5154
"node"
5255
],
5356
"rules": {
54-
"object-shorthand": [2, "never"]
57+
"object-shorthand": [
58+
2,
59+
"never"
60+
]
5561
}
5662
}
5763
}

src/inject/index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var getFilepath = require('../path');
1212
var PluginError = gutil.PluginError;
1313
var magenta = gutil.colors.magenta;
1414
var cyan = gutil.colors.cyan;
15+
var noop = function noop() {};
1516

1617
/**
1718
* Constants
@@ -103,19 +104,20 @@ function handleVinylStream(sources, opt) {
103104
* @returns {Buffer}
104105
*/
105106
function getNewContent(target, collection, opt) {
106-
if (!opt.quiet) {
107-
if (collection.length) {
108-
log(cyan(collection.length) + ' files into ' + magenta(target.relative) + '.');
107+
var logger = opt.quiet ? noop : function (filesCount) {
108+
if (filesCount) {
109+
log(cyan(filesCount) + ' files into ' + magenta(target.relative) + '.');
109110
} else {
110111
log('Nothing to inject into ' + magenta(target.relative) + '.');
111112
}
112-
}
113+
};
113114
var content = String(target.contents);
114115
var targetExt = extname(target.path);
115116
var files = prepareFiles(collection, targetExt, opt);
116117
var filesPerTags = groupArray(files, 'tagKey');
117118
var startAndEndTags = Object.keys(filesPerTags);
118119
var matches = [];
120+
var injectedFilesCount = 0;
119121

120122
startAndEndTags.forEach(function (tagKey) {
121123
var files = filesPerTags[tagKey];
@@ -128,12 +130,17 @@ function getNewContent(target, collection, opt) {
128130
endTag: endTag,
129131
tagsToInject: tagsToInject,
130132
removeTags: opt.removeTags,
133+
willInject: function (filesToInject) {
134+
injectedFilesCount += filesToInject.length;
135+
},
131136
onMatch: function (match) {
132137
matches.push(match[0]);
133138
}
134139
});
135140
});
136141

142+
logger(injectedFilesCount);
143+
137144
if (opt.empty) {
138145
var ext = '{{ANY}}';
139146
var startTag = getTagRegExp(opt.tags.start(targetExt, ext, opt.starttag), ext, opt);
@@ -191,6 +198,11 @@ function inject(content, opt) {
191198
throw error('Missing end tag for start tag: ' + startMatch[0]);
192199
}
193200
var toInject = opt.tagsToInject.slice();
201+
202+
if (typeof opt.willInject === 'function') {
203+
opt.willInject(toInject);
204+
}
205+
194206
// <everything before startMatch>:
195207
var newContents = content.slice(0, startMatch.index);
196208

src/inject/inject_test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var path = require('path');
66
var es = require('event-stream');
77
var should = require('should');
88
var gutil = require('gulp-util');
9+
var stripColor = require('strip-color');
910
var inject = require('../../.');
1011

1112
describe('gulp-inject', function () {
@@ -522,6 +523,33 @@ describe('gulp-inject', function () {
522523
});
523524
});
524525

526+
it('should produce log output only for files actually injected (issue #184)', function (done) {
527+
var logOutput = [];
528+
gutil.log = function (a, b) {
529+
logOutput.push(a + ' ' + b);
530+
};
531+
532+
var target = src(['template2.html'], {read: true});
533+
var sources = src([
534+
'lib.js',
535+
'component.html',
536+
'styles.css',
537+
'image.png'
538+
]);
539+
540+
var stream = target.pipe(inject(sources));
541+
542+
// Dummy data reader to make the `end` event be triggered
543+
stream.on('data', function () {
544+
});
545+
546+
stream.on('end', function () {
547+
logOutput.should.have.length(1);
548+
stripColor(logOutput[0]).should.equal('gulp-inject 1 files into template2.html.');
549+
done();
550+
});
551+
});
552+
525553
it('should be able to modify only the filepath (Issue #107)', function (done) {
526554
var version = '1.0.0';
527555

0 commit comments

Comments
 (0)