@fgv/ts-json-base 4.1.0 → 4.2.1
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/CHANGELOG.json +18 -0
- package/CHANGELOG.md +13 -1
- package/dist/ts-json-base.d.ts +5 -5
- package/lib/packlets/json-file/file.d.ts +2 -2
- package/lib/packlets/json-file/file.d.ts.map +1 -1
- package/lib/packlets/json-file/file.js.map +1 -1
- package/lib/packlets/json-file/jsonFsHelper.d.ts +3 -3
- package/lib/packlets/json-file/jsonFsHelper.d.ts.map +1 -1
- package/lib/packlets/json-file/jsonFsHelper.js +6 -6
- package/lib/packlets/json-file/jsonFsHelper.js.map +1 -1
- package/package.json +5 -5
package/CHANGELOG.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgv/ts-json-base",
|
|
3
3
|
"entries": [
|
|
4
|
+
{
|
|
5
|
+
"version": "4.2.1",
|
|
6
|
+
"tag": "@fgv/ts-json-base_v4.2.1",
|
|
7
|
+
"date": "Tue, 21 Jan 2025 04:19:21 GMT",
|
|
8
|
+
"comments": {}
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"version": "4.2.0",
|
|
12
|
+
"tag": "@fgv/ts-json-base_v4.2.0",
|
|
13
|
+
"date": "Mon, 20 Jan 2025 09:46:53 GMT",
|
|
14
|
+
"comments": {
|
|
15
|
+
"none": [
|
|
16
|
+
{
|
|
17
|
+
"comment": "plumb conversion context through"
|
|
18
|
+
}
|
|
19
|
+
]
|
|
20
|
+
}
|
|
21
|
+
},
|
|
4
22
|
{
|
|
5
23
|
"version": "4.1.0",
|
|
6
24
|
"tag": "@fgv/ts-json-base_v4.1.0",
|
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
# Change Log - @fgv/ts-json-base
|
|
2
2
|
|
|
3
|
-
This log was last generated on
|
|
3
|
+
This log was last generated on Tue, 21 Jan 2025 04:19:21 GMT and should not be manually modified.
|
|
4
|
+
|
|
5
|
+
## 4.2.1
|
|
6
|
+
Tue, 21 Jan 2025 04:19:21 GMT
|
|
7
|
+
|
|
8
|
+
_Version update only_
|
|
9
|
+
|
|
10
|
+
## 4.2.0
|
|
11
|
+
Mon, 20 Jan 2025 09:46:53 GMT
|
|
12
|
+
|
|
13
|
+
### Updates
|
|
14
|
+
|
|
15
|
+
- plumb conversion context through
|
|
4
16
|
|
|
5
17
|
## 4.1.0
|
|
6
18
|
Thu, 09 Jan 2025 05:33:39 GMT
|
package/dist/ts-json-base.d.ts
CHANGED
|
@@ -30,7 +30,7 @@ export { Converters }
|
|
|
30
30
|
* conversion and filtering
|
|
31
31
|
* @public
|
|
32
32
|
*/
|
|
33
|
-
declare function convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>;
|
|
33
|
+
declare function convertJsonDirectorySync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryOptions<T, TC>): Result<IReadDirectoryItem<T>[]>;
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* Reads and converts all JSON files from a directory, returning a
|
|
@@ -50,7 +50,7 @@ declare function convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string,
|
|
|
50
50
|
* @returns `Success` with a result of type `<T>`, or `Failure`* with a message if an error occurs.
|
|
51
51
|
* @public
|
|
52
52
|
*/
|
|
53
|
-
declare function convertJsonFileSync<T>(srcPath: string, converter: Converter<T>): Result<T>;
|
|
53
|
+
declare function convertJsonFileSync<T, TC = unknown>(srcPath: string, converter: Converter<T, TC>): Result<T>;
|
|
54
54
|
|
|
55
55
|
/**
|
|
56
56
|
* @public
|
|
@@ -258,14 +258,14 @@ declare class JsonFsHelper {
|
|
|
258
258
|
* @returns `Success` with a result of type `<T>`, or `Failure`
|
|
259
259
|
* with a message if an error occurs.
|
|
260
260
|
*/
|
|
261
|
-
convertJsonFileSync<T, TC = unknown>(srcPath: string, cv: Converter<T, TC> | Validator<T, TC
|
|
261
|
+
convertJsonFileSync<T, TC = unknown>(srcPath: string, cv: Converter<T, TC> | Validator<T, TC>, context?: TC): Result<T>;
|
|
262
262
|
/**
|
|
263
263
|
* Reads all JSON files from a directory and apply a supplied converter or validator.
|
|
264
264
|
* @param srcPath - The path of the folder to be read.
|
|
265
265
|
* @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
|
|
266
266
|
* conversion and filtering
|
|
267
267
|
*/
|
|
268
|
-
convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T
|
|
268
|
+
convertJsonDirectorySync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryOptions<T>, context?: TC): Result<IReadDirectoryItem<T>[]>;
|
|
269
269
|
/**
|
|
270
270
|
* Reads and converts or validates all JSON files from a directory, returning a
|
|
271
271
|
* `Map<string, T>` indexed by file base name (i.e. minus the extension)
|
|
@@ -274,7 +274,7 @@ declare class JsonFsHelper {
|
|
|
274
274
|
* @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
|
|
275
275
|
* filtering and naming.
|
|
276
276
|
*/
|
|
277
|
-
convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC
|
|
277
|
+
convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC>, context?: unknown): Result<Map<string, T>>;
|
|
278
278
|
/**
|
|
279
279
|
* Write type-safe JSON to a file.
|
|
280
280
|
* @param srcPath - Path of the file to write.
|
|
@@ -13,7 +13,7 @@ export declare function readJsonFileSync(srcPath: string): Result<JsonValue>;
|
|
|
13
13
|
* @returns `Success` with a result of type `<T>`, or `Failure`* with a message if an error occurs.
|
|
14
14
|
* @public
|
|
15
15
|
*/
|
|
16
|
-
export declare function convertJsonFileSync<T>(srcPath: string, converter: Converter<T>): Result<T>;
|
|
16
|
+
export declare function convertJsonFileSync<T, TC = unknown>(srcPath: string, converter: Converter<T, TC>): Result<T>;
|
|
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.
|
|
@@ -21,7 +21,7 @@ export declare function convertJsonFileSync<T>(srcPath: string, converter: Conve
|
|
|
21
21
|
* conversion and filtering
|
|
22
22
|
* @public
|
|
23
23
|
*/
|
|
24
|
-
export declare function convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>;
|
|
24
|
+
export declare function convertJsonDirectorySync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryOptions<T, TC>): 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)
|
|
@@ -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,uBAAuB,EACvB,4BAA4B,EAC5B,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,
|
|
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,uBAAuB,EACvB,4BAA4B,EAC5B,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,EAAE,GAAG,OAAO,EACjD,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,GAC1B,MAAM,CAAC,CAAC,CAAC,CAEX;AAED;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO,EACtD,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,uBAAuB,CAAC,CAAC,EAAE,EAAE,CAAC,GACtC,MAAM,CAAC,kBAAkB,CAAC,CAAC,CAAC,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"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file.js","sourceRoot":"","sources":["../../../src/packlets/json-file/file.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;AAiBH,4CAEC;AASD,
|
|
1
|
+
{"version":3,"file":"file.js","sourceRoot":"","sources":["../../../src/packlets/json-file/file.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;AAiBH,4CAEC;AASD,kDAKC;AASD,4DAKC;AAWD,sEAKC;AAWD,8CAEC;AAxED,iDAMwB;AACxB,yCAA6C;AAE7C;;;GAGG;AACH,SAAgB,gBAAgB,CAAC,OAAe;IAC9C,OAAO,kCAAmB,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;AACvD,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,mBAAmB,CACjC,OAAe,EACf,SAA2B;IAE3B,OAAO,kCAAmB,CAAC,mBAAmB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;AACrE,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,wBAAwB,CACtC,OAAe,EACf,OAAuC;IAEvC,OAAO,kCAAmB,CAAC,wBAAwB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AACxE,CAAC;AAED;;;;;;;;GAQG;AACH,SAAgB,6BAA6B,CAC3C,OAAe,EACf,OAA4C;IAE5C,OAAO,kCAAmB,CAAC,6BAA6B,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAC7E,CAAC;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","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, TC = unknown>(\n srcPath: string,\n converter: Converter<T, TC>\n): 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, TC = unknown>(\n srcPath: string,\n options: IJsonFsDirectoryOptions<T, TC>\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"]}
|
|
@@ -92,14 +92,14 @@ export declare class JsonFsHelper {
|
|
|
92
92
|
* @returns `Success` with a result of type `<T>`, or `Failure`
|
|
93
93
|
* with a message if an error occurs.
|
|
94
94
|
*/
|
|
95
|
-
convertJsonFileSync<T, TC = unknown>(srcPath: string, cv: Converter<T, TC> | Validator<T, TC
|
|
95
|
+
convertJsonFileSync<T, TC = unknown>(srcPath: string, cv: Converter<T, TC> | Validator<T, TC>, context?: TC): Result<T>;
|
|
96
96
|
/**
|
|
97
97
|
* Reads all JSON files from a directory and apply a supplied converter or validator.
|
|
98
98
|
* @param srcPath - The path of the folder to be read.
|
|
99
99
|
* @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
|
|
100
100
|
* conversion and filtering
|
|
101
101
|
*/
|
|
102
|
-
convertJsonDirectorySync<T>(srcPath: string, options: IJsonFsDirectoryOptions<T
|
|
102
|
+
convertJsonDirectorySync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryOptions<T>, context?: TC): Result<IReadDirectoryItem<T>[]>;
|
|
103
103
|
/**
|
|
104
104
|
* Reads and converts or validates all JSON files from a directory, returning a
|
|
105
105
|
* `Map<string, T>` indexed by file base name (i.e. minus the extension)
|
|
@@ -108,7 +108,7 @@ export declare class JsonFsHelper {
|
|
|
108
108
|
* @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
|
|
109
109
|
* filtering and naming.
|
|
110
110
|
*/
|
|
111
|
-
convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC
|
|
111
|
+
convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: IJsonFsDirectoryToMapOptions<T, TC>, context?: unknown): Result<Map<string, T>>;
|
|
112
112
|
/**
|
|
113
113
|
* Write type-safe JSON to a file.
|
|
114
114
|
* @param srcPath - Path of the file to write.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsonFsHelper.d.ts","sourceRoot":"","sources":["../../../src/packlets/json-file/jsonFsHelper.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAA4C,MAAM,eAAe,CAAC;AAIvG,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAmB,SAAS,EAAE,MAAM,YAAY,CAAC;AAExD;;;;GAIG;AACH,MAAM,WAAW,uBAAuB,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO;IACtD;;OAEG;IACH,SAAS,EAAE,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAE/C;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;CAClB;AAED;;;GAGG;AACH,MAAM,WAAW,kBAAkB,CAAC,CAAC;IACnC;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,IAAI,EAAE,CAAC,CAAC;CACT;AAED;;;;GAIG;AACH,MAAM,MAAM,yBAAyB,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,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AMG;IACI,mBAAmB,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO,EACxC,OAAO,EAAE,MAAM,EACf,EAAE,EAAE,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,EACvC,OAAO,CAAC,EAAE,EAAE,GACX,MAAM,CAAC,CAAC,CAAC;IAMZ;;;;;OAKG;IACI,wBAAwB,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO,EAC7C,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,uBAAuB,CAAC,CAAC,CAAC,EACnC,OAAO,CAAC,EAAE,EAAE,GACX,MAAM,CAAC,kBAAkB,CAAC,CAAC,CAAC,EAAE,CAAC;IA6BlC;;;;;;;OAOG;IACI,6BAA6B,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO,EAClD,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,4BAA4B,CAAC,CAAC,EAAE,EAAE,CAAC,EAC5C,OAAO,CAAC,EAAE,OAAO,GAChB,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAgBzB;;;;OAIG;IACI,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC;IAa5E,SAAS,CAAC,mBAAmB,CAAC,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,uBAAuB,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO;CAKrG;AAED;;GAEG;AACH,eAAO,MAAM,mBAAmB,EAAE,YAAiC,CAAC"}
|
|
@@ -110,9 +110,9 @@ class JsonFsHelper {
|
|
|
110
110
|
* @returns `Success` with a result of type `<T>`, or `Failure`
|
|
111
111
|
* with a message if an error occurs.
|
|
112
112
|
*/
|
|
113
|
-
convertJsonFileSync(srcPath, cv) {
|
|
113
|
+
convertJsonFileSync(srcPath, cv, context) {
|
|
114
114
|
return this.readJsonFileSync(srcPath).onSuccess((json) => {
|
|
115
|
-
return cv.convert(json);
|
|
115
|
+
return cv.convert(json, context);
|
|
116
116
|
});
|
|
117
117
|
}
|
|
118
118
|
/**
|
|
@@ -121,7 +121,7 @@ class JsonFsHelper {
|
|
|
121
121
|
* @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
|
|
122
122
|
* conversion and filtering
|
|
123
123
|
*/
|
|
124
|
-
convertJsonDirectorySync(srcPath, options) {
|
|
124
|
+
convertJsonDirectorySync(srcPath, options, context) {
|
|
125
125
|
return (0, ts_utils_1.captureResult)(() => {
|
|
126
126
|
const fullPath = path.resolve(srcPath);
|
|
127
127
|
if (!fs.statSync(fullPath).isDirectory()) {
|
|
@@ -132,7 +132,7 @@ class JsonFsHelper {
|
|
|
132
132
|
.map((fi) => {
|
|
133
133
|
if (fi.isFile() && this._pathMatchesOptions(options, fi.name)) {
|
|
134
134
|
const filePath = path.resolve(fullPath, fi.name);
|
|
135
|
-
return this.convertJsonFileSync(filePath, options.converter)
|
|
135
|
+
return this.convertJsonFileSync(filePath, options.converter, context)
|
|
136
136
|
.onSuccess((payload) => {
|
|
137
137
|
return (0, ts_utils_1.succeed)({
|
|
138
138
|
filename: fi.name,
|
|
@@ -157,8 +157,8 @@ class JsonFsHelper {
|
|
|
157
157
|
* @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
|
|
158
158
|
* filtering and naming.
|
|
159
159
|
*/
|
|
160
|
-
convertJsonDirectoryToMapSync(srcPath, options) {
|
|
161
|
-
return this.convertJsonDirectorySync(srcPath, options).onSuccess((items) => {
|
|
160
|
+
convertJsonDirectoryToMapSync(srcPath, options, context) {
|
|
161
|
+
return this.convertJsonDirectorySync(srcPath, options, context).onSuccess((items) => {
|
|
162
162
|
var _a;
|
|
163
163
|
const transformName = (_a = options.transformName) !== null && _a !== void 0 ? _a : defaultNameTransformer;
|
|
164
164
|
return (0, ts_utils_1.mapResults)(items.map((item) => {
|
|
@@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,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;YACpE,sBAAsB;YACtB,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;IAES,mBAAmB,CAAQ,OAAuC,EAAE,IAAY;;QACxF,sBAAsB;QACtB,MAAM,KAAK,GAAG,MAAA,OAAO,CAAC,KAAK,mCAAI,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC;QACxD,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACzC,CAAC;CACF;AAxID,oCAwIC;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 /* c8 ignore next 3 */\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 /* c8 ignore next 1 */\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"]}
|
|
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;;;;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,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YACvC,MAAM,IAAI,GAAG,EAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,QAAQ,EAAE,CAAC;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,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,sBAAsB;YACtB,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;IAES,mBAAmB,CAAQ,OAAuC,EAAE,IAAY;;QACxF,sBAAsB;QACtB,MAAM,KAAK,GAAG,MAAA,OAAO,CAAC,KAAK,mCAAI,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC;QACxD,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACzC,CAAC;CACF;AA3ID,oCA2IC;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 context?: TC\n ): Result<T> {\n return this.readJsonFileSync(srcPath).onSuccess((json) => {\n return cv.convert(json, context);\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, TC = unknown>(\n srcPath: string,\n options: IJsonFsDirectoryOptions<T>,\n context?: TC\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, context)\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 context?: unknown\n ): Result<Map<string, T>> {\n return this.convertJsonDirectorySync(srcPath, options, context).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 /* c8 ignore next 3 */\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 /* c8 ignore next 1 */\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": "4.1
|
|
3
|
+
"version": "4.2.1",
|
|
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": "4.1
|
|
20
|
-
"@fgv/ts-utils-jest": "4.1
|
|
19
|
+
"@fgv/ts-utils": "4.2.1",
|
|
20
|
+
"@fgv/ts-utils-jest": "4.2.1",
|
|
21
21
|
"@types/jest": "^29.5.14",
|
|
22
22
|
"@types/node": "^20.14.9",
|
|
23
23
|
"@typescript-eslint/eslint-plugin": "^7.14.1",
|
|
@@ -38,10 +38,10 @@
|
|
|
38
38
|
"heft-jest": "~1.0.2",
|
|
39
39
|
"@types/heft-jest": "1.0.6",
|
|
40
40
|
"@microsoft/api-documenter": "^7.26.5",
|
|
41
|
-
"@fgv/ts-extras": "4.1
|
|
41
|
+
"@fgv/ts-extras": "4.2.1"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@fgv/ts-utils": "4.1
|
|
44
|
+
"@fgv/ts-utils": "4.2.1"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "heft test --clean",
|