@fgv/ts-json-base 3.0.1-alpha.2 → 3.0.1-alpha.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.
@@ -26,22 +26,22 @@ export { Converters }
26
26
  /**
27
27
  * Reads all JSON files from a directory and apply a supplied converter.
28
28
  * @param srcPath - The path of the folder to be read.
29
- * @param options - {@link JsonFile.IDirectoryConvertOptions | Options} to control
29
+ * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
30
30
  * conversion and filtering
31
31
  * @public
32
32
  */
33
- declare function convertJsonDirectorySync<T>(srcPath: string, options: IDirectoryConvertOptions<T>): Result<IReadDirectoryItem<T>[]>;
33
+ declare function convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>;
34
34
 
35
35
  /**
36
36
  * Reads and converts all JSON files from a directory, returning a
37
37
  * `Map<string, T>` indexed by file base name (i.e. minus the extension)
38
38
  * with an optional name transformation applied if present.
39
39
  * @param srcPath - The path of the folder to be read.
40
- * @param options - {@link JsonFile.IDirectoryToMapConvertOptions | Options} to control conversion,
40
+ * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
41
41
  * filtering and naming.
42
42
  * @public
43
43
  */
44
- declare function convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IDirectoryToMapConvertOptions<T, TC>): Result<Map<string, T>>;
44
+ declare function convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>;
45
45
 
46
46
  /**
47
47
  * Read a JSON file and apply a supplied converter.
@@ -69,52 +69,35 @@ declare const DefaultJsonFsHelperConfig: IJsonFsHelperConfig;
69
69
  declare const DefaultJsonLike: IJsonLike;
70
70
 
71
71
  /**
72
- * Options for directory conversion.
73
- * TODO: add filtering, allowed and excluded.
74
- * @public
75
- */
76
- declare interface IDirectoryConvertOptions<T, TC = unknown> {
77
- /**
78
- * The converter used to convert incoming JSON objects.
79
- */
80
- converter: Converter<T, TC>;
81
- validator?: undefined;
82
- }
83
-
84
- /**
85
- * Options controlling conversion of a directory to a `Map`.
86
- * @public
87
- */
88
- declare interface IDirectoryToMapConvertOptions<T, TC = unknown> extends IDirectoryConvertOptions<T, TC> {
89
- transformName?: ItemNameTransformFunction<T>;
90
- }
91
-
92
- /**
93
- * Options controlling validation of a directory to a `Map`.
72
+ * Conversion context for JSON converters.
94
73
  * @public
95
74
  */
96
- declare interface IDirectoryToMapValidateOptions<T, TC = unknown> extends IDirectoryValidateOptions<T, TC> {
97
- transformName?: ItemNameTransformFunction<T>;
75
+ declare interface IJsonConverterContext {
76
+ ignoreUndefinedProperties?: boolean;
98
77
  }
99
78
 
100
79
  /**
101
- * Options for directory validation.
80
+ * Options for directory conversion.
81
+ * TODO: add filtering, allowed and excluded.
102
82
  * @public
103
83
  */
104
- declare interface IDirectoryValidateOptions<T, TC = unknown> {
105
- converter?: undefined;
84
+ declare interface IJsonFsDirectoryOptions<T, TC = unknown> {
85
+ /**
86
+ * The converter used to convert incoming JSON objects.
87
+ */
88
+ converter: Converter<T, TC> | Validator<T, TC>;
106
89
  /**
107
- * The validator used to validate incoming JSON objects
90
+ * Filter applied to items in the directory
108
91
  */
109
- validator: Validator<T, TC>;
92
+ files?: RegExp[];
110
93
  }
111
94
 
112
95
  /**
113
- * Conversion context for JSON converters.
96
+ * Options controlling conversion of a directory to a `Map`.
114
97
  * @public
115
98
  */
116
- declare interface IJsonConverterContext {
117
- ignoreUndefinedProperties?: boolean;
99
+ declare interface IJsonFsDirectoryToMapOptions<T, TC = unknown> extends IJsonFsDirectoryOptions<T, TC> {
100
+ transformName?: ItemNameTransformFunction<T>;
118
101
  }
119
102
 
120
103
  /**
@@ -124,6 +107,7 @@ declare interface IJsonConverterContext {
124
107
  declare interface IJsonFsHelperConfig {
125
108
  json: IJsonLike;
126
109
  allowUndefinedWrite: boolean;
110
+ defaultFiles: RegExp[];
127
111
  }
128
112
 
129
113
  /**
@@ -225,14 +209,10 @@ declare namespace JsonFile {
225
209
  convertJsonDirectorySync,
226
210
  convertJsonDirectoryToMapSync,
227
211
  writeJsonFileSync,
228
- IDirectoryConvertOptions,
229
- IDirectoryValidateOptions,
230
- JsonFsDirectoryOptions,
212
+ IJsonFsDirectoryOptions,
231
213
  IReadDirectoryItem,
232
214
  ItemNameTransformFunction,
233
- IDirectoryToMapConvertOptions,
234
- IDirectoryToMapValidateOptions,
235
- JsonFsDirectoryToMapOptions,
215
+ IJsonFsDirectoryToMapOptions,
236
216
  IJsonFsHelperConfig,
237
217
  JsonFsHelperInitOptions,
238
218
  DefaultJsonFsHelperConfig,
@@ -248,18 +228,6 @@ declare namespace JsonFile {
248
228
  }
249
229
  export { JsonFile }
250
230
 
251
- /**
252
- * Options for directory conversion or validation.
253
- * @public
254
- */
255
- declare type JsonFsDirectoryOptions<T, TC = unknown> = IDirectoryConvertOptions<T, TC> | IDirectoryValidateOptions<T, TC>;
256
-
257
- /**
258
- * Options controlling processing of a directory to a `Map`.
259
- * @public
260
- */
261
- declare type JsonFsDirectoryToMapOptions<T, TC = unknown> = IDirectoryToMapConvertOptions<T, TC> | IDirectoryToMapValidateOptions<T, TC>;
262
-
263
231
  /**
264
232
  * Helper class to simplify common filesystem operations involving JSON (or JSON-like)
265
233
  * files.
@@ -294,25 +262,26 @@ declare class JsonFsHelper {
294
262
  /**
295
263
  * Reads all JSON files from a directory and apply a supplied converter or validator.
296
264
  * @param srcPath - The path of the folder to be read.
297
- * @param options - {@link JsonFile.JsonFsDirectoryOptions | Options} to control
265
+ * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
298
266
  * conversion and filtering
299
267
  */
300
- convertJsonDirectorySync<T>(srcPath: string, options: JsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>;
268
+ convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>;
301
269
  /**
302
270
  * Reads and converts or validates all JSON files from a directory, returning a
303
271
  * `Map<string, T>` indexed by file base name (i.e. minus the extension)
304
272
  * with an optional name transformation applied if present.
305
273
  * @param srcPath - The path of the folder to be read.
306
- * @param options - {@link JsonFile.JsonFsDirectoryToMapOptions | Options} to control conversion,
274
+ * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
307
275
  * filtering and naming.
308
276
  */
309
- convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: JsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>;
277
+ convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>;
310
278
  /**
311
279
  * Write type-safe JSON to a file.
312
280
  * @param srcPath - Path of the file to write.
313
281
  * @param value - The {@link JsonValue | JsonValue} to be written.
314
282
  */
315
283
  writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean>;
284
+ protected _pathMatchesOptions<T, TC>(options: IJsonFsDirectoryOptions<T, TC>, path: string): boolean;
316
285
  }
317
286
 
318
287
  /**
@@ -1,6 +1,6 @@
1
1
  import { Converter, Result } from '@fgv/ts-utils';
2
2
  import { JsonValue } from '../json';
3
- import { IDirectoryConvertOptions, IDirectoryToMapConvertOptions, IReadDirectoryItem } from './jsonFsHelper';
3
+ import { IJsonFsDirectoryOptions, IJsonFsDirectoryToMapOptions, IReadDirectoryItem } from './jsonFsHelper';
4
4
  /**
5
5
  * {@inheritdoc JsonFile.JsonFsHelper.readJsonFileSync}
6
6
  * @public
@@ -17,21 +17,21 @@ export declare function convertJsonFileSync<T>(srcPath: string, converter: Conve
17
17
  /**
18
18
  * Reads all JSON files from a directory and apply a supplied converter.
19
19
  * @param srcPath - The path of the folder to be read.
20
- * @param options - {@link JsonFile.IDirectoryConvertOptions | Options} to control
20
+ * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
21
21
  * conversion and filtering
22
22
  * @public
23
23
  */
24
- export declare function convertJsonDirectorySync<T>(srcPath: string, options: IDirectoryConvertOptions<T>): Result<IReadDirectoryItem<T>[]>;
24
+ export declare function convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>;
25
25
  /**
26
26
  * Reads and converts all JSON files from a directory, returning a
27
27
  * `Map<string, T>` indexed by file base name (i.e. minus the extension)
28
28
  * with an optional name transformation applied if present.
29
29
  * @param srcPath - The path of the folder to be read.
30
- * @param options - {@link JsonFile.IDirectoryToMapConvertOptions | Options} to control conversion,
30
+ * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
31
31
  * filtering and naming.
32
32
  * @public
33
33
  */
34
- export declare function convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IDirectoryToMapConvertOptions<T, TC>): Result<Map<string, T>>;
34
+ export declare function convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>;
35
35
  /**
36
36
  * {@inheritDoc JsonFile.JsonFsHelper.writeJsonFileSync}
37
37
  * @public
@@ -1 +1 @@
1
- {"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../../src/packlets/json-file/file.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAEL,wBAAwB,EACxB,6BAA6B,EAC7B,kBAAkB,EAEnB,MAAM,gBAAgB,CAAC;AAGxB;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,CAEnE;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAA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EAAE,CAAC,CAEjC;AAED;;;;;;;;GAQG;AACH,wBAAgB,6BAA6B,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO,EAC3D,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,4BAA4B,CAAC,CAAC,EAAE,EAAE,CAAC,GAC3C,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAExB;AAOD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAEpF"}
@@ -46,7 +46,7 @@ exports.convertJsonFileSync = convertJsonFileSync;
46
46
  /**
47
47
  * Reads all JSON files from a directory and apply a supplied converter.
48
48
  * @param srcPath - The path of the folder to be read.
49
- * @param options - {@link JsonFile.IDirectoryConvertOptions | Options} to control
49
+ * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
50
50
  * conversion and filtering
51
51
  * @public
52
52
  */
@@ -59,7 +59,7 @@ exports.convertJsonDirectorySync = convertJsonDirectorySync;
59
59
  * `Map<string, T>` indexed by file base name (i.e. minus the extension)
60
60
  * with an optional name transformation applied if present.
61
61
  * @param srcPath - The path of the folder to be read.
62
- * @param options - {@link JsonFile.IDirectoryToMapConvertOptions | Options} to control conversion,
62
+ * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
63
63
  * filtering and naming.
64
64
  * @public
65
65
  */
@@ -1 +1 @@
1
- {"version":3,"file":"file.js","sourceRoot":"","sources":["../../../src/packlets/json-file/file.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAIH,iDAMwB;AACxB,yCAA6C;AAE7C;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,OAAe;IAC9C,OAAO,kCAAmB,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;AACvD,CAAC;AAFD,4CAEC;AAED;;;;;;GAMG;AACH,SAAgB,mBAAmB,CAAI,OAAe,EAAE,SAAuB;IAC7E,OAAO,kCAAmB,CAAC,mBAAmB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;AACrE,CAAC;AAFD,kDAEC;AAED;;;;;;GAMG;AACH,SAAgB,wBAAwB,CACtC,OAAe,EACf,OAAoC;IAEpC,OAAO,kCAAmB,CAAC,wBAAwB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AACxE,CAAC;AALD,4DAKC;AAED;;;;;;;;GAQG;AACH,SAAgB,6BAA6B,CAC3C,OAAe,EACf,OAA6C;IAE7C,OAAO,kCAAmB,CAAC,6BAA6B,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAC7E,CAAC;AALD,sEAKC;AAED,MAAM,kBAAkB,GAAiB,IAAI,2BAAY,CAAC;IACxD,IAAI,EAAE,0BAAe;IACrB,mBAAmB,EAAE,IAAI,CAAC,oBAAoB;CAC/C,CAAC,CAAC;AAEH;;;GAGG;AACH,SAAgB,iBAAiB,CAAC,OAAe,EAAE,KAAgB;IACjE,OAAO,kBAAkB,CAAC,iBAAiB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;AAC9D,CAAC;AAFD,8CAEC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Converter, Result } from '@fgv/ts-utils';\nimport { JsonValue } from '../json';\nimport {\n DefaultJsonFsHelper,\n IDirectoryConvertOptions,\n IDirectoryToMapConvertOptions,\n IReadDirectoryItem,\n JsonFsHelper\n} from './jsonFsHelper';\nimport { DefaultJsonLike } from './jsonLike';\n\n/**\n * {@inheritdoc JsonFile.JsonFsHelper.readJsonFileSync}\n * @public\n */\nexport function readJsonFileSync(srcPath: string): Result<JsonValue> {\n return DefaultJsonFsHelper.readJsonFileSync(srcPath);\n}\n\n/**\n * Read a JSON file and apply a supplied converter.\n * @param srcPath - Path of the file to read.\n * @param converter - `Converter` used to convert the file contents\n * @returns `Success` with a result of type `<T>`, or `Failure`* with a message if an error occurs.\n * @public\n */\nexport function convertJsonFileSync<T>(srcPath: string, converter: Converter<T>): Result<T> {\n return DefaultJsonFsHelper.convertJsonFileSync(srcPath, converter);\n}\n\n/**\n * Reads all JSON files from a directory and apply a supplied converter.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.IDirectoryConvertOptions | Options} to control\n * conversion and filtering\n * @public\n */\nexport function convertJsonDirectorySync<T>(\n srcPath: string,\n options: IDirectoryConvertOptions<T>\n): Result<IReadDirectoryItem<T>[]> {\n return DefaultJsonFsHelper.convertJsonDirectorySync(srcPath, options);\n}\n\n/**\n * Reads and converts all JSON files from a directory, returning a\n * `Map<string, T>` indexed by file base name (i.e. minus the extension)\n * with an optional name transformation applied if present.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.IDirectoryToMapConvertOptions | Options} to control conversion,\n * filtering and naming.\n * @public\n */\nexport function convertJsonDirectoryToMapSync<T, TC = unknown>(\n srcPath: string,\n options: IDirectoryToMapConvertOptions<T, TC>\n): Result<Map<string, T>> {\n return DefaultJsonFsHelper.convertJsonDirectoryToMapSync(srcPath, options);\n}\n\nconst CompatJsonFsHelper: JsonFsHelper = new JsonFsHelper({\n json: DefaultJsonLike,\n allowUndefinedWrite: true // for compatibility\n});\n\n/**\n * {@inheritDoc JsonFile.JsonFsHelper.writeJsonFileSync}\n * @public\n */\nexport function writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean> {\n return CompatJsonFsHelper.writeJsonFileSync(srcPath, value);\n}\n"]}
1
+ {"version":3,"file":"file.js","sourceRoot":"","sources":["../../../src/packlets/json-file/file.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAIH,iDAMwB;AACxB,yCAA6C;AAE7C;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,OAAe;IAC9C,OAAO,kCAAmB,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;AACvD,CAAC;AAFD,4CAEC;AAED;;;;;;GAMG;AACH,SAAgB,mBAAmB,CAAI,OAAe,EAAE,SAAuB;IAC7E,OAAO,kCAAmB,CAAC,mBAAmB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;AACrE,CAAC;AAFD,kDAEC;AAED;;;;;;GAMG;AACH,SAAgB,wBAAwB,CACtC,OAAe,EACf,OAAmC;IAEnC,OAAO,kCAAmB,CAAC,wBAAwB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AACxE,CAAC;AALD,4DAKC;AAED;;;;;;;;GAQG;AACH,SAAgB,6BAA6B,CAC3C,OAAe,EACf,OAA4C;IAE5C,OAAO,kCAAmB,CAAC,6BAA6B,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAC7E,CAAC;AALD,sEAKC;AAED,MAAM,kBAAkB,GAAiB,IAAI,2BAAY,CAAC;IACxD,IAAI,EAAE,0BAAe;IACrB,mBAAmB,EAAE,IAAI,CAAC,oBAAoB;CAC/C,CAAC,CAAC;AAEH;;;GAGG;AACH,SAAgB,iBAAiB,CAAC,OAAe,EAAE,KAAgB;IACjE,OAAO,kBAAkB,CAAC,iBAAiB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;AAC9D,CAAC;AAFD,8CAEC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Converter, Result } from '@fgv/ts-utils';\nimport { JsonValue } from '../json';\nimport {\n DefaultJsonFsHelper,\n IJsonFsDirectoryOptions,\n IJsonFsDirectoryToMapOptions,\n IReadDirectoryItem,\n JsonFsHelper\n} from './jsonFsHelper';\nimport { DefaultJsonLike } from './jsonLike';\n\n/**\n * {@inheritdoc JsonFile.JsonFsHelper.readJsonFileSync}\n * @public\n */\nexport function readJsonFileSync(srcPath: string): Result<JsonValue> {\n return DefaultJsonFsHelper.readJsonFileSync(srcPath);\n}\n\n/**\n * Read a JSON file and apply a supplied converter.\n * @param srcPath - Path of the file to read.\n * @param converter - `Converter` used to convert the file contents\n * @returns `Success` with a result of type `<T>`, or `Failure`* with a message if an error occurs.\n * @public\n */\nexport function convertJsonFileSync<T>(srcPath: string, converter: Converter<T>): Result<T> {\n return DefaultJsonFsHelper.convertJsonFileSync(srcPath, converter);\n}\n\n/**\n * Reads all JSON files from a directory and apply a supplied converter.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control\n * conversion and filtering\n * @public\n */\nexport function convertJsonDirectorySync<T>(\n srcPath: string,\n options: IJsonFsDirectoryOptions<T>\n): Result<IReadDirectoryItem<T>[]> {\n return DefaultJsonFsHelper.convertJsonDirectorySync(srcPath, options);\n}\n\n/**\n * Reads and converts all JSON files from a directory, returning a\n * `Map<string, T>` indexed by file base name (i.e. minus the extension)\n * with an optional name transformation applied if present.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,\n * filtering and naming.\n * @public\n */\nexport function convertJsonDirectoryToMapSync<T, TC = unknown>(\n srcPath: string,\n options: IJsonFsDirectoryToMapOptions<T, TC>\n): Result<Map<string, T>> {\n return DefaultJsonFsHelper.convertJsonDirectoryToMapSync(srcPath, options);\n}\n\nconst CompatJsonFsHelper: JsonFsHelper = new JsonFsHelper({\n json: DefaultJsonLike,\n allowUndefinedWrite: true // for compatibility\n});\n\n/**\n * {@inheritDoc JsonFile.JsonFsHelper.writeJsonFileSync}\n * @public\n */\nexport function writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean> {\n return CompatJsonFsHelper.writeJsonFileSync(srcPath, value);\n}\n"]}
@@ -6,29 +6,16 @@ import { IJsonLike } from './jsonLike';
6
6
  * TODO: add filtering, allowed and excluded.
7
7
  * @public
8
8
  */
9
- export interface IDirectoryConvertOptions<T, TC = unknown> {
9
+ export interface IJsonFsDirectoryOptions<T, TC = unknown> {
10
10
  /**
11
11
  * The converter used to convert incoming JSON objects.
12
12
  */
13
- converter: Converter<T, TC>;
14
- validator?: undefined;
15
- }
16
- /**
17
- * Options for directory validation.
18
- * @public
19
- */
20
- export interface IDirectoryValidateOptions<T, TC = unknown> {
21
- converter?: undefined;
13
+ converter: Converter<T, TC> | Validator<T, TC>;
22
14
  /**
23
- * The validator used to validate incoming JSON objects
15
+ * Filter applied to items in the directory
24
16
  */
25
- validator: Validator<T, TC>;
17
+ files?: RegExp[];
26
18
  }
27
- /**
28
- * Options for directory conversion or validation.
29
- * @public
30
- */
31
- export type JsonFsDirectoryOptions<T, TC = unknown> = IDirectoryConvertOptions<T, TC> | IDirectoryValidateOptions<T, TC>;
32
19
  /**
33
20
  * Return value for one item in a directory conversion.
34
21
  * @public
@@ -53,21 +40,9 @@ export type ItemNameTransformFunction<T> = (name: string, item: T) => Result<str
53
40
  * Options controlling conversion of a directory to a `Map`.
54
41
  * @public
55
42
  */
56
- export interface IDirectoryToMapConvertOptions<T, TC = unknown> extends IDirectoryConvertOptions<T, TC> {
43
+ export interface IJsonFsDirectoryToMapOptions<T, TC = unknown> extends IJsonFsDirectoryOptions<T, TC> {
57
44
  transformName?: ItemNameTransformFunction<T>;
58
45
  }
59
- /**
60
- * Options controlling validation of a directory to a `Map`.
61
- * @public
62
- */
63
- export interface IDirectoryToMapValidateOptions<T, TC = unknown> extends IDirectoryValidateOptions<T, TC> {
64
- transformName?: ItemNameTransformFunction<T>;
65
- }
66
- /**
67
- * Options controlling processing of a directory to a `Map`.
68
- * @public
69
- */
70
- export type JsonFsDirectoryToMapOptions<T, TC = unknown> = IDirectoryToMapConvertOptions<T, TC> | IDirectoryToMapValidateOptions<T, TC>;
71
46
  /**
72
47
  * Configuration for {@link JsonFile.JsonFsHelper | JsonFsHelper}.
73
48
  * @public
@@ -75,6 +50,7 @@ export type JsonFsDirectoryToMapOptions<T, TC = unknown> = IDirectoryToMapConver
75
50
  export interface IJsonFsHelperConfig {
76
51
  json: IJsonLike;
77
52
  allowUndefinedWrite: boolean;
53
+ defaultFiles: RegExp[];
78
54
  }
79
55
  /**
80
56
  * Initialization options for {@link JsonFile.JsonFsHelper | JsonFsHelper}.
@@ -120,25 +96,26 @@ export declare class JsonFsHelper {
120
96
  /**
121
97
  * Reads all JSON files from a directory and apply a supplied converter or validator.
122
98
  * @param srcPath - The path of the folder to be read.
123
- * @param options - {@link JsonFile.JsonFsDirectoryOptions | Options} to control
99
+ * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
124
100
  * conversion and filtering
125
101
  */
126
- convertJsonDirectorySync<T>(srcPath: string, options: JsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>;
102
+ convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>;
127
103
  /**
128
104
  * Reads and converts or validates all JSON files from a directory, returning a
129
105
  * `Map<string, T>` indexed by file base name (i.e. minus the extension)
130
106
  * with an optional name transformation applied if present.
131
107
  * @param srcPath - The path of the folder to be read.
132
- * @param options - {@link JsonFile.JsonFsDirectoryToMapOptions | Options} to control conversion,
108
+ * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
133
109
  * filtering and naming.
134
110
  */
135
- convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: JsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>;
111
+ convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>;
136
112
  /**
137
113
  * Write type-safe JSON to a file.
138
114
  * @param srcPath - Path of the file to write.
139
115
  * @param value - The {@link JsonValue | JsonValue} to be written.
140
116
  */
141
117
  writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean>;
118
+ protected _pathMatchesOptions<T, TC>(options: IJsonFsDirectoryOptions<T, TC>, path: string): boolean;
142
119
  }
143
120
  /**
144
121
  * @public
@@ -1 +1 @@
1
- {"version":3,"file":"jsonFsHelper.d.ts","sourceRoot":"","sources":["../../../src/packlets/json-file/jsonFsHelper.ts"],"names":[],"mappings":"AAsBA,OA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B,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC;IAY5E,SAAS,CAAC,mBAAmB,CAAC,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,uBAAuB,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO;CAIrG;AAED;;GAEG;AACH,eAAO,MAAM,mBAAmB,EAAE,YAAiC,CAAC"}
@@ -63,7 +63,8 @@ const defaultNameTransformer = (n) => (0, ts_utils_1.succeed)(n);
63
63
  */
64
64
  exports.DefaultJsonFsHelperConfig = {
65
65
  json: jsonLike_1.DefaultJsonLike,
66
- allowUndefinedWrite: false
66
+ allowUndefinedWrite: false,
67
+ defaultFiles: [/.*.json/]
67
68
  };
68
69
  /**
69
70
  * Helper class to simplify common filesystem operations involving JSON (or JSON-like)
@@ -107,7 +108,7 @@ class JsonFsHelper {
107
108
  /**
108
109
  * Reads all JSON files from a directory and apply a supplied converter or validator.
109
110
  * @param srcPath - The path of the folder to be read.
110
- * @param options - {@link JsonFile.JsonFsDirectoryOptions | Options} to control
111
+ * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
111
112
  * conversion and filtering
112
113
  */
113
114
  convertJsonDirectorySync(srcPath, options) {
@@ -119,10 +120,9 @@ class JsonFsHelper {
119
120
  const files = fs.readdirSync(fullPath, { withFileTypes: true });
120
121
  const results = files
121
122
  .map((fi) => {
122
- var _a;
123
- if (fi.isFile() && path.extname(fi.name) === '.json') {
123
+ if (fi.isFile() && this._pathMatchesOptions(options, fi.name)) {
124
124
  const filePath = path.resolve(fullPath, fi.name);
125
- return this.convertJsonFileSync(filePath, (_a = options.converter) !== null && _a !== void 0 ? _a : options.validator)
125
+ return this.convertJsonFileSync(filePath, options.converter)
126
126
  .onSuccess((payload) => {
127
127
  return (0, ts_utils_1.succeed)({
128
128
  filename: fi.name,
@@ -144,7 +144,7 @@ class JsonFsHelper {
144
144
  * `Map<string, T>` indexed by file base name (i.e. minus the extension)
145
145
  * with an optional name transformation applied if present.
146
146
  * @param srcPath - The path of the folder to be read.
147
- * @param options - {@link JsonFile.JsonFsDirectoryToMapOptions | Options} to control conversion,
147
+ * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
148
148
  * filtering and naming.
149
149
  */
150
150
  convertJsonDirectoryToMapSync(srcPath, options) {
@@ -177,6 +177,11 @@ class JsonFsHelper {
177
177
  return true;
178
178
  });
179
179
  }
180
+ _pathMatchesOptions(options, path) {
181
+ var _a;
182
+ const match = (_a = options.files) !== null && _a !== void 0 ? _a : this.config.defaultFiles;
183
+ return match.some((m) => m.exec(path));
184
+ }
180
185
  }
181
186
  exports.JsonFsHelper = JsonFsHelper;
182
187
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"jsonFsHelper.js","sourceRoot":"","sources":["../../../src/packlets/json-file/jsonFsHelper.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,4CAAuG;AACvG,uCAAyB;AACzB,2CAA6B;AAG7B,yCAAwD;AAkFxD;;;;;;GAMG;AACH,MAAM,sBAAsB,GAAG,CAAC,CAAS,EAAkB,EAAE,CAAC,IAAA,kBAAO,EAAC,CAAC,CAAC,CAAC;AAiBzE;;;GAGG;AACU,QAAA,yBAAyB,GAAwB;IAC5D,IAAI,EAAE,0BAAe;IACrB,mBAAmB,EAAE,KAAK;CAC3B,CAAC;AAEF;;;;GAIG;AACH,MAAa,YAAY;IAMvB;;;;OAIG;IACH,YAAmB,IAA8B;QAC/C,IAAI,CAAC,MAAM,mCACN,iCAAyB,GACzB,CAAC,IAAI,aAAJ,IAAI,cAAJ,IAAI,GAAI,EAAE,CAAC,CAChB,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACI,gBAAgB,CAAC,OAAe;QACrC,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE;YACxB,MAAM,QAAQ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AAI,CAAC,CAAC,CAAC;gBACjD,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CACH,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,OAAO,IAAA,kBAAO,EAAC,IAAI,GAAG,CAAY,KAAK,CAAC,CAAC,CAAC;YAC5C,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;OAIG;IACI,iBAAiB,CAAC,OAAe,EAAE,KAAgB;QACxD,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE;YACxB,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YACvC,MAAM,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;YACpE,IAAI,WAAW,KAAK,SAAS,IAAI,IAAI,CAAC,MAAM,CAAC,mBAAmB,KAAK,IAAI,EAAE,CAAC;gBAC1E,MAAM,IAAI,KAAK,CAAC,uBAAuB,KAAK,EAAE,CAAC,CAAC;YAClD,CAAC;YACD,EAAE,CAAC,aAAa,CAAC,QAAQ,EAAE,WAAY,CAAC,CAAC;YACzC,OAAO,IAAI,CAAC;QACd,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAjID,oCAiIC;AAED;;GAEG;AACU,QAAA,mBAAmB,GAAiB,IAAI,YAAY,EAAE,CAAC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Converter, Result, Validator, captureResult, fail, mapResults, succeed } from '@fgv/ts-utils';\nimport * as fs from 'fs';\nimport * as path from 'path';\n\nimport { JsonValue } from '../json';\nimport { DefaultJsonLike, IJsonLike } from './jsonLike';\n\n/**\n * Options for directory conversion.\n * TODO: add filtering, allowed and excluded.\n * @public\n */\nexport interface IDirectoryConvertOptions<T, TC = unknown> {\n /**\n * The converter used to convert incoming JSON objects.\n */\n converter: Converter<T, TC>;\n validator?: undefined;\n}\n\n/**\n * Options for directory validation.\n * @public\n */\nexport interface IDirectoryValidateOptions<T, TC = unknown> {\n converter?: undefined;\n /**\n * The validator used to validate incoming JSON objects\n */\n validator: Validator<T, TC>;\n}\n\n/**\n * Options for directory conversion or validation.\n * @public\n */\nexport type JsonFsDirectoryOptions<T, TC = unknown> =\n | IDirectoryConvertOptions<T, TC>\n | IDirectoryValidateOptions<T, TC>;\n\n/**\n * Return value for one item in a directory conversion.\n * @public\n */\nexport interface IReadDirectoryItem<T> {\n /**\n * Relative name of the file that was processed\n */\n filename: string;\n\n /**\n * The payload of the file.\n */\n item: T;\n}\n\n/**\n * Function to transform the name of a some entity, given an original name\n * and the contents of the entity.\n * @public\n */\nexport type ItemNameTransformFunction<T> = (name: string, item: T) => Result<string>;\n\n/**\n * Options controlling conversion of a directory to a `Map`.\n * @public\n */\nexport interface IDirectoryToMapConvertOptions<T, TC = unknown> extends IDirectoryConvertOptions<T, TC> {\n transformName?: ItemNameTransformFunction<T>;\n}\n\n/**\n * Options controlling validation of a directory to a `Map`.\n * @public\n */\nexport interface IDirectoryToMapValidateOptions<T, TC = unknown> extends IDirectoryValidateOptions<T, TC> {\n transformName?: ItemNameTransformFunction<T>;\n}\n\n/**\n * Options controlling processing of a directory to a `Map`.\n * @public\n */\nexport type JsonFsDirectoryToMapOptions<T, TC = unknown> =\n | IDirectoryToMapConvertOptions<T, TC>\n | IDirectoryToMapValidateOptions<T, TC>;\n\n/**\n * Function to transform the name of some entity, given only a previous name. This\n * default implementation always returns `Success` with the value that was passed in.\n * @param n - The name to be transformed.\n * @returns - `Success` with the string that was passed in.\n * @public\n */\nconst defaultNameTransformer = (n: string): Result<string> => succeed(n);\n\n/**\n * Configuration for {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @public\n */\nexport interface IJsonFsHelperConfig {\n json: IJsonLike;\n allowUndefinedWrite: boolean;\n}\n\n/**\n * Initialization options for {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @public\n */\nexport type JsonFsHelperInitOptions = Partial<IJsonFsHelperConfig>;\n\n/**\n * Default configuration for {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @public\n */\nexport const DefaultJsonFsHelperConfig: IJsonFsHelperConfig = {\n json: DefaultJsonLike,\n allowUndefinedWrite: false\n};\n\n/**\n * Helper class to simplify common filesystem operations involving JSON (or JSON-like)\n * files.\n * @public\n */\nexport class JsonFsHelper {\n /**\n * Configuration for this {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n */\n public readonly config: IJsonFsHelperConfig;\n\n /**\n * Construct a new {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @param json - Optional {@link JsonFile.IJsonLike | IJsonLike} used to process strings\n * and JSON values.\n */\n public constructor(init?: JsonFsHelperInitOptions) {\n this.config = {\n ...DefaultJsonFsHelperConfig,\n ...(init ?? {})\n };\n }\n\n /**\n * Read type-safe JSON from a file.\n * @param srcPath - Path of the file to read\n * @returns `Success` with a {@link JsonValue | JsonValue} or `Failure`\n * with a message if an error occurs.\n */\n public readJsonFileSync(srcPath: string): Result<JsonValue> {\n return captureResult(() => {\n const fullPath = path.resolve(srcPath);\n const body = fs.readFileSync(fullPath, 'utf8').toString();\n return this.config.json.parse(body) as JsonValue;\n });\n }\n\n /**\n * Read a JSON file and apply a supplied converter or validator.\n * @param srcPath - Path of the file to read.\n * @param cv - Converter or validator used to process the file.\n * @returns `Success` with a result of type `<T>`, or `Failure`\n * with a message if an error occurs.\n */\n public convertJsonFileSync<T, TC = unknown>(\n srcPath: string,\n cv: Converter<T, TC> | Validator<T, TC>\n ): Result<T> {\n return this.readJsonFileSync(srcPath).onSuccess((json) => {\n return cv.convert(json);\n });\n }\n\n /**\n * Reads all JSON files from a directory and apply a supplied converter or validator.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.JsonFsDirectoryOptions | Options} to control\n * conversion and filtering\n */\n public convertJsonDirectorySync<T>(\n srcPath: string,\n options: JsonFsDirectoryOptions<T>\n ): Result<IReadDirectoryItem<T>[]> {\n return captureResult<IReadDirectoryItem<T>[]>(() => {\n const fullPath = path.resolve(srcPath);\n if (!fs.statSync(fullPath).isDirectory()) {\n throw new Error(`${fullPath}: Not a directory`);\n }\n const files = fs.readdirSync(fullPath, { withFileTypes: true });\n const results = files\n .map((fi) => {\n if (fi.isFile() && path.extname(fi.name) === '.json') {\n const filePath = path.resolve(fullPath, fi.name);\n return this.convertJsonFileSync(filePath, options.converter ?? options.validator)\n .onSuccess((payload) => {\n return succeed({\n filename: fi.name,\n item: payload\n });\n })\n .onFailure((message) => {\n return fail(`${fi.name}: ${message}`);\n });\n }\n return undefined;\n })\n .filter((r): r is Result<IReadDirectoryItem<T>> => r !== undefined);\n return mapResults(results).orThrow();\n });\n }\n\n /**\n * Reads and converts or validates all JSON files from a directory, returning a\n * `Map<string, T>` indexed by file base name (i.e. minus the extension)\n * with an optional name transformation applied if present.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.JsonFsDirectoryToMapOptions | Options} to control conversion,\n * filtering and naming.\n */\n public convertJsonDirectoryToMapSync<T, TC = unknown>(\n srcPath: string,\n options: JsonFsDirectoryToMapOptions<T, TC>\n ): Result<Map<string, T>> {\n return this.convertJsonDirectorySync(srcPath, options).onSuccess((items) => {\n const transformName = options.transformName ?? defaultNameTransformer;\n return mapResults(\n items.map((item) => {\n const basename = path.basename(item.filename, '.json');\n return transformName(basename, item.item).onSuccess((name) => {\n return succeed<[string, T]>([name, item.item]);\n });\n })\n ).onSuccess((items) => {\n return succeed(new Map<string, T>(items));\n });\n });\n }\n\n /**\n * Write type-safe JSON to a file.\n * @param srcPath - Path of the file to write.\n * @param value - The {@link JsonValue | JsonValue} to be written.\n */\n public writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean> {\n return captureResult(() => {\n const fullPath = path.resolve(srcPath);\n const stringified = this.config.json.stringify(value, undefined, 2);\n if (stringified === undefined && this.config.allowUndefinedWrite !== true) {\n throw new Error(`Could not stringify ${value}`);\n }\n fs.writeFileSync(fullPath, stringified!);\n return true;\n });\n }\n}\n\n/**\n * @public\n */\nexport const DefaultJsonFsHelper: JsonFsHelper = new JsonFsHelper();\n"]}
1
+ {"version":3,"file":"jsonFsHelper.js","sourceRoot":"","sources":["../../../src/packlets/json-file/jsonFsHelper.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,4CAAuG;AACvG,uCAAyB;AACzB,2CAA6B;AAG7B,yCAAwD;AAkDxD;;;;;;GAMG;AACH,MAAM,sBAAsB,GAAG,CAAC,CAAS,EAAkB,EAAE,CAAC,IAAA,kBAAO,EAAC,CAAC,CAAC,CAAC;AAkBzE;;;GAGG;AACU,QAAA,yBAAyB,GAAwB;IAC5D,IAAI,EAAE,0BAAe;IACrB,mBAAmB,EAAE,KAAK;IAC1B,YAAY,EAAE,CAAC,SAAS,CAAC;CAC1B,CAAC;AAEF;;;;GAI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,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACzC,CAAC;CACF;AAtID,oCAsIC;AAED;;GAEG;AACU,QAAA,mBAAmB,GAAiB,IAAI,YAAY,EAAE,CAAC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Converter, Result, Validator, captureResult, fail, mapResults, succeed } from '@fgv/ts-utils';\nimport * as fs from 'fs';\nimport * as path from 'path';\n\nimport { JsonValue } from '../json';\nimport { DefaultJsonLike, IJsonLike } from './jsonLike';\n\n/**\n * Options for directory conversion.\n * TODO: add filtering, allowed and excluded.\n * @public\n */\nexport interface IJsonFsDirectoryOptions<T, TC = unknown> {\n /**\n * The converter used to convert incoming JSON objects.\n */\n converter: Converter<T, TC> | Validator<T, TC>;\n\n /**\n * Filter applied to items in the directory\n */\n files?: RegExp[];\n}\n\n/**\n * Return value for one item in a directory conversion.\n * @public\n */\nexport interface IReadDirectoryItem<T> {\n /**\n * Relative name of the file that was processed\n */\n filename: string;\n\n /**\n * The payload of the file.\n */\n item: T;\n}\n\n/**\n * Function to transform the name of a some entity, given an original name\n * and the contents of the entity.\n * @public\n */\nexport type ItemNameTransformFunction<T> = (name: string, item: T) => Result<string>;\n\n/**\n * Options controlling conversion of a directory to a `Map`.\n * @public\n */\nexport interface IJsonFsDirectoryToMapOptions<T, TC = unknown> extends IJsonFsDirectoryOptions<T, TC> {\n transformName?: ItemNameTransformFunction<T>;\n}\n\n/**\n * Function to transform the name of some entity, given only a previous name. This\n * default implementation always returns `Success` with the value that was passed in.\n * @param n - The name to be transformed.\n * @returns - `Success` with the string that was passed in.\n * @public\n */\nconst defaultNameTransformer = (n: string): Result<string> => succeed(n);\n\n/**\n * Configuration for {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @public\n */\nexport interface IJsonFsHelperConfig {\n json: IJsonLike;\n allowUndefinedWrite: boolean;\n defaultFiles: RegExp[];\n}\n\n/**\n * Initialization options for {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @public\n */\nexport type JsonFsHelperInitOptions = Partial<IJsonFsHelperConfig>;\n\n/**\n * Default configuration for {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @public\n */\nexport const DefaultJsonFsHelperConfig: IJsonFsHelperConfig = {\n json: DefaultJsonLike,\n allowUndefinedWrite: false,\n defaultFiles: [/.*.json/]\n};\n\n/**\n * Helper class to simplify common filesystem operations involving JSON (or JSON-like)\n * files.\n * @public\n */\nexport class JsonFsHelper {\n /**\n * Configuration for this {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n */\n public readonly config: IJsonFsHelperConfig;\n\n /**\n * Construct a new {@link JsonFile.JsonFsHelper | JsonFsHelper}.\n * @param json - Optional {@link JsonFile.IJsonLike | IJsonLike} used to process strings\n * and JSON values.\n */\n public constructor(init?: JsonFsHelperInitOptions) {\n this.config = {\n ...DefaultJsonFsHelperConfig,\n ...(init ?? {})\n };\n }\n\n /**\n * Read type-safe JSON from a file.\n * @param srcPath - Path of the file to read\n * @returns `Success` with a {@link JsonValue | JsonValue} or `Failure`\n * with a message if an error occurs.\n */\n public readJsonFileSync(srcPath: string): Result<JsonValue> {\n return captureResult(() => {\n const fullPath = path.resolve(srcPath);\n const body = fs.readFileSync(fullPath, 'utf8').toString();\n return this.config.json.parse(body) as JsonValue;\n });\n }\n\n /**\n * Read a JSON file and apply a supplied converter or validator.\n * @param srcPath - Path of the file to read.\n * @param cv - Converter or validator used to process the file.\n * @returns `Success` with a result of type `<T>`, or `Failure`\n * with a message if an error occurs.\n */\n public convertJsonFileSync<T, TC = unknown>(\n srcPath: string,\n cv: Converter<T, TC> | Validator<T, TC>\n ): Result<T> {\n return this.readJsonFileSync(srcPath).onSuccess((json) => {\n return cv.convert(json);\n });\n }\n\n /**\n * Reads all JSON files from a directory and apply a supplied converter or validator.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control\n * conversion and filtering\n */\n public convertJsonDirectorySync<T>(\n srcPath: string,\n options: IJsonFsDirectoryOptions<T>\n ): Result<IReadDirectoryItem<T>[]> {\n return captureResult<IReadDirectoryItem<T>[]>(() => {\n const fullPath = path.resolve(srcPath);\n if (!fs.statSync(fullPath).isDirectory()) {\n throw new Error(`${fullPath}: Not a directory`);\n }\n const files = fs.readdirSync(fullPath, { withFileTypes: true });\n const results = files\n .map((fi) => {\n if (fi.isFile() && this._pathMatchesOptions(options, fi.name)) {\n const filePath = path.resolve(fullPath, fi.name);\n return this.convertJsonFileSync(filePath, options.converter)\n .onSuccess((payload) => {\n return succeed({\n filename: fi.name,\n item: payload\n });\n })\n .onFailure((message) => {\n return fail(`${fi.name}: ${message}`);\n });\n }\n return undefined;\n })\n .filter((r): r is Result<IReadDirectoryItem<T>> => r !== undefined);\n return mapResults(results).orThrow();\n });\n }\n\n /**\n * Reads and converts or validates all JSON files from a directory, returning a\n * `Map<string, T>` indexed by file base name (i.e. minus the extension)\n * with an optional name transformation applied if present.\n * @param srcPath - The path of the folder to be read.\n * @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,\n * filtering and naming.\n */\n public convertJsonDirectoryToMapSync<T, TC = unknown>(\n srcPath: string,\n options: IJsonFsDirectoryToMapOptions<T, TC>\n ): Result<Map<string, T>> {\n return this.convertJsonDirectorySync(srcPath, options).onSuccess((items) => {\n const transformName = options.transformName ?? defaultNameTransformer;\n return mapResults(\n items.map((item) => {\n const basename = path.basename(item.filename, '.json');\n return transformName(basename, item.item).onSuccess((name) => {\n return succeed<[string, T]>([name, item.item]);\n });\n })\n ).onSuccess((items) => {\n return succeed(new Map<string, T>(items));\n });\n });\n }\n\n /**\n * Write type-safe JSON to a file.\n * @param srcPath - Path of the file to write.\n * @param value - The {@link JsonValue | JsonValue} to be written.\n */\n public writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean> {\n return captureResult(() => {\n const fullPath = path.resolve(srcPath);\n const stringified = this.config.json.stringify(value, undefined, 2);\n if (stringified === undefined && this.config.allowUndefinedWrite !== true) {\n throw new Error(`Could not stringify ${value}`);\n }\n fs.writeFileSync(fullPath, stringified!);\n return true;\n });\n }\n\n protected _pathMatchesOptions<T, TC>(options: IJsonFsDirectoryOptions<T, TC>, path: string): boolean {\n const match = options.files ?? this.config.defaultFiles;\n return match.some((m) => m.exec(path));\n }\n}\n\n/**\n * @public\n */\nexport const DefaultJsonFsHelper: JsonFsHelper = new JsonFsHelper();\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-json-base",
3
- "version": "3.0.1-alpha.2",
3
+ "version": "3.0.1-alpha.4",
4
4
  "description": "Typescript types and basic functions for working with json",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-json-base.d.ts",
@@ -16,8 +16,8 @@
16
16
  "homepage": "https://github.com/ErikFortune/fgv/tree/main/libraries/ts-json-base#readme",
17
17
  "sideEffects": false,
18
18
  "devDependencies": {
19
- "@fgv/ts-utils": "3.0.1-alpha.2",
20
- "@fgv/ts-utils-jest": "3.0.1-alpha.2",
19
+ "@fgv/ts-utils": "3.0.1-alpha.4",
20
+ "@fgv/ts-utils-jest": "3.0.1-alpha.4",
21
21
  "@types/jest": "^29.5.12",
22
22
  "@types/mustache": "^4.2.5",
23
23
  "@types/node": "^20.11.16",
@@ -43,7 +43,7 @@
43
43
  "@microsoft/api-documenter": "^7.23.20"
44
44
  },
45
45
  "peerDependencies": {
46
- "@fgv/ts-utils": "3.0.1-alpha.2",
46
+ "@fgv/ts-utils": "3.0.1-alpha.4",
47
47
  "mustache": "^4.2.0"
48
48
  },
49
49
  "scripts": {