configre 1.0.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 +98 -0
- package/demo/config/hostname.js +6 -0
- package/demo/config/index.js +10 -0
- package/demo/demo.js +4 -0
- package/index.js +44 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# π Configre
|
|
2
|
+
|
|
3
|
+
Welcome to Configre, the coolest way to manage your project's configuration with a twist of personality based on your environment! Whether you're in development, testing, or production, Configre seamlessly adjusts to your project's needs by loading specific configurations tailored to each environment. Say goodbye to manual config tweaks and hello to automatic, hassle-free setup! π
|
|
4
|
+
|
|
5
|
+
## β¨ Features
|
|
6
|
+
|
|
7
|
+
- **Environment-Specific Configurations**: Automatically loads configurations based on the hostname or a custom profile passed via command line arguments.
|
|
8
|
+
- **Fallback to Defaults**: Uses a default configuration as a baseline, ensuring your application always has the necessary settings.
|
|
9
|
+
- **Easy Integration**: A simple setup process that integrates effortlessly into any project.
|
|
10
|
+
- **Support for `.js` Config Files**: Allows for dynamic configuration values and comments for better clarity.
|
|
11
|
+
|
|
12
|
+
## π Getting Started
|
|
13
|
+
|
|
14
|
+
To get started with Configre, follow these steps:
|
|
15
|
+
|
|
16
|
+
1. **Install the Library**
|
|
17
|
+
|
|
18
|
+
First, make sure you have `Node.js` installed. Then, add Configre to your project:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install Configre --save
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
2. **Setup Your Configuration Files**
|
|
25
|
+
|
|
26
|
+
Organize your configuration files within a directory (e.g., `config`). Create a default configuration file and environment-specific files as needed.
|
|
27
|
+
|
|
28
|
+
- `index.js`: Your default configuration.
|
|
29
|
+
- `[hostname].js`: Override configurations for specific hosts.
|
|
30
|
+
- `[hostname].dev.js`: Development-specific configurations.
|
|
31
|
+
|
|
32
|
+
3. **Use Configre in Your Project**
|
|
33
|
+
|
|
34
|
+
Import and use Configre to load your configurations:
|
|
35
|
+
|
|
36
|
+
```javascript
|
|
37
|
+
const cfg = require("Configre")(__dirname + "/config");
|
|
38
|
+
console.log(cfg.db); // Access your db configuration
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## π Example
|
|
42
|
+
|
|
43
|
+
Imagine you have the following structure in your `config` directory:
|
|
44
|
+
|
|
45
|
+
- `index.js`: Contains default settings.
|
|
46
|
+
- `myhostname.js`: Contains overrides for the host named `myhostname`.
|
|
47
|
+
|
|
48
|
+
Your `index.js` might look like this:
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
module.exports = {
|
|
52
|
+
db: {
|
|
53
|
+
host: 'localhost',
|
|
54
|
+
user: 'myuser',
|
|
55
|
+
password: 'mypassword'
|
|
56
|
+
},
|
|
57
|
+
api: {
|
|
58
|
+
key: 'development-api-key'
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
And your `myhostname.js`:
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
66
|
+
module.exports = {
|
|
67
|
+
db: {
|
|
68
|
+
user: 'john',
|
|
69
|
+
password: 'johns-password'
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Configre merges these configurations based on your environment, making your app adaptable and easier to manage.
|
|
75
|
+
|
|
76
|
+
## π€ Why Configre?
|
|
77
|
+
|
|
78
|
+
- **No More Manual Switching**: Automatically adjusts your configuration based on the environment.
|
|
79
|
+
- **Clarity and Convenience**: Keep your configuration organized and easy to understand.
|
|
80
|
+
- **Flexibility**: Supports dynamic configuration values for complex setups.
|
|
81
|
+
|
|
82
|
+
## π€Contribute
|
|
83
|
+
|
|
84
|
+
Found a bug or have a feature request? Contributions are welcome! Feel free to open an issue or submit a pull request.
|
|
85
|
+
|
|
86
|
+
Let's make Configre even better, together! π
|
|
87
|
+
|
|
88
|
+
## π License
|
|
89
|
+
|
|
90
|
+
The MIT License (MIT)
|
|
91
|
+
|
|
92
|
+
Copyright (c) Martin Clasen
|
|
93
|
+
|
|
94
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
95
|
+
|
|
96
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
97
|
+
|
|
98
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/demo/demo.js
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const os = require("os");
|
|
3
|
+
const obj = require("lodash/object");
|
|
4
|
+
const LemonLog = require("lemonlog");
|
|
5
|
+
const log = new LemonLog("Configre");
|
|
6
|
+
|
|
7
|
+
class ConfigreClass {
|
|
8
|
+
constructor(path) {
|
|
9
|
+
this.defaultSettings = require(path);
|
|
10
|
+
this.dirname = path;
|
|
11
|
+
this.profile = process.argv.length > 2 ? process.argv[2] : os.hostname();
|
|
12
|
+
this.devFile = `${this.dirname}/${this.profile}.dev.js`;
|
|
13
|
+
this.localFile = `${this.dirname}/${this.profile}.js`;
|
|
14
|
+
this.profileSettings = this.loadProfileSettings();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
loadProfileSettings() {
|
|
18
|
+
if (fs.existsSync(this.devFile)) {
|
|
19
|
+
log.warn(this.devFile, "DEV CONFIG");
|
|
20
|
+
return require(this.devFile);
|
|
21
|
+
} else if (fs.existsSync(this.localFile)) {
|
|
22
|
+
log.warn(this.localFile, "PRO CONFIG");
|
|
23
|
+
return require(this.localFile);
|
|
24
|
+
} else {
|
|
25
|
+
log.warn(this.localFile, "NOT FOUND, USING DEFAULTS");
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get() {
|
|
31
|
+
return obj.merge({}, this.defaultSettings, this.profileSettings);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Wrapper function to support both constructor and function usage
|
|
36
|
+
function Configre(path) {
|
|
37
|
+
if (this instanceof Configre) {
|
|
38
|
+
return new ConfigreClass(path);
|
|
39
|
+
} else {
|
|
40
|
+
return new ConfigreClass(path).get();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
module.exports = Configre;
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "configre",
|
|
3
|
+
"dependencies": {
|
|
4
|
+
"lemonlog": "^1.0.2",
|
|
5
|
+
"lodash": "^4.17.21"
|
|
6
|
+
},
|
|
7
|
+
"description": "Welcome to Configre, the coolest way to manage your project's configuration with a twist of personality based on your environment! Whether you're in development, testing, or production, Configre seamlessly adjusts to your project's needs by loading specific configurations tailored to each environment. Say goodbye to manual config tweaks and hello to automatic, hassle-free setup! π",
|
|
8
|
+
"version": "1.0.0",
|
|
9
|
+
"main": "index.js",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "mocha"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"json",
|
|
15
|
+
"config",
|
|
16
|
+
"setup",
|
|
17
|
+
"deployment",
|
|
18
|
+
"configuration",
|
|
19
|
+
"convention",
|
|
20
|
+
"stage",
|
|
21
|
+
"environment",
|
|
22
|
+
"development",
|
|
23
|
+
"production",
|
|
24
|
+
"inheritance",
|
|
25
|
+
"objects",
|
|
26
|
+
"user-friendly",
|
|
27
|
+
"intuitive"
|
|
28
|
+
],
|
|
29
|
+
"author": "Martin Clasen",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/clasen/Configre.git"
|
|
34
|
+
},
|
|
35
|
+
"bugs": {
|
|
36
|
+
"url": "https://github.com/clasen/Configre/issues"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://github.com/clasen/Configre#readme",
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"mocha": "^10.2.0"
|
|
41
|
+
},
|
|
42
|
+
"directories": {
|
|
43
|
+
"test": "test"
|
|
44
|
+
}
|
|
45
|
+
}
|