configre 1.1.2 → 1.1.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
CHANGED
|
@@ -5,25 +5,57 @@ const log = require("lemonlog")("Configre");
|
|
|
5
5
|
|
|
6
6
|
class ConfigreClass {
|
|
7
7
|
constructor(path = __dirname + "/../../config") {
|
|
8
|
-
this.defaultSettings =
|
|
8
|
+
this.defaultSettings = this.tryRequire([
|
|
9
|
+
path,
|
|
10
|
+
path + '/index.cjs',
|
|
11
|
+
path + '.cjs'
|
|
12
|
+
]);
|
|
13
|
+
|
|
9
14
|
this.dirname = path;
|
|
10
15
|
this.profile = process.argv.length > 2 ? process.argv[2] : os.hostname();
|
|
11
|
-
this.devFile = `${this.dirname}/${this.profile}.dev.js`;
|
|
12
|
-
this.localFile = `${this.dirname}/${this.profile}.js`;
|
|
13
16
|
this.profileSettings = this.loadProfileSettings();
|
|
14
17
|
}
|
|
15
18
|
|
|
19
|
+
// Helper method to try requiring files with different extensions or paths
|
|
20
|
+
tryRequire(paths) {
|
|
21
|
+
for (const p of paths) {
|
|
22
|
+
try {
|
|
23
|
+
return require(p);
|
|
24
|
+
} catch (e) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
throw new Error(`Could not load config from any of: ${paths.join(', ')}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Helper method to try loading a file with .js or .cjs extension
|
|
32
|
+
tryRequireWithExtensions(basePath) {
|
|
33
|
+
const extensions = ['.js', '.cjs'];
|
|
34
|
+
for (const ext of extensions) {
|
|
35
|
+
const fullPath = basePath + ext;
|
|
36
|
+
if (fs.existsSync(fullPath)) {
|
|
37
|
+
return { path: fullPath, module: require(fullPath) };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
16
43
|
loadProfileSettings() {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
44
|
+
const basePaths = [
|
|
45
|
+
{ base: `${this.dirname}/${this.profile}.dev`, type: "DEV CONFIG" },
|
|
46
|
+
{ base: `${this.dirname}/${this.profile}`, type: "PRO CONFIG" }
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
for (const { base, type } of basePaths) {
|
|
50
|
+
const result = this.tryRequireWithExtensions(base);
|
|
51
|
+
if (result) {
|
|
52
|
+
log.warn(result.path, type);
|
|
53
|
+
return result.module;
|
|
54
|
+
}
|
|
26
55
|
}
|
|
56
|
+
|
|
57
|
+
log.warn(`${this.dirname}/${this.profile}.js`, "NOT FOUND, USING DEFAULTS");
|
|
58
|
+
return {};
|
|
27
59
|
}
|
|
28
60
|
|
|
29
61
|
get() {
|
package/package.json
CHANGED
|
File without changes
|
|
File without changes
|