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 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:
@@ -3,4 +3,4 @@ module.exports = {
3
3
  user: 'john',
4
4
  password: 'johns-password'
5
5
  }
6
- }
6
+ };
@@ -7,4 +7,4 @@ module.exports = {
7
7
  api: {
8
8
  key: 'development-api-key'
9
9
  }
10
- }
10
+ };
@@ -3,5 +3,5 @@ module.exports = {
3
3
  user: 'john',
4
4
  password: 'johns-password'
5
5
  }
6
- }
6
+ };
7
7
 
@@ -7,5 +7,5 @@ module.exports = {
7
7
  api: {
8
8
  key: 'development-api-key'
9
9
  }
10
- }
10
+ };
11
11
 
@@ -1,11 +1,12 @@
1
- import Configre from "../../index.mjs";
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 + "/config");
11
- log.info(cfg.db);
10
+ const cfg = Configre(join(__dirname, "config"));
11
+ log.info(cfg.db);
12
+
@@ -1,7 +1,17 @@
1
1
  {
2
- "name": "configre-module-demo",
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
- "private": true
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.core = new ConfigreCore(path, log);
7
- this.defaultSettings = require(path);
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 { 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);
20
- } else {
21
- this.core.logConfig(localFile, "NOT FOUND, USING DEFAULTS");
22
- return {};
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 this.core.merge(this.defaultSettings, this.profileSettings);
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.0",
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;