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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "2.1.25",
3
+ "version": "2.1.26",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -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
- var config = getConfigPosition(key);
23
- var filtered = isArray(filters) ? filter(object, filters) : object;
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
- return failed(error, object);
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
- var filePath = path.resolve(rootPath, filepath);
39
- return this.object(json(filePath), options);
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
- var filePath = path.resolve(rootPath, filepath);
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