-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
228 lines (167 loc) · 4.96 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
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
#!/usr/bin/env node
global.Snakeskin = require('./snakeskin');
var program = require('commander');
var beautify = require('js-beautify');
var path = require('path');
var fs = require('fs'),
exists = fs.existsSync || path.existsSync;
program
.version(Snakeskin.VERSION.join('.'))
.usage('[options] [dir|file ...]')
.option('-p, --params [options]', 'path to the options file (JSON) or options JSON')
.option('-s, --source [src]', 'path to the template file')
.option('-f, --file [src]', 'path to the template file (meta-information)')
.option('-o, --output [src]', 'path to the file to save')
.option('-n, --common-js', 'common.js export (for node.js)')
.option('-e, --exec', 'execute compiled template')
.option('-d, --data [src]', 'path to the data file (JSON) or data JSON')
.option('-t, --tpl [name]', 'name of the main template')
.option('--disable-localization', 'disable support for localization')
.option('--i18n-fn', 'i18n function name')
.option('--language [src]', 'path to the localization file (JSON) or localization JSON')
.option('--words [src]', 'path to the localization file to save')
.option('--disable-xml', 'disable default xml validation')
.option('--interface', 'render all templates as interface')
.option('--string-buffer', 'use StringBuffer for concatenate strings')
.option('--inline-iterators', 'inline forEach and forIn')
.option('--disable-escape-output', 'disable default "html" filter')
.option('--pretty-print', 'formatting output')
.parse(process.argv);
var params = program['params'] ?
parse(params) : {};
params.xml = 'disableXml' in program ?
!program['disableXml'] : params.xml;
params.commonJS = 'commonJs' in program ?
program['commonJs'] : params.commonJS;
params.localization = 'disableLocalization' in program ?
!program['disableLocalization'] : params.localization;
params.i18nFn = 'i18nFn' in program ?
program['i18nFn'] : params.i18nFn;
params.language = 'language' in program ?
program['language'] : params.language;
params.words = 'words' in program ?
program['words'] : params.words;
params.interface = 'interface' in program ?
program['interface'] : params.interface;
params.stringBuffer = 'stringBuffer' in program ?
program['stringBuffer'] : params.stringBuffer;
params.inlineIterators = 'inlineIterators' in program ?
program['inlineIterators'] : params.inlineIterators;
params.escapeOutput = 'disableEscapeOutput' in program ?
!program['disableEscapeOutput'] : params.escapeOutput;
params.prettyPrint = 'prettyPrint' in program ?
program['prettyPrint'] : params.prettyPrint;
var prettyPrint = params.prettyPrint;
if (params.language) {
params.language = parse(params.language);
}
var exec = program['exec'];
var tplData = program['data'],
mainTpl = program['tpl'];
var words = params.words;
if (words) {
params.words = {};
}
var args = program['args'],
input;
var file = program['source'],
newFile = program['output'];
if (!file && args.length) {
input = args.join(' ');
if (exists(input)) {
file = input;
input = false;
}
}
function parse(val) {
if (exists(val)) {
return JSON.parse(fs.readFileSync(val));
}
var res;
try {
res = eval((("(" + val) + ")"));
} catch (ignore) {
res = {};
}
return res || {};
}
function action(data) {
var tpls = {};
if (tplData || mainTpl || exec) {
params.commonJS = true;
params.context = tpls;
params.prettyPrint = false;
}
var res = Snakeskin.compile(
String(data),
params,
{
file: program['file'] || file
}
);
var toConsole = input && !program['output'] ||
!newFile;
if (res !== false) {
if (tplData || mainTpl || exec) {
var tpl;
if (mainTpl && mainTpl !== true) {
tpl = tpls[mainTpl];
} else {
if (file) {
tpl = tpls[path.basename(file, path.extname(file))] || tpls.main || tpls[Object.keys(tpls)[0]];
} else {
tpl = tpls.main || tpls[Object.keys(tpls)[0]];
}
}
if (!tpl) {
console.error('Template is not defined');
process.exit(1);
}
if (tplData && tplData !== true) {
tplData = parse(tplData);
} else {
tplData = void 0;
}
res = tpl(tplData);
if (prettyPrint) {
if (toConsole) {
res = beautify['html'](res);
} else {
res = beautify[path.extname(newFile).replace(/^\./, '')](res);
}
}
}
if (toConsole) {
console.log(res);
} else {
fs.writeFileSync(newFile, res);
console.log((("File \"" + file) + ("\" has been successfully compiled \"" + newFile) + "\"."));
}
} else {
process.exit(1);
}
if (words) {
fs.writeFileSync(words, JSON.stringify(params.words, null, '\t'));
}
process.exit(0);
}
if (!file && input == null) {
var buf = '';
var stdin = process.stdin,
stdout = process.stdout;
stdin.setEncoding('utf8');
stdin.on('data', function(chunk) {
buf += chunk;
});
stdin.on('end', function() {
action(buf);
}).resume();
process.on('SIGINT', function() {
stdout.write('\n');
stdin.emit('end');
stdout.write('\n');
process.exit();
});
} else {
action(file ? fs.readFileSync(file) : input);
}