@fgv/ts-json-base 3.0.1-alpha.0 → 3.0.1-alpha.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/ts-json-base.d.ts +3 -4
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/packlets/json-file/file.js +3 -3
- package/lib/packlets/json-file/file.js.map +1 -1
- package/lib/packlets/json-file/jsonFsHelper.d.ts +4 -4
- 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 +12 -12
package/dist/ts-json-base.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Converter } from '@fgv/ts-utils';
|
|
2
2
|
import { Result } from '@fgv/ts-utils';
|
|
3
|
-
import { Validation } from '@fgv/ts-utils';
|
|
4
3
|
import { Validator } from '@fgv/ts-utils';
|
|
5
4
|
|
|
6
5
|
/**
|
|
@@ -291,14 +290,14 @@ declare class JsonFsHelper {
|
|
|
291
290
|
* @returns `Success` with a result of type `<T>`, or `Failure`
|
|
292
291
|
* with a message if an error occurs.
|
|
293
292
|
*/
|
|
294
|
-
|
|
293
|
+
convertJsonFileSync<T, TC = unknown>(srcPath: string, cv: Converter<T, TC> | Validator<T, TC>): Result<T>;
|
|
295
294
|
/**
|
|
296
295
|
* Reads all JSON files from a directory and apply a supplied converter or validator.
|
|
297
296
|
* @param srcPath - The path of the folder to be read.
|
|
298
297
|
* @param options - {@link JsonFile.JsonFsDirectoryOptions | Options} to control
|
|
299
298
|
* conversion and filtering
|
|
300
299
|
*/
|
|
301
|
-
|
|
300
|
+
convertJsonDirectorySync<T>(srcPath: string, options: JsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>;
|
|
302
301
|
/**
|
|
303
302
|
* Reads and converts or validates all JSON files from a directory, returning a
|
|
304
303
|
* `Map<string, T>` indexed by file base name (i.e. minus the extension)
|
|
@@ -307,7 +306,7 @@ declare class JsonFsHelper {
|
|
|
307
306
|
* @param options - {@link JsonFile.JsonFsDirectoryToMapOptions | Options} to control conversion,
|
|
308
307
|
* filtering and naming.
|
|
309
308
|
*/
|
|
310
|
-
|
|
309
|
+
convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: JsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>;
|
|
311
310
|
/**
|
|
312
311
|
* Write type-safe JSON to a file.
|
|
313
312
|
* @param srcPath - Path of the file to write.
|
package/dist/tsdoc-metadata.json
CHANGED
|
@@ -40,7 +40,7 @@ exports.readJsonFileSync = readJsonFileSync;
|
|
|
40
40
|
* @public
|
|
41
41
|
*/
|
|
42
42
|
function convertJsonFileSync(srcPath, converter) {
|
|
43
|
-
return jsonFsHelper_1.DefaultJsonFsHelper.
|
|
43
|
+
return jsonFsHelper_1.DefaultJsonFsHelper.convertJsonFileSync(srcPath, converter);
|
|
44
44
|
}
|
|
45
45
|
exports.convertJsonFileSync = convertJsonFileSync;
|
|
46
46
|
/**
|
|
@@ -51,7 +51,7 @@ exports.convertJsonFileSync = convertJsonFileSync;
|
|
|
51
51
|
* @public
|
|
52
52
|
*/
|
|
53
53
|
function convertJsonDirectorySync(srcPath, options) {
|
|
54
|
-
return jsonFsHelper_1.DefaultJsonFsHelper.
|
|
54
|
+
return jsonFsHelper_1.DefaultJsonFsHelper.convertJsonDirectorySync(srcPath, options);
|
|
55
55
|
}
|
|
56
56
|
exports.convertJsonDirectorySync = convertJsonDirectorySync;
|
|
57
57
|
/**
|
|
@@ -64,7 +64,7 @@ exports.convertJsonDirectorySync = convertJsonDirectorySync;
|
|
|
64
64
|
* @public
|
|
65
65
|
*/
|
|
66
66
|
function convertJsonDirectoryToMapSync(srcPath, options) {
|
|
67
|
-
return jsonFsHelper_1.DefaultJsonFsHelper.
|
|
67
|
+
return jsonFsHelper_1.DefaultJsonFsHelper.convertJsonDirectoryToMapSync(srcPath, options);
|
|
68
68
|
}
|
|
69
69
|
exports.convertJsonDirectoryToMapSync = convertJsonDirectoryToMapSync;
|
|
70
70
|
const CompatJsonFsHelper = new jsonFsHelper_1.JsonFsHelper({
|
|
@@ -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.
|
|
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,4 +1,4 @@
|
|
|
1
|
-
import { Converter, Result,
|
|
1
|
+
import { Converter, Result, Validator } from '@fgv/ts-utils';
|
|
2
2
|
import { JsonValue } from '../json';
|
|
3
3
|
import { IJsonLike } from './jsonLike';
|
|
4
4
|
/**
|
|
@@ -116,14 +116,14 @@ export declare class JsonFsHelper {
|
|
|
116
116
|
* @returns `Success` with a result of type `<T>`, or `Failure`
|
|
117
117
|
* with a message if an error occurs.
|
|
118
118
|
*/
|
|
119
|
-
|
|
119
|
+
convertJsonFileSync<T, TC = unknown>(srcPath: string, cv: Converter<T, TC> | Validator<T, TC>): Result<T>;
|
|
120
120
|
/**
|
|
121
121
|
* Reads all JSON files from a directory and apply a supplied converter or validator.
|
|
122
122
|
* @param srcPath - The path of the folder to be read.
|
|
123
123
|
* @param options - {@link JsonFile.JsonFsDirectoryOptions | Options} to control
|
|
124
124
|
* conversion and filtering
|
|
125
125
|
*/
|
|
126
|
-
|
|
126
|
+
convertJsonDirectorySync<T>(srcPath: string, options: JsonFsDirectoryOptions<T>): Result<IReadDirectoryItem<T>[]>;
|
|
127
127
|
/**
|
|
128
128
|
* Reads and converts or validates all JSON files from a directory, returning a
|
|
129
129
|
* `Map<string, T>` indexed by file base name (i.e. minus the extension)
|
|
@@ -132,7 +132,7 @@ export declare class JsonFsHelper {
|
|
|
132
132
|
* @param options - {@link JsonFile.JsonFsDirectoryToMapOptions | Options} to control conversion,
|
|
133
133
|
* filtering and naming.
|
|
134
134
|
*/
|
|
135
|
-
|
|
135
|
+
convertJsonDirectoryToMapSync<T, TC = unknown>(srcPath: string, options: JsonFsDirectoryToMapOptions<T, TC>): Result<Map<string, T>>;
|
|
136
136
|
/**
|
|
137
137
|
* Write type-safe JSON to a file.
|
|
138
138
|
* @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,
|
|
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,wBAAwB,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO;IACvD;;OAEG;IACH,SAAS,EAAE,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC5B,SAAS,CAAC,EAAE,SAAS,CAAC;CACvB;AAED;;;GAGG;AACH,MAAM,WAAW,yBAAyB,CAAC,CAAC,EAAE,EAAE,GAAG,OAAO;IACxD,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB;;OAEG;IACH,SAAS,EAAE,SAAS,CAAC,CAAC,EAAE,EAA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;AACH,eAAO,MAAM,yBAAyB,EAAE,mBAGvC,CAAC;AAEF;;;;GAIG;AACH,qBAAa,YAAY;IACvB;;OAEG;IACH,SAAgB,MAAM,EAAE,mBAAmB,CAAC;IAE5C;;;;OAIG;gBACgB,IAAI,CAAC,EAAE,uBAAuB;IAOjD;;;;;OAKG;IACI,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC;IAQ3D;;;;;;OAMG;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,GACtC,MAAM,CAAC,CAAC,CAAC;IAMZ;;;;;OAKG;IACI,wBAAwB,CAAC,CAAC,EAC/B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,sBAAsB,CAAC,CAAC,CAAC,GACjC,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,2BAA2B,CAAC,CAAC,EAAE,EAAE,CAAC,GAC1C,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;CAW7E;AAED;;GAEG;AACH,eAAO,MAAM,mBAAmB,EAAE,YAAiC,CAAC"}
|
|
@@ -99,9 +99,9 @@ class JsonFsHelper {
|
|
|
99
99
|
* @returns `Success` with a result of type `<T>`, or `Failure`
|
|
100
100
|
* with a message if an error occurs.
|
|
101
101
|
*/
|
|
102
|
-
|
|
102
|
+
convertJsonFileSync(srcPath, cv) {
|
|
103
103
|
return this.readJsonFileSync(srcPath).onSuccess((json) => {
|
|
104
|
-
return cv.
|
|
104
|
+
return cv.convert(json);
|
|
105
105
|
});
|
|
106
106
|
}
|
|
107
107
|
/**
|
|
@@ -110,7 +110,7 @@ class JsonFsHelper {
|
|
|
110
110
|
* @param options - {@link JsonFile.JsonFsDirectoryOptions | Options} to control
|
|
111
111
|
* conversion and filtering
|
|
112
112
|
*/
|
|
113
|
-
|
|
113
|
+
convertJsonDirectorySync(srcPath, options) {
|
|
114
114
|
return (0, ts_utils_1.captureResult)(() => {
|
|
115
115
|
const fullPath = path.resolve(srcPath);
|
|
116
116
|
if (!fs.statSync(fullPath).isDirectory()) {
|
|
@@ -122,7 +122,7 @@ class JsonFsHelper {
|
|
|
122
122
|
var _a;
|
|
123
123
|
if (fi.isFile() && path.extname(fi.name) === '.json') {
|
|
124
124
|
const filePath = path.resolve(fullPath, fi.name);
|
|
125
|
-
return this.
|
|
125
|
+
return this.convertJsonFileSync(filePath, (_a = options.converter) !== null && _a !== void 0 ? _a : options.validator)
|
|
126
126
|
.onSuccess((payload) => {
|
|
127
127
|
return (0, ts_utils_1.succeed)({
|
|
128
128
|
filename: fi.name,
|
|
@@ -147,8 +147,8 @@ class JsonFsHelper {
|
|
|
147
147
|
* @param options - {@link JsonFile.JsonFsDirectoryToMapOptions | Options} to control conversion,
|
|
148
148
|
* filtering and naming.
|
|
149
149
|
*/
|
|
150
|
-
|
|
151
|
-
return this.
|
|
150
|
+
convertJsonDirectoryToMapSync(srcPath, options) {
|
|
151
|
+
return this.convertJsonDirectorySync(srcPath, options).onSuccess((items) => {
|
|
152
152
|
var _a;
|
|
153
153
|
const transformName = (_a = options.transformName) !== null && _a !== void 0 ? _a : defaultNameTransformer;
|
|
154
154
|
return (0, ts_utils_1.mapResults)(items.map((item) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsonFsHelper.js","sourceRoot":"","sources":["../../../src/packlets/json-file/jsonFsHelper.ts"],"names":[],"mappings":";A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mB,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;AA9HD,oCA8HC;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 {\n Converter,\n Result,\n Validation,\n Validator,\n captureResult,\n fail,\n mapResults,\n succeed\n} 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 processJsonFileSync<T>(srcPath: string, cv: Validation.Convalidator<T>): Result<T> {\n return this.readJsonFileSync(srcPath).onSuccess((json) => {\n return cv.convalidate(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 processJsonDirectorySync<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.processJsonFileSync(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 processJsonDirectoryToMapSync<T, TC = unknown>(\n srcPath: string,\n options: JsonFsDirectoryToMapOptions<T, TC>\n ): Result<Map<string, T>> {\n return this.processJsonDirectorySync(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;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,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YACvC,MAAM,IAAI,GAAG,EAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,QAAQ,EAAE,CAAC;YAC1D,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAc,CAAC;QACnD,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;;OAMG;IACI,mBAAmB,CACxB,OAAe,EACf,EAAuC;QAEvC,OAAO,IAAI,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AAE;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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgv/ts-json-base",
|
|
3
|
-
"version": "3.0.1-alpha.
|
|
3
|
+
"version": "3.0.1-alpha.2",
|
|
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,13 +16,13 @@
|
|
|
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.
|
|
20
|
-
"@fgv/ts-utils-jest": "3.0.1-alpha.
|
|
21
|
-
"@types/jest": "^29.5.
|
|
19
|
+
"@fgv/ts-utils": "3.0.1-alpha.2",
|
|
20
|
+
"@fgv/ts-utils-jest": "3.0.1-alpha.2",
|
|
21
|
+
"@types/jest": "^29.5.12",
|
|
22
22
|
"@types/mustache": "^4.2.5",
|
|
23
|
-
"@types/node": "^20.11.
|
|
24
|
-
"@typescript-eslint/eslint-plugin": "^6.
|
|
25
|
-
"@typescript-eslint/parser": "^6.
|
|
23
|
+
"@types/node": "^20.11.16",
|
|
24
|
+
"@typescript-eslint/eslint-plugin": "^6.20.0",
|
|
25
|
+
"@typescript-eslint/parser": "^6.20.0",
|
|
26
26
|
"eslint": "^8.56.0",
|
|
27
27
|
"eslint-config-standard": "^17.1.0",
|
|
28
28
|
"eslint-plugin-import": "^2.29.1",
|
|
@@ -32,18 +32,18 @@
|
|
|
32
32
|
"jest-extended": "^4.0.2",
|
|
33
33
|
"mustache": "^4.2.0",
|
|
34
34
|
"rimraf": "^5.0.5",
|
|
35
|
-
"ts-jest": "^29.1.
|
|
35
|
+
"ts-jest": "^29.1.2",
|
|
36
36
|
"ts-node": "^10.9.2",
|
|
37
37
|
"typescript": "^5.3.3",
|
|
38
38
|
"eslint-plugin-n": "^16.6.2",
|
|
39
|
-
"@rushstack/heft-node-rig": "~2.4.
|
|
40
|
-
"@rushstack/heft": "~0.64.
|
|
39
|
+
"@rushstack/heft-node-rig": "~2.4.5",
|
|
40
|
+
"@rushstack/heft": "~0.64.3",
|
|
41
41
|
"heft-jest": "~1.0.2",
|
|
42
42
|
"@types/heft-jest": "1.0.6",
|
|
43
|
-
"@microsoft/api-documenter": "^7.23.
|
|
43
|
+
"@microsoft/api-documenter": "^7.23.20"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"@fgv/ts-utils": "3.0.1-alpha.
|
|
46
|
+
"@fgv/ts-utils": "3.0.1-alpha.2",
|
|
47
47
|
"mustache": "^4.2.0"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|