This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
forked from nodemailer/wild-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
273 lines (241 loc) · 8.24 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
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
/* eslint no-console: 0, global-require: 0 */
'use strict';
if (process.env.DISABLE_WILD_CONFIG === 'true') {
return;
}
const EventEmitter = require('events');
const env =
(process.env.NODE_ENV || '')
.toString()
.toLowerCase()
.replace(/[^0-9a-z-_]/g, '') || 'development';
const fs = require('fs');
const glob = require('glob');
const toml = require('toml');
const path = require('path');
const deepExtend = require('deep-extend');
const configDirectory = process.env.NODE_CONFIG_DIR || path.join(process.cwd(), 'config');
const events = new EventEmitter();
const vm = require('vm');
const argv = require('minimist')(process.argv.slice(2));
const configPath = process.env.NODE_CONFIG_PATH || argv.config || false;
events.setMaxListeners(0);
module.exports = {
configDirectory
};
let loadConfig = skipEvent => {
let sources = [{}];
function extendToml(basePath, contents) {
// # @include "/path/to/toml"
let c = 0;
return contents.replace(/^\s*#\s*@include\s*"([^"]+)"/gim, (m, p) => {
if (!path.isAbsolute(p)) {
p = path.join(basePath, p);
}
p = p.replace(/\{ENV\}/gi, env);
let res = m;
let files;
if (p.indexOf('*') >= 0) {
files = glob.sync(p);
} else {
files = [p];
}
files.forEach(file => {
let stat = fs.statSync(file);
if (!stat.isFile()) {
throw new Error(file + ' is not a file');
}
});
res = '\n__include_file_path_' + ++c + '=' + JSON.stringify(files);
return res;
});
}
function parseFile(filePath) {
let pathParts = path.parse(filePath);
let ext = pathParts.ext.toLowerCase();
let basePath = pathParts.dir;
let parsed;
try {
let contents = fs.readFileSync(filePath, 'utf-8');
switch (ext) {
case '.js': {
let script = new vm.Script(contents);
const sandbox = {
require,
__dirname: basePath,
__filename: filePath,
module: {
exports: {}
}
};
script.runInNewContext(sandbox);
parsed = sandbox.module.exports;
break;
}
case '.toml':
parsed = tomlParser(basePath, contents);
break;
case '.json':
parsed = JSON.parse(contents);
break;
}
} catch (E) {
E.message = filePath + ': ' + E.message;
throw E;
}
return parsed;
}
function tomlParser(basePath, contents) {
let parsed = toml.parse(extendToml(basePath, contents));
// find includes
let walk = (node, parentNode, nodeKey, level) => {
if (level > 100) {
throw new Error('Too much nesting in configuration file');
}
if (Array.isArray(node)) {
node.forEach(entry => walk(entry, node, false, level + 1));
} else if (node && typeof node === 'object') {
Object.keys(node || {}).forEach(key => {
if (/^__include_file_path_\d+$/.test(key) && Array.isArray(node[key])) {
let filePaths = node[key];
delete node[key];
filePaths.forEach(filePath => {
let parsed = parseFile(filePath);
if (Array.isArray(parsed)) {
if (parentNode && nodeKey && Object.keys(node).length === 0) {
parentNode[nodeKey] = parsed;
}
} else {
Object.keys(parsed || {}).forEach(subKey => {
node[subKey] = parsed[subKey];
});
}
});
} else if (node[key] && typeof node[key] === 'object') {
walk(node[key], node, key, level + 1);
}
});
}
};
walk(parsed, false, false, 0);
return parsed;
}
let loadFromFile = (filePath, ignoreMissing) => {
if (!filePath) {
// do nothing
return;
}
try {
let parsed = parseFile(filePath);
if (parsed) {
sources.push(parsed);
}
} catch (E) {
if (E.code !== 'ENOENT' || !ignoreMissing) {
// file missing, ignore
console.error('[' + filePath + '] ' + E.message);
process.exit(1);
}
}
};
try {
let listing = fs.readdirSync(configDirectory);
listing
.map(file => ({
name: file,
isDefault: file.toLowerCase().indexOf('default.') === 0,
path: path.join(configDirectory, file)
}))
.filter(file => {
let parts = path.parse(file.name);
if (!['.toml', '.json', '.js'].includes(parts.ext.toLowerCase())) {
return false;
}
if (!['default', env].includes(parts.name.toLowerCase())) {
return false;
}
return true;
})
.sort((a, b) => {
if (a.isDefault) {
return -1;
}
if (b.isDefault) {
return 1;
}
return a.path.localeCompare(b.path);
})
.forEach(file => loadFromFile(file.path));
} catch (E) {
// failed to list files
}
// try user specified file
loadFromFile(configPath);
// join found files
let data = deepExtend(...sources);
delete argv._;
delete argv.config;
let walkConfig = (cParent, eParent) => {
Object.keys(eParent || {}).forEach(key => {
if (!(key in cParent)) {
return;
}
if (typeof cParent[key] === 'object') {
if (!cParent[key]) {
// null
return;
}
if (eParent[key] === 'object') {
if (!eParent[key]) {
// null
return;
}
if (Array.isArray(cParent[key])) {
if (Array.isArray(eParent[key])) {
return;
}
// convert to array
eParent[key] = eParent[key].trim().split(/\s*,\s*/);
return;
}
return walkConfig(cParent[key], eParent[key]);
}
}
let value = eParent[key];
if (typeof cParent[key] === 'number') {
eParent[key] = Number(eParent[key]);
} else if (typeof cParent[key] === 'boolean') {
if (!isNaN(value)) {
value = Number(value);
} else {
value = value.toLowerCase();
}
let falsy = ['false', 'null', 'undefined', 'no', '0', '', 0];
eParent[key] = falsy.includes(value) ? false : !!value;
}
});
};
if (Object.keys(argv || {}).length) {
walkConfig(data, argv);
data = deepExtend(data, argv);
}
Object.keys(data).forEach(key => {
if (key !== 'on') {
module.exports[key] = data[key];
}
});
if (!skipEvent) {
events.emit('reload');
}
};
events.reload = loadConfig;
Object.defineProperty(module.exports, 'on', {
enumerable: false,
configurable: false,
writable: false,
value: (...args) => events.on(...args)
});
process.on('SIGHUP', () => {
setImmediate(loadConfig);
});
loadConfig(true);