/
preprocess.js
409 lines (337 loc) · 11.5 KB
/
preprocess.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* preprocess
* https://github.com/onehealth/preprocess
*
* Copyright (c) 2012 OneHealth Solutions, Inc.
* Written by Jarrod Overson - http://jarrodoverson.com/
* Licensed under the Apache 2.0 license.
*/
'use strict';
exports.preprocess = preprocess;
exports.preprocessFile = preprocessFile;
exports.preprocessFileSync = preprocessFileSync;
var path = require('path'),
fs = require('fs'),
os = require('os'),
delim = require('./regexrules'),
XRegExp = require('xregexp');
function preprocessFile(srcFile, destFile, context, callback, options) {
options = getOptionsForFile(srcFile, options);
context.src = srcFile;
fs.readFile(srcFile, function (err, data) {
if (err) return callback(err, data);
var parsed = preprocess(data, context, options);
fs.writeFile(destFile, parsed, callback);
});
}
function preprocessFileSync(srcFile, destFile, context, options) {
options = getOptionsForFile(srcFile, options);
context.src = srcFile;
var data = fs.readFileSync(srcFile);
var parsed = preprocess(data, context, options);
return fs.writeFileSync(destFile, parsed);
}
function getOptionsForFile(srcFile, options) {
options = options || {};
options.srcDir = options.srcDir || path.dirname(srcFile);
options.type = options.type || getExtension(srcFile);
return options;
}
function getExtension(filename) {
var ext = path.extname(filename||'').split('.');
return ext[ext.length - 1];
}
function preprocess(src, context, typeOrOptions) {
src = src.toString();
context = context || process.env;
// default values
var options = {
fileNotFoundSilentFail: false,
srcDir: process.cwd(),
srcEol: getEolType(src),
type: delim['html']
};
// needed for backward compatibility with 2.x.x series
if (typeof typeOrOptions === 'string') {
typeOrOptions = {
type: typeOrOptions
};
}
// needed for backward compatibility with 2.x.x series
if (typeof context.srcDir === "string") {
typeOrOptions = typeOrOptions || {};
typeOrOptions.srcDir = context.srcDir;
}
if (typeOrOptions && typeof typeOrOptions === 'object') {
options.srcDir = typeOrOptions.srcDir || options.srcDir;
options.fileNotFoundSilentFail = typeOrOptions.fileNotFoundSilentFail || options.fileNotFoundSilentFail;
options.srcEol = typeOrOptions.srcEol || options.srcEol;
options.type = delim[typeOrOptions.type] || options.type;
}
context = copy(context);
return preprocessor(src, context, options);
}
function preprocessor(src, context, opts, noRestoreEol) {
src = normalizeEol(src);
var rv = src;
rv = replace(rv, opts.type.include, processIncludeDirective.bind(null, false, context, opts));
if (opts.type.extend) {
rv = replaceRecursive(rv, opts.type.extend, function(startMatches, endMatches, include, recurse) {
var file = (startMatches[1] || '').trim();
var extendedContext = copy(context);
var extendedOpts = copy(opts);
extendedContext.src = path.join(opts.srcDir, file);
extendedOpts.srcDir = path.dirname(extendedContext.src);
var fileContents = getFileContents(extendedContext.src, opts.fileNotFoundSilentFail, context.src);
if (fileContents.error) {
return fileContents.contents;
}
var extendedSource = preprocessor(fileContents.contents, extendedContext, extendedOpts, true).trim();
if (extendedSource) {
include = include.replace(/^\n?|\n?$/g, '');
return replace(extendedSource, opts.type.extendable, recurse(include));
} else {
return '';
}
});
}
if (opts.type.foreach) {
rv = replaceRecursive(rv, opts.type.foreach, function(startMatches, endMatches, include, recurse) {
var variable = (startMatches[1] || '').trim();
var forParams = variable.split(' ');
if (forParams.length === 3) {
var contextVar = forParams[2];
var arrString = getDeepPropFromObj(context, contextVar);
var eachArr;
if (arrString.match(/\{(.*)\}/)) {
eachArr = JSON.parse(arrString);
} else if (arrString.match(/\[(.*)\]/)) {
eachArr = arrString.slice(1, -1);
eachArr = eachArr.split(',');
eachArr = eachArr.map(function(arrEntry){
return arrEntry.replace(/\s*(['"])(.*)\1\s*/, '$2');
});
} else {
eachArr = arrString.split(',');
}
var replaceToken = new RegExp(XRegExp.escape(forParams[0]), 'g');
var recursedInclude = recurse(include);
return Object.keys(eachArr).reduce(function(stringBuilder, arrKey){
var arrEntry = eachArr[arrKey];
return stringBuilder + recursedInclude.replace(replaceToken, arrEntry);
}, '');
} else {
return '';
}
});
}
if (opts.type.exclude) {
rv = replaceRecursive(rv, opts.type.exclude, function(startMatches, endMatches, include, recurse){
var test = (startMatches[1] || '').trim();
return testPasses(test,context) ? '' : recurse(include);
});
}
if (opts.type.if) {
rv = replaceRecursive(rv, opts.type.if, function (startMatches, endMatches, include, recurse) {
var variant = startMatches[1];
var test = (startMatches[2] || '').trim();
switch(variant) {
case 'if':
return testPasses(test,context) ? recurse(include) : '';
case 'ifdef':
return typeof getDeepPropFromObj(context, test) !== 'undefined' ? recurse(include) : '';
case 'ifndef':
return typeof getDeepPropFromObj(context, test) === 'undefined' ? recurse(include) : '';
default:
throw new Error('Unknown if variant ' + variant + '.');
}
});
}
rv = replace(rv, opts.type.echo, function (match, variable) {
variable = (variable || '').trim();
// if we are surrounded by quotes, echo as a string
var stringMatch = variable.match(/^(['"])(.*)\1$/);
if (stringMatch) return stringMatch[2];
return getDeepPropFromObj(context, (variable || '').trim());
});
rv = replace(rv, opts.type.exec, function (match, name, value) {
name = (name || '').trim();
value = value || '';
var params = value.split(',');
var stringRegex = /^['"](.*)['"]$/;
params = params.map(function(param){
param = param.trim();
if (stringRegex.test(param)) { // handle string parameter
return param.replace(stringRegex, '$1');
} else { // handle variable parameter
return getDeepPropFromObj(context, param);
}
});
var fn = getDeepPropFromObj(context, name);
if (!fn || typeof fn !== 'function') return '';
return fn.apply(context, params);
});
rv = replace(rv, opts.type['include-static'], processIncludeDirective.bind(null, true, context, opts));
if (!noRestoreEol) {
rv = restoreEol(rv, opts.srcEol);
}
return rv;
}
function getEolType(source) {
var eol;
var foundEolTypeCnt = 0;
if (source.indexOf('\r\n') >= 0) {
eol = '\r\n';
foundEolTypeCnt++;
}
if (/\r[^\n]/.test(source)) {
eol = '\r';
foundEolTypeCnt++;
}
if (/[^\r]\n/.test(source)) {
eol = '\n';
foundEolTypeCnt++;
}
if (eol == null || foundEolTypeCnt > 1) {
eol = os.EOL;
}
return eol;
}
function normalizeEol(source, indent) {
// only process any kind of EOL if indentation has to be added, otherwise replace only non \n EOLs
if (indent) {
source = source.replace(/(?:\r?\n)|\r/g, '\n' + indent);
} else {
source = source.replace(/(?:\r\n)|\r/g, '\n');
}
return source;
}
function restoreEol(normalizedSource, originalEol) {
if (originalEol !== '\n') {
normalizedSource = normalizedSource.replace(/\n/g, originalEol);
}
return normalizedSource;
}
function replace(rv, rule, processor) {
var isRegex = typeof rule === 'string' || rule instanceof RegExp;
var isArray = Array.isArray(rule);
if (isRegex) {
rule = [new RegExp(rule,'gmi')];
} else if (isArray) {
rule = rule.map(function(subRule){
return new RegExp(subRule,'gmi');
});
} else {
throw new Error('Rule must be a String, a RegExp, or an Array.');
}
return rule.reduce(function(rv, rule){
return rv.replace(rule, processor);
}, rv);
}
function replaceRecursive(rv, rule, processor) {
if(!rule.start || !rule.end) {
throw new Error('Recursive rule must have start and end.');
}
var startRegex = new RegExp(rule.start, 'mi');
var endRegex = new RegExp(rule.end, 'mi');
function matchReplacePass(content) {
var matches = XRegExp.matchRecursive(content, rule.start, rule.end, 'gmi', {
valueNames: ['between', 'left', 'match', 'right']
});
var matchGroup = {
left: null,
match: null,
right: null
};
return matches.reduce(function (builder, match) {
switch(match.name) {
case 'between':
builder += match.value;
break;
case 'left':
matchGroup.left = startRegex.exec(match.value);
break;
case 'match':
matchGroup.match = match.value;
break;
case 'right':
matchGroup.right = endRegex.exec(match.value);
builder += processor(matchGroup.left, matchGroup.right, matchGroup.match, matchReplacePass);
break;
}
return builder;
}, '');
}
return matchReplacePass(rv);
}
function processIncludeDirective(isStatic, context, opts, match, linePrefix, file) {
file = (file || '').trim();
var indent = linePrefix.replace(/\S/g, ' ');
var includedContext = copy(context);
var includedOpts = copy(opts);
includedContext.src = path.join(opts.srcDir,file);
includedOpts.srcDir = path.dirname(includedContext.src);
var fileContents = getFileContents(includedContext.src, opts.fileNotFoundSilentFail, context.src);
if (fileContents.error) {
return linePrefix + fileContents.contents;
}
var includedSource = fileContents.contents;
if (isStatic) {
includedSource = fileContents.contents;
} else {
includedSource = preprocessor(fileContents.contents, includedContext, includedOpts, true);
}
includedSource = normalizeEol(includedSource, indent);
if (includedSource) {
return linePrefix + includedSource;
} else {
return linePrefix;
}
}
function getTestTemplate(test) {
/*jshint evil:true*/
test = test || 'true';
test = test.trim();
// force single equals replacement
test = test.replace(/([^=!])=([^=])/g, '$1==$2');
return new Function("context", "with (context||{}){ return ( " + test + " ); }");
}
function testPasses(test,context) {
var testFn = getTestTemplate(test);
return testFn(context, getDeepPropFromObj);
}
function getFileContents(path, failSilent, requesterPath) {
try {
fs.statSync(path);
} catch (e) {
if (failSilent) {
return {error: true, contents: path + ' not found!'};
} else {
var errMsg = path;
errMsg = requesterPath ? errMsg + ' requested from ' + requesterPath : errMsg;
errMsg += ' not found!';
throw new Error(errMsg);
}
}
return {error: false, contents: fs.readFileSync(path).toString()};
}
function copy(obj) {
return Object.keys(obj).reduce(function (copyObj, objKey) {
copyObj[objKey] = obj[objKey];
return copyObj;
}, {});
}
function getDeepPropFromObj(obj, propPath) {
propPath.replace(/\[([^\]+?])\]/g, '.$1');
propPath = propPath.split('.');
// fast path, no need to loop if structurePath contains only a single segment
if (propPath.length === 1) {
return obj[propPath[0]];
}
// loop only as long as possible (no exceptions for null/undefined property access)
propPath.some(function (pathSegment) {
obj = obj[pathSegment];
return (obj == null);
});
return obj;
}