-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
41 lines (32 loc) · 1.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';
var through = require('through2');
var path = require('path');
var extend = require('node.extend');
var _ = require('lodash');
module.exports = function(options) {
options = extend({
template: 'define([], function() { return <%= content %>; });',
ext: '.js',
isExtensionAppended: true
}, options);
var stream = through.obj(function(file, enc, callback) {
if (file.isNull() || !file.contents || file.isStream() || !file.contents.length) {
this.push(file);
callback();
return;
}
try {
var fileContent = file.contents.toString('utf8');
if (options.isExtensionAppended) {
file.path += options.ext;
} else if (options.ext) {
file.path = file.path.replace(path.extname(file.path), options.ext);
}
file.contents = new Buffer(_.template(typeof options.template === 'function' ? options.template(file) : options.template, { content: JSON.stringify(fileContent) }));
}
catch (e) { console.log('error: ' + e); }
this.push(file);
callback();
});
return stream;
};