-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
328 lines (281 loc) · 9.59 KB
/
server.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
/*
pp a javascript preprocessor
Copyright (c) 2010 Mike Coolin
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
var fs = require('fs'),
files = [],
opts = require('tav').set({
src: {
note: 'Source file(s) e.g. --src=file1.js,file2.js'
},
dst: {
note: 'Destination to write the file e.g. --dst=o.js'
},
def: {
note: 'Defintion(s) to create e.g. --def=DEBUG,NODE,BROWSER',
value: ''
},
debug : {
note : 'Enable debugging',
value : false
}
}, "Usage: npm Node-JavaScript-Preprocessor pp.js with the following options");
doPreprocess(opts);
function doPreprocess(opts){
if(debug){
console.log(opts);
}
var sa,
writestream = fs.createWriteStream(opts.dst,
{ flags: 'w',
encoding: 'utf8',
mode: 0666
}),
debug = opts.debug,
defines = [],
pplines=[],
comment = '// ',
commentCode='',
cnt;
// handle defines
sa=opts.def.split(',');
for(cnt=0;cnt<sa.length;cnt++){
defines.push(sa[cnt]);
log('Passed in define '+sa[cnt]);
}
sa=opts.src.split(',');
for(cnt=0;cnt<sa.length;cnt++){
loadFileandAddLineObject(sa[cnt],pplines);
}
ifPass(pplines, defines);
if(debug){
dumpLineObjarr(pplines);
}
for(cnt=0;cnt<pplines.length;cnt++){
if(pplines[cnt].addComment){
commentCode=comment;
} else {
commentCode='';
}
writestream.write(commentCode + pplines[cnt].text + '\n', 'utf8');
}
writestream.end('');
}
function parseIf(str){
return trim(str.replace(/^\s*\/\/@if\b/,''));
}
function parseInclude(str){
return trim(str.replace(/^\s*\/\/@include\b/,''));
}
function parseDefine(str){
return trim(str.replace(/^\s*\/\/@define\b/,''));
}
function trim(str){
return str.replace(/^\s\s*/, '')
.replace(/\s\s*$/, '');
}
function include(arr,obj) {
return (arr.indexOf(obj) != -1);
}
// Add file to process
// write a header and footer line of file that is being processed
// append file to file list
function addFile(fileName) {
// add logic to detect infinite loops
if(include(files,fileName)){
throw new Error('file '+fileName+' has already been included');
}
files.push(fileName);
}
// produce a string of x characters
function repeat(str, num) {
var cnt, rv='';
for (cnt = 0; cnt < num; cnt++) {
rv += str;
}
return rv;
}
// loadFileandAddLineObject
function loadFileandAddLineObject(fileName,arr){
var cnt, temparr=[],
textheader, textfooter,
headerlen = Math.round((80 - (fileName.length + 5)) / 2),
footerlen = Math.round((80 - (fileName.length + 9)) / 2);
temparr = fs.readFileSync(fileName, 'utf8').split('\n');
// add a header and footer to the array for the file
textheader = '// ' + repeat("-", headerlen) + ' ' + fileName + ' ' + repeat("-", headerlen);
// log(textheader);
textfooter = '// ' + repeat("-", footerlen) + ' end ' + fileName + ' ' + repeat("-", footerlen);
// log(textfooter)
temparr.unshift(textheader);
temparr.push(textfooter);
for(cnt=0;cnt<temparr.length;cnt++){
arr.push(new lineObj(temparr[cnt]));
}
}
// dumplineObj
function dumpLineObjarr(arr){
var cnt;
for(cnt=0;cnt<arr.length;cnt++){
log(cnt+' ac('+arr[cnt].addComment+') vl('+arr[cnt].ifval+')\t'+arr[cnt].text)
}
}
// loop through lines and locate the if/endif blocks
function ifPass(arr,defines){
var startEndStack = [],
iv=false,
dopop=false,
pv, pa, pc,
ifpattern=/^\s*\/\/@if\b/g,
includepattern=/^\s*\/\/@include\b/g,
elsepattern=/^\s*\/\/@else/g,
definepattern = /^\s*\/\/@define\b/g,
endifpattern = /^\s*\/\/@endif/g;
for(cnt=0;cnt<arr.length;cnt++){
if(!arr[cnt].addcomment){
//define processing
if (definepattern.test(arr[cnt].text)) {
// get the parameter(s) and add then to the defines list
//(assuming a csv list)
pv = parseDefine(arr[cnt].text);
log('parsedefine returned (' + pv + ')');
pa = pv.split(',');
for (pc = 0; pc < pa.length; pc++) {
log('adding ' + pa[pc] + ' to defines');
defines.push(pa[pc]);
}
arr[cnt].addComment=true;
}
// include processing
if (includepattern.test(arr[cnt].text)) {
pv = parseInclude(arr[cnt].text);
arr[cnt].addComment=true;
addFile(pv);
if(arr[cnt].ifval!==false){
if(startEndStack.length>0 ){
if(startEndStack[(startEndStack.length-1)].iv){
loadFileandPrepend(pv, arr, cnt+1);
}
}
}
}
//if else endif
if(ifpattern.test(arr[cnt].text)){
pv = parseIf(arr[cnt].text);
pa = pv.split(',');
iv=false;
for (pc = 0; pc < pa.length; pc++) {
if (include(defines, pa[pc])) {
iv = true;
}
}
arr[cnt].addComment=true;
arr[cnt].ifval=iv;
if(startEndStack.length==1){
//arr[cnt].ifval=iv;
} else {
if(startEndStack.length>0){
iv=arr[cnt].addComment=startEndStack[0].iv;
}
}
startEndStack.push(new ifobj(iv));
} else {
if(endifpattern.test(arr[cnt].text)){
arr[cnt].addComment=true;
if(startEndStack.length>0){
dopop=true;
}
} else {
if(elsepattern.test(arr[cnt].text)){
if(startEndStack.length>0 ){
log('cnt='+cnt+' elselength='+startEndStack.length);
if(startEndStack[0].iv){
startEndStack[(startEndStack.length-1)].iv= (!startEndStack[(startEndStack.length-1)].iv);
arr[cnt].addcomment=startEndStack[0].iv;
}
}
arr[cnt].addComment=true;
}
}
}
if(!arr[cnt].addComment){
// log(cnt);
if(startEndStack.length>0){
arr[cnt].ifval=startEndStack[(startEndStack.length-1)].iv;
if(arr[cnt].ifval==false){
arr[cnt].addComment=true;
}
}
}
// do line settings
if(startEndStack>0){
// arr[cnt].addComment=startEndStack[startEndStack.length-1].iv;
}
// log('cnt='+cnt+' '+startEndStack.length)
if(dopop){
startEndStack.pop();
dopop=false;
}
}
}
}
// load a file and prepend it to an array
function loadFileandPrepend(fileName, arr, at) {
var cnt,
textheader, textfooter,
headerlen = Math.round((80 - (fileName.length + 5)) / 2),
footerlen = Math.round((80 - (fileName.length + 9)) / 2),
temparr = [];
temparr = fs.readFileSync(fileName, 'utf8').split('\n');
// add a header and footer to the array for the file
textheader = '// ' + repeat("-", headerlen) + ' ' + fileName + ' ' + repeat("-", headerlen);
// log(textheader);
textfooter = '// ' + repeat("-", footerlen) + ' end ' + fileName + ' ' + repeat("-", footerlen);
// log(textfooter)
temparr.unshift(textheader);
temparr.push(textfooter);
// loop through the array and insert the items to arr
for (cnt=0; cnt<temparr.length; cnt++) {
log('adding '+temparr[cnt]+' from '+fileName)
arr.splice(at+cnt,0, new lineObj(temparr[cnt]));
}
}
function log(str) {
if (!opts.debug) {
return;
}
console.log(str);
}
function lineObj(line){
this.addComment=false;
this.ifvars='';
this.ifval=null;
this.text=line;
}
function dumpobj(obj){
var n;
for(n in obj){
log(n+'='+obj[n]);
}
}
function ifobj(iv){
this.iv=iv;
}