@fgv/ts-json-base 5.0.0-30 → 5.0.0-31
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 +616 -7
- package/lib/index.d.ts +2 -1
- package/lib/index.js +3 -1
- package/lib/packlets/file-tree/directoryItem.d.ts +47 -0
- package/lib/packlets/file-tree/directoryItem.js +71 -0
- package/lib/packlets/file-tree/fileItem.d.ts +97 -0
- package/lib/packlets/file-tree/fileItem.js +130 -0
- package/lib/packlets/file-tree/fileTree.d.ts +49 -0
- package/lib/packlets/file-tree/fileTree.js +89 -0
- package/lib/packlets/file-tree/fileTreeAccessors.d.ts +158 -0
- package/lib/packlets/file-tree/fileTreeAccessors.js +24 -0
- package/lib/packlets/file-tree/fileTreeHelpers.d.ts +43 -0
- package/lib/packlets/file-tree/fileTreeHelpers.js +38 -0
- package/lib/packlets/file-tree/fsTree.d.ts +57 -0
- package/lib/packlets/file-tree/fsTree.js +129 -0
- package/lib/packlets/file-tree/in-memory/inMemoryTree.d.ts +83 -0
- package/lib/packlets/file-tree/in-memory/inMemoryTree.js +181 -0
- package/lib/packlets/file-tree/in-memory/index.d.ts +2 -0
- package/lib/packlets/file-tree/in-memory/index.js +39 -0
- package/lib/packlets/file-tree/in-memory/treeBuilder.d.ts +113 -0
- package/lib/packlets/file-tree/in-memory/treeBuilder.js +179 -0
- package/lib/packlets/file-tree/index.d.ts +8 -0
- package/lib/packlets/file-tree/index.js +50 -0
- package/lib/packlets/json-file/index.d.ts +2 -1
- package/lib/packlets/json-file/index.js +6 -1
- package/lib/packlets/json-file/jsonFsHelper.js +14 -44
- package/lib/packlets/json-file/jsonTreeHelper.d.ts +65 -0
- package/lib/packlets/json-file/jsonTreeHelper.js +120 -0
- package/package.json +9 -6
package/dist/ts-json-base.d.ts
CHANGED
|
@@ -12,6 +12,12 @@ import { Validator } from '@fgv/ts-utils';
|
|
|
12
12
|
*/
|
|
13
13
|
export declare function classifyJsonValue(from: unknown): Result<JsonValueType>;
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Type of function to infer the content type of a file.
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
declare type ContentTypeFactory<TCT extends string = string> = (filePath: string, provided?: string) => Result<TCT | undefined>;
|
|
20
|
+
|
|
15
21
|
declare namespace Converters {
|
|
16
22
|
export {
|
|
17
23
|
IJsonConverterContext,
|
|
@@ -68,6 +74,466 @@ declare const DefaultJsonFsHelperConfig: IJsonFsHelperConfig;
|
|
|
68
74
|
*/
|
|
69
75
|
declare const DefaultJsonLike: IJsonLike;
|
|
70
76
|
|
|
77
|
+
/**
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
declare const DefaultJsonTreeHelper: JsonTreeHelper;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Class representing a directory in a file tree.
|
|
84
|
+
* @public
|
|
85
|
+
*/
|
|
86
|
+
declare class DirectoryItem<TCT extends string = string> implements IFileTreeDirectoryItem<TCT> {
|
|
87
|
+
/**
|
|
88
|
+
* {@inheritdoc FileTree.IFileTreeDirectoryItem."type"}
|
|
89
|
+
*/
|
|
90
|
+
readonly type: 'directory';
|
|
91
|
+
/**
|
|
92
|
+
* {@inheritdoc FileTree.IFileTreeDirectoryItem.absolutePath}
|
|
93
|
+
*/
|
|
94
|
+
readonly absolutePath: string;
|
|
95
|
+
/**
|
|
96
|
+
* {@inheritdoc FileTree.IFileTreeDirectoryItem.name}
|
|
97
|
+
*/
|
|
98
|
+
get name(): string;
|
|
99
|
+
/**
|
|
100
|
+
* The {@link FileTree.IFileTreeAccessors | accessors} to use for file system operations.
|
|
101
|
+
* @public
|
|
102
|
+
*/
|
|
103
|
+
protected readonly _hal: IFileTreeAccessors<TCT>;
|
|
104
|
+
/**
|
|
105
|
+
* Protected constructor for derived classes.
|
|
106
|
+
* @param path - Relative path of the directory.
|
|
107
|
+
* @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
|
|
108
|
+
* file system operations.
|
|
109
|
+
* @public
|
|
110
|
+
*/
|
|
111
|
+
protected constructor(path: string, hal: IFileTreeAccessors<TCT>);
|
|
112
|
+
/**
|
|
113
|
+
* Creates a new DirectoryItem instance.
|
|
114
|
+
* @param path - Relative path of the directory.
|
|
115
|
+
* @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
|
|
116
|
+
* file system operations.
|
|
117
|
+
* @returns `Success` with the new {@link FileTree.DirectoryItem | DirectoryItem} instance if successful,
|
|
118
|
+
* or `Failure` with an error message otherwise.
|
|
119
|
+
*/
|
|
120
|
+
static create<TCT extends string = string>(path: string, hal: IFileTreeAccessors<TCT>): Result<DirectoryItem<TCT>>;
|
|
121
|
+
/**
|
|
122
|
+
* {@inheritdoc FileTree.IFileTreeDirectoryItem.getChildren}
|
|
123
|
+
*/
|
|
124
|
+
getChildren(): Result<ReadonlyArray<FileTreeItem<TCT>>>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Class representing a file in a file tree.
|
|
129
|
+
* @public
|
|
130
|
+
*/
|
|
131
|
+
declare class FileItem<TCT extends string = string> implements IFileTreeFileItem<TCT> {
|
|
132
|
+
/**
|
|
133
|
+
* {@inheritdoc FileTree.IFileTreeFileItem."type"}
|
|
134
|
+
*/
|
|
135
|
+
readonly type: 'file';
|
|
136
|
+
/**
|
|
137
|
+
* {@inheritdoc FileTree.IFileTreeFileItem.absolutePath}
|
|
138
|
+
*/
|
|
139
|
+
readonly absolutePath: string;
|
|
140
|
+
/**
|
|
141
|
+
* {@inheritdoc FileTree.IFileTreeFileItem.name}
|
|
142
|
+
*/
|
|
143
|
+
get name(): string;
|
|
144
|
+
/**
|
|
145
|
+
* {@inheritdoc FileTree.IFileTreeFileItem.baseName}
|
|
146
|
+
*/
|
|
147
|
+
get baseName(): string;
|
|
148
|
+
/**
|
|
149
|
+
* {@inheritdoc FileTree.IFileTreeFileItem.extension}
|
|
150
|
+
*/
|
|
151
|
+
get extension(): string;
|
|
152
|
+
/**
|
|
153
|
+
* {@inheritdoc FileTree.IFileTreeFileItem.contentType}
|
|
154
|
+
*/
|
|
155
|
+
get contentType(): TCT | undefined;
|
|
156
|
+
/**
|
|
157
|
+
* Mutable content type of the file.
|
|
158
|
+
* @public
|
|
159
|
+
*/
|
|
160
|
+
private _contentType;
|
|
161
|
+
/**
|
|
162
|
+
* The {@link FileTree.IFileTreeAccessors | accessors} to use for file system operations.
|
|
163
|
+
* @public
|
|
164
|
+
*/
|
|
165
|
+
protected readonly _hal: IFileTreeAccessors<TCT>;
|
|
166
|
+
/**
|
|
167
|
+
* Protected constructor for derived classes.
|
|
168
|
+
* @param path - Relative path of the file.
|
|
169
|
+
* @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
|
|
170
|
+
* file system operations.
|
|
171
|
+
* @public
|
|
172
|
+
*/
|
|
173
|
+
protected constructor(path: string, hal: IFileTreeAccessors<TCT>);
|
|
174
|
+
/**
|
|
175
|
+
* Creates a new {@link FileTree.FileItem} instance.
|
|
176
|
+
* @param path - Relative path of the file.
|
|
177
|
+
* @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
|
|
178
|
+
* file system operations.
|
|
179
|
+
* @public
|
|
180
|
+
*/
|
|
181
|
+
static create<TCT extends string = string>(path: string, hal: IFileTreeAccessors<TCT>): Result<FileItem<TCT>>;
|
|
182
|
+
/**
|
|
183
|
+
* {@inheritdoc FileTree.IFileTreeFileItem.(getContents:1)}
|
|
184
|
+
*/
|
|
185
|
+
getContents(): Result<JsonValue>;
|
|
186
|
+
/**
|
|
187
|
+
* {@inheritdoc FileTree.IFileTreeFileItem.(getContents:2)}
|
|
188
|
+
*/
|
|
189
|
+
getContents<T>(converter: Validator<T> | Converter<T>): Result<T>;
|
|
190
|
+
/**
|
|
191
|
+
* {@inheritdoc FileTree.IFileTreeFileItem.getRawContents}
|
|
192
|
+
*/
|
|
193
|
+
getRawContents(): Result<string>;
|
|
194
|
+
/**
|
|
195
|
+
* Sets the content type of the file.
|
|
196
|
+
* @param contentType - The content type of the file.
|
|
197
|
+
*/
|
|
198
|
+
setContentType(contentType: TCT | undefined): void;
|
|
199
|
+
/**
|
|
200
|
+
* Default function to infer the content type of a file.
|
|
201
|
+
* @param filePath - The path of the file.
|
|
202
|
+
* @returns `Success` with the content type of the file if successful, or
|
|
203
|
+
* `Failure` with an error message otherwise.
|
|
204
|
+
* @remarks This default implementation always returns `Success` with `undefined`.
|
|
205
|
+
* @public
|
|
206
|
+
*/
|
|
207
|
+
static defaultInferContentType<TCT extends string = string>(__filePath: string, __provided?: string): Result<TCT | undefined>;
|
|
208
|
+
/**
|
|
209
|
+
* Default function to accept the content type of a file.
|
|
210
|
+
* @param filePath - The path of the file.
|
|
211
|
+
* @param provided - Optional supplied content type.
|
|
212
|
+
* @returns `Success` with the content type of the file if successful, or
|
|
213
|
+
* `Failure` with an error message otherwise.
|
|
214
|
+
* @remarks This default implementation always returns `Success` with `undefined`.
|
|
215
|
+
* @public
|
|
216
|
+
*/
|
|
217
|
+
static defaultAcceptContentType<TCT extends string = string>(__filePath: string, provided?: TCT): Result<TCT | undefined>;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
declare namespace FileTree {
|
|
221
|
+
export {
|
|
222
|
+
FileTreeItemType,
|
|
223
|
+
ContentTypeFactory,
|
|
224
|
+
IFileTreeInitParams,
|
|
225
|
+
IFileTreeFileItem,
|
|
226
|
+
IFileTreeDirectoryItem,
|
|
227
|
+
FileTreeItem,
|
|
228
|
+
IFileTreeAccessors,
|
|
229
|
+
FileTree_2 as FileTree,
|
|
230
|
+
DirectoryItem,
|
|
231
|
+
FileItem,
|
|
232
|
+
forFilesystem,
|
|
233
|
+
inMemory,
|
|
234
|
+
IInMemoryFile,
|
|
235
|
+
InMemoryTreeAccessors,
|
|
236
|
+
FsFileTreeAccessors
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
export { FileTree }
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Represents a file tree.
|
|
243
|
+
* @public
|
|
244
|
+
*/
|
|
245
|
+
declare class FileTree_2<TCT extends string = string> {
|
|
246
|
+
/**
|
|
247
|
+
* The {@link FileTree.IFileTreeAccessors | accessors} to use for file system operations.
|
|
248
|
+
* @public
|
|
249
|
+
*/
|
|
250
|
+
hal: IFileTreeAccessors<TCT>;
|
|
251
|
+
/**
|
|
252
|
+
* Protected constructor for derived classes.
|
|
253
|
+
* @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
|
|
254
|
+
* file system operations.
|
|
255
|
+
* @public
|
|
256
|
+
*/
|
|
257
|
+
protected constructor(hal: IFileTreeAccessors<TCT>);
|
|
258
|
+
/**
|
|
259
|
+
* Creates a new {@link FileTree} instance with the supplied
|
|
260
|
+
* accessors.
|
|
261
|
+
* @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
|
|
262
|
+
* file system operations.
|
|
263
|
+
*/
|
|
264
|
+
static create<TCT extends string = string>(hal: IFileTreeAccessors<TCT>): Result<FileTree_2<TCT>>;
|
|
265
|
+
/**
|
|
266
|
+
* Gets an item from the tree.
|
|
267
|
+
* @param itemPath - The path to the item.
|
|
268
|
+
* @returns `Success` with the item if successful,
|
|
269
|
+
* or `Failure` with an error message otherwise.
|
|
270
|
+
*/
|
|
271
|
+
getItem(itemPath: string): Result<FileTreeItem<TCT>>;
|
|
272
|
+
/**
|
|
273
|
+
* Gets a file item from the tree.
|
|
274
|
+
* @param filePath - The path to the file.
|
|
275
|
+
* @returns `Success` with the {@link FileTree.IFileTreeFileItem | file item}
|
|
276
|
+
* if successful, or `Failure` with an error message otherwise.
|
|
277
|
+
*/
|
|
278
|
+
getFile(filePath: string): Result<IFileTreeFileItem<TCT>>;
|
|
279
|
+
/**
|
|
280
|
+
* Gets a directory item from the tree.
|
|
281
|
+
* @param directoryPath - The path to the directory.
|
|
282
|
+
* @returns `Success` with the {@link FileTree.IFileTreeDirectoryItem | directory item}
|
|
283
|
+
* if successful, or `Failure` with an error message otherwise.
|
|
284
|
+
*/
|
|
285
|
+
getDirectory(directoryPath: string): Result<IFileTreeDirectoryItem<TCT>>;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Type for an item in a file tree.
|
|
290
|
+
* @public
|
|
291
|
+
*/
|
|
292
|
+
declare type FileTreeItem<TCT extends string = string> = IFileTreeFileItem<TCT> | IFileTreeDirectoryItem<TCT>;
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Type of item in a file tree.
|
|
296
|
+
* @public
|
|
297
|
+
*/
|
|
298
|
+
declare type FileTreeItemType = 'directory' | 'file';
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Helper function to create a new {@link FileTree.FileTree | FileTree} instance
|
|
302
|
+
* with accessors for the filesystem.
|
|
303
|
+
* @param prefix - An optional prefix to prepended to supplied relative paths.
|
|
304
|
+
* @returns `Success` with the new {@link FileTree.FileTree | FileTree} instance
|
|
305
|
+
* if successful, or `Failure` with an error message otherwise.
|
|
306
|
+
* @public
|
|
307
|
+
*/
|
|
308
|
+
declare function forFilesystem<TCT extends string = string>(prefix?: string): Result<FileTree_2<TCT>>;
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Helper function to create a new {@link FileTree.FileTree | FileTree} instance
|
|
312
|
+
* with accessors for the filesystem.
|
|
313
|
+
* @param params - Optional {@link FileTree.IFileTreeInitParams | initialization parameters} for the file tree.
|
|
314
|
+
* @returns `Success` with the new {@link FileTree.FileTree | FileTree} instance
|
|
315
|
+
* if successful, or `Failure` with an error message otherwise.
|
|
316
|
+
* @public
|
|
317
|
+
*/
|
|
318
|
+
declare function forFilesystem<TCT extends string = string>(params?: IFileTreeInitParams<TCT>): Result<FileTree_2<TCT>>;
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Implementation of {@link FileTree.IFileTreeAccessors} that uses the
|
|
322
|
+
* file system to access files and directories.
|
|
323
|
+
* @public
|
|
324
|
+
*/
|
|
325
|
+
declare class FsFileTreeAccessors<TCT extends string = string> implements IFileTreeAccessors<TCT> {
|
|
326
|
+
/**
|
|
327
|
+
* Optional path prefix to prepend to all paths.
|
|
328
|
+
*/
|
|
329
|
+
readonly prefix: string | undefined;
|
|
330
|
+
/**
|
|
331
|
+
* Function to infer the content type of a file.
|
|
332
|
+
* @public
|
|
333
|
+
*/
|
|
334
|
+
protected readonly _inferContentType: (filePath: string) => Result<TCT | undefined>;
|
|
335
|
+
/**
|
|
336
|
+
* Construct a new instance of the {@link FileTree.FsFileTreeAccessors | FsFileTreeAccessors} class.
|
|
337
|
+
* @param params - Optional {@link FileTree.IFileTreeInitParams | initialization parameters}.
|
|
338
|
+
* @public
|
|
339
|
+
*/
|
|
340
|
+
constructor(params?: IFileTreeInitParams<TCT>);
|
|
341
|
+
/**
|
|
342
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.resolveAbsolutePath}
|
|
343
|
+
*/
|
|
344
|
+
resolveAbsolutePath(...paths: string[]): string;
|
|
345
|
+
/**
|
|
346
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getExtension}
|
|
347
|
+
*/
|
|
348
|
+
getExtension(itemPath: string): string;
|
|
349
|
+
/**
|
|
350
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getBaseName}
|
|
351
|
+
*/
|
|
352
|
+
getBaseName(itemPath: string, suffix?: string): string;
|
|
353
|
+
/**
|
|
354
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.joinPaths}
|
|
355
|
+
*/
|
|
356
|
+
joinPaths(...paths: string[]): string;
|
|
357
|
+
/**
|
|
358
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getItem}
|
|
359
|
+
*/
|
|
360
|
+
getItem(itemPath: string): Result<FileTreeItem<TCT>>;
|
|
361
|
+
/**
|
|
362
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getFileContents}
|
|
363
|
+
*/
|
|
364
|
+
getFileContents(filePath: string): Result<string>;
|
|
365
|
+
/**
|
|
366
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getFileContentType}
|
|
367
|
+
*/
|
|
368
|
+
getFileContentType(filePath: string, provided?: string): Result<TCT | undefined>;
|
|
369
|
+
/**
|
|
370
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getChildren}
|
|
371
|
+
*/
|
|
372
|
+
getChildren(dirPath: string): Result<ReadonlyArray<FileTreeItem<TCT>>>;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Common abstraction layer interface for a tree of files
|
|
377
|
+
* (e.g. a file system or a zip file).
|
|
378
|
+
* @public
|
|
379
|
+
*/
|
|
380
|
+
declare interface IFileTreeAccessors<TCT extends string = string> {
|
|
381
|
+
/**
|
|
382
|
+
* Resolves paths to an absolute path.
|
|
383
|
+
* @param paths - Paths to resolve.
|
|
384
|
+
* @returns The resolved absolute path.
|
|
385
|
+
*/
|
|
386
|
+
resolveAbsolutePath(...paths: string[]): string;
|
|
387
|
+
/**
|
|
388
|
+
* Gets the extension of a path.
|
|
389
|
+
* @param path - Path to get the extension of.
|
|
390
|
+
* @returns The extension of the path.
|
|
391
|
+
*/
|
|
392
|
+
getExtension(path: string): string;
|
|
393
|
+
/**
|
|
394
|
+
* Gets the base name of a path.
|
|
395
|
+
* @param path - Path to get the base name of.
|
|
396
|
+
* @param suffix - Optional suffix to remove from the base name.
|
|
397
|
+
* @returns The base name of the path.
|
|
398
|
+
*/
|
|
399
|
+
getBaseName(path: string, suffix?: string): string;
|
|
400
|
+
/**
|
|
401
|
+
* Joins paths together.
|
|
402
|
+
* @param paths - Paths to join.
|
|
403
|
+
* @returns The joined paths.
|
|
404
|
+
*/
|
|
405
|
+
joinPaths(...paths: string[]): string;
|
|
406
|
+
/**
|
|
407
|
+
* Gets an item from the file tree.
|
|
408
|
+
* @param path - Path of the item to get.
|
|
409
|
+
* @returns The item if it exists.
|
|
410
|
+
*/
|
|
411
|
+
getItem(path: string): Result<FileTreeItem<TCT>>;
|
|
412
|
+
/**
|
|
413
|
+
* Gets the contents of a file in the file tree.
|
|
414
|
+
* @param path - Absolute path of the file.
|
|
415
|
+
* @returns The contents of the file.
|
|
416
|
+
*/
|
|
417
|
+
getFileContents(path: string): Result<string>;
|
|
418
|
+
/**
|
|
419
|
+
* Gets the content type of a file in the file tree.
|
|
420
|
+
* @param path - Absolute path of the file.
|
|
421
|
+
* @param provided - Optional supplied content type.
|
|
422
|
+
* @returns The content type of the file.
|
|
423
|
+
*/
|
|
424
|
+
getFileContentType(path: string, provided?: string): Result<TCT | undefined>;
|
|
425
|
+
/**
|
|
426
|
+
* Gets the children of a directory in the file tree.
|
|
427
|
+
* @param path - Path of the directory.
|
|
428
|
+
* @returns The children of the directory.
|
|
429
|
+
*/
|
|
430
|
+
getChildren(path: string): Result<ReadonlyArray<FileTreeItem<TCT>>>;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Interface for a directory in a file tree.
|
|
435
|
+
* @public
|
|
436
|
+
*/
|
|
437
|
+
declare interface IFileTreeDirectoryItem<TCT extends string = string> {
|
|
438
|
+
/**
|
|
439
|
+
* Indicates that this {@link FileTree.FileTreeItem | file tree item} is a directory
|
|
440
|
+
*/
|
|
441
|
+
readonly type: 'directory';
|
|
442
|
+
/**
|
|
443
|
+
* The absolute path of the directory.
|
|
444
|
+
*/
|
|
445
|
+
readonly absolutePath: string;
|
|
446
|
+
/**
|
|
447
|
+
* The name of the directory
|
|
448
|
+
*/
|
|
449
|
+
readonly name: string;
|
|
450
|
+
/**
|
|
451
|
+
* Gets the children of the directory.
|
|
452
|
+
* @returns `Success` with the children of the directory if successful,
|
|
453
|
+
* or `Failure` with an error message otherwise.
|
|
454
|
+
*/
|
|
455
|
+
getChildren(): Result<ReadonlyArray<FileTreeItem<TCT>>>;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Interface for a file in a file tree.
|
|
460
|
+
* @public
|
|
461
|
+
*/
|
|
462
|
+
declare interface IFileTreeFileItem<TCT extends string = string> {
|
|
463
|
+
/**
|
|
464
|
+
* Indicates that this {@link FileTree.FileTreeItem | file tree item} is a file.
|
|
465
|
+
*/
|
|
466
|
+
readonly type: 'file';
|
|
467
|
+
/**
|
|
468
|
+
* The absolute path of the file.
|
|
469
|
+
*/
|
|
470
|
+
readonly absolutePath: string;
|
|
471
|
+
/**
|
|
472
|
+
* The name of the file
|
|
473
|
+
*/
|
|
474
|
+
readonly name: string;
|
|
475
|
+
/**
|
|
476
|
+
* The base name of the file (without extension)
|
|
477
|
+
*/
|
|
478
|
+
readonly baseName: string;
|
|
479
|
+
/**
|
|
480
|
+
* The extension of the file
|
|
481
|
+
*/
|
|
482
|
+
readonly extension: string;
|
|
483
|
+
/**
|
|
484
|
+
* An optional content type (e.g. mime type) for the file.
|
|
485
|
+
*/
|
|
486
|
+
readonly contentType: TCT | undefined;
|
|
487
|
+
/**
|
|
488
|
+
* Gets the contents of the file as parsed JSON.
|
|
489
|
+
* @returns `Success` with the parsed JSON-compatible contents if successful, or
|
|
490
|
+
* `Failure` with an error message otherwise.
|
|
491
|
+
*/
|
|
492
|
+
getContents(): Result<JsonValue>;
|
|
493
|
+
/**
|
|
494
|
+
* Gets the contents of the file as parsed JSON, converted to a specific type.
|
|
495
|
+
* @param converter - A `Validator` or `Converter`
|
|
496
|
+
* to convert the parsed JSON contents to the desired type.
|
|
497
|
+
* @returns `Success` with the converted contents if successful, or
|
|
498
|
+
* `Failure` with an error message otherwise.
|
|
499
|
+
*/
|
|
500
|
+
getContents<T>(converter: Validator<T> | Converter<T>): Result<T>;
|
|
501
|
+
/**
|
|
502
|
+
* Gets the raw contents of the file as a string.
|
|
503
|
+
* @returns `Success` with the raw contents if successful, or
|
|
504
|
+
* `Failure` with an error message otherwise.
|
|
505
|
+
*/
|
|
506
|
+
getRawContents(): Result<string>;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Initialization parameters for a file tree.
|
|
511
|
+
* @public
|
|
512
|
+
*/
|
|
513
|
+
declare interface IFileTreeInitParams<TCT extends string = string> {
|
|
514
|
+
prefix?: string;
|
|
515
|
+
inferContentType?: ContentTypeFactory<TCT>;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Represents a single file in an in-memory {@link FileTree | file tree}.
|
|
520
|
+
* @public
|
|
521
|
+
*/
|
|
522
|
+
declare interface IInMemoryFile<TCT extends string = string> {
|
|
523
|
+
/**
|
|
524
|
+
* The absolute path of the file in the tree.
|
|
525
|
+
*/
|
|
526
|
+
readonly path: string;
|
|
527
|
+
/**
|
|
528
|
+
* The contents of the file
|
|
529
|
+
*/
|
|
530
|
+
readonly contents: unknown;
|
|
531
|
+
/**
|
|
532
|
+
* The content type of the file.
|
|
533
|
+
*/
|
|
534
|
+
readonly contentType?: TCT;
|
|
535
|
+
}
|
|
536
|
+
|
|
71
537
|
/**
|
|
72
538
|
* Conversion context for JSON converters.
|
|
73
539
|
* @public
|
|
@@ -126,6 +592,91 @@ declare interface IJsonValidatorContext {
|
|
|
126
592
|
ignoreUndefinedProperties?: boolean;
|
|
127
593
|
}
|
|
128
594
|
|
|
595
|
+
/**
|
|
596
|
+
* Helper function to create a new {@link FileTree.FileTree | FileTree} instance
|
|
597
|
+
* with accessors for an in-memory file tree.
|
|
598
|
+
* @param files - An array of File |{@link FileTree.IInMemoryFile | in-memory files} to include in the tree.
|
|
599
|
+
* @param prefix - An optional prefix to add to the paths of all files in the tree.
|
|
600
|
+
* @returns `Success` with the new {@link FileTree.FileTree | FileTree} instance
|
|
601
|
+
* if successful, or `Failure` with an error message otherwise.
|
|
602
|
+
* @public
|
|
603
|
+
*/
|
|
604
|
+
declare function inMemory<TCT extends string = string>(files: IInMemoryFile<TCT>[], prefix?: string): Result<FileTree_2<TCT>>;
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Helper function to create a new {@link FileTree.FileTree | FileTree} instance
|
|
608
|
+
* with accessors for an in-memory file tree.
|
|
609
|
+
* @param files - An array of File |{@link FileTree.IInMemoryFile | in-memory files} to include in the tree.
|
|
610
|
+
* @param params - Optional {@link FileTree.IFileTreeInitParams | initialization parameters} for the file tree.
|
|
611
|
+
* @returns `Success` with the new {@link FileTree.FileTree | FileTree} instance
|
|
612
|
+
* if successful, or `Failure` with an error message otherwise.
|
|
613
|
+
* @public
|
|
614
|
+
*/
|
|
615
|
+
declare function inMemory<TCT extends string = string>(files: IInMemoryFile<TCT>[], params?: IFileTreeInitParams<TCT>): Result<FileTree_2<TCT>>;
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Implementation of {@link FileTree.IFileTreeAccessors} that uses an in-memory
|
|
619
|
+
* tree to access files and directories.
|
|
620
|
+
* @public
|
|
621
|
+
*/
|
|
622
|
+
declare class InMemoryTreeAccessors<TCT extends string = string> implements IFileTreeAccessors<TCT> {
|
|
623
|
+
private readonly _tree;
|
|
624
|
+
private readonly _inferContentType;
|
|
625
|
+
/**
|
|
626
|
+
* Protected constructor for derived classes.
|
|
627
|
+
* @param files - An array of {@link FileTree.IInMemoryFile | in-memory files} to include in the tree.
|
|
628
|
+
* @param params - Optional params for the tree.
|
|
629
|
+
* @public
|
|
630
|
+
*/
|
|
631
|
+
protected constructor(files: IInMemoryFile<TCT>[], params?: IFileTreeInitParams<TCT>);
|
|
632
|
+
/**
|
|
633
|
+
* Creates a new {@link FileTree.InMemoryTreeAccessors | InMemoryTreeAccessors} instance with the supplied
|
|
634
|
+
* in-memory files.
|
|
635
|
+
* @param files - An array of {@link FileTree.IInMemoryFile | in-memory files} to include in the tree.
|
|
636
|
+
* @param prefix - Optional prefix for the tree.
|
|
637
|
+
*/
|
|
638
|
+
static create<TCT extends string = string>(files: IInMemoryFile<TCT>[], prefix?: string): Result<InMemoryTreeAccessors<TCT>>;
|
|
639
|
+
/**
|
|
640
|
+
* Creates a new {@link FileTree.InMemoryTreeAccessors | InMemoryTreeAccessors} instance with the supplied
|
|
641
|
+
* in-memory files.
|
|
642
|
+
* @param files - An array of {@link FileTree.IInMemoryFile | in-memory files} to include in the tree.
|
|
643
|
+
* @param params - Optional params for the tree.
|
|
644
|
+
*/
|
|
645
|
+
static create<TCT extends string = string>(files: IInMemoryFile<TCT>[], params?: IFileTreeInitParams<TCT>): Result<InMemoryTreeAccessors<TCT>>;
|
|
646
|
+
/**
|
|
647
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.resolveAbsolutePath}
|
|
648
|
+
*/
|
|
649
|
+
resolveAbsolutePath(...paths: string[]): string;
|
|
650
|
+
/**
|
|
651
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getExtension}
|
|
652
|
+
*/
|
|
653
|
+
getExtension(path: string): string;
|
|
654
|
+
/**
|
|
655
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getBaseName}
|
|
656
|
+
*/
|
|
657
|
+
getBaseName(path: string, suffix?: string): string;
|
|
658
|
+
/**
|
|
659
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.joinPaths}
|
|
660
|
+
*/
|
|
661
|
+
joinPaths(...paths: string[]): string;
|
|
662
|
+
/**
|
|
663
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getItem}
|
|
664
|
+
*/
|
|
665
|
+
getItem(itemPath: string): Result<FileTreeItem<TCT>>;
|
|
666
|
+
/**
|
|
667
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getFileContents}
|
|
668
|
+
*/
|
|
669
|
+
getFileContents(path: string): Result<string>;
|
|
670
|
+
/**
|
|
671
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getFileContentType}
|
|
672
|
+
*/
|
|
673
|
+
getFileContentType(path: string, provided?: string): Result<TCT | undefined>;
|
|
674
|
+
/**
|
|
675
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getChildren}
|
|
676
|
+
*/
|
|
677
|
+
getChildren(path: string): Result<ReadonlyArray<FileTreeItem<TCT>>>;
|
|
678
|
+
}
|
|
679
|
+
|
|
129
680
|
/**
|
|
130
681
|
* Return value for one item in a directory conversion.
|
|
131
682
|
* @public
|
|
@@ -265,6 +816,14 @@ declare namespace JsonFile {
|
|
|
265
816
|
convertJsonDirectorySync,
|
|
266
817
|
convertJsonDirectoryToMapSync,
|
|
267
818
|
writeJsonFileSync,
|
|
819
|
+
JsonReviver,
|
|
820
|
+
JsonReplacerFunction,
|
|
821
|
+
JsonReplacerArray,
|
|
822
|
+
JsonReplacer,
|
|
823
|
+
IJsonLike,
|
|
824
|
+
DefaultJsonLike,
|
|
825
|
+
JsonTreeHelper,
|
|
826
|
+
DefaultJsonTreeHelper,
|
|
268
827
|
IJsonFsDirectoryOptions,
|
|
269
828
|
IReadDirectoryItem,
|
|
270
829
|
ItemNameTransformFunction,
|
|
@@ -273,13 +832,7 @@ declare namespace JsonFile {
|
|
|
273
832
|
JsonFsHelperInitOptions,
|
|
274
833
|
DefaultJsonFsHelperConfig,
|
|
275
834
|
JsonFsHelper,
|
|
276
|
-
DefaultJsonFsHelper
|
|
277
|
-
JsonReviver,
|
|
278
|
-
JsonReplacerFunction,
|
|
279
|
-
JsonReplacerArray,
|
|
280
|
-
JsonReplacer,
|
|
281
|
-
IJsonLike,
|
|
282
|
-
DefaultJsonLike
|
|
835
|
+
DefaultJsonFsHelper
|
|
283
836
|
}
|
|
284
837
|
}
|
|
285
838
|
export { JsonFile }
|
|
@@ -414,6 +967,62 @@ declare type JsonReplacerFunction = (key: string, value: JsonValue) => JsonValue
|
|
|
414
967
|
*/
|
|
415
968
|
declare type JsonReviver = (key: string, value: JsonValue) => JsonValue;
|
|
416
969
|
|
|
970
|
+
/**
|
|
971
|
+
* Helper class to work with JSON files using FileTree API (web-compatible).
|
|
972
|
+
* @public
|
|
973
|
+
*/
|
|
974
|
+
declare class JsonTreeHelper {
|
|
975
|
+
/**
|
|
976
|
+
* Configuration for this JsonTreeHelper.
|
|
977
|
+
*/
|
|
978
|
+
readonly json: IJsonLike;
|
|
979
|
+
/**
|
|
980
|
+
* Construct a new JsonTreeHelper.
|
|
981
|
+
* @param json - Optional {@link JsonFile.IJsonLike | IJsonLike} used to process strings
|
|
982
|
+
* and JSON values.
|
|
983
|
+
*/
|
|
984
|
+
constructor(json?: IJsonLike);
|
|
985
|
+
/**
|
|
986
|
+
* Read type-safe JSON from a file in a FileTree.
|
|
987
|
+
* @param fileTree - The FileTree to read from
|
|
988
|
+
* @param filePath - Path of the file to read within the tree
|
|
989
|
+
* @returns `Success` with a {@link JsonValue | JsonValue} or `Failure`
|
|
990
|
+
* with a message if an error occurs.
|
|
991
|
+
*/
|
|
992
|
+
readJsonFromTree(fileTree: FileTree.FileTree, filePath: string): Result<JsonValue>;
|
|
993
|
+
/**
|
|
994
|
+
* Read a JSON file from a FileTree and apply a supplied converter or validator.
|
|
995
|
+
* @param fileTree - The FileTree to read from
|
|
996
|
+
* @param filePath - Path of the file to read within the tree
|
|
997
|
+
* @param cv - Converter or validator used to process the file.
|
|
998
|
+
* @param context - Optional context for the converter/validator
|
|
999
|
+
* @returns `Success` with a result of type `<T>`, or `Failure`
|
|
1000
|
+
* with a message if an error occurs.
|
|
1001
|
+
*/
|
|
1002
|
+
convertJsonFromTree<T, TC = unknown>(fileTree: FileTree.FileTree, filePath: string, cv: Converter<T, TC> | Validator<T, TC>, context?: TC): Result<T>;
|
|
1003
|
+
/**
|
|
1004
|
+
* Reads all JSON files from a directory in a FileTree and applies a converter or validator.
|
|
1005
|
+
* @param fileTree - The FileTree to read from
|
|
1006
|
+
* @param dirPath - The path of the directory within the tree
|
|
1007
|
+
* @param cv - Converter or validator to apply to each JSON file
|
|
1008
|
+
* @param filePattern - Optional regex pattern to filter files (defaults to .json files)
|
|
1009
|
+
* @param context - Optional context for the converter/validator
|
|
1010
|
+
* @returns Array of items with filename and converted content
|
|
1011
|
+
*/
|
|
1012
|
+
convertJsonDirectoryFromTree<T, TC = unknown>(fileTree: FileTree.FileTree, dirPath: string, cv: Converter<T, TC> | Validator<T, TC>, filePattern?: RegExp, context?: TC): Result<IReadDirectoryItem<T>[]>;
|
|
1013
|
+
/**
|
|
1014
|
+
* Reads and converts all JSON files from a directory in a FileTree,
|
|
1015
|
+
* returning a Map indexed by file base name.
|
|
1016
|
+
* @param fileTree - The FileTree to read from
|
|
1017
|
+
* @param dirPath - The path of the directory within the tree
|
|
1018
|
+
* @param cv - Converter or validator to apply to each JSON file
|
|
1019
|
+
* @param filePattern - Optional regex pattern to filter files
|
|
1020
|
+
* @param context - Optional context for the converter/validator
|
|
1021
|
+
* @returns Map of basename to converted content
|
|
1022
|
+
*/
|
|
1023
|
+
convertJsonDirectoryToMapFromTree<T, TC = unknown>(fileTree: FileTree.FileTree, dirPath: string, cv: Converter<T, TC> | Validator<T, TC>, filePattern?: RegExp, context?: TC): Result<Map<string, T>>;
|
|
1024
|
+
}
|
|
1025
|
+
|
|
417
1026
|
/**
|
|
418
1027
|
* A {@link JsonValue | JsonValue} is one of: a {@link JsonPrimitive | JsonPrimitive},
|
|
419
1028
|
* a {@link JsonObject | JsonObject} or an {@link JsonArray | JsonArray}.
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as Converters from './packlets/converters';
|
|
2
|
+
import * as FileTree from './packlets/file-tree';
|
|
2
3
|
import * as JsonFile from './packlets/json-file';
|
|
3
4
|
import * as Validators from './packlets/validators';
|
|
4
5
|
export * from './packlets/json';
|
|
5
|
-
export { Converters, JsonFile, Validators };
|
|
6
|
+
export { Converters, FileTree, JsonFile, Validators };
|
|
6
7
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.js
CHANGED
|
@@ -57,9 +57,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
57
57
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
58
58
|
};
|
|
59
59
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
60
|
-
exports.Validators = exports.JsonFile = exports.Converters = void 0;
|
|
60
|
+
exports.Validators = exports.JsonFile = exports.FileTree = exports.Converters = void 0;
|
|
61
61
|
const Converters = __importStar(require("./packlets/converters"));
|
|
62
62
|
exports.Converters = Converters;
|
|
63
|
+
const FileTree = __importStar(require("./packlets/file-tree"));
|
|
64
|
+
exports.FileTree = FileTree;
|
|
63
65
|
const JsonFile = __importStar(require("./packlets/json-file"));
|
|
64
66
|
exports.JsonFile = JsonFile;
|
|
65
67
|
const Validators = __importStar(require("./packlets/validators"));
|