Skip to content

Commit 1956ae3

Browse files
committed
core: fix lodash template data
1 parent e2b12fb commit 1956ae3

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ module.exports = function gulpHeaderComment(options = {}) {
3939

4040
read(options)
4141
.then((content) => {
42-
const template = _.template(content, {_, moment})();
42+
const templateFn = _.template(content);
43+
const template = templateFn({_, moment});
4344
const extension = path.extname(file.path);
4445
const header = commenting(template.trim(), {extension}) + separator;
4546

test/index.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const fs = require('fs');
2626
const path = require('path');
2727
const Vinyl = require('vinyl');
2828
const Stream = require('stream');
29+
const moment = require('moment');
2930
const gulpHeaderComment = require('../src/index');
3031
const EOL = '\n';
3132

@@ -296,4 +297,35 @@ describe('gulp-header-comment', () => {
296297
stream.write(vinyl);
297298
stream.end();
298299
});
300+
301+
it('should prepend header with interpolated template', (done) => {
302+
const filePath = path.join(base, 'test.js');
303+
const code = fs.readFileSync(filePath, 'utf-8');
304+
expect(code).not.toEqual('');
305+
306+
const contents = new Buffer(code);
307+
const vinyl = new Vinyl({cwd, base, contents, path: filePath});
308+
const header = `Generated on <%= moment().format('YYYY') %>`;
309+
const expectedHeader =
310+
'/**' + EOL +
311+
' * Generated on ' + moment().format('YYYY') + EOL +
312+
' */';
313+
314+
const stream = gulpHeaderComment(header);
315+
316+
stream.on('data', (newFile) => {
317+
expect(newFile.contents).toEqual(new Buffer(expectedHeader + EOL + EOL + code));
318+
});
319+
320+
stream.once('error', (err) => {
321+
done.fail(err);
322+
});
323+
324+
stream.once('end', () => {
325+
done();
326+
});
327+
328+
stream.write(vinyl);
329+
stream.end();
330+
});
299331
});

0 commit comments

Comments
 (0)