configre 1.0.4 → 1.0.8
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 +2 -2
- package/core.js +36 -0
- package/demo/module/config/hostname.js +7 -0
- package/demo/module/config/index.js +11 -0
- package/demo/module/demo.js +11 -0
- package/demo/module/package.json +7 -0
- package/index.js +13 -16
- package/index.mjs +53 -0
- package/package.json +10 -3
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ To get started with Configre, follow these steps:
|
|
|
18
18
|
First, make sure you have `Node.js` installed. Then, add Configre to your project:
|
|
19
19
|
|
|
20
20
|
```bash
|
|
21
|
-
npm install
|
|
21
|
+
npm install configre --save
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
2. **Setup Your Configuration Files**
|
|
@@ -34,7 +34,7 @@ To get started with Configre, follow these steps:
|
|
|
34
34
|
Import and use Configre to load your configurations:
|
|
35
35
|
|
|
36
36
|
```javascript
|
|
37
|
-
const cfg = require("Configre")(
|
|
37
|
+
const cfg = require("Configre")(); // default dir '/config'
|
|
38
38
|
console.log(cfg.db); // Access your db configuration
|
|
39
39
|
```
|
|
40
40
|
|
package/core.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const os = require("os");
|
|
3
|
+
const obj = require("lodash/object");
|
|
4
|
+
|
|
5
|
+
class ConfigreCore {
|
|
6
|
+
constructor(path, log) {
|
|
7
|
+
this.dirname = path;
|
|
8
|
+
this.log = log;
|
|
9
|
+
this.profile = process.argv.length > 2 ? process.argv[2] : os.hostname();
|
|
10
|
+
this.devFile = `${this.dirname}/${this.profile}.dev.js`;
|
|
11
|
+
this.localFile = `${this.dirname}/${this.profile}.js`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
getFiles() {
|
|
15
|
+
return {
|
|
16
|
+
devFile: this.devFile,
|
|
17
|
+
localFile: this.localFile,
|
|
18
|
+
indexFile: `${this.dirname}/index.js`
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
checkFileExists(file) {
|
|
23
|
+
return fs.existsSync(file);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
merge(defaultSettings, profileSettings) {
|
|
27
|
+
return obj.merge({}, defaultSettings, profileSettings);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
logConfig(file, type) {
|
|
31
|
+
this.log.warn(file, type);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = ConfigreCore;
|
|
36
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import Configre from "../../index.mjs";
|
|
2
|
+
import lemonlog from "lemonlog";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
import { dirname } from "path";
|
|
5
|
+
|
|
6
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
+
const __dirname = dirname(__filename);
|
|
8
|
+
const log = lemonlog("Configre");
|
|
9
|
+
|
|
10
|
+
const cfg = await Configre(__dirname + "/config");
|
|
11
|
+
log.info(cfg.db);
|
package/index.js
CHANGED
|
@@ -1,33 +1,30 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const os = require("os");
|
|
3
|
-
const obj = require("lodash/object");
|
|
4
1
|
const log = require("lemonlog")("Configre");
|
|
2
|
+
const ConfigreCore = require("./core");
|
|
5
3
|
|
|
6
4
|
class ConfigreClass {
|
|
7
|
-
constructor(path) {
|
|
5
|
+
constructor(path = __dirname + "/../../config") {
|
|
6
|
+
this.core = new ConfigreCore(path, log);
|
|
8
7
|
this.defaultSettings = require(path);
|
|
9
|
-
this.dirname = path;
|
|
10
|
-
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
8
|
this.profileSettings = this.loadProfileSettings();
|
|
14
9
|
}
|
|
15
10
|
|
|
16
11
|
loadProfileSettings() {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
const { devFile, localFile } = this.core.getFiles();
|
|
13
|
+
|
|
14
|
+
if (this.core.checkFileExists(devFile)) {
|
|
15
|
+
this.core.logConfig(devFile, "DEV CONFIG");
|
|
16
|
+
return require(devFile);
|
|
17
|
+
} else if (this.core.checkFileExists(localFile)) {
|
|
18
|
+
this.core.logConfig(localFile, "PRO CONFIG");
|
|
19
|
+
return require(localFile);
|
|
23
20
|
} else {
|
|
24
|
-
|
|
21
|
+
this.core.logConfig(localFile, "NOT FOUND, USING DEFAULTS");
|
|
25
22
|
return {};
|
|
26
23
|
}
|
|
27
24
|
}
|
|
28
25
|
|
|
29
26
|
get() {
|
|
30
|
-
return
|
|
27
|
+
return this.core.merge(this.defaultSettings, this.profileSettings);
|
|
31
28
|
}
|
|
32
29
|
}
|
|
33
30
|
|
package/index.mjs
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import lemonlog from "lemonlog";
|
|
2
|
+
import { fileURLToPath } from "url";
|
|
3
|
+
import { dirname } from "path";
|
|
4
|
+
import { pathToFileURL } from "url";
|
|
5
|
+
import { createRequire } from "module";
|
|
6
|
+
|
|
7
|
+
const require = createRequire(import.meta.url);
|
|
8
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
+
const __dirname = dirname(__filename);
|
|
10
|
+
const log = lemonlog("Configre");
|
|
11
|
+
const ConfigreCore = require("./core.js");
|
|
12
|
+
|
|
13
|
+
class ConfigreClass {
|
|
14
|
+
constructor(path = __dirname + "/../../config") {
|
|
15
|
+
this.core = new ConfigreCore(path, log);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async loadDefaultSettings() {
|
|
19
|
+
const { indexFile } = this.core.getFiles();
|
|
20
|
+
const module = await import(pathToFileURL(indexFile).href);
|
|
21
|
+
return module.default;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async loadProfileSettings() {
|
|
25
|
+
const { devFile, localFile } = this.core.getFiles();
|
|
26
|
+
|
|
27
|
+
if (this.core.checkFileExists(devFile)) {
|
|
28
|
+
this.core.logConfig(devFile, "DEV CONFIG");
|
|
29
|
+
const module = await import(pathToFileURL(devFile).href);
|
|
30
|
+
return module.default;
|
|
31
|
+
} else if (this.core.checkFileExists(localFile)) {
|
|
32
|
+
this.core.logConfig(localFile, "PRO CONFIG");
|
|
33
|
+
const module = await import(pathToFileURL(localFile).href);
|
|
34
|
+
return module.default;
|
|
35
|
+
} else {
|
|
36
|
+
this.core.logConfig(localFile, "NOT FOUND, USING DEFAULTS");
|
|
37
|
+
return {};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async get() {
|
|
42
|
+
const defaultSettings = await this.loadDefaultSettings();
|
|
43
|
+
const profileSettings = await this.loadProfileSettings();
|
|
44
|
+
return this.core.merge(defaultSettings, profileSettings);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Wrapper function to support both constructor and function usage
|
|
49
|
+
async function Configre(path) {
|
|
50
|
+
return await new ConfigreClass(path).get();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default Configre;
|
package/package.json
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "configre",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "🔧 Effortlessly Tailor Your Settings",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./index.mjs",
|
|
9
|
+
"require": "./index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
5
12
|
"dependencies": {
|
|
6
13
|
"lemonlog": "^1.0.2",
|
|
7
14
|
"lodash": "^4.17.21"
|
|
8
15
|
},
|
|
9
|
-
"main": "index.js",
|
|
10
16
|
"keywords": [
|
|
11
17
|
"json",
|
|
12
18
|
"config",
|
|
@@ -21,7 +27,8 @@
|
|
|
21
27
|
"inheritance",
|
|
22
28
|
"objects",
|
|
23
29
|
"user-friendly",
|
|
24
|
-
"intuitive"
|
|
30
|
+
"intuitive",
|
|
31
|
+
"clasen"
|
|
25
32
|
],
|
|
26
33
|
"author": "Martin Clasen",
|
|
27
34
|
"license": "MIT",
|