-
Notifications
You must be signed in to change notification settings - Fork 11
/
jst.js
executable file
·136 lines (119 loc) · 4.73 KB
/
jst.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
#!/usr/bin/env node
var nopt = require("nopt")
, fs = require('fs')
, coffee = require('coffee-script') /* only for watchr */
, watch = require('watchr').watch
, Path = require('path')
, join = Path.join
, _ = require('underscore')
, engines = require('../lib/index')
, allowedengine = ['string', 'underscore', '_', 'lodash', 'jquery-tmpl', 'handlebars', 'hbs', 'dust', 'hogan', 'mustache', 'mu']
, knownOpts = { "template" : allowedengine
, "inputdir" : Path
, "output" : Path
, "watch" : Boolean
, "namespace" : String
, "include" : String
, "stdout" : Boolean
, "verbose" : Boolean
, "variable" : String
}
, description = { "template" : "format : " + allowedengine.join('|')
, "inputdir" : "directory containings the templates to compile"
, "output" : "output where templates will be compiled"
, "watch" : "watch `inputdir` for change"
, "namespace" : "object in the browser containing the templates"
, "include" : "Glob patterns for templates files to include in `inputdir`"
, "stdout" : "Print the result in stdout instead of writing in a file"
, "verbose" : "Print logs for debug"
, "variable" : "Set templateSettings.variable"
}
, defaults = { "inputdir" : process.cwd()
, "watch" : false
, "namespace" : engines.defaults.namespace
, "include" : engines.defaults.include
, "stdout" : false
, "verbose" : engines.defaults.verbose
}
, shortHands = { "t" : ["--template"]
, "i" : ["--inputdir"]
, "o" : ["--output"]
, "w" : ["--watch"]
, "ns" : ["--namespace"]
, "I" : ["--include"]
, "s" : ["--stdout"]
, "v" : ["--verbose"]
, "V" : ["--variable"]
}
, options = nopt(knownOpts, shortHands, process.argv, 2)
, inputdir;
// defaults value
_(defaults).forEach(function(value, key){
options[key] = options[key] || value;
})
if(!options.template){
showUsage();
process.exit(-1);
function showUsage(){
var usage = 'Usage: jst [--template format: ' + allowedengine.join('|') + '] [INPUT_DIR] [OUTPUT?]';
var out = {}
, getLenght = function( it ){ return it.length }
, optsLen = _(_(description).keys() ).max(getLenght).length
, descLen = _(_(description).values()).max(getLenght).length
, shortLen = _(_(shortHands).keys() ).max(getLenght).length;
var shortHands2 = {};
_(shortHands).forEach(function(value, key){
var opt = value[0].replace('--', '');
shortHands2[opt] = key;
});
_(description).forEach(function(value, key){
var cmd = rpad('--' + key + ' -' + shortHands2[key], ' ', optsLen + 8);
var txt = cmd + value || '';
out[key] = ' ' + rpad(txt, ' ', optsLen + 12 + descLen);
});
_(defaults).forEach(function(value, key){
out[key] += value
});
// string right padding helper
function rpad(str, padString, length) {
while (str.length < length)
str = str + padString;
return str;
}
console.error(usage + '\n');
console.error(_(out).values().join('\n'));
}
}
if(options.argv.remain && options.argv.remain.length >=1 ) options.inputdir = options.argv.remain[0];
if(options.argv.remain && options.argv.remain.length >=2 ) options.output = options.argv.remain[1];
if(!options.output) options.stdout = true;
if(!options.inputdir) return optimist.showUsage();
if(!options.templates in engines) {
return console.error('--template ' + options.template + ' is not allowed. Use ' + allowedengine);
}
var inputdir = options.inputdir;
var engine = engines[options.template];
function compile(){
if( options.verbose ) console.log("Use template format : " + options.template);
engine( inputdir, options, function( err, compiledTemplates ){
if( err ) return engines.handleError( err );
write(compiledTemplates, function(err, output){
if( err ) return engines.handleError( err );
if( options.verbose ) console.log(output + " written.");
});
});
}
function write( data, callback ){
data = data.join('\n');
if(options.stdout) {
return console.log(data);
}
var output = options.output;
fs.writeFile( output, data, 'utf8', function( err ){
if( typeof callback == "function" ) callback(err, output);
});
};
compile();
if(options.watch){
return watch(options.inputdir, _.debounce(compile, 100));
}