-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.js
executable file
·58 lines (48 loc) · 1.21 KB
/
factory.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var _ = require('lodash');
var through = require('through2');
var gutil = require('gulp-util');
var name = 'gulp-oswst';
module.exports = function(T) {
var plugin = function(options){
var options = _.defaults(_.isObject(options)? options : {}, plugin.options);
return through.obj(function(file, enc, callback){
var flow = this;
if (file.isNull()) {
flow.push(file);
} else if (file.isStream()) {
this.emit(
'error',
new gutil.PluginError(name, 'Streaming not supported')
);
} else if(file.isBuffer()){
try {
options.handler(
T.compile(String(file.contents), file.path),
options,
file,
function(error, result) {
file.contents = new Buffer(result);
file.path = gutil.replaceExtension(file.path, '.html');
flow.push(file);
}
);
} catch(error) {
callback(new gutil.PluginError(name, error));
}
} else {
callback(null, file);
}
callback();
});
};
plugin.options = {
arguments: [],
context: {},
handler: function(template, options, file, callback) {
T.Module(template)
.apply(null, options.arguments)
.render(callback, options.context);
}
};
return plugin;
};