UNPKG

1.21 kBJavaScriptView Raw
1'use strict';
2
3var es = require('event-stream');
4var gutil = require('gulp-util');
5var tpl = require('lodash.template');
6var fs = require('fs');
7var PluginError = gutil.PluginError;
8
9var PLUGIN_NAME = 'gulp-wrap';
10
11function compile(contents, template, data, options){
12 data = data !== undefined ? data : {};
13 data.contents = contents;
14 return tpl(template, data, options);
15}
16
17module.exports = function(opts, data, options){
18 if (!opts) {
19 throw new PluginError(PLUGIN_NAME, PLUGIN_NAME + ': Missing template parameter');
20 }
21
22 var template;
23
24 if (typeof(opts) === 'object') {
25 template = fs.readFileSync(opts.src, 'utf-8');
26 } else {
27 template = opts;
28 }
29
30 function wrap(file, callback){
31 if (gutil.isStream(file.contents)) {
32 var through = es.through();
33 var wait = es.wait(function(err, contents){
34 through.write(compile(contents, template, data, options));
35 through.end();
36 });
37 file.contents.pipe(wait);
38 file.contents = through;
39 }
40
41 if (gutil.isBuffer(file.contents)) {
42 file.contents = new Buffer(compile(file.contents.toString('utf-8'), template, data, options));
43 }
44
45 callback(null, file);
46 }
47
48 return es.map(wrap);
49};
\No newline at end of file