configre 1.2.0 → 1.2.4
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/index.js +21 -12
- package/merge.js +81 -0
- package/package.json +3 -4
package/index.js
CHANGED
|
@@ -1,20 +1,27 @@
|
|
|
1
1
|
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
2
3
|
const os = require("os");
|
|
3
|
-
const obj = require("
|
|
4
|
+
const obj = require("./merge");
|
|
4
5
|
const log = require("lemonlog")("Configre");
|
|
5
6
|
|
|
6
7
|
class ConfigreClass {
|
|
7
|
-
constructor(
|
|
8
|
+
constructor(pathOrDir = __dirname + "/../../config") {
|
|
9
|
+
const dir = path.join(pathOrDir);
|
|
10
|
+
const isNested = (ConfigreClass._nesting || 0) > 0;
|
|
11
|
+
ConfigreClass._nesting = (ConfigreClass._nesting || 0) + 1;
|
|
12
|
+
|
|
8
13
|
this.defaultSettings = this.tryRequire([
|
|
9
|
-
|
|
10
|
-
path
|
|
11
|
-
|
|
14
|
+
dir,
|
|
15
|
+
path.join(dir, "index.cjs"),
|
|
16
|
+
pathOrDir + ".cjs"
|
|
12
17
|
]);
|
|
13
|
-
|
|
14
|
-
this.dirname =
|
|
18
|
+
|
|
19
|
+
this.dirname = dir;
|
|
20
|
+
this._isNested = isNested;
|
|
15
21
|
const configArg = process.argv.find(arg => arg.startsWith('--config='));
|
|
16
22
|
this.profile = configArg ? configArg.slice('--config='.length) : os.hostname();
|
|
17
23
|
this.profileSettings = this.loadProfileSettings();
|
|
24
|
+
ConfigreClass._nesting -= 1;
|
|
18
25
|
}
|
|
19
26
|
|
|
20
27
|
// Helper method to try requiring files with different extensions or paths
|
|
@@ -32,7 +39,7 @@ class ConfigreClass {
|
|
|
32
39
|
// Helper method to try loading a file with .cjs extension only
|
|
33
40
|
tryRequireWithExtensions(basePath) {
|
|
34
41
|
const ext = '.cjs';
|
|
35
|
-
const fullPath = basePath + ext;
|
|
42
|
+
const fullPath = path.join(basePath + ext);
|
|
36
43
|
if (fs.existsSync(fullPath)) {
|
|
37
44
|
return { path: fullPath, module: require(fullPath) };
|
|
38
45
|
}
|
|
@@ -41,19 +48,21 @@ class ConfigreClass {
|
|
|
41
48
|
|
|
42
49
|
loadProfileSettings() {
|
|
43
50
|
const basePaths = [
|
|
44
|
-
{ base:
|
|
45
|
-
{ base:
|
|
51
|
+
{ base: path.join(this.dirname, `${this.profile}.dev`), type: "DEV CONFIG" },
|
|
52
|
+
{ base: path.join(this.dirname, this.profile), type: "PRO CONFIG" }
|
|
46
53
|
];
|
|
47
54
|
|
|
48
55
|
for (const { base, type } of basePaths) {
|
|
49
56
|
const result = this.tryRequireWithExtensions(base);
|
|
50
57
|
if (result) {
|
|
51
|
-
log.warn(result.path, type);
|
|
58
|
+
if (!this._isNested) log.warn(result.path, type);
|
|
52
59
|
return result.module;
|
|
53
60
|
}
|
|
54
61
|
}
|
|
55
62
|
|
|
56
|
-
|
|
63
|
+
if (!this._isNested) {
|
|
64
|
+
log.warn(path.join(this.dirname, `${this.profile}.cjs`), "NOT FOUND, USING DEFAULTS");
|
|
65
|
+
}
|
|
57
66
|
return {};
|
|
58
67
|
}
|
|
59
68
|
|
package/merge.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
function isPlainObject(value) {
|
|
2
|
+
if (value === null || typeof value !== "object") {
|
|
3
|
+
return false;
|
|
4
|
+
}
|
|
5
|
+
const proto = Object.getPrototypeOf(value);
|
|
6
|
+
return proto === Object.prototype || proto === null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function isUnsafeKey(key) {
|
|
10
|
+
return key === "__proto__" || key === "constructor" || key === "prototype";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function mergeArray(target, source) {
|
|
14
|
+
const result = target.slice();
|
|
15
|
+
|
|
16
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
17
|
+
const srcValue = source[index];
|
|
18
|
+
const objValue = result[index];
|
|
19
|
+
|
|
20
|
+
if (Array.isArray(srcValue)) {
|
|
21
|
+
result[index] = mergeArray(Array.isArray(objValue) ? objValue : [], srcValue);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (isPlainObject(srcValue)) {
|
|
26
|
+
result[index] = baseMerge(isPlainObject(objValue) ? objValue : {}, srcValue);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (srcValue !== undefined || result[index] === undefined) {
|
|
31
|
+
result[index] = srcValue;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function baseMerge(object, source) {
|
|
39
|
+
if (object === source || source === null || typeof source !== "object") {
|
|
40
|
+
return object;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
for (const key in source) {
|
|
44
|
+
if (isUnsafeKey(key)) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const srcValue = source[key];
|
|
49
|
+
const objValue = object[key];
|
|
50
|
+
|
|
51
|
+
if (Array.isArray(srcValue)) {
|
|
52
|
+
object[key] = mergeArray(Array.isArray(objValue) ? objValue : [], srcValue);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (isPlainObject(srcValue)) {
|
|
57
|
+
object[key] = baseMerge(isPlainObject(objValue) ? objValue : {}, srcValue);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (srcValue !== undefined || object[key] === undefined) {
|
|
62
|
+
object[key] = srcValue;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return object;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function merge(object, ...sources) {
|
|
70
|
+
const output = (object !== null && typeof object === "object") ? object : {};
|
|
71
|
+
|
|
72
|
+
for (const source of sources) {
|
|
73
|
+
baseMerge(output, source);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return output;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = {
|
|
80
|
+
merge
|
|
81
|
+
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "configre",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.4",
|
|
4
4
|
"description": "🔧 Effortlessly Tailor Your Settings",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"lemonlog": "^1.
|
|
7
|
-
"lodash": "^4.17.21"
|
|
6
|
+
"lemonlog": "^1.2.2"
|
|
8
7
|
},
|
|
9
8
|
"main": "index.js",
|
|
10
9
|
"keywords": [
|
|
@@ -34,4 +33,4 @@
|
|
|
34
33
|
"url": "https://github.com/clasen/Configre/issues"
|
|
35
34
|
},
|
|
36
35
|
"homepage": "https://github.com/clasen/Configre#readme"
|
|
37
|
-
}
|
|
36
|
+
}
|