-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
52 lines (47 loc) · 1.31 KB
/
config.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
const YAML = require("yaml");
const { readFileSync } = require("fs");
const _ = require("lodash");
const ConfigStore = require("configstore");
let config = new ConfigStore("sso").get("config");
const YAML_CONFIG_FILE_PATH =
process.env.YAML_CONFIG_FILE_PATH || "./config.yml";
try {
let configFilePath = YAML_CONFIG_FILE_PATH;
const yamlFile = readFileSync(configFilePath, "utf8");
config = YAML.parse(yamlFile).config;
} catch (e) {
console.log("[CONFIG] unable to read config file", e);
process.exit(1);
}
if (!config) {
console.log("[CONFIG] configuration file is required");
process.exit(1);
}
try {
_.map(process.env, (value, key) => {
if (key && key.startsWith("CONFIG__")) {
const configKeys = key.split("__");
switch (configKeys.length) {
case 2:
config[key[1]] = value;
break;
case 3:
config[configKeys[1]][configKeys[2]] = value;
break;
case 4:
config[configKeys[1]][configKeys[2]][configKeys[3]] = value;
break;
case 5:
config[configKeys[1]][configKeys[2]][configKeys[3]][configKeys[4]] =
value;
break;
}
}
});
} catch (e) {
console.log(
"[CONFIG] Failed to read config variables from environment: ",
e.message
);
}
module.exports = config;