Skip to content

Commit

Permalink
fix: only log file count for files actually injected (closes #184)
Browse files Browse the repository at this point in the history
  • Loading branch information
joakimbeng committed May 18, 2016
1 parent 68add8a commit b4fd0d6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"scripts": {
"lint": "xo",
"pretest": "npm run -s lint",
"test": "mocha -R spec src/**/*_test.js"
"test": "mocha -R spec src/**/*_test.js",
"release": "standard-version"
},
"dependencies": {
"arrify": "^1.0.1",
Expand All @@ -39,6 +40,8 @@
"devDependencies": {
"mocha": "~2.0.1",
"should": "^4.0.4",
"standard-version": "^2.2.1",
"strip-color": "^0.1.0",
"xo": "^0.13.0"
},
"engines": {
Expand All @@ -51,7 +54,10 @@
"node"
],
"rules": {
"object-shorthand": [2, "never"]
"object-shorthand": [
2,
"never"
]
}
}
}
20 changes: 16 additions & 4 deletions src/inject/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var getFilepath = require('../path');
var PluginError = gutil.PluginError;
var magenta = gutil.colors.magenta;
var cyan = gutil.colors.cyan;
var noop = function noop() {};

/**
* Constants
Expand Down Expand Up @@ -103,19 +104,20 @@ function handleVinylStream(sources, opt) {
* @returns {Buffer}
*/
function getNewContent(target, collection, opt) {
if (!opt.quiet) {
if (collection.length) {
log(cyan(collection.length) + ' files into ' + magenta(target.relative) + '.');
var logger = opt.quiet ? noop : function (filesCount) {
if (filesCount) {
log(cyan(filesCount) + ' files into ' + magenta(target.relative) + '.');
} else {
log('Nothing to inject into ' + magenta(target.relative) + '.');
}
}
};
var content = String(target.contents);
var targetExt = extname(target.path);
var files = prepareFiles(collection, targetExt, opt);
var filesPerTags = groupArray(files, 'tagKey');
var startAndEndTags = Object.keys(filesPerTags);
var matches = [];
var injectedFilesCount = 0;

startAndEndTags.forEach(function (tagKey) {
var files = filesPerTags[tagKey];
Expand All @@ -128,12 +130,17 @@ function getNewContent(target, collection, opt) {
endTag: endTag,
tagsToInject: tagsToInject,
removeTags: opt.removeTags,
willInject: function (filesToInject) {
injectedFilesCount += filesToInject.length;
},
onMatch: function (match) {
matches.push(match[0]);
}
});
});

logger(injectedFilesCount);

if (opt.empty) {
var ext = '{{ANY}}';
var startTag = getTagRegExp(opt.tags.start(targetExt, ext, opt.starttag), ext, opt);
Expand Down Expand Up @@ -191,6 +198,11 @@ function inject(content, opt) {
throw error('Missing end tag for start tag: ' + startMatch[0]);
}
var toInject = opt.tagsToInject.slice();

if (typeof opt.willInject === 'function') {
opt.willInject(toInject);
}

// <everything before startMatch>:
var newContents = content.slice(0, startMatch.index);

Expand Down
28 changes: 28 additions & 0 deletions src/inject/inject_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var path = require('path');
var es = require('event-stream');
var should = require('should');
var gutil = require('gulp-util');
var stripColor = require('strip-color');
var inject = require('../../.');

describe('gulp-inject', function () {
Expand Down Expand Up @@ -522,6 +523,33 @@ describe('gulp-inject', function () {
});
});

it('should produce log output only for files actually injected (issue #184)', function (done) {
var logOutput = [];
gutil.log = function (a, b) {
logOutput.push(a + ' ' + b);
};

var target = src(['template2.html'], {read: true});
var sources = src([
'lib.js',
'component.html',
'styles.css',
'image.png'
]);

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);
stripColor(logOutput[0]).should.equal('gulp-inject 1 files into template2.html.');
done();
});
});

it('should be able to modify only the filepath (Issue #107)', function (done) {
var version = '1.0.0';

Expand Down

0 comments on commit b4fd0d6

Please sign in to comment.