configre 1.1.0 → 1.1.4
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 -0
- package/demo/config/hostname.js +1 -1
- package/demo/config/index.js +1 -1
- package/demo/module/config/{hostname.js → hostname.cjs} +1 -1
- package/demo/module/config/{index.js → index.cjs} +1 -1
- package/demo/module/demo.js +6 -5
- package/demo/module/package.json +12 -2
- package/index.js +50 -15
- package/package.json +2 -8
- package/core.js +0 -36
- package/index.mjs +0 -12
package/README.md
CHANGED
|
@@ -29,6 +29,14 @@ To get started with Configre, follow these steps:
|
|
|
29
29
|
- `[hostname].js`: Override configurations for specific hosts.
|
|
30
30
|
- `[hostname].dev.js`: Development-specific configurations.
|
|
31
31
|
|
|
32
|
+
> **Note:** If your main project uses `"type": "module"` in its `package.json`, add a `package.json` inside the `config/` directory with the following content so you can keep using `module.exports` in your config files:
|
|
33
|
+
>
|
|
34
|
+
> ```json
|
|
35
|
+
> {
|
|
36
|
+
> "type": "commonjs"
|
|
37
|
+
> }
|
|
38
|
+
> ```
|
|
39
|
+
|
|
32
40
|
3. **Use Configre in Your Project**
|
|
33
41
|
|
|
34
42
|
Import and use Configre to load your configurations:
|
package/demo/config/hostname.js
CHANGED
package/demo/config/index.js
CHANGED
package/demo/module/demo.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import Configre from "../../index.
|
|
1
|
+
import Configre from "../../index.js";
|
|
2
2
|
import lemonlog from "lemonlog";
|
|
3
3
|
import { fileURLToPath } from "url";
|
|
4
|
-
import { dirname } from "path";
|
|
4
|
+
import { dirname, join } from "path";
|
|
5
5
|
|
|
6
|
+
const log = lemonlog("Configre");
|
|
6
7
|
const __filename = fileURLToPath(import.meta.url);
|
|
7
8
|
const __dirname = dirname(__filename);
|
|
8
|
-
const log = lemonlog("Configre");
|
|
9
9
|
|
|
10
|
-
const cfg = Configre(__dirname
|
|
11
|
-
log.info(cfg.db);
|
|
10
|
+
const cfg = Configre(join(__dirname, "config"));
|
|
11
|
+
log.info(cfg.db);
|
|
12
|
+
|
package/demo/module/package.json
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
{
|
|
2
|
-
"name": "configre-module
|
|
2
|
+
"name": "configre-demo-module",
|
|
3
3
|
"version": "1.0.0",
|
|
4
|
+
"description": "Demo module using Configre with ES modules",
|
|
4
5
|
"type": "module",
|
|
5
|
-
"
|
|
6
|
+
"main": "demo.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "node demo.js"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"configre": "file:../..",
|
|
12
|
+
"lemonlog": "^1.0.2"
|
|
13
|
+
},
|
|
14
|
+
"author": "Martin Clasen",
|
|
15
|
+
"license": "MIT"
|
|
6
16
|
}
|
|
7
17
|
|
package/index.js
CHANGED
|
@@ -1,30 +1,65 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const os = require("os");
|
|
3
|
+
const obj = require("lodash/object");
|
|
1
4
|
const log = require("lemonlog")("Configre");
|
|
2
|
-
const ConfigreCore = require("./core");
|
|
3
5
|
|
|
4
6
|
class ConfigreClass {
|
|
5
7
|
constructor(path = __dirname + "/../../config") {
|
|
6
|
-
this.
|
|
7
|
-
|
|
8
|
+
this.defaultSettings = this.tryRequire([
|
|
9
|
+
path,
|
|
10
|
+
path + '/index.cjs',
|
|
11
|
+
path + '.cjs'
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
this.dirname = path;
|
|
15
|
+
this.profile = process.argv.length > 2 ? process.argv[2] : os.hostname();
|
|
8
16
|
this.profileSettings = this.loadProfileSettings();
|
|
9
17
|
}
|
|
10
18
|
|
|
19
|
+
// Helper method to try requiring files with different extensions or paths
|
|
20
|
+
tryRequire(paths) {
|
|
21
|
+
for (const p of paths) {
|
|
22
|
+
try {
|
|
23
|
+
return require(p);
|
|
24
|
+
} catch (e) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
throw new Error(`Could not load config from any of: ${paths.join(', ')}`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Helper method to try loading a file with .js or .cjs extension
|
|
32
|
+
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
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
11
43
|
loadProfileSettings() {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
this.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
44
|
+
const basePaths = [
|
|
45
|
+
{ base: `${this.dirname}/${this.profile}.dev`, type: "DEV CONFIG" },
|
|
46
|
+
{ base: `${this.dirname}/${this.profile}`, type: "PRO CONFIG" }
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
for (const { base, type } of basePaths) {
|
|
50
|
+
const result = this.tryRequireWithExtensions(base);
|
|
51
|
+
if (result) {
|
|
52
|
+
log.warn(result.path, type);
|
|
53
|
+
return result.module;
|
|
54
|
+
}
|
|
23
55
|
}
|
|
56
|
+
|
|
57
|
+
log.warn(`${this.dirname}/${this.profile}.js`, "NOT FOUND, USING DEFAULTS");
|
|
58
|
+
return {};
|
|
24
59
|
}
|
|
25
60
|
|
|
26
61
|
get() {
|
|
27
|
-
return
|
|
62
|
+
return obj.merge({}, this.defaultSettings, this.profileSettings);
|
|
28
63
|
}
|
|
29
64
|
}
|
|
30
65
|
|
package/package.json
CHANGED
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "configre",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "🔧 Effortlessly Tailor Your Settings",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"exports": {
|
|
7
|
-
".": {
|
|
8
|
-
"import": "./index.mjs",
|
|
9
|
-
"require": "./index.js"
|
|
10
|
-
}
|
|
11
|
-
},
|
|
12
5
|
"dependencies": {
|
|
13
6
|
"lemonlog": "^1.0.2",
|
|
14
7
|
"lodash": "^4.17.21"
|
|
15
8
|
},
|
|
9
|
+
"main": "index.js",
|
|
16
10
|
"keywords": [
|
|
17
11
|
"json",
|
|
18
12
|
"config",
|
package/core.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
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
|
-
|
package/index.mjs
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { createRequire } from "module";
|
|
2
|
-
import { fileURLToPath } from "url";
|
|
3
|
-
import { dirname } from "path";
|
|
4
|
-
|
|
5
|
-
const require = createRequire(import.meta.url);
|
|
6
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
-
const __dirname = dirname(__filename);
|
|
8
|
-
|
|
9
|
-
// Simplemente re-exportar la versión CommonJS que ya funciona síncronamente
|
|
10
|
-
const Configre = require("./index.js");
|
|
11
|
-
|
|
12
|
-
export default Configre;
|