configre 1.0.6 → 1.1.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/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,7 @@
1
+ module.exports = {
2
+ db: {
3
+ user: 'john',
4
+ password: 'johns-password'
5
+ }
6
+ }
7
+
@@ -0,0 +1,11 @@
1
+ module.exports = {
2
+ db: {
3
+ host: 'localhost',
4
+ user: 'myuser',
5
+ password: 'mypassword'
6
+ },
7
+ api: {
8
+ key: 'development-api-key'
9
+ }
10
+ }
11
+
@@ -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 = Configre(__dirname + "/config");
11
+ log.info(cfg.db);
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "configre-module-demo",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "private": true
6
+ }
7
+
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
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
- if (fs.existsSync(this.devFile)) {
18
- log.warn(this.devFile, "DEV CONFIG");
19
- return require(this.devFile);
20
- } else if (fs.existsSync(this.localFile)) {
21
- log.warn(this.localFile, "PRO CONFIG");
22
- return require(this.localFile);
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
- log.warn(this.localFile, "NOT FOUND, USING DEFAULTS");
21
+ this.core.logConfig(localFile, "NOT FOUND, USING DEFAULTS");
25
22
  return {};
26
23
  }
27
24
  }
28
25
 
29
26
  get() {
30
- return obj.merge({}, this.defaultSettings, this.profileSettings);
27
+ return this.core.merge(this.defaultSettings, this.profileSettings);
31
28
  }
32
29
  }
33
30
 
package/index.mjs ADDED
@@ -0,0 +1,12 @@
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;
package/package.json CHANGED
@@ -1,12 +1,18 @@
1
1
  {
2
2
  "name": "configre",
3
- "version": "1.0.6",
3
+ "version": "1.1.0",
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",