forked from node-config/node-config
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.js
368 lines (327 loc) · 10.2 KB
/
parser.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
// External libraries are lazy-loaded only if these file types exist.
const util = require("util");
// webpack can't solve dynamic module
// @see https://github.com/node-config/node-config/issues/755
// @see https://webpack.js.org/guides/dependency-management/#require-with-expression
const JSON5Module = require('json5');
// webpack resolves json5 with module field out of the box which lead to this usage
// @see https://github.com/node-config/node-config/issues/755
// @see https://github.com/json5/json5/issues/240
const JSON5 = JSON5Module.default || JSON5Module;
var Yaml = null,
VisionmediaYaml = null,
Coffee = null,
Iced = null,
CSON = null,
PPARSER = null,
TOML = null,
HJSON = null,
XML = null;
// Define soft dependencies so transpilers don't include everything
var COFFEE_2_DEP = 'coffeescript',
COFFEE_DEP = 'coffee-script',
ICED_DEP = 'iced-coffee-script',
JS_YAML_DEP = 'js-yaml',
YAML_DEP = 'yaml',
JSON5_DEP = 'json5',
HJSON_DEP = 'hjson',
TOML_DEP = 'toml',
CSON_DEP = 'cson',
PPARSER_DEP = 'properties',
XML_DEP = 'x2js',
TS_DEP = 'ts-node';
var Parser = module.exports;
Parser.parse = function(filename, content) {
var parserName = filename.substr(filename.lastIndexOf('.') +1); // file extension
if (typeof definitions[parserName] === 'function') {
return definitions[parserName](filename, content);
}
// TODO: decide what to do in case of a missing parser
};
Parser.xmlParser = function(filename, content) {
if (!XML) {
XML = require(XML_DEP);
}
var x2js = new XML();
var configObject = x2js.xml2js(content);
var rootKeys = Object.keys(configObject);
if(rootKeys.length === 1) {
return configObject[rootKeys[0]];
}
return configObject;
};
Parser.jsParser = function(filename, content) {
var configObject = require(filename);
if (configObject.__esModule && util.isObject(configObject.default)) {
return configObject.default
}
return configObject;
};
Parser.tsParser = function(filename, content) {
if (!require.extensions['.ts']) {
require(TS_DEP).register({
lazy: true,
ignore: ['(?:^|/)node_modules/', '.*(?<!\.ts)$'],
transpileOnly: true,
compilerOptions: {
allowJs: true,
}
});
}
// Imports config if it is exported via module.exports = ...
// See https://github.com/node-config/node-config/issues/524
var configObject = require(filename);
// Because of ES6 modules usage, `default` is treated as named export (like any other)
// Therefore config is a value of `default` key.
if (configObject.default) {
return configObject.default
}
return configObject;
};
Parser.coffeeParser = function(filename, content) {
// .coffee files can be loaded with either coffee-script or iced-coffee-script.
// Prefer iced-coffee-script, if it exists.
// Lazy load the appropriate extension
if (!Coffee) {
Coffee = {};
// The following enables iced-coffee-script on .coffee files, if iced-coffee-script is available.
// This is commented as per a decision on a pull request.
//try {
// Coffee = require('iced-coffee-script');
//}
//catch (e) {
// Coffee = require('coffee-script');
//}
try {
// Try to load coffeescript
Coffee = require(COFFEE_2_DEP);
}
catch (e) {
// If it doesn't exist, try to load it using the deprecated module name
Coffee = require(COFFEE_DEP);
}
// coffee-script >= 1.7.0 requires explicit registration for require() to work
if (Coffee.register) {
Coffee.register();
}
}
// Use the built-in parser for .coffee files with coffee-script
return require(filename);
};
Parser.icedParser = function(filename, content) {
Iced = require(ICED_DEP);
// coffee-script >= 1.7.0 requires explicit registration for require() to work
if (Iced.register) {
Iced.register();
}
};
Parser.yamlParser = function(filename, content) {
if (!Yaml && !VisionmediaYaml) {
// Lazy loading
try {
// Try to load the better js-yaml module
Yaml = require(JS_YAML_DEP);
}
catch (e) {
try {
// If it doesn't exist, load the fallback visionmedia yaml module.
VisionmediaYaml = require(YAML_DEP);
}
catch (e) { }
}
}
if (Yaml) {
return Yaml.load(content);
}
else if (VisionmediaYaml) {
// The yaml library doesn't like strings that have newlines but don't
// end in a newline: https://github.com/visionmedia/js-yaml/issues/issue/13
content += '\n';
if (typeof VisionmediaYaml.eval === 'function') {
return VisionmediaYaml.eval(Parser.stripYamlComments(content));
}
return VisionmediaYaml.parse(Parser.stripYamlComments(content));
}
else {
console.error('No YAML parser loaded. Suggest adding js-yaml dependency to your package.json file.')
}
};
Parser.jsonParser = function(filename, content) {
/**
* Default JSON parsing to JSON5 parser.
* This is due to issues with removing supported comments.
* More information can be found here: https://github.com/node-config/node-config/issues/715
*/
return JSON5.parse(content);
};
Parser.json5Parser = function(filename, content) {
return JSON5.parse(content);
};
Parser.hjsonParser = function(filename, content) {
if (!HJSON) {
HJSON = require(HJSON_DEP);
}
return HJSON.parse(content);
};
Parser.tomlParser = function(filename, content) {
if(!TOML) {
TOML = require(TOML_DEP);
}
return TOML.parse(content);
};
Parser.csonParser = function(filename, content) {
if (!CSON) {
CSON = require(CSON_DEP);
}
// Allow comments in CSON files
if (typeof CSON.parseSync === 'function') {
return CSON.parseSync(content);
}
return CSON.parse(content);
};
Parser.propertiesParser = function(filename, content) {
if (!PPARSER) {
PPARSER = require(PPARSER_DEP);
}
return PPARSER.parse(content, { namespaces: true, variables: true, sections: true });
};
/**
* Strip all Javascript type comments from the string.
*
* The string is usually a file loaded from the O/S, containing
* newlines and javascript type comments.
*
* Thanks to James Padolsey, and all who contributed to this implementation.
* http://james.padolsey.com/javascript/javascript-comment-removal-revisted/
*
* @protected
* @method stripComments
* @param fileStr {string} The string to strip comments from
* @param stringRegex {RegExp} Optional regular expression to match strings that
* make up the config file
* @return {string} The string with comments stripped.
*/
Parser.stripComments = function(fileStr, stringRegex) {
stringRegex = stringRegex || /"((?:[^"\\]|\\.)*)"/g;
var uid = '_' + +new Date(),
primitives = [],
primIndex = 0;
return (
fileStr
/* Remove strings */
.replace(stringRegex, function(match){
primitives[primIndex] = match;
return (uid + '') + primIndex++;
})
/* Remove Regexes */
.replace(/([^\/])(\/(?!\*|\/)(\\\/|.)+?\/[gim]{0,3})/g, function(match, $1, $2){
primitives[primIndex] = $2;
return $1 + (uid + '') + primIndex++;
})
/*
- Remove single-line comments that contain would-be multi-line delimiters
E.g. // Comment /* <--
- Remove multi-line comments that contain would be single-line delimiters
E.g. /* // <--
*/
.replace(/\/\/.*?\/?\*.+?(?=\n|\r|$)|\/\*[\s\S]*?\/\/[\s\S]*?\*\//g, '')
/*
Remove single and multi-line comments,
no consideration of inner-contents
*/
.replace(/\/\/.+?(?=\n|\r|$)|\/\*[\s\S]+?\*\//g, '')
/*
Remove multi-line comments that have a replaced ending (string/regex)
Greedy, so no inner strings/regexes will stop it.
*/
.replace(RegExp('\\/\\*[\\s\\S]+' + uid + '\\d+', 'g'), '')
/* Bring back strings & regexes */
.replace(RegExp(uid + '(\\d+)', 'g'), function(match, n){
return primitives[n];
})
);
};
/**
* Strip YAML comments from the string
*
* The 2.0 yaml parser doesn't allow comment-only or blank lines. Strip them.
*
* @protected
* @method stripYamlComments
* @param fileStr {string} The string to strip comments from
* @return {string} The string with comments stripped.
*/
Parser.stripYamlComments = function(fileStr) {
// First replace removes comment-only lines
// Second replace removes blank lines
return fileStr.replace(/^\s*#.*/mg,'').replace(/^\s*[\n|\r]+/mg,'');
};
/**
* Parses the environment variable to the boolean equivalent.
* Defaults to false
*
* @param {String} content - Environment variable value
* @return {boolean} - Boolean value fo the passed variable value
*/
Parser.booleanParser = function(filename, content) {
return content === 'true';
};
/**
* Parses the environment variable to the number equivalent.
* Defaults to undefined
*
* @param {String} content - Environment variable value
* @return {Number} - Number value fo the passed variable value
*/
Parser.numberParser = function(filename, content) {
const numberValue = Number(content);
return Number.isNaN(numberValue) ? undefined : numberValue;
};
var order = ['js', 'cjs', 'ts', 'json', 'json5', 'hjson', 'toml', 'coffee', 'iced', 'yaml', 'yml', 'cson', 'properties', 'xml',
'boolean', 'number'];
var definitions = {
cjs: Parser.jsParser,
coffee: Parser.coffeeParser,
cson: Parser.csonParser,
hjson: Parser.hjsonParser,
iced: Parser.icedParser,
js: Parser.jsParser,
json: Parser.jsonParser,
json5: Parser.json5Parser,
properties: Parser.propertiesParser,
toml: Parser.tomlParser,
ts: Parser.tsParser,
xml: Parser.xmlParser,
yaml: Parser.yamlParser,
yml: Parser.yamlParser,
boolean: Parser.booleanParser,
number: Parser.numberParser
};
Parser.getParser = function(name) {
return definitions[name];
};
Parser.setParser = function(name, parser) {
definitions[name] = parser;
if (order.indexOf(name) === -1) {
order.push(name);
}
};
Parser.getFilesOrder = function(name) {
if (name) {
return order.indexOf(name);
}
return order;
};
Parser.setFilesOrder = function(name, newIndex) {
if (Array.isArray(name)) {
return order = name;
}
if (typeof newIndex === 'number') {
var index = order.indexOf(name);
order.splice(newIndex, 0, name);
if (index > -1) {
order.splice(index >= newIndex ? index +1 : index, 1);
}
}
return order;
};