binhend 2.1.25 → 2.1.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/configuration.js +25 -12
package/package.json
CHANGED
package/src/configuration.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
const path = require('path');
|
|
3
|
-
const { isEmptyArray, isArray, isObject, mustString } = require('./utils/types');
|
|
3
|
+
const { isEmptyArray, isArray, isObject, mustString, isNullish } = require('./utils/types');
|
|
4
4
|
|
|
5
5
|
// @ts-ignore
|
|
6
6
|
function ConfigLoader(configObject, { rootPath } = {}) {
|
|
@@ -8,10 +8,6 @@ function ConfigLoader(configObject, { rootPath } = {}) {
|
|
|
8
8
|
|
|
9
9
|
rootPath = mustString(rootPath) || require.main.path;
|
|
10
10
|
|
|
11
|
-
this.getConfigs = () => {
|
|
12
|
-
return configs;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
11
|
function getConfigPosition(key) {
|
|
16
12
|
return key == undefined ? configs : configs[key] || (configs[key] = {});
|
|
17
13
|
};
|
|
@@ -19,12 +15,12 @@ function ConfigLoader(configObject, { rootPath } = {}) {
|
|
|
19
15
|
// @ts-ignore
|
|
20
16
|
this.object = (object, { key, filter: filters } = {}) => {
|
|
21
17
|
try {
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
let config = getConfigPosition(key);
|
|
19
|
+
let filtered = isArray(filters) ? filter(object, filters) : object;
|
|
24
20
|
Object.assign(config, filtered);
|
|
25
21
|
}
|
|
26
22
|
catch (error) {
|
|
27
|
-
|
|
23
|
+
failed(error, object);
|
|
28
24
|
}
|
|
29
25
|
|
|
30
26
|
return this;
|
|
@@ -34,17 +30,34 @@ function ConfigLoader(configObject, { rootPath } = {}) {
|
|
|
34
30
|
return this.object(cli(), options);
|
|
35
31
|
};
|
|
36
32
|
|
|
37
|
-
this.json = (filepath, options) => {
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
this.json = (filepath, options = {}) => {
|
|
34
|
+
let filePath = path.resolve(rootPath, filepath);
|
|
35
|
+
let jsonObject = json(filePath);
|
|
36
|
+
|
|
37
|
+
if (options.hasOwnProperty('env')) {
|
|
38
|
+
let env = options.env;
|
|
39
|
+
for (let key in jsonObject) {
|
|
40
|
+
let value = jsonObject[key];
|
|
41
|
+
if (isObject(value) && value.hasOwnProperty(env)) {
|
|
42
|
+
// if object has no "env" key, it means the intended value is the whole object => no new assignment
|
|
43
|
+
jsonObject[key] = value[env];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return this.object(jsonObject, options);
|
|
40
49
|
};
|
|
41
50
|
|
|
42
51
|
this.file = (filepath, options) => {
|
|
43
|
-
|
|
52
|
+
let filePath = path.resolve(rootPath, filepath);
|
|
44
53
|
return this.object(file(filePath, options?.encoding), options);
|
|
45
54
|
};
|
|
46
55
|
}
|
|
47
56
|
|
|
57
|
+
ConfigLoader.cli = cli;
|
|
58
|
+
ConfigLoader.json = json;
|
|
59
|
+
ConfigLoader.file = file;
|
|
60
|
+
|
|
48
61
|
function filter(object, filterKeys) {
|
|
49
62
|
if (!isObject(object) || isEmptyArray(filterKeys)) return {};
|
|
50
63
|
|