-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
117 lines (103 loc) · 4.82 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
"use strict";
const fs = require("fs");
class ConfigManager {
constructor(configDefaultKeyValues, configFilePath, environment, envPrefix, backwardCompatibilityFunction, keyTypeMapping) {
let configContent = Object.assign({}, configDefaultKeyValues);
let isFileExist;
try {
fs.accessSync(configFilePath, fs.constants.F_OK);
isFileExist = true;
} catch (err) {
isFileExist = false;
}
let configFileContent = isFileExist ? require(configFilePath) : {};
if (backwardCompatibilityFunction) {
configFileContent = backwardCompatibilityFunction(configFileContent);
}
configFileContent = fattenObjectKeys(configFileContent);
configContent = Object.assign(configContent, configFileContent);
for (let environmentVariable of Object.keys(environment)) {
if (environmentVariable.startsWith(envPrefix)) {
const configKey = environmentVariable.slice(envPrefix.length).toLowerCase();
configContent[configKey] = environment[environmentVariable];
}
}
if (keyTypeMapping) {
for (let key of Object.keys(configContent)) {
if (keyTypeMapping.hasOwnProperty(key)) {
const stringValue = configContent[key];
switch (keyTypeMapping[key]) {
case "boolean":
if (typeof stringValue === "string") {
configContent[key] = (stringValue === "true");
} else if (typeof stringValue !== "boolean") {
throw new Error("can not cast \"" + typeof stringValue + "\" type to \"boolean\" type");
}
break;
case "integer":
if (typeof stringValue === "string") {
configContent[key] = parseInt(stringValue);
} else if (typeof stringValue !== "number") {
throw new Error("can not cast \"" + typeof stringValue + "\" type to \"integer\" type");
}
break;
case "try_integer":
if (typeof stringValue === "string") {
const parsedIntValue = parseInt(stringValue);
configContent[key] = !isNaN(parsedIntValue)
? parsedIntValue
: stringValue;
} else if (typeof stringValue !== "number") {
throw new Error("can not cast \"" + typeof stringValue + "\" type to \"try_integer\" type");
}
break;
case "double":
if (typeof stringValue === "string") {
configContent[key] = parseFloat(stringValue);
} else if (typeof stringValue !== "number") {
throw new Error("can not cast \"" + typeof stringValue + "\" type to \"double\" type");
}
break;
case "array":
if (typeof stringValue === "string") {
configContent[key] = stringValue.split(";");
} else if (!Array.isArray(stringValue)) {
throw new Error("can not cast \"" + typeof stringValue + "\" type to \"array\" type");
}
break;
}
}
}
}
this.config = configContent;
}
get(configKey) {
if (configKey) {
return this.config[configKey];
} else {
return Object.assign({}, this.config);
}
}
}
function fattenObjectKeys(nestedKeysObject) {
const isObject = val => typeof val === "object" && !Array.isArray(val);
const addDelimiter = (a, b) => a ? a + "_" + b : b;
const flattenKeysObject = {};
const paths = (obj = {}, head = "") => {
return Object.entries(obj)
.reduce((product, [key, value]) =>
{
const fullPath = addDelimiter(head, key);
const valueIsObject = isObject(value);
if (!valueIsObject) {
flattenKeysObject[fullPath] = value;
}
return valueIsObject
? product.concat(paths(value, fullPath))
: product.concat(fullPath)
}, []);
};
paths(nestedKeysObject);
return flattenKeysObject;
}
module.exports = ConfigManager;