-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (48 loc) · 1.58 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
const path = require('path');
const nconf = require('nconf');
const glob = require('glob');
const fs = require('fs');
const template = require('string-template');
const configFolder = process.env.TWIAGE_CONFIG_DIR || 'config';
function loadEnvSpecificConfig(dir) {
const baseDir = path.join(process.cwd(), dir);
const envFile = path.join(baseDir, `${process.env.ACTIVE_PROFILE}.json`);
const defaultFile = path.join(baseDir, 'default.json');
nconf.file(envFile, envFile);
nconf.file(defaultFile, defaultFile);
}
function loadConfigFromModules() {
glob.sync('**/*/twiage-*/config').forEach(loadEnvSpecificConfig);
}
function loadConfigFromProject() {
loadEnvSpecificConfig(configFolder);
}
function deleteEmptyPropertiesOf(object) {
for (const property in object) {
if (object.hasOwnProperty(property) && !object[property]) {
delete object[property];
} else if (typeof object[property] === 'object') {
deleteEmptyPropertiesOf(object[property]);
}
}
}
function mapCustomEnvVariables() {
const customEnvFile = path.join(process.cwd(), configFolder, 'custom-env.json');
if (fs.existsSync(customEnvFile)) {
const customEnvContent = fs.readFileSync(customEnvFile).toString();
const mappedConf = JSON.parse(template(customEnvContent, process.env));
deleteEmptyPropertiesOf(mappedConf);
nconf.defaults(mappedConf);
}
}
function loadConfig(config) {
if (config) {
nconf.overrides(config);
}
mapCustomEnvVariables();
loadConfigFromProject();
loadConfigFromModules();
}
loadConfig();
nconf.update = loadConfig;
module.exports = nconf;