-
Notifications
You must be signed in to change notification settings - Fork 508
Expand file tree
/
Copy pathparser.js
More file actions
360 lines (329 loc) · 8.62 KB
/
parser.js
File metadata and controls
360 lines (329 loc) · 8.62 KB
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
// External libraries are lazy-loaded only if these file types exist.
import Path from 'path';
import { createRequire } from 'node:module';
import JSON5 from 'json5';
const moduleRequire = createRequire(Path.join(process.cwd(), 'package.json'));
const require = createRequire(process.cwd());
let Yaml = null,
JSYaml = null,
Coffee = null,
Iced = null,
CSON = null,
PPARSER = null,
TOML = null,
HJSON = null,
XML = null,
TS = null;
// Define soft dependencies so transpilers don't include everything
let COFFEE_2_DEP = 'coffeescript',
COFFEE_DEP = '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';
/**
* @template [T=any]
* @typedef {(filename: string, content: string) => T | undefined} ParserFn<T>
*/
let Parser = {};
/**
* @param {string} filename
* @param {string} content
* @returns {object | undefined}
*/
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
};
/**
* @param {string} filename
* @param {string} content
* @returns {object}
*/
Parser.xmlParser = function(filename, content) {
if (!XML) {
XML = moduleRequire(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;
};
/**
* @param {string} filename
* @param {string} content
* @returns {object}
*/
Parser.jsParser = function(filename, content) {
var configObject = require(filename);
if (configObject.__esModule && isObject(configObject.default)) {
return configObject.default
}
return configObject;
};
/**
* @param {string} filename
* @param {string} content
* @returns {object}
*/
Parser.tsParser = function(filename, content) {
if (require?.extensions?.['.ts'] === undefined) {
if (TS === null) {
TS = moduleRequire(TS_DEP);
TS.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;
};
/**
* @param {string} filename
* @param {string} content
* @returns {object}
*/
Parser.coffeeParser = function(filename, content) {
// Lazy load the appropriate extension
if (!Coffee) {
Coffee = {};
try {
// Try to load coffeescript
Coffee = moduleRequire(COFFEE_2_DEP);
}
catch (e) {
// If it doesn't exist, try to load it using the deprecated module name
Coffee = moduleRequire(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);
};
/**
* @param {string} filename
* @param {string} content
* @returns {object | undefined}
*/
Parser.yamlParser = function(filename, content) {
if (!Yaml && !JSYaml) {
// Lazy loading
try {
Yaml = moduleRequire(YAML_DEP);
} catch (e) {
try {
JSYaml = moduleRequire(JS_YAML_DEP);
} catch (e) {}
}
}
if (Yaml) {
return Yaml.parse(content);
} else if (JSYaml) {
return JSYaml.load(content);
} else {
console.error('No YAML parser loaded. Suggest adding yaml dependency to your package.json file.')
}
};
/**
* @param {string} filename
* @param {string} content
* @returns {object}
*/
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);
};
/**
* @param {string} filename
* @param {string} content
* @returns {object}
*/
Parser.json5Parser = function(filename, content) {
return JSON5.parse(content);
};
/**
* @param {string} filename
* @param {string} content
* @returns {object}
*/
Parser.hjsonParser = function(filename, content) {
if (!HJSON) {
HJSON = moduleRequire(HJSON_DEP);
}
return HJSON.parse(content);
};
/**
* @param {string} filename
* @param {string} content
* @returns {object}
*/
Parser.tomlParser = function(filename, content) {
if(!TOML) {
TOML = moduleRequire(TOML_DEP);
}
return TOML.parse(content);
};
/**
* @param {string} filename
* @param {string} content
* @returns {object}
*/
Parser.csonParser = function(filename, content) {
if (!CSON) {
CSON = moduleRequire(CSON_DEP);
}
// Allow comments in CSON files
if (typeof CSON.parseSync === 'function') {
return CSON.parseSync(content);
}
return CSON.parse(content);
};
/**
* @param {string} filename
* @param {string} content
* @returns {object}
*/
Parser.propertiesParser = function(filename, content) {
if (!PPARSER) {
PPARSER = moduleRequire(PPARSER_DEP);
}
return PPARSER.parse(content, { namespaces: true, variables: true, sections: true });
};
/**
* 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 {string} fileStr 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} filename - Filename of the env variable (not used)
* @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} filename - Filename of the env variable (not used)
* @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', 'mjs', 'ts', 'json', 'jsonc', 'json5', 'hjson', 'toml', 'coffee', 'yaml', 'yml', 'cson', 'properties', 'xml',
'boolean', 'number'];
var definitions = {
cjs: Parser.jsParser,
coffee: Parser.coffeeParser,
cson: Parser.csonParser,
hjson: Parser.hjsonParser,
js: Parser.jsParser,
json: Parser.jsonParser,
jsonc: Parser.jsonParser,
json5: Parser.json5Parser,
mjs: Parser.jsParser,
properties: Parser.propertiesParser,
toml: Parser.tomlParser,
ts: Parser.tsParser,
xml: Parser.xmlParser,
yaml: Parser.yamlParser,
yml: Parser.yamlParser,
boolean: Parser.booleanParser,
number: Parser.numberParser
};
/**
* @param {string} name
* @returns {ParserFn | undefined}
*/
Parser.getParser = function(name) {
return definitions[name];
};
/**
* @param {string} name
* @param {ParserFn} parser
*/
Parser.setParser = function(name, parser) {
definitions[name] = parser;
if (order.indexOf(name) === -1) {
order.push(name);
}
};
/**
* @param {string=} name
* @returns {string[] | number}
*/
Parser.getFilesOrder = function(name) {
if (name) {
return order.indexOf(name);
}
return order;
};
/**
* @param {string|string[]} name
* @param {number=} newIndex
* @returns {string[]}
*/
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;
};
function isObject(arg) {
return (arg !== null) && (typeof arg === 'object');
}
export default Parser;