configre 1.1.8 β†’ 1.2.0

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 CHANGED
@@ -4,10 +4,10 @@ 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 passed via command line arguments.
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,6 +80,18 @@ 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 hostname / config
84
+
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
+
87
+ ```bash
88
+ node demo.js --config=staging
89
+ node demo.js --config=production
90
+ node demo.js --port=3000 --config=myhost --debug
91
+ ```
92
+
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.
94
+
83
95
  ## πŸ€” Why Configre?
84
96
 
85
97
  - **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
- this.profile = process.argv.length > 2 ? process.argv[2] : os.hostname();
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
 
@@ -28,14 +29,12 @@ class ConfigreClass {
28
29
  throw new Error(`Could not load config from any of: ${paths.join(', ')}`);
29
30
  }
30
31
 
31
- // Helper method to try loading a file with .js or .cjs extension
32
+ // Helper method to try loading a file with .cjs extension only
32
33
  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
- }
34
+ const ext = '.cjs';
35
+ const fullPath = basePath + ext;
36
+ if (fs.existsSync(fullPath)) {
37
+ return { path: fullPath, module: require(fullPath) };
39
38
  }
40
39
  return null;
41
40
  }
@@ -54,7 +53,7 @@ class ConfigreClass {
54
53
  }
55
54
  }
56
55
 
57
- log.warn(`${this.dirname}/${this.profile}.js`, "NOT FOUND, USING DEFAULTS");
56
+ log.warn(`${this.dirname}/${this.profile}.cjs`, "NOT FOUND, USING DEFAULTS");
58
57
  return {};
59
58
  }
60
59
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "configre",
3
- "version": "1.1.8",
3
+ "version": "1.2.0",
4
4
  "description": "πŸ”§ Effortlessly Tailor Your Settings",
5
5
  "dependencies": {
6
6
  "lemonlog": "^1.0.2",
@@ -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.js # Default settings (required)
27
- β”‚ β”œβ”€β”€ myhostname.js # Host-specific overrides (optional)
28
- β”‚ └── myhostname.dev.js # Dev override for host (optional)
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
- If the project uses `"type": "module"` in `package.json`, create `config/package.json`:
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>.js` with only the keys that differ β€” they are deep-merged over defaults:
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 from `process.argv[2]` (CLI argument) or `os.hostname()` as fallback. It then checks for overrides in this order (first match wins):
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.js` or `.cjs` β€” dev override
89
- 2. `config/<profile>.js` or `.cjs` β€” production override
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.js` with project defaults, load with `require("configre")()`
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.js` with defaults, create `config/<hostname>.js` per server with db overrides
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.js`, run app with `node app.js 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
- - **Both `.js` and `.cjs`** extensions are supported for all config files
115
+ - **Config files must use the `.cjs` extension** (`.js` is not accepted; 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)