configre 1.0.8 → 1.1.2

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
+ };
@@ -1,7 +1,7 @@
1
- export default {
1
+ module.exports = {
2
2
  db: {
3
3
  user: 'john',
4
4
  password: 'johns-password'
5
5
  }
6
- }
6
+ };
7
7
 
@@ -1,4 +1,4 @@
1
- export default {
1
+ module.exports = {
2
2
  db: {
3
3
  host: 'localhost',
4
4
  user: 'myuser',
@@ -7,5 +7,5 @@ export default {
7
7
  api: {
8
8
  key: 'development-api-key'
9
9
  }
10
- }
10
+ };
11
11
 
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -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 = await 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,33 @@
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
8
  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`;
8
13
  this.profileSettings = this.loadProfileSettings();
9
14
  }
10
15
 
11
16
  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);
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);
20
23
  } else {
21
- this.core.logConfig(localFile, "NOT FOUND, USING DEFAULTS");
24
+ log.warn(this.localFile, "NOT FOUND, USING DEFAULTS");
22
25
  return {};
23
26
  }
24
27
  }
25
28
 
26
29
  get() {
27
- return this.core.merge(this.defaultSettings, this.profileSettings);
30
+ return obj.merge({}, this.defaultSettings, this.profileSettings);
28
31
  }
29
32
  }
30
33
 
package/package.json CHANGED
@@ -1,18 +1,12 @@
1
1
  {
2
2
  "name": "configre",
3
- "version": "1.0.8",
3
+ "version": "1.1.2",
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,53 +0,0 @@
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;