configre 1.1.8 → 1.1.10
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 +12 -1
- package/index.js +2 -1
- package/package.json +1 -1
- package/skills/configre/SKILL.md +24 -21
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Welcome to Configre, the coolest way to manage your project's configuration with
|
|
|
4
4
|
|
|
5
5
|
## ✨ Features
|
|
6
6
|
|
|
7
|
-
- **Environment-Specific Configurations**: Automatically loads configurations based on the hostname or a custom profile
|
|
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
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.
|
|
@@ -80,6 +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 Profile
|
|
84
|
+
|
|
85
|
+
You can override the hostname-based profile detection by passing `--config=<profile>` anywhere in the CLI arguments:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
node app.js --config=staging
|
|
89
|
+
node app.js --port=3000 --config=production --debug
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
This loads `config/staging.cjs` (or `config/staging.dev.cjs`) regardless of the hostname. The `--config=` flag is safe to use alongside any other arguments.
|
|
93
|
+
|
|
83
94
|
## 🤔 Why Configre?
|
|
84
95
|
|
|
85
96
|
- **No More Manual Switching**: Automatically adjusts your configuration based on the environment.
|
package/index.js
CHANGED
|
@@ -12,7 +12,8 @@ class ConfigreClass {
|
|
|
12
12
|
]);
|
|
13
13
|
|
|
14
14
|
this.dirname = path;
|
|
15
|
-
|
|
15
|
+
const configArg = process.argv.find(arg => arg.startsWith('--config='));
|
|
16
|
+
this.profile = configArg ? configArg.slice('--config='.length) : os.hostname();
|
|
16
17
|
this.profileSettings = this.loadProfileSettings();
|
|
17
18
|
}
|
|
18
19
|
|
package/package.json
CHANGED
package/skills/configre/SKILL.md
CHANGED
|
@@ -20,23 +20,17 @@ npm install configre --save
|
|
|
20
20
|
|
|
21
21
|
### Step 2: Create the config directory
|
|
22
22
|
|
|
23
|
+
Config files **must always use the `.cjs` extension**. This works in both CommonJS and ESM projects and allows using `module.exports`.
|
|
24
|
+
|
|
23
25
|
```
|
|
24
26
|
project/
|
|
25
27
|
├── config/
|
|
26
|
-
│ ├── index.
|
|
27
|
-
│ ├── myhostname.
|
|
28
|
-
│ └── myhostname.dev.
|
|
28
|
+
│ ├── index.cjs # Default settings (required)
|
|
29
|
+
│ ├── myhostname.cjs # Host-specific overrides (optional)
|
|
30
|
+
│ └── myhostname.dev.cjs # Dev override for host (optional)
|
|
29
31
|
```
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
```json
|
|
34
|
-
{ "type": "commonjs" }
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
This is needed because config files use `module.exports`.
|
|
38
|
-
|
|
39
|
-
### Step 3: Write the default config (`config/index.js`)
|
|
33
|
+
### Step 3: Write the default config (`config/index.cjs`)
|
|
40
34
|
|
|
41
35
|
```javascript
|
|
42
36
|
module.exports = {
|
|
@@ -55,7 +49,7 @@ module.exports = {
|
|
|
55
49
|
|
|
56
50
|
### Step 4: Write host/profile overrides
|
|
57
51
|
|
|
58
|
-
Create `config/<hostname>.
|
|
52
|
+
Create `config/<hostname>.cjs` with only the keys that differ — they are deep-merged over defaults:
|
|
59
53
|
|
|
60
54
|
```javascript
|
|
61
55
|
module.exports = {
|
|
@@ -83,31 +77,40 @@ const cfg = require("configre")(__dirname + "/settings");
|
|
|
83
77
|
|
|
84
78
|
## Profile resolution
|
|
85
79
|
|
|
86
|
-
Configre determines the active profile
|
|
80
|
+
Configre determines the active profile by looking for a `--config=<profile>` argument anywhere in `process.argv`, or falls back to `os.hostname()`. It then checks for overrides in this order (first match wins):
|
|
87
81
|
|
|
88
|
-
1. `config/<profile>.dev.
|
|
89
|
-
2. `config/<profile>.
|
|
82
|
+
1. `config/<profile>.dev.cjs` — dev override
|
|
83
|
+
2. `config/<profile>.cjs` — production override
|
|
90
84
|
3. No match — uses defaults only
|
|
91
85
|
|
|
86
|
+
To force a specific profile at runtime:
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
node app.js --config=staging
|
|
90
|
+
node app.js --port=3000 --config=production --debug
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Using `--config=` (instead of a positional argument) avoids conflicts with other CLI flags.
|
|
94
|
+
|
|
92
95
|
## Examples
|
|
93
96
|
|
|
94
97
|
**Example 1: Basic setup**
|
|
95
98
|
User says: "Add configuration management to my Node.js project"
|
|
96
|
-
Actions: install configre, create `config/index.
|
|
99
|
+
Actions: install configre, create `config/index.cjs` with project defaults, load with `require("configre")()`
|
|
97
100
|
Result: merged config object ready to use
|
|
98
101
|
|
|
99
102
|
**Example 2: Multi-environment**
|
|
100
103
|
User says: "I need different database settings per server"
|
|
101
|
-
Actions: create `config/index.
|
|
104
|
+
Actions: create `config/index.cjs` with defaults, create `config/<hostname>.cjs` per server with db overrides
|
|
102
105
|
Result: each server automatically loads its own config based on hostname
|
|
103
106
|
|
|
104
107
|
**Example 3: Custom profile via CLI**
|
|
105
108
|
User says: "I want to run my app with a staging config"
|
|
106
|
-
Actions: create `config/staging.
|
|
107
|
-
Result: staging overrides are merged over defaults
|
|
109
|
+
Actions: create `config/staging.cjs`, run app with `node app.js --config=staging`
|
|
110
|
+
Result: staging overrides are merged over defaults, without conflicting with other CLI arguments
|
|
108
111
|
|
|
109
112
|
## Key behaviors
|
|
110
113
|
|
|
111
114
|
- **Deep merge**: nested objects merge recursively via lodash `_.merge`
|
|
112
|
-
- **
|
|
115
|
+
- **Config files must use the `.cjs` extension** (works in both CommonJS and ESM projects)
|
|
113
116
|
- **Function vs constructor**: `Configre(path)` returns the merged config directly; `new Configre(path)` returns the instance (use `.get()` to retrieve config)
|