@fgv/ts-json-base 5.0.0-9 → 5.0.1-0

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.
Files changed (65) hide show
  1. package/CHANGELOG.json +9 -6
  2. package/README.md +150 -0
  3. package/dist/ts-json-base.d.ts +674 -9
  4. package/dist/tsdoc-metadata.json +1 -1
  5. package/eslint.config.js +16 -0
  6. package/lib/index.browser.d.ts +7 -0
  7. package/lib/index.browser.js +72 -0
  8. package/lib/index.d.ts +2 -1
  9. package/lib/index.js +3 -1
  10. package/lib/packlets/file-tree/directoryItem.d.ts +47 -0
  11. package/lib/packlets/file-tree/directoryItem.js +71 -0
  12. package/lib/packlets/file-tree/fileItem.d.ts +97 -0
  13. package/lib/packlets/file-tree/fileItem.js +130 -0
  14. package/lib/packlets/file-tree/fileTree.d.ts +49 -0
  15. package/lib/packlets/file-tree/fileTree.js +89 -0
  16. package/lib/packlets/file-tree/fileTreeAccessors.d.ts +158 -0
  17. package/lib/packlets/file-tree/fileTreeAccessors.js +24 -0
  18. package/lib/packlets/file-tree/fileTreeHelpers.d.ts +43 -0
  19. package/lib/packlets/file-tree/fileTreeHelpers.js +38 -0
  20. package/lib/packlets/file-tree/fsTree.d.ts +57 -0
  21. package/lib/packlets/file-tree/fsTree.js +129 -0
  22. package/lib/packlets/file-tree/in-memory/inMemoryTree.d.ts +83 -0
  23. package/lib/packlets/file-tree/in-memory/inMemoryTree.js +181 -0
  24. package/lib/packlets/file-tree/in-memory/index.d.ts +2 -0
  25. package/lib/packlets/file-tree/in-memory/index.js +39 -0
  26. package/lib/packlets/file-tree/in-memory/treeBuilder.d.ts +113 -0
  27. package/lib/packlets/file-tree/in-memory/treeBuilder.js +179 -0
  28. package/lib/packlets/file-tree/index.browser.d.ts +6 -0
  29. package/lib/packlets/file-tree/index.browser.js +49 -0
  30. package/lib/packlets/file-tree/index.d.ts +8 -0
  31. package/lib/packlets/file-tree/index.js +50 -0
  32. package/lib/packlets/json/common.d.ts +56 -2
  33. package/lib/packlets/json/common.js +7 -3
  34. package/lib/packlets/json-file/index.browser.d.ts +4 -0
  35. package/lib/packlets/json-file/index.browser.js +46 -0
  36. package/lib/packlets/json-file/index.d.ts +2 -1
  37. package/lib/packlets/json-file/index.js +6 -1
  38. package/lib/packlets/json-file/jsonFsHelper.js +14 -44
  39. package/lib/packlets/json-file/jsonTreeHelper.d.ts +65 -0
  40. package/lib/packlets/json-file/jsonTreeHelper.js +120 -0
  41. package/lib/packlets/validators/validators.js +4 -4
  42. package/package.json +35 -19
  43. package/CHANGELOG.md +0 -100
  44. package/lib/index.d.ts.map +0 -1
  45. package/lib/index.js.map +0 -1
  46. package/lib/packlets/converters/converters.d.ts.map +0 -1
  47. package/lib/packlets/converters/converters.js.map +0 -1
  48. package/lib/packlets/converters/index.d.ts.map +0 -1
  49. package/lib/packlets/converters/index.js.map +0 -1
  50. package/lib/packlets/json/common.d.ts.map +0 -1
  51. package/lib/packlets/json/common.js.map +0 -1
  52. package/lib/packlets/json/index.d.ts.map +0 -1
  53. package/lib/packlets/json/index.js.map +0 -1
  54. package/lib/packlets/json-file/file.d.ts.map +0 -1
  55. package/lib/packlets/json-file/file.js.map +0 -1
  56. package/lib/packlets/json-file/index.d.ts.map +0 -1
  57. package/lib/packlets/json-file/index.js.map +0 -1
  58. package/lib/packlets/json-file/jsonFsHelper.d.ts.map +0 -1
  59. package/lib/packlets/json-file/jsonFsHelper.js.map +0 -1
  60. package/lib/packlets/json-file/jsonLike.d.ts.map +0 -1
  61. package/lib/packlets/json-file/jsonLike.js.map +0 -1
  62. package/lib/packlets/validators/index.d.ts.map +0 -1
  63. package/lib/packlets/validators/index.js.map +0 -1
  64. package/lib/packlets/validators/validators.d.ts.map +0 -1
  65. package/lib/packlets/validators/validators.js.map +0 -1
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.52.8"
8
+ "packageVersion": "7.52.12"
9
9
  }
10
10
  ]
11
11
  }
@@ -0,0 +1,16 @@
1
+ // ESLint 9 flat config
2
+ const nodeProfile = require('@rushstack/eslint-config/flat/profile/node');
3
+ const packletsPlugin = require('@rushstack/eslint-config/flat/mixins/packlets');
4
+ const tsdocPlugin = require('@rushstack/eslint-config/flat/mixins/tsdoc');
5
+
6
+ module.exports = [
7
+ ...nodeProfile,
8
+ packletsPlugin,
9
+ ...tsdocPlugin,
10
+ {
11
+ // Override specific rules if needed
12
+ rules: {
13
+ '@rushstack/packlets/mechanics': 'warn'
14
+ }
15
+ }
16
+ ];
@@ -0,0 +1,7 @@
1
+ import * as Converters from './packlets/converters';
2
+ import * as FileTree from './packlets/file-tree/index.browser';
3
+ import * as JsonFile from './packlets/json-file/index.browser';
4
+ import * as Validators from './packlets/validators';
5
+ export * from './packlets/json';
6
+ export { Converters, FileTree, JsonFile, Validators };
7
+ //# sourceMappingURL=index.browser.d.ts.map
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2023 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
24
+ if (k2 === undefined) k2 = k;
25
+ var desc = Object.getOwnPropertyDescriptor(m, k);
26
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
27
+ desc = { enumerable: true, get: function() { return m[k]; } };
28
+ }
29
+ Object.defineProperty(o, k2, desc);
30
+ }) : (function(o, m, k, k2) {
31
+ if (k2 === undefined) k2 = k;
32
+ o[k2] = m[k];
33
+ }));
34
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
35
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
36
+ }) : function(o, v) {
37
+ o["default"] = v;
38
+ });
39
+ var __importStar = (this && this.__importStar) || (function () {
40
+ var ownKeys = function(o) {
41
+ ownKeys = Object.getOwnPropertyNames || function (o) {
42
+ var ar = [];
43
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
44
+ return ar;
45
+ };
46
+ return ownKeys(o);
47
+ };
48
+ return function (mod) {
49
+ if (mod && mod.__esModule) return mod;
50
+ var result = {};
51
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
52
+ __setModuleDefault(result, mod);
53
+ return result;
54
+ };
55
+ })();
56
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
57
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
58
+ };
59
+ Object.defineProperty(exports, "__esModule", { value: true });
60
+ exports.Validators = exports.JsonFile = exports.FileTree = exports.Converters = void 0;
61
+ const Converters = __importStar(require("./packlets/converters"));
62
+ exports.Converters = Converters;
63
+ // eslint-disable-next-line @rushstack/packlets/mechanics
64
+ const FileTree = __importStar(require("./packlets/file-tree/index.browser"));
65
+ exports.FileTree = FileTree;
66
+ // eslint-disable-next-line @rushstack/packlets/mechanics
67
+ const JsonFile = __importStar(require("./packlets/json-file/index.browser"));
68
+ exports.JsonFile = JsonFile;
69
+ const Validators = __importStar(require("./packlets/validators"));
70
+ exports.Validators = Validators;
71
+ __exportStar(require("./packlets/json"), exports);
72
+ //# sourceMappingURL=index.browser.js.map
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"));
@@ -0,0 +1,47 @@
1
+ import { Result } from '@fgv/ts-utils';
2
+ import { FileTreeItem, IFileTreeAccessors, IFileTreeDirectoryItem } from './fileTreeAccessors';
3
+ /**
4
+ * Class representing a directory in a file tree.
5
+ * @public
6
+ */
7
+ export declare class DirectoryItem<TCT extends string = string> implements IFileTreeDirectoryItem<TCT> {
8
+ /**
9
+ * {@inheritdoc FileTree.IFileTreeDirectoryItem."type"}
10
+ */
11
+ readonly type: 'directory';
12
+ /**
13
+ * {@inheritdoc FileTree.IFileTreeDirectoryItem.absolutePath}
14
+ */
15
+ readonly absolutePath: string;
16
+ /**
17
+ * {@inheritdoc FileTree.IFileTreeDirectoryItem.name}
18
+ */
19
+ get name(): string;
20
+ /**
21
+ * The {@link FileTree.IFileTreeAccessors | accessors} to use for file system operations.
22
+ * @public
23
+ */
24
+ protected readonly _hal: IFileTreeAccessors<TCT>;
25
+ /**
26
+ * Protected constructor for derived classes.
27
+ * @param path - Relative path of the directory.
28
+ * @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
29
+ * file system operations.
30
+ * @public
31
+ */
32
+ protected constructor(path: string, hal: IFileTreeAccessors<TCT>);
33
+ /**
34
+ * Creates a new DirectoryItem instance.
35
+ * @param path - Relative path of the directory.
36
+ * @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
37
+ * file system operations.
38
+ * @returns `Success` with the new {@link FileTree.DirectoryItem | DirectoryItem} instance if successful,
39
+ * or `Failure` with an error message otherwise.
40
+ */
41
+ static create<TCT extends string = string>(path: string, hal: IFileTreeAccessors<TCT>): Result<DirectoryItem<TCT>>;
42
+ /**
43
+ * {@inheritdoc FileTree.IFileTreeDirectoryItem.getChildren}
44
+ */
45
+ getChildren(): Result<ReadonlyArray<FileTreeItem<TCT>>>;
46
+ }
47
+ //# sourceMappingURL=directoryItem.d.ts.map
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.DirectoryItem = void 0;
25
+ const ts_utils_1 = require("@fgv/ts-utils");
26
+ /**
27
+ * Class representing a directory in a file tree.
28
+ * @public
29
+ */
30
+ class DirectoryItem {
31
+ /**
32
+ * {@inheritdoc FileTree.IFileTreeDirectoryItem.name}
33
+ */
34
+ get name() {
35
+ return this._hal.getBaseName(this.absolutePath);
36
+ }
37
+ /**
38
+ * Protected constructor for derived classes.
39
+ * @param path - Relative path of the directory.
40
+ * @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
41
+ * file system operations.
42
+ * @public
43
+ */
44
+ constructor(path, hal) {
45
+ /**
46
+ * {@inheritdoc FileTree.IFileTreeDirectoryItem."type"}
47
+ */
48
+ this.type = 'directory';
49
+ this._hal = hal;
50
+ this.absolutePath = hal.resolveAbsolutePath(path);
51
+ }
52
+ /**
53
+ * Creates a new DirectoryItem instance.
54
+ * @param path - Relative path of the directory.
55
+ * @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
56
+ * file system operations.
57
+ * @returns `Success` with the new {@link FileTree.DirectoryItem | DirectoryItem} instance if successful,
58
+ * or `Failure` with an error message otherwise.
59
+ */
60
+ static create(path, hal) {
61
+ return (0, ts_utils_1.captureResult)(() => new DirectoryItem(path, hal));
62
+ }
63
+ /**
64
+ * {@inheritdoc FileTree.IFileTreeDirectoryItem.getChildren}
65
+ */
66
+ getChildren() {
67
+ return this._hal.getChildren(this.absolutePath);
68
+ }
69
+ }
70
+ exports.DirectoryItem = DirectoryItem;
71
+ //# sourceMappingURL=directoryItem.js.map
@@ -0,0 +1,97 @@
1
+ import { Result } from '@fgv/ts-utils';
2
+ import { Converter, Validator } from '@fgv/ts-utils';
3
+ import { JsonValue } from '../json';
4
+ import { IFileTreeAccessors, IFileTreeFileItem } from './fileTreeAccessors';
5
+ /**
6
+ * Class representing a file in a file tree.
7
+ * @public
8
+ */
9
+ export declare class FileItem<TCT extends string = string> implements IFileTreeFileItem<TCT> {
10
+ /**
11
+ * {@inheritdoc FileTree.IFileTreeFileItem."type"}
12
+ */
13
+ readonly type: 'file';
14
+ /**
15
+ * {@inheritdoc FileTree.IFileTreeFileItem.absolutePath}
16
+ */
17
+ readonly absolutePath: string;
18
+ /**
19
+ * {@inheritdoc FileTree.IFileTreeFileItem.name}
20
+ */
21
+ get name(): string;
22
+ /**
23
+ * {@inheritdoc FileTree.IFileTreeFileItem.baseName}
24
+ */
25
+ get baseName(): string;
26
+ /**
27
+ * {@inheritdoc FileTree.IFileTreeFileItem.extension}
28
+ */
29
+ get extension(): string;
30
+ /**
31
+ * {@inheritdoc FileTree.IFileTreeFileItem.contentType}
32
+ */
33
+ get contentType(): TCT | undefined;
34
+ /**
35
+ * Mutable content type of the file.
36
+ * @public
37
+ */
38
+ private _contentType;
39
+ /**
40
+ * The {@link FileTree.IFileTreeAccessors | accessors} to use for file system operations.
41
+ * @public
42
+ */
43
+ protected readonly _hal: IFileTreeAccessors<TCT>;
44
+ /**
45
+ * Protected constructor for derived classes.
46
+ * @param path - Relative path of the file.
47
+ * @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
48
+ * file system operations.
49
+ * @public
50
+ */
51
+ protected constructor(path: string, hal: IFileTreeAccessors<TCT>);
52
+ /**
53
+ * Creates a new {@link FileTree.FileItem} instance.
54
+ * @param path - Relative path of the file.
55
+ * @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
56
+ * file system operations.
57
+ * @public
58
+ */
59
+ static create<TCT extends string = string>(path: string, hal: IFileTreeAccessors<TCT>): Result<FileItem<TCT>>;
60
+ /**
61
+ * {@inheritdoc FileTree.IFileTreeFileItem.(getContents:1)}
62
+ */
63
+ getContents(): Result<JsonValue>;
64
+ /**
65
+ * {@inheritdoc FileTree.IFileTreeFileItem.(getContents:2)}
66
+ */
67
+ getContents<T>(converter: Validator<T> | Converter<T>): Result<T>;
68
+ /**
69
+ * {@inheritdoc FileTree.IFileTreeFileItem.getRawContents}
70
+ */
71
+ getRawContents(): Result<string>;
72
+ /**
73
+ * Sets the content type of the file.
74
+ * @param contentType - The content type of the file.
75
+ */
76
+ setContentType(contentType: TCT | undefined): void;
77
+ /**
78
+ * Default function to infer the content type of a file.
79
+ * @param filePath - The path of the file.
80
+ * @returns `Success` with the content type of the file if successful, or
81
+ * `Failure` with an error message otherwise.
82
+ * @remarks This default implementation always returns `Success` with `undefined`.
83
+ * @public
84
+ */
85
+ static defaultInferContentType<TCT extends string = string>(__filePath: string, __provided?: string): Result<TCT | undefined>;
86
+ /**
87
+ * Default function to accept the content type of a file.
88
+ * @param filePath - The path of the file.
89
+ * @param provided - Optional supplied content type.
90
+ * @returns `Success` with the content type of the file if successful, or
91
+ * `Failure` with an error message otherwise.
92
+ * @remarks This default implementation always returns `Success` with `undefined`.
93
+ * @public
94
+ */
95
+ static defaultAcceptContentType<TCT extends string = string>(__filePath: string, provided?: TCT): Result<TCT | undefined>;
96
+ }
97
+ //# sourceMappingURL=fileItem.d.ts.map
@@ -0,0 +1,130 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.FileItem = void 0;
25
+ const ts_utils_1 = require("@fgv/ts-utils");
26
+ /**
27
+ * Class representing a file in a file tree.
28
+ * @public
29
+ */
30
+ class FileItem {
31
+ /**
32
+ * {@inheritdoc FileTree.IFileTreeFileItem.name}
33
+ */
34
+ get name() {
35
+ return this._hal.getBaseName(this.absolutePath);
36
+ }
37
+ /**
38
+ * {@inheritdoc FileTree.IFileTreeFileItem.baseName}
39
+ */
40
+ get baseName() {
41
+ return this._hal.getBaseName(this.absolutePath, this.extension);
42
+ }
43
+ /**
44
+ * {@inheritdoc FileTree.IFileTreeFileItem.extension}
45
+ */
46
+ get extension() {
47
+ return this._hal.getExtension(this.absolutePath);
48
+ }
49
+ /**
50
+ * {@inheritdoc FileTree.IFileTreeFileItem.contentType}
51
+ */
52
+ get contentType() {
53
+ return this._contentType;
54
+ }
55
+ /**
56
+ * Protected constructor for derived classes.
57
+ * @param path - Relative path of the file.
58
+ * @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
59
+ * file system operations.
60
+ * @public
61
+ */
62
+ constructor(path, hal) {
63
+ /**
64
+ * {@inheritdoc FileTree.IFileTreeFileItem."type"}
65
+ */
66
+ this.type = 'file';
67
+ this._hal = hal;
68
+ this.absolutePath = hal.resolveAbsolutePath(path);
69
+ this._contentType = hal.getFileContentType(this.absolutePath).orDefault();
70
+ }
71
+ /**
72
+ * Creates a new {@link FileTree.FileItem} instance.
73
+ * @param path - Relative path of the file.
74
+ * @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
75
+ * file system operations.
76
+ * @public
77
+ */
78
+ static create(path, hal) {
79
+ return (0, ts_utils_1.captureResult)(() => new FileItem(path, hal));
80
+ }
81
+ getContents(converter) {
82
+ return this._hal
83
+ .getFileContents(this.absolutePath)
84
+ .onSuccess((body) => (0, ts_utils_1.captureResult)(() => JSON.parse(body)).onFailure(() => (0, ts_utils_1.succeed)(body)))
85
+ .onSuccess((parsed) => {
86
+ if (converter !== undefined) {
87
+ return converter.convert(parsed);
88
+ }
89
+ return (0, ts_utils_1.succeed)(parsed);
90
+ });
91
+ }
92
+ /**
93
+ * {@inheritdoc FileTree.IFileTreeFileItem.getRawContents}
94
+ */
95
+ getRawContents() {
96
+ return this._hal.getFileContents(this.absolutePath);
97
+ }
98
+ /**
99
+ * Sets the content type of the file.
100
+ * @param contentType - The content type of the file.
101
+ */
102
+ setContentType(contentType) {
103
+ this._contentType = contentType;
104
+ }
105
+ /**
106
+ * Default function to infer the content type of a file.
107
+ * @param filePath - The path of the file.
108
+ * @returns `Success` with the content type of the file if successful, or
109
+ * `Failure` with an error message otherwise.
110
+ * @remarks This default implementation always returns `Success` with `undefined`.
111
+ * @public
112
+ */
113
+ static defaultInferContentType(__filePath, __provided) {
114
+ return (0, ts_utils_1.succeed)(undefined);
115
+ }
116
+ /**
117
+ * Default function to accept the content type of a file.
118
+ * @param filePath - The path of the file.
119
+ * @param provided - Optional supplied content type.
120
+ * @returns `Success` with the content type of the file if successful, or
121
+ * `Failure` with an error message otherwise.
122
+ * @remarks This default implementation always returns `Success` with `undefined`.
123
+ * @public
124
+ */
125
+ static defaultAcceptContentType(__filePath, provided) {
126
+ return (0, ts_utils_1.succeed)(provided);
127
+ }
128
+ }
129
+ exports.FileItem = FileItem;
130
+ //# sourceMappingURL=fileItem.js.map
@@ -0,0 +1,49 @@
1
+ import { Result } from '@fgv/ts-utils';
2
+ import { FileTreeItem, IFileTreeAccessors, IFileTreeDirectoryItem, IFileTreeFileItem } from './fileTreeAccessors';
3
+ /**
4
+ * Represents a file tree.
5
+ * @public
6
+ */
7
+ export declare class FileTree<TCT extends string = string> {
8
+ /**
9
+ * The {@link FileTree.IFileTreeAccessors | accessors} to use for file system operations.
10
+ * @public
11
+ */
12
+ hal: IFileTreeAccessors<TCT>;
13
+ /**
14
+ * Protected constructor for derived classes.
15
+ * @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
16
+ * file system operations.
17
+ * @public
18
+ */
19
+ protected constructor(hal: IFileTreeAccessors<TCT>);
20
+ /**
21
+ * Creates a new {@link FileTree} instance with the supplied
22
+ * accessors.
23
+ * @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
24
+ * file system operations.
25
+ */
26
+ static create<TCT extends string = string>(hal: IFileTreeAccessors<TCT>): Result<FileTree<TCT>>;
27
+ /**
28
+ * Gets an item from the tree.
29
+ * @param itemPath - The path to the item.
30
+ * @returns `Success` with the item if successful,
31
+ * or `Failure` with an error message otherwise.
32
+ */
33
+ getItem(itemPath: string): Result<FileTreeItem<TCT>>;
34
+ /**
35
+ * Gets a file item from the tree.
36
+ * @param filePath - The path to the file.
37
+ * @returns `Success` with the {@link FileTree.IFileTreeFileItem | file item}
38
+ * if successful, or `Failure` with an error message otherwise.
39
+ */
40
+ getFile(filePath: string): Result<IFileTreeFileItem<TCT>>;
41
+ /**
42
+ * Gets a directory item from the tree.
43
+ * @param directoryPath - The path to the directory.
44
+ * @returns `Success` with the {@link FileTree.IFileTreeDirectoryItem | directory item}
45
+ * if successful, or `Failure` with an error message otherwise.
46
+ */
47
+ getDirectory(directoryPath: string): Result<IFileTreeDirectoryItem<TCT>>;
48
+ }
49
+ //# sourceMappingURL=fileTree.d.ts.map
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.FileTree = void 0;
25
+ const ts_utils_1 = require("@fgv/ts-utils");
26
+ /**
27
+ * Represents a file tree.
28
+ * @public
29
+ */
30
+ class FileTree {
31
+ /**
32
+ * Protected constructor for derived classes.
33
+ * @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
34
+ * file system operations.
35
+ * @public
36
+ */
37
+ constructor(hal) {
38
+ this.hal = hal;
39
+ }
40
+ /**
41
+ * Creates a new {@link FileTree} instance with the supplied
42
+ * accessors.
43
+ * @param hal - The {@link FileTree.IFileTreeAccessors | accessors} to use for
44
+ * file system operations.
45
+ */
46
+ static create(hal) {
47
+ return (0, ts_utils_1.captureResult)(() => new FileTree(hal));
48
+ }
49
+ /**
50
+ * Gets an item from the tree.
51
+ * @param itemPath - The path to the item.
52
+ * @returns `Success` with the item if successful,
53
+ * or `Failure` with an error message otherwise.
54
+ */
55
+ getItem(itemPath) {
56
+ const absolutePath = this.hal.resolveAbsolutePath(itemPath);
57
+ return this.hal.getItem(absolutePath);
58
+ }
59
+ /**
60
+ * Gets a file item from the tree.
61
+ * @param filePath - The path to the file.
62
+ * @returns `Success` with the {@link FileTree.IFileTreeFileItem | file item}
63
+ * if successful, or `Failure` with an error message otherwise.
64
+ */
65
+ getFile(filePath) {
66
+ return this.getItem(filePath).onSuccess((item) => {
67
+ if (item.type === 'file') {
68
+ return (0, ts_utils_1.succeed)(item);
69
+ }
70
+ return (0, ts_utils_1.fail)(`${filePath}: not a file`);
71
+ });
72
+ }
73
+ /**
74
+ * Gets a directory item from the tree.
75
+ * @param directoryPath - The path to the directory.
76
+ * @returns `Success` with the {@link FileTree.IFileTreeDirectoryItem | directory item}
77
+ * if successful, or `Failure` with an error message otherwise.
78
+ */
79
+ getDirectory(directoryPath) {
80
+ return this.getItem(directoryPath).onSuccess((item) => {
81
+ if (item.type === 'directory') {
82
+ return (0, ts_utils_1.succeed)(item);
83
+ }
84
+ return (0, ts_utils_1.fail)(`${directoryPath}: not a directory`);
85
+ });
86
+ }
87
+ }
88
+ exports.FileTree = FileTree;
89
+ //# sourceMappingURL=fileTree.js.map