configre 1.1.10 β 1.2.2
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/README.md +8 -7
- package/index.js +24 -17
- package/package.json +1 -1
- package/skills/configre/SKILL.md +1 -1
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ Welcome to Configre, the coolest way to manage your project's configuration with
|
|
|
7
7
|
- **Environment-Specific Configurations**: Automatically loads configurations based on the hostname or a custom profile forced via `--config=<profile>` CLI argument.
|
|
8
8
|
- **Fallback to Defaults**: Uses a default configuration as a baseline, ensuring your application always has the necessary settings.
|
|
9
9
|
- **Easy Integration**: A simple setup process that integrates effortlessly into any project.
|
|
10
|
-
- **Support for `.cjs` Config Files**: Config files must use the `.cjs` extension. This allows dynamic configuration values and comments, and works the same in both CommonJS and ESM projects.
|
|
10
|
+
- **Support for `.cjs` Config Files Only**: Config files must use the `.cjs` extension. This allows dynamic configuration values and comments, and works the same in both CommonJS and ESM projects.
|
|
11
11
|
|
|
12
12
|
## π Getting Started
|
|
13
13
|
|
|
@@ -34,7 +34,7 @@ To get started with Configre, follow these steps:
|
|
|
34
34
|
- `[hostname].cjs`: Override configurations for specific hosts.
|
|
35
35
|
- `[hostname].dev.cjs`: Development-specific configurations.
|
|
36
36
|
|
|
37
|
-
> **Note:** Config files must always use the `.cjs` extension. That way they work in both CommonJS and ESM projects and you can use `module.exports` in them.
|
|
37
|
+
> **Note:** Config files must always use the `.cjs` extension; `.js` config files are not accepted. That way they work in both CommonJS and ESM projects and you can use `module.exports` in them.
|
|
38
38
|
|
|
39
39
|
3. **Use Configre in Your Project**
|
|
40
40
|
|
|
@@ -80,16 +80,17 @@ module.exports = {
|
|
|
80
80
|
|
|
81
81
|
Configre merges these configurations based on your environment, making your app adaptable and easier to manage.
|
|
82
82
|
|
|
83
|
-
## π― Forcing a
|
|
83
|
+
## π― Forcing a hostname / config
|
|
84
84
|
|
|
85
|
-
You can
|
|
85
|
+
By default Configre uses the machineβs hostname to choose the config file (e.g. `config/<hostname>.cjs`). You can force which hostname or profile to use with the `--config=<hostname>` CLI argument:
|
|
86
86
|
|
|
87
87
|
```bash
|
|
88
|
-
node
|
|
89
|
-
node
|
|
88
|
+
node demo.js --config=staging
|
|
89
|
+
node demo.js --config=production
|
|
90
|
+
node demo.js --port=3000 --config=myhost --debug
|
|
90
91
|
```
|
|
91
92
|
|
|
92
|
-
This loads `config/staging.cjs` (or `config/staging.dev.cjs`)
|
|
93
|
+
This loads `config/staging.cjs` (or `config/staging.dev.cjs`) instead of the one for the actual hostname. The `--config=` flag can appear anywhere in the command and works alongside other arguments.
|
|
93
94
|
|
|
94
95
|
## π€ Why Configre?
|
|
95
96
|
|
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
4
|
const obj = require("lodash/object");
|
|
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
|
|
@@ -29,33 +36,33 @@ class ConfigreClass {
|
|
|
29
36
|
throw new Error(`Could not load config from any of: ${paths.join(', ')}`);
|
|
30
37
|
}
|
|
31
38
|
|
|
32
|
-
// Helper method to try loading a file with .
|
|
39
|
+
// Helper method to try loading a file with .cjs extension only
|
|
33
40
|
tryRequireWithExtensions(basePath) {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return { path: fullPath, module: require(fullPath) };
|
|
39
|
-
}
|
|
41
|
+
const ext = '.cjs';
|
|
42
|
+
const fullPath = path.join(basePath + ext);
|
|
43
|
+
if (fs.existsSync(fullPath)) {
|
|
44
|
+
return { path: fullPath, module: require(fullPath) };
|
|
40
45
|
}
|
|
41
46
|
return null;
|
|
42
47
|
}
|
|
43
48
|
|
|
44
49
|
loadProfileSettings() {
|
|
45
50
|
const basePaths = [
|
|
46
|
-
{ base:
|
|
47
|
-
{ base:
|
|
51
|
+
{ base: path.join(this.dirname, `${this.profile}.dev`), type: "DEV CONFIG" },
|
|
52
|
+
{ base: path.join(this.dirname, this.profile), type: "PRO CONFIG" }
|
|
48
53
|
];
|
|
49
54
|
|
|
50
55
|
for (const { base, type } of basePaths) {
|
|
51
56
|
const result = this.tryRequireWithExtensions(base);
|
|
52
57
|
if (result) {
|
|
53
|
-
log.warn(result.path, type);
|
|
58
|
+
if (!this._isNested) log.warn(result.path, type);
|
|
54
59
|
return result.module;
|
|
55
60
|
}
|
|
56
61
|
}
|
|
57
62
|
|
|
58
|
-
|
|
63
|
+
if (!this._isNested) {
|
|
64
|
+
log.warn(path.join(this.dirname, `${this.profile}.cjs`), "NOT FOUND, USING DEFAULTS");
|
|
65
|
+
}
|
|
59
66
|
return {};
|
|
60
67
|
}
|
|
61
68
|
|
package/package.json
CHANGED
package/skills/configre/SKILL.md
CHANGED
|
@@ -112,5 +112,5 @@ Result: staging overrides are merged over defaults, without conflicting with oth
|
|
|
112
112
|
## Key behaviors
|
|
113
113
|
|
|
114
114
|
- **Deep merge**: nested objects merge recursively via lodash `_.merge`
|
|
115
|
-
- **Config files must use the `.cjs` extension** (works in both CommonJS and ESM projects)
|
|
115
|
+
- **Config files must use the `.cjs` extension** (`.js` is not accepted; works in both CommonJS and ESM projects)
|
|
116
116
|
- **Function vs constructor**: `Configre(path)` returns the merged config directly; `new Configre(path)` returns the instance (use `.get()` to retrieve config)
|