-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
318 lines (279 loc) · 6.9 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
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
const path = require('path');
const fs = require('fs');
const _ = require('lodash');
let config;
const fileCache = {};
const env = function () {
return process.env.NODE_ENV || 'development';
};
/**
* These are all available through cfg
* @see cfg
* @namespace config
*/
function isObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}
// merge configs, takes care of getters while merging
function merge(obj, src) {
if (!src) return obj;
Object.keys(src).forEach((key) => {
const objVal = Object.getOwnPropertyDescriptor(obj, key);
const srcVal = Object.getOwnPropertyDescriptor(src, key);
if (
objVal &&
!objVal.get &&
!srcVal.get &&
isObject(srcVal.value) &&
isObject(objVal.value)
) {
merge(obj[key], src[key]);
return;
}
Object.defineProperty(obj, key, srcVal);
});
return obj;
}
/**
* Reads a config value
* @param {string} key key to read, can be nested like `a.b.c`
* @param {*} defaultValue value to return if key is not found
* @return {any}
*/
function cfg(key, defaultValue) {
// eslint-disable-next-line no-use-before-define
readDefaultConfigFiles();
return _.get(config, key, defaultValue);
}
/**
* Will read env vars of the format CFG__JSON_KEY__PATH=$VAL
* It will set the key 'jsonKey.path' with $VAL
* _ to specify where to capitalize for camelCase
* __ to seperate the key path
* @private
*/
function readEnvVariables() {
const vals = Object.keys(process.env).filter(val => val.indexOf('CFG__') === 0);
vals.forEach((val) => {
const key = val.slice(5).split('__').map(el => _.camelCase(el)).join('.');
let envVal = process.env[val];
if (envVal && envVal.startsWith('@JSON:')) {
try {
envVal = JSON.parse(envVal.substring(6));
}
catch (e) {
console.error(`Error while parsing JSON value of env variable ${val}`);
}
}
cfg.set(key, envVal);
});
}
function readDefaultConfigFiles() {
if (config) return;
config = {};
const cwd = process.cwd();
const privatePath = path.join(cwd, 'private');
cfg.file(path.join(cwd, 'config.js'), {ignoreNotFound: true});
cfg.file(path.join(cwd, `config.${env()}.js`), {ignoreNotFound: true});
if (cfg.isCI()) cfg.file(path.join(cwd, 'config.CI.js'), {ignoreNotFound: true});
cfg.file(path.join(privatePath, 'config.js'), {ignoreNotFound: true});
cfg.file(path.join(privatePath, `config.${env()}.js`), {ignoreNotFound: true});
if (cfg.isCI()) cfg.file(path.join(privatePath, 'config.CI.js'), {ignoreNotFound: true});
if (config.$privateConfigFile) {
cfg.file(config.$privateConfigFile, {ignoreNotFound: true});
}
readEnvVariables();
}
/**
* @memberof config
* @param {string} key
* @param {any} defaultValue
* @return {any}
*/
cfg.get = function (key, defaultValue) {
return cfg(key, defaultValue);
};
/**
* @memberof config
* @return {object}
*/
cfg._getConfig = function () {
return config;
};
/** set values in global config
* you can also give key as an object to assign all key values from it
* @memberof config
* @param {string | object} key
* @param {any} [value]
* @return {null | any}
*/
cfg.set = function (key, value) {
readDefaultConfigFiles();
// if key is Object then merge it with existing config
if (value === undefined && key instanceof Object) {
Object.assign(config, key);
Object.assign(config, key[`$env_${env()}`]);
if (cfg.isCI()) Object.assign(config, key.$env_CI);
return null;
}
const prev = _.get(config, key);
_.set(config, key, value);
return prev;
};
/**
* set values in global config with an object to assign all key values from it
* if a key already exists then it is merged with new value
* if obj is not an Object then nothing happens
* @memberof config
* @param {object} obj
* @return {void}
*/
cfg.merge = function (obj) {
readDefaultConfigFiles();
if (obj instanceof Object) {
merge(config, obj);
merge(config, obj[`$env_${env()}`]);
if (cfg.isCI()) merge(config, obj.$env_CI);
}
};
/**
* set values in global config with an object to assign all key values from it
* if a key already exists then it is assigned with new value
* if obj is not an Object then nothing happens
* @memberof config
* @param {object} obj
* @return {void}
*/
cfg.assign = function (obj) {
readDefaultConfigFiles();
if (obj instanceof Object) {
Object.assign(config, obj);
Object.assign(config, obj[`$env_${env()}`]);
if (cfg.isCI()) Object.assign(config, obj.$env_CI);
}
};
/**
* @memberof config
* @param {string} key
* @return {void}
*/
cfg.delete = function (key) {
readDefaultConfigFiles();
delete config[key];
};
/**
* read config from a file, and merge with existing config
* @memberof config
* @param {string} file path of the file to read (only absolute paths)
* @param {object} options options obj
* @param {boolean} options.ignoreErrors ignore all errors
* @param {boolean} options.ignoreNotFound ignore if file not found
* @param {boolean} options.overwrite Overwrite config not merge
*/
cfg.file = function (file, options = {}) {
if (!path.isAbsolute(file)) {
throw new Error('Only absolute paths are allowed');
}
try {
// eslint-disable-next-line global-require, import/no-dynamic-require
const data = require(file);
if (options.overwrite === true && typeof data === 'object') {
config = data;
}
else {
cfg.merge(data);
}
}
catch (e) {
if (e.code === 'MODULE_NOT_FOUND' && options.ignoreNotFound) {
// Ignore Not Found Errors
return;
}
if (options.ignoreErrors) {
// Ignore All Errors
return;
}
throw e;
}
};
/**
* read the file specified by the key, and then cache it
* @memberof config
* @param {string} key
* @return {any} value
*/
cfg.read = function (key) {
if (key in fileCache) {
return fileCache[key];
}
const filePath = cfg(key);
if (!filePath) {
fileCache[key] = undefined;
}
else {
try {
fileCache[key] = fs.readFileSync(filePath);
}
catch (e) {
console.error(`[cfg] can't read file ${key}: ${filePath}`, e);
}
}
return fileCache[key];
};
/**
* @memberof config
* @return {boolean}
*/
cfg.isProduction = function () {
return env() === 'production';
};
/**
* @memberof config
* @return {boolean}
*/
cfg.isStaging = function () {
return env() === 'staging';
};
/**
* Returns true if env is production or staging
* @memberof config
* @return {boolean}
*/
cfg.isProductionLike = function () {
return (env() === 'production') || (env() === 'staging');
};
/**
* @memberof config
* @return {boolean}
*/
cfg.isTest = function () {
return env() === 'test';
};
/**
* @memberof config
* @return {boolean}
*/
cfg.isDev = function () {
return (env() !== 'production') && (env() !== 'staging');
};
/**
* @memberof config
* @return {boolean}
*/
cfg.isCI = function () {
return !!process.env.CI;
};
/**
* @memberof config
* @returns {string}
*/
cfg.env = env;
/**
* @memberof config
* @return {string}
*/
cfg.getEnv = env;
cfg.isProd = cfg.isProduction;
cfg.isProdLike = cfg.isProductionLike;
cfg.isDevelopment = cfg.isDev;
module.exports = cfg;