-
Notifications
You must be signed in to change notification settings - Fork 0
/
templateready.js
333 lines (270 loc) · 9.14 KB
/
templateready.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* User: Staltec
* Date: 05.11.12
* Time: 22:42
*/
var util = require("util")
, path = require("path")
, fs = require("fs")
, async = require("async")
, jsp = require("uglify-js").parser
, pro = require("uglify-js").uglify
, zlib = require('zlib')
, meta = require("./package.json");
var cfg = {}
, sourceDirRegExp
, templateNames
, runtimeCode
, alreadyWatched = {}
, compilers = [];
exports.addEngine = function(engine, forcePattern){
if(engine && engine.filePattern instanceof RegExp && engine.compiler instanceof Function){
if(forcePattern instanceof RegExp) engine.filePattern = forcePattern;
compilers.push(engine);
}
return this;
};
exports.run = function (args){
var arg;
if(!args.length) return help();
var nodeVersion = process.version.split(".");
cfg.isWindowsWithoutWatchFile = process.platform === 'win32' && parseInt(nodeVersion[1]) <= 6;
cfg.poll_interval = 100;
while (arg = args.shift()) {
if(arg === "--help" || arg === "-h" || arg === "-?"){
return help();
} else if(arg === "--version" || arg === "-V"){
return console.log('templateready version: '+meta.version);
} else if(arg === "--watch" || arg === "-w"){
cfg.watch = true;
} else if(arg === "--runtime" || arg === "-r"){
cfg.runtime = true;
} else if(arg === "--dir" || arg === "-d"){
cfg.workDir = args.shift();
} else if(arg === "--file" || arg === "-f"){
cfg.outFile = args.shift();
} else if(arg === "--source" || arg === "-s"){
cfg.sourceDir = args.shift();
} else if(arg === "--target" || arg === "-t"){
cfg.targetOject = args.shift();
} else if (arg === "--poll-interval" || arg === "-p") {
cfg.poll_interval = parseInt(args.shift());
} else if (arg === "--quiet" || arg === "-q") {
cfg.debug = false;
util.debug = function(){};
util.puts = function(){};
}
}
if(!cfg.workDir){
cfg.workDir = ".";
}
cfg.workDir = path.resolve(cfg.workDir);
if(!cfg.outFile){
cfg.outFile = cfg.workDir+"/templates";
}else{
if(!/\//.test(cfg.outFile)){
cfg.outFile = cfg.workDir+"/"+cfg.outFile;
}
}
cfg.outFile = cfg.outFile.replace(/\.js$/,'');
if(!cfg.sourceDir){
cfg.sourceDir = cfg.workDir+"/templates";
}else{
if(!/\//.test(cfg.sourceDir)){
cfg.sourceDir = cfg.workDir+"/"+cfg.sourceDir;
}
}
cfg.sourceDir = path.resolve(cfg.sourceDir);
sourceDirRegExp = new RegExp('^'+cfg.sourceDir+'\/?');
if(!cfg.targetOject){
cfg.targetOject = "TemplateReady";
}
getWatchedFiles(cfg.sourceDir);
return true;
};
function getWatchedFiles (dir, state){
dir = path.resolve(dir);
if(!state) state = {
files: [],
entropia: 0
};
state.entropia++;
fs.stat(dir, function(err, stats){
state.entropia--;
if (err) {
util.error('\nError retrieving stats for file: ' + dir+'\n');
} else {
if (stats.isDirectory()){
if(cfg.isWindowsWithoutWatchFile) watchGivenFile(dir);
state.entropia++;
fs.readdir(dir, function(err, fileNames) {
state.entropia--;
if(err) {
util.error('\nError reading path: ' + dir+'\n');
}
else {
fileNames.forEach(function (fileName) {
getWatchedFiles(path.join(dir, fileName), state);
});
}
if(state.entropia<1) compileFiles(state.files);
});
}else{
state.files.push(dir);
if(state.entropia<1) compileFiles(state.files);
}
}
});
}
function minifier(code){
var ast = jsp.parse(code); // parse code and get the initial AST
ast = pro.ast_mangle(ast); // get a new AST with mangled names
ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
return pro.gen_code(ast); // compressed code here
}
var _requireSample = function (n){ return this[this._n[n]] };
function compileFiles (files){
var hdr, buf = '';
templateNames = {};
runtimeCode = {};
async.map(files,
function(f, cb){
compileFile(f, cb);
},
function(err, res){
hdr = '// Assembled by TemplateReady '+meta.version+'\n';
hdr += '// At '+new Date()+'\n\n';
buf += 'var '+cfg.targetOject+' = {\n\n';
buf += res.join('\n\n')+'\n\n';
buf += '_n: '+JSON.stringify(templateNames)+',\n';
buf += 'require: '+_requireSample.toString()+'\n';
buf += '}\n\n';
for(var key in runtimeCode) if (runtimeCode.hasOwnProperty(key)) {
buf += runtimeCode[key]+'\n\n';
}
// Save common JS file
saveToFile(cfg.outFile+'.js', hdr + buf);
// Save minified JS file
buf = minifier(buf);
saveToFile(cfg.outFile+'.min.js', buf);
// Save compressed JS file
zlib.gzip(buf, function(err, result){
saveToFile(cfg.outFile+'.min.js.gz', result);
});
util.debug('save canges...');
})
}
function saveToFile(name, data){
fs.writeFile(name, data, 'utf8', function(err){
if(err) util.error('\nCan`t write script file: "'+name+'"\n');
});
}
function getFuncName(file){
var fileName = file.replace(sourceDirRegExp,'');
var funcName = fileName.replace(/ /g,'_').replace(/\..+?$/,'').replace(/\/\w/g, function(t){return t.charAt(1).toUpperCase()});
templateNames[fileName] = funcName;
return funcName;
}
function compileFile (file, callback){
var engine;
for(var i=0, l=compilers.length; i<l; i++){
if(compilers[i].filePattern.test(file)){
engine = compilers[i];
break;
}
}
if(engine && engine.compiler){
engine.compiler({file:file}, function(err, funcCode){
if(err){
callback(err, '');
util.error('Trouble with template file: ' + file + ' > '+ err);
}else{
// get runtime code if exists
if(cfg.runtime && engine.type && engine.runtime) runtimeCode[engine.type] = engine.runtime;
// return template code
callback(null, funcCode ? getFuncName(file)+': ' + funcCode + ',' : '');
}
if(!cfg.isWindowsWithoutWatchFile) watchGivenFile(file);
});
}else{
callback(null, '');
}
}
function watchGivenFile (file){
if(cfg.watch && !alreadyWatched[file]){
alreadyWatched[file] = true;
if(cfg.isWindowsWithoutWatchFile){
fs.watch(file, { persistent: true, interval: cfg.poll_interval }, onChangeWin);
}else{
fs.watchFile(file, { persistent: true, interval: cfg.poll_interval }, onChangeOther);
}
}
}
function onChangeWin(event, filename){
if( event === 'change' && filename) {
getWatchedFiles(cfg.sourceDir);
}
}
function onChangeOther(oldStat, newStat){
if ( newStat.mtime.getTime() !== oldStat.mtime.getTime() ) {
getWatchedFiles(cfg.sourceDir);
}
}
function help (){
function print (m, n) { util.print(m+(!n?"\n":"")); return print; }
print
("")
("Template Ready is template on the fly pre-compiler.")
("")
("Usage:")
(" templateready [options]")
("")
("Options:")
(" -d|--dir <workDirectory>")
(" The work directory. Use as base path for source directory and output JS")
(" Default is '.'")
("")
(" -s|--source <sourceDirectory>")
(" The source directory with templates.")
(" If set directory name without path then use work directory as base path: <workDirectory>/templates/")
(" If set directory name with path then use it as is.")
(" Default is 'templates'")
("")
(" -f|--file <outputFile>")
(" The name of output script file.")
(" If set file name without path then use work directory as base path: <workDirectory>/templates.js")
(" If set file name with path then use it as is.")
(" Pre-comiler will generate common and minified versions of output files.")
(" Default is 'templates.js'")
("")
(" -t| --target <targetVariable>")
(" Name of the global object containing templates.")
(" Default is 'TemplateReady'")
("")
(" -r| --runtime")
(" Add template library runtime code (if exists) into the output script file.")
(" No default")
("")
(" -w|--watch")
(" When a change template file occurs, rebuild output js file.")
(" No default")
("")
(" -p|--poll-interval <milliseconds>")
(" How often to poll watched files for changes.")
(" Defaults to Node default.")
("")
(" -h|--help|-?")
(" Display these usage instructions.")
("")
(" -q|--quiet")
(" Suppress DEBUG messages")
("")
(" -V|--version")
(" Show version and exit.")
("")
("Examples:")
(" templateready -d ./wwwroot/app --runtime")
(" templateready -d ./wwwroot/app -s mytemplates -w -p 1000")
(" templateready --source ./wwwroot/mytemplates --file ./wwwroot/app/comiled.js --target 'MyApplicationTemplates'")
("");
}