@james-pre/config 1.0.1 → 1.0.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/dist/manager.d.ts +14 -12
- package/dist/manager.js +9 -4
- package/package.json +3 -3
package/dist/manager.d.ts
CHANGED
|
@@ -3,12 +3,13 @@ import type { FlattenKeys, GetByString, PartialRecursive } from 'utilium';
|
|
|
3
3
|
import * as z from 'zod';
|
|
4
4
|
export type FileType = 'system' | 'user' | 'local';
|
|
5
5
|
/** Information about a loaded config file */
|
|
6
|
-
interface File<Value> {
|
|
6
|
+
interface File<Value, Options extends LoadOptions = LoadOptions> {
|
|
7
7
|
path: string;
|
|
8
8
|
wasIncluded: boolean;
|
|
9
9
|
data: Value;
|
|
10
10
|
type: FileType;
|
|
11
11
|
auto: boolean;
|
|
12
|
+
options: Partial<Options>;
|
|
12
13
|
}
|
|
13
14
|
/**
|
|
14
15
|
* How a file is being loaded
|
|
@@ -23,21 +24,21 @@ export interface LoadOptions {
|
|
|
23
24
|
/**
|
|
24
25
|
* If enabled, the config file will still be loaded if it does not match the schema.
|
|
25
26
|
*/
|
|
26
|
-
loose
|
|
27
|
+
loose: boolean;
|
|
27
28
|
/**
|
|
28
29
|
* If enabled, the config file will be skipped if it does not exist.
|
|
29
30
|
*/
|
|
30
|
-
optional
|
|
31
|
+
optional: boolean;
|
|
31
32
|
/**
|
|
32
33
|
* If enabled, an empty config file will be created when it does not exist.
|
|
33
34
|
* Included files are never created.
|
|
34
35
|
*/
|
|
35
|
-
create
|
|
36
|
+
create: boolean;
|
|
36
37
|
/**
|
|
37
38
|
* Used to mark files that are included, auto-loaded, etc.
|
|
38
39
|
* @internal
|
|
39
40
|
*/
|
|
40
|
-
[kInit]
|
|
41
|
+
[kInit]: FileLoadInit;
|
|
41
42
|
}
|
|
42
43
|
declare const kInit: unique symbol;
|
|
43
44
|
export interface ManagerOptions {
|
|
@@ -57,9 +58,9 @@ export interface ManagerOptions {
|
|
|
57
58
|
/**
|
|
58
59
|
* Manager for configuration files
|
|
59
60
|
*/
|
|
60
|
-
export declare class Manager<Shape extends Readonly<Record<string, z.ZodType
|
|
61
|
-
load: [path: string, config: FileData];
|
|
62
|
-
post_load: [path: string, config: FileData];
|
|
61
|
+
export declare class Manager<LoadOpts extends LoadOptions = LoadOptions, Shape extends Readonly<Record<string, z.ZodType>> = Readonly<Record<string, z.ZodType>>, out In extends z.input<z.ZodObject<Shape>> = z.input<z.ZodObject<Shape>>, out FileData = z.output<ReturnType<typeof z.deepPartial<z.ZodObject<Shape>>>>> extends EventEmitter<{
|
|
62
|
+
load: [path: string, config: FileData, options: Partial<LoadOpts>];
|
|
63
|
+
post_load: [path: string, config: FileData, options: Partial<LoadOpts>];
|
|
63
64
|
load_error: [path: string, stage: 'read' | 'create' | 'parse', error: Error];
|
|
64
65
|
create: [path: string];
|
|
65
66
|
change: [];
|
|
@@ -69,7 +70,8 @@ export declare class Manager<Shape extends Readonly<Record<string, z.ZodType>>,
|
|
|
69
70
|
protected options: ManagerOptions;
|
|
70
71
|
readonly schema: z.ZodObject<Shape>;
|
|
71
72
|
readonly fileSchema: ReturnType<typeof z.deepPartial<z.ZodObject<Shape>>>;
|
|
72
|
-
protected files: Map<string, File<FileData>>;
|
|
73
|
+
protected readonly files: Map<string, File<FileData>>;
|
|
74
|
+
get filePaths(): MapIterator<string>;
|
|
73
75
|
readonly data: z.output<z.ZodObject<Shape>>;
|
|
74
76
|
constructor(shape: Shape, options?: ManagerOptions);
|
|
75
77
|
/**
|
|
@@ -84,7 +86,7 @@ export declare class Manager<Shape extends Readonly<Record<string, z.ZodType>>,
|
|
|
84
86
|
merge(config: PartialRecursive<In>): void;
|
|
85
87
|
/** Replace the current config with `config`, applying defaults for anything missing. */
|
|
86
88
|
replace(config: In): void;
|
|
87
|
-
loadFile(path: string, options: LoadOpts): void;
|
|
89
|
+
loadFile(path: string, options: Partial<LoadOpts>): void;
|
|
88
90
|
/** Get the entry for `path`, adding one for a file that has not been loaded */
|
|
89
91
|
protected fileAt(path: string): File<FileData>;
|
|
90
92
|
protected writeFile(file: File<FileData>): void;
|
|
@@ -108,7 +110,7 @@ export declare class Manager<Shape extends Readonly<Record<string, z.ZodType>>,
|
|
|
108
110
|
/**
|
|
109
111
|
* Load the files from `defaultPaths`. Missing files are skipped unless `options` says otherwise.
|
|
110
112
|
*/
|
|
111
|
-
loadDefaults(options
|
|
113
|
+
loadDefaults(options?: Partial<LoadOpts>): void;
|
|
112
114
|
/**
|
|
113
115
|
* The file a change of the given type is written to:
|
|
114
116
|
* the most recently loaded file of that type, or the path `loadDefaults` would use for it.
|
|
@@ -118,6 +120,6 @@ export declare class Manager<Shape extends Readonly<Record<string, z.ZodType>>,
|
|
|
118
120
|
* Merge `config` into the current config and save it.
|
|
119
121
|
*/
|
|
120
122
|
update(config: PartialRecursive<In>, type?: FileType): void;
|
|
121
|
-
reloadFiles(options
|
|
123
|
+
reloadFiles(options?: Partial<LoadOpts>): void;
|
|
122
124
|
}
|
|
123
125
|
export {};
|
package/dist/manager.js
CHANGED
|
@@ -149,6 +149,9 @@ let Manager = (() => {
|
|
|
149
149
|
schema;
|
|
150
150
|
fileSchema;
|
|
151
151
|
files = new Map();
|
|
152
|
+
get filePaths() {
|
|
153
|
+
return this.files.keys();
|
|
154
|
+
}
|
|
152
155
|
data;
|
|
153
156
|
constructor(shape, options = {}) {
|
|
154
157
|
super({ captureRejections: true });
|
|
@@ -227,8 +230,9 @@ let Manager = (() => {
|
|
|
227
230
|
...(options[kInit] || {}),
|
|
228
231
|
data: file,
|
|
229
232
|
path,
|
|
233
|
+
options,
|
|
230
234
|
});
|
|
231
|
-
this.emit('load', path, file);
|
|
235
|
+
this.emit('load', path, file, options);
|
|
232
236
|
this.merge(file);
|
|
233
237
|
if (canInclude(this.options, file))
|
|
234
238
|
for (const include of file.include ?? []) {
|
|
@@ -239,7 +243,7 @@ let Manager = (() => {
|
|
|
239
243
|
[kInit]: { ...options[kInit], wasIncluded: true },
|
|
240
244
|
});
|
|
241
245
|
}
|
|
242
|
-
this.emit('post_load', path, file);
|
|
246
|
+
this.emit('post_load', path, file, options);
|
|
243
247
|
}
|
|
244
248
|
/** Get the entry for `path`, adding one for a file that has not been loaded */
|
|
245
249
|
fileAt(path) {
|
|
@@ -252,6 +256,7 @@ let Manager = (() => {
|
|
|
252
256
|
type: typeFromPath(path),
|
|
253
257
|
auto: false,
|
|
254
258
|
wasIncluded: false,
|
|
259
|
+
options: {},
|
|
255
260
|
};
|
|
256
261
|
this.files.set(path, file);
|
|
257
262
|
return file;
|
|
@@ -298,7 +303,7 @@ let Manager = (() => {
|
|
|
298
303
|
/**
|
|
299
304
|
* Load the files from `defaultPaths`. Missing files are skipped unless `options` says otherwise.
|
|
300
305
|
*/
|
|
301
|
-
loadDefaults(options) {
|
|
306
|
+
loadDefaults(options = {}) {
|
|
302
307
|
for (const { path, type } of this.defaultPaths) {
|
|
303
308
|
this.loadFile(path, { optional: true, ...options, [kInit]: { type, auto: true } });
|
|
304
309
|
}
|
|
@@ -326,7 +331,7 @@ let Manager = (() => {
|
|
|
326
331
|
update(config, type) {
|
|
327
332
|
this.updateFile(this.findPath(type), config);
|
|
328
333
|
}
|
|
329
|
-
reloadFiles(options) {
|
|
334
|
+
reloadFiles(options = {}) {
|
|
330
335
|
const files = this.files
|
|
331
336
|
.values()
|
|
332
337
|
.filter(file => !file.wasIncluded)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@james-pre/config",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Manage configuration files",
|
|
5
5
|
"funding": {
|
|
6
6
|
"type": "individual",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"scripts": {
|
|
21
21
|
"format:check": "prettier --check .",
|
|
22
22
|
"format": "prettier --write .",
|
|
23
|
-
"build": "tsc",
|
|
24
|
-
"lint": "tsc --noEmit && eslint src",
|
|
23
|
+
"build": "tsc -b",
|
|
24
|
+
"lint": "tsc -b --noEmit && eslint src",
|
|
25
25
|
"prepublishOnly": "npx tsc"
|
|
26
26
|
},
|
|
27
27
|
"repository": {
|