-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
executable file
·115 lines (94 loc) · 2.63 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'use strict';
var fs = require('fs');
var path = require('path');
var gutil = require('gulp-util');
var through = require('through2');
var TmodJS = require('tmodjs');
var extend = require("xtend");
var PluginError = gutil.PluginError;
var File = gutil.File;
var PLUGIN_NAME = 'gulp-tmod';
module.exports = function(opt) {
var templatePaths = [];
var defaults = {
type: 'default',
combo: true,
watch: false,
minify: false,
output: false,
verbose: false,
runtime: 'template.js',
templateBase: process.cwd()
};
var config = extend({}, defaults, opt);
config.runtime = path.basename(config.runtime);
if(config.type !== 'default') config.combo = false;
if(config.output !== false) config.output = path.join(process.cwd(), config.output);
var tmodjs = new TmodJS(config.templateBase, config);
var transformFiles = [];
var hasOnCompile = false;
var compileCount = 0;
function transform(file, enc, cb) {
if (file.isNull()) {
cb();
return;
}
var templatePath = path.normalize(path.relative(config.templateBase, file.path));
if(!config.combo) {
if (!hasOnCompile) {
hasOnCompile = true;
tmodjs.on('compile', function(error, data) {
if (error) {
cb(new gutil.PluginError(PLUGIN_NAME, error));
return;
}
var cfile = transformFiles[compileCount];
cfile.contents = new Buffer(data.output);
cfile.path = gutil.replaceExtension(transformFiles[compileCount++].path, '.js');
this.push(cfile);
cb();
}.bind(this));
}
transformFiles.push(file);
tmodjs.compile(templatePath);
} else {
templatePaths.push(templatePath);
cb();
}
}
function flush(cb) {
if (config.combo && templatePaths.length) {
tmodjs.on('combo', function (error, data) {
if (error) {
cb(new gutil.PluginError(PLUGIN_NAME, error));
return;
}
var file = new File({
path: config.runtime,
contents: new Buffer(data.output)
});
this.push(file);
cb();
}.bind(this));
tmodjs.compile(templatePaths);
return;
}
if (!config.combo && compileCount) {
tmodjs._buildRuntime(null, null, function(error, data) {
if (error) {
cb(new gutil.PluginError(PLUGIN_NAME, error));
return;
}
var file = new File({
path: config.runtime,
contents: new Buffer(data)
});
this.push(file);
cb();
return;
}.bind(this))
}
cb();
}
return through.obj(transform, flush);
}