@bemoje/config 1.1.3 → 2.0.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/README.md +11 -12
- package/lib/index.cjs +9 -0
- package/lib/index.d.cts +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +9 -0
- package/package.json +15 -55
package/README.md
CHANGED
|
@@ -2,8 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
Configuration file management with JSON persistence and TypeBox schema validation.
|
|
4
4
|
|
|
5
|
-
[](https://www.typescriptlang.org/)
|
|
6
|
-
|
|
5
|
+
[](https://www.typescriptlang.org/) [](https://nodejs.org/api/esm.html)
|
|
6
|
+
|
|
7
|
+
## Exports
|
|
8
|
+
|
|
9
|
+
<!-- EXPORTS_START -->
|
|
10
|
+
|
|
11
|
+
- **ConfigFile**: Configuration file manager that provides schema validation and file I/O using the Strategy pattern. Ensures that only one instance of ConfigFile exists per file path, and allows for loading and updating configuration data with validation and default values applied.
|
|
12
|
+
- **JsonFileStrategy**: Strategy for loading and saving configuration data as JSON files.
|
|
13
|
+
- **SchemaConfigStrategy**: Strategy for validating configuration data against a TypeBox schema and applying default values.
|
|
14
|
+
|
|
15
|
+
<!-- EXPORTS_END -->
|
|
7
16
|
|
|
8
17
|
## Installation
|
|
9
18
|
|
|
@@ -71,13 +80,3 @@ validation.isValid({ name: 'app', version: '2.0.0' }) // => true
|
|
|
71
80
|
validation.applyDefaults({ name: 'app' })
|
|
72
81
|
// => { name: 'app', version: '1.0.0' }
|
|
73
82
|
```
|
|
74
|
-
|
|
75
|
-
## API Reference
|
|
76
|
-
|
|
77
|
-
| Export | Description |
|
|
78
|
-
| -------------------------- | -------------------------------------------------------------------------------------- |
|
|
79
|
-
| `ConfigFile` | Main config class with singleton per filepath, schema validation, and JSON persistence |
|
|
80
|
-
| `JsonFileStrategy` | Strategy for reading/writing JSON config files |
|
|
81
|
-
| `SchemaConfigStrategy` | Strategy for TypeBox schema validation with default merging |
|
|
82
|
-
| `ConfigDataStrategy` | Interface for data serialization strategies |
|
|
83
|
-
| `ConfigValidationStrategy` | Interface for validation strategies |
|
package/lib/index.cjs
CHANGED
|
@@ -26,6 +26,15 @@ var JsonFileStrategy = class {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
save(config) {
|
|
29
|
+
if (fs__default.default.existsSync(this.filepath)) {
|
|
30
|
+
try {
|
|
31
|
+
const current = fs__default.default.readJsonSync(this.filepath);
|
|
32
|
+
if (JSON.stringify(current) === JSON.stringify(config)) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
} catch (_) {
|
|
36
|
+
}
|
|
37
|
+
}
|
|
29
38
|
fs__default.default.outputFileSync(this.filepath, `${JSON.stringify(config, null, 2)}
|
|
30
39
|
`);
|
|
31
40
|
}
|
package/lib/index.d.cts
CHANGED
|
@@ -13,6 +13,7 @@ interface ConfigValidationStrategy<T> {
|
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Configuration file manager that provides schema validation and file I/O using the Strategy pattern.
|
|
16
|
+
* Ensures that only one instance of ConfigFile exists per file path, and allows for loading and updating configuration data with validation and default values applied.
|
|
16
17
|
*/
|
|
17
18
|
declare class ConfigFile<Schema extends TSchema> {
|
|
18
19
|
protected static instances: Map<string, ConfigFile<TSchema>>;
|
package/lib/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ interface ConfigValidationStrategy<T> {
|
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Configuration file manager that provides schema validation and file I/O using the Strategy pattern.
|
|
16
|
+
* Ensures that only one instance of ConfigFile exists per file path, and allows for loading and updating configuration data with validation and default values applied.
|
|
16
17
|
*/
|
|
17
18
|
declare class ConfigFile<Schema extends TSchema> {
|
|
18
19
|
protected static instances: Map<string, ConfigFile<TSchema>>;
|
package/lib/index.js
CHANGED
|
@@ -20,6 +20,15 @@ var JsonFileStrategy = class {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
save(config) {
|
|
23
|
+
if (fs.existsSync(this.filepath)) {
|
|
24
|
+
try {
|
|
25
|
+
const current = fs.readJsonSync(this.filepath);
|
|
26
|
+
if (JSON.stringify(current) === JSON.stringify(config)) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
} catch (_) {
|
|
30
|
+
}
|
|
31
|
+
}
|
|
23
32
|
fs.outputFileSync(this.filepath, `${JSON.stringify(config, null, 2)}
|
|
24
33
|
`);
|
|
25
34
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemoje/config",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Configuration file management with JSON persistence and TypeBox schema validation.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@sinclair/typebox": "^0.34.48",
|
|
12
|
+
"fs-extra": "^11.3.4",
|
|
13
|
+
"@types/fs-extra": "^11.0.4"
|
|
14
|
+
},
|
|
7
15
|
"keywords": [
|
|
8
16
|
"config",
|
|
9
17
|
"configuration",
|
|
@@ -35,47 +43,7 @@
|
|
|
35
43
|
"default": "./lib/index.cjs"
|
|
36
44
|
}
|
|
37
45
|
},
|
|
38
|
-
"
|
|
39
|
-
"import": {
|
|
40
|
-
"types": "./lib/index.d.ts",
|
|
41
|
-
"default": "./lib/index.js"
|
|
42
|
-
},
|
|
43
|
-
"require": {
|
|
44
|
-
"types": "./lib/index.d.cts",
|
|
45
|
-
"default": "./lib/index.cjs"
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
"./ConfigDataStrategy": {
|
|
49
|
-
"import": {
|
|
50
|
-
"types": "./lib/index.d.ts",
|
|
51
|
-
"default": "./lib/index.js"
|
|
52
|
-
},
|
|
53
|
-
"require": {
|
|
54
|
-
"types": "./lib/index.d.cts",
|
|
55
|
-
"default": "./lib/index.cjs"
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
"./ConfigValidationStrategy": {
|
|
59
|
-
"import": {
|
|
60
|
-
"types": "./lib/index.d.ts",
|
|
61
|
-
"default": "./lib/index.js"
|
|
62
|
-
},
|
|
63
|
-
"require": {
|
|
64
|
-
"types": "./lib/index.d.cts",
|
|
65
|
-
"default": "./lib/index.cjs"
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
"./JsonFileStrategy": {
|
|
69
|
-
"import": {
|
|
70
|
-
"types": "./lib/index.d.ts",
|
|
71
|
-
"default": "./lib/index.js"
|
|
72
|
-
},
|
|
73
|
-
"require": {
|
|
74
|
-
"types": "./lib/index.d.cts",
|
|
75
|
-
"default": "./lib/index.cjs"
|
|
76
|
-
}
|
|
77
|
-
},
|
|
78
|
-
"./SchemaConfigStrategy": {
|
|
46
|
+
"./*": {
|
|
79
47
|
"import": {
|
|
80
48
|
"types": "./lib/index.d.ts",
|
|
81
49
|
"default": "./lib/index.js"
|
|
@@ -86,23 +54,15 @@
|
|
|
86
54
|
}
|
|
87
55
|
}
|
|
88
56
|
},
|
|
89
|
-
"
|
|
90
|
-
"
|
|
91
|
-
"
|
|
92
|
-
"
|
|
93
|
-
},
|
|
94
|
-
"publishConfig": {
|
|
95
|
-
"access": "public"
|
|
57
|
+
"repository": {
|
|
58
|
+
"type": "git",
|
|
59
|
+
"url": "git+https://github.com/bemoje/mono.git",
|
|
60
|
+
"directory": "libs/config"
|
|
96
61
|
},
|
|
97
|
-
"license": "MIT",
|
|
98
62
|
"author": {
|
|
99
63
|
"name": "Benjamin Møller Jensen",
|
|
100
64
|
"email": "bemoje@bemoje.net",
|
|
101
65
|
"url": "https://github.com/bemoje/"
|
|
102
66
|
},
|
|
103
|
-
"
|
|
104
|
-
"type": "git",
|
|
105
|
-
"url": "git+https://github.com/bemoje/mono.git",
|
|
106
|
-
"directory": "libs/config"
|
|
107
|
-
}
|
|
67
|
+
"license": "MIT"
|
|
108
68
|
}
|