@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.
- package/CHANGELOG.json +9 -6
- package/README.md +150 -0
- package/dist/ts-json-base.d.ts +674 -9
- package/dist/tsdoc-metadata.json +1 -1
- package/eslint.config.js +16 -0
- package/lib/index.browser.d.ts +7 -0
- package/lib/index.browser.js +72 -0
- 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.browser.d.ts +6 -0
- package/lib/packlets/file-tree/index.browser.js +49 -0
- package/lib/packlets/file-tree/index.d.ts +8 -0
- package/lib/packlets/file-tree/index.js +50 -0
- package/lib/packlets/json/common.d.ts +56 -2
- package/lib/packlets/json/common.js +7 -3
- package/lib/packlets/json-file/index.browser.d.ts +4 -0
- package/lib/packlets/json-file/index.browser.js +46 -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/lib/packlets/validators/validators.js +4 -4
- package/package.json +35 -19
- package/CHANGELOG.md +0 -100
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/packlets/converters/converters.d.ts.map +0 -1
- package/lib/packlets/converters/converters.js.map +0 -1
- package/lib/packlets/converters/index.d.ts.map +0 -1
- package/lib/packlets/converters/index.js.map +0 -1
- package/lib/packlets/json/common.d.ts.map +0 -1
- package/lib/packlets/json/common.js.map +0 -1
- package/lib/packlets/json/index.d.ts.map +0 -1
- package/lib/packlets/json/index.js.map +0 -1
- package/lib/packlets/json-file/file.d.ts.map +0 -1
- package/lib/packlets/json-file/file.js.map +0 -1
- package/lib/packlets/json-file/index.d.ts.map +0 -1
- package/lib/packlets/json-file/index.js.map +0 -1
- package/lib/packlets/json-file/jsonFsHelper.d.ts.map +0 -1
- package/lib/packlets/json-file/jsonFsHelper.js.map +0 -1
- package/lib/packlets/json-file/jsonLike.d.ts.map +0 -1
- package/lib/packlets/json-file/jsonLike.js.map +0 -1
- package/lib/packlets/validators/index.d.ts.map +0 -1
- package/lib/packlets/validators/index.js.map +0 -1
- package/lib/packlets/validators/validators.d.ts.map +0 -1
- package/lib/packlets/validators/validators.js.map +0 -1
|
@@ -0,0 +1,181 @@
|
|
|
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.InMemoryTreeAccessors = void 0;
|
|
25
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
26
|
+
const directoryItem_1 = require("../directoryItem");
|
|
27
|
+
const fileItem_1 = require("../fileItem");
|
|
28
|
+
const treeBuilder_1 = require("./treeBuilder");
|
|
29
|
+
/**
|
|
30
|
+
* Implementation of {@link FileTree.IFileTreeAccessors} that uses an in-memory
|
|
31
|
+
* tree to access files and directories.
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
class InMemoryTreeAccessors {
|
|
35
|
+
/**
|
|
36
|
+
* Protected constructor for derived classes.
|
|
37
|
+
* @param files - An array of {@link FileTree.IInMemoryFile | in-memory files} to include in the tree.
|
|
38
|
+
* @param params - Optional params for the tree.
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
constructor(files, params) {
|
|
42
|
+
var _a, _b;
|
|
43
|
+
this._tree = treeBuilder_1.TreeBuilder.create(params === null || params === void 0 ? void 0 : params.prefix).orThrow();
|
|
44
|
+
this._inferContentType = (_a = params === null || params === void 0 ? void 0 : params.inferContentType) !== null && _a !== void 0 ? _a : fileItem_1.FileItem.defaultInferContentType;
|
|
45
|
+
for (const file of files) {
|
|
46
|
+
const contentType = (_b = file.contentType) !== null && _b !== void 0 ? _b : this._inferContentType(file.path).orDefault();
|
|
47
|
+
this._tree.addFile(file.path, file.contents, contentType).orThrow();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Creates a new {@link FileTree.InMemoryTreeAccessors | InMemoryTreeAccessors} instance with the supplied
|
|
52
|
+
* in-memory files.
|
|
53
|
+
* @param files - An array of {@link FileTree.IInMemoryFile | in-memory files} to include in the tree.
|
|
54
|
+
* @param params - Optional params for the tree.
|
|
55
|
+
*/
|
|
56
|
+
static create(files, params) {
|
|
57
|
+
/* c8 ignore next 2 - tested but code coverage has intermittent issues */
|
|
58
|
+
params = typeof params === 'string' ? { prefix: params } : params;
|
|
59
|
+
return (0, ts_utils_1.captureResult)(() => new InMemoryTreeAccessors(files, params));
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.resolveAbsolutePath}
|
|
63
|
+
*/
|
|
64
|
+
resolveAbsolutePath(...paths) {
|
|
65
|
+
const parts = paths[0].startsWith('/') ? paths : [this._tree.prefix, ...paths];
|
|
66
|
+
const joined = parts.flatMap((p) => p.split('/').filter((s) => s.length > 0)).join('/');
|
|
67
|
+
return `/${joined}`;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getExtension}
|
|
71
|
+
*/
|
|
72
|
+
getExtension(path) {
|
|
73
|
+
const parts = path.split('.');
|
|
74
|
+
if (parts.length === 1) {
|
|
75
|
+
return '';
|
|
76
|
+
}
|
|
77
|
+
return `.${parts.pop()}`;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getBaseName}
|
|
81
|
+
*/
|
|
82
|
+
getBaseName(path, suffix) {
|
|
83
|
+
var _a;
|
|
84
|
+
/* c8 ignore next 1 - ?? is defense in depth should never happen */
|
|
85
|
+
const base = (_a = path.split('/').pop()) !== null && _a !== void 0 ? _a : '';
|
|
86
|
+
if (suffix && base.endsWith(suffix)) {
|
|
87
|
+
return base.slice(0, base.length - suffix.length);
|
|
88
|
+
}
|
|
89
|
+
return base;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.joinPaths}
|
|
93
|
+
*/
|
|
94
|
+
joinPaths(...paths) {
|
|
95
|
+
return paths.join('/');
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getItem}
|
|
99
|
+
*/
|
|
100
|
+
getItem(itemPath) {
|
|
101
|
+
const existing = this._tree.byAbsolutePath.get(itemPath);
|
|
102
|
+
if (existing) {
|
|
103
|
+
if (existing instanceof treeBuilder_1.InMemoryFile) {
|
|
104
|
+
return fileItem_1.FileItem.create(existing.absolutePath, this);
|
|
105
|
+
}
|
|
106
|
+
else if (existing instanceof treeBuilder_1.InMemoryDirectory) {
|
|
107
|
+
return directoryItem_1.DirectoryItem.create(existing.absolutePath, this);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return (0, ts_utils_1.fail)(`${itemPath}: not found`);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getFileContents}
|
|
114
|
+
*/
|
|
115
|
+
getFileContents(path) {
|
|
116
|
+
const item = this._tree.byAbsolutePath.get(path);
|
|
117
|
+
if (item === undefined) {
|
|
118
|
+
return (0, ts_utils_1.fail)(`${path}: not found`);
|
|
119
|
+
}
|
|
120
|
+
/* c8 ignore next 3 - local coverage is 100% but build coverage has intermittent issues */
|
|
121
|
+
if (!(item instanceof treeBuilder_1.InMemoryFile)) {
|
|
122
|
+
return (0, ts_utils_1.fail)(`${path}: not a file`);
|
|
123
|
+
}
|
|
124
|
+
// if the body is a string we don't want to add quotes
|
|
125
|
+
if (typeof item.contents === 'string') {
|
|
126
|
+
return (0, ts_utils_1.succeed)(item.contents);
|
|
127
|
+
}
|
|
128
|
+
/* c8 ignore next 2 - local coverage is 100% but build coverage has intermittent issues */
|
|
129
|
+
return (0, ts_utils_1.captureResult)(() => JSON.stringify(item.contents));
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getFileContentType}
|
|
133
|
+
*/
|
|
134
|
+
getFileContentType(path, provided) {
|
|
135
|
+
// If provided contentType is given, use it directly (highest priority)
|
|
136
|
+
if (provided !== undefined) {
|
|
137
|
+
return (0, ts_utils_1.succeed)(provided);
|
|
138
|
+
}
|
|
139
|
+
const item = this._tree.byAbsolutePath.get(path);
|
|
140
|
+
if (item === undefined) {
|
|
141
|
+
// If file doesn't exist, still try to infer content type from path
|
|
142
|
+
return this._inferContentType(path);
|
|
143
|
+
}
|
|
144
|
+
if (!(item instanceof treeBuilder_1.InMemoryFile)) {
|
|
145
|
+
// For directories, return undefined
|
|
146
|
+
return (0, ts_utils_1.succeed)(undefined);
|
|
147
|
+
}
|
|
148
|
+
// Return stored contentType if it exists, otherwise infer
|
|
149
|
+
if (item.contentType !== undefined) {
|
|
150
|
+
return (0, ts_utils_1.succeed)(item.contentType);
|
|
151
|
+
}
|
|
152
|
+
return this._inferContentType(path);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getChildren}
|
|
156
|
+
*/
|
|
157
|
+
getChildren(path) {
|
|
158
|
+
const item = this._tree.byAbsolutePath.get(path);
|
|
159
|
+
if (item === undefined) {
|
|
160
|
+
return (0, ts_utils_1.fail)(`${path}: not found`);
|
|
161
|
+
}
|
|
162
|
+
/* c8 ignore next 3 - local coverage is 100% but build coverage has intermittent issues */
|
|
163
|
+
if (!(item instanceof treeBuilder_1.InMemoryDirectory)) {
|
|
164
|
+
return (0, ts_utils_1.fail)(`${path}: not a directory`);
|
|
165
|
+
}
|
|
166
|
+
return (0, ts_utils_1.captureResult)(() => {
|
|
167
|
+
const children = [];
|
|
168
|
+
for (const child of item.children.values()) {
|
|
169
|
+
if (child instanceof treeBuilder_1.InMemoryFile) {
|
|
170
|
+
children.push(fileItem_1.FileItem.create(child.absolutePath, this).orThrow());
|
|
171
|
+
}
|
|
172
|
+
else if (child instanceof treeBuilder_1.InMemoryDirectory) {
|
|
173
|
+
children.push(directoryItem_1.DirectoryItem.create(child.absolutePath, this).orThrow());
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return children;
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
exports.InMemoryTreeAccessors = InMemoryTreeAccessors;
|
|
181
|
+
//# sourceMappingURL=inMemoryTree.js.map
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
35
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
__exportStar(require("./inMemoryTree"), exports);
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Result } from '@fgv/ts-utils';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a file in an in-memory file tree.
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export declare class InMemoryFile<TCT extends string = string> {
|
|
7
|
+
/**
|
|
8
|
+
* The absolute path of the file.
|
|
9
|
+
*/
|
|
10
|
+
readonly absolutePath: string;
|
|
11
|
+
/**
|
|
12
|
+
* The contents of the file.
|
|
13
|
+
*/
|
|
14
|
+
readonly contents: unknown;
|
|
15
|
+
/**
|
|
16
|
+
* The content type of the file.
|
|
17
|
+
*/
|
|
18
|
+
readonly contentType?: TCT;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a new {@link FileTree.InMemoryFile | InMemoryFile} instance.
|
|
21
|
+
* @param absolutePath - The absolute path of the file.
|
|
22
|
+
* @param contents - The contents of the file.
|
|
23
|
+
* @param contentType - Optional content type of the file.
|
|
24
|
+
*/
|
|
25
|
+
constructor(absolutePath: string, contents: unknown, contentType?: TCT);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Represents a directory in an in-memory file tree.
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
export declare class InMemoryDirectory<TCT extends string = string> {
|
|
32
|
+
/**
|
|
33
|
+
* The absolute path of the directory.
|
|
34
|
+
*/
|
|
35
|
+
readonly absolutePath: string;
|
|
36
|
+
protected _children: Map<string, InMemoryDirectory<TCT> | InMemoryFile<TCT>>;
|
|
37
|
+
/**
|
|
38
|
+
* The children of the directory.
|
|
39
|
+
*/
|
|
40
|
+
get children(): ReadonlyMap<string, InMemoryDirectory<TCT> | InMemoryFile<TCT>>;
|
|
41
|
+
/**
|
|
42
|
+
* Creates an empty new {@link FileTree.InMemoryDirectory | InMemoryDirectory} instance.
|
|
43
|
+
* @param absolutePath - The absolute path of the directory.
|
|
44
|
+
*/
|
|
45
|
+
constructor(absolutePath: string);
|
|
46
|
+
/**
|
|
47
|
+
* Gets or adds a child directory with the specified name.
|
|
48
|
+
* @param name - The name of the child directory.
|
|
49
|
+
* @returns `Success` with the child directory if successful, or
|
|
50
|
+
* `Failure` with an error message otherwise.
|
|
51
|
+
*/
|
|
52
|
+
getOrAddDirectory(name: string): Result<InMemoryDirectory<TCT>>;
|
|
53
|
+
/**
|
|
54
|
+
* Adds a file to the directory.
|
|
55
|
+
* @param name - The name of the file.
|
|
56
|
+
* @param contents - The contents of the file.
|
|
57
|
+
* @param contentType - Optional content type of the file.
|
|
58
|
+
* @returns `Success` with the new file if successful, or
|
|
59
|
+
* `Failure` with an error message otherwise.
|
|
60
|
+
*/
|
|
61
|
+
addFile(name: string, contents: unknown, contentType?: TCT): Result<InMemoryFile<TCT>>;
|
|
62
|
+
/**
|
|
63
|
+
* Gets the absolute path for a child of this directory with the supplied
|
|
64
|
+
* name.
|
|
65
|
+
* @param name - The name of the child.
|
|
66
|
+
* @returns `Success` with the absolute path if successful, or
|
|
67
|
+
* `Failure` with an error message otherwise.
|
|
68
|
+
*/
|
|
69
|
+
getChildPath(name: string): string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Helper class to build an in-memory file tree.
|
|
73
|
+
* @public
|
|
74
|
+
*/
|
|
75
|
+
export declare class TreeBuilder<TCT extends string = string> {
|
|
76
|
+
/**
|
|
77
|
+
* The prefix for all paths in the tree.
|
|
78
|
+
*/
|
|
79
|
+
readonly prefix: string;
|
|
80
|
+
/**
|
|
81
|
+
* The root directory of the tree.
|
|
82
|
+
*/
|
|
83
|
+
readonly root: InMemoryDirectory<TCT>;
|
|
84
|
+
/**
|
|
85
|
+
* A map of all directories and files in the tree by absolute path.
|
|
86
|
+
*/
|
|
87
|
+
readonly byAbsolutePath: Map<string, InMemoryDirectory<TCT> | InMemoryFile<TCT>>;
|
|
88
|
+
/**
|
|
89
|
+
* Protected constructor for derived classes.
|
|
90
|
+
* @param prefix - The prefix for all paths in the tree.
|
|
91
|
+
* @public
|
|
92
|
+
*/
|
|
93
|
+
protected constructor(prefix?: string);
|
|
94
|
+
/**
|
|
95
|
+
* Creates a new {@link TreeBuilder} instance.
|
|
96
|
+
* @param prefix - The prefix for all paths in the tree.
|
|
97
|
+
* @returns `Success` with the new {@link TreeBuilder} instance if successful,
|
|
98
|
+
* or `Failure` with an error message otherwise.
|
|
99
|
+
* @public
|
|
100
|
+
*/
|
|
101
|
+
static create<TCT extends string = string>(prefix?: string): Result<TreeBuilder<TCT>>;
|
|
102
|
+
/**
|
|
103
|
+
* Adds a file to the tree.
|
|
104
|
+
* @param absolutePath - The absolute path of the file.
|
|
105
|
+
* @param contents - The contents of the file.
|
|
106
|
+
* @param contentType - The content type of the file.
|
|
107
|
+
* @returns `Success` with the new file if successful, or
|
|
108
|
+
* `Failure` with an error message otherwise.
|
|
109
|
+
* @public
|
|
110
|
+
*/
|
|
111
|
+
addFile(absolutePath: string, contents: unknown, contentType?: TCT): Result<InMemoryFile<TCT>>;
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=treeBuilder.d.ts.map
|
|
@@ -0,0 +1,179 @@
|
|
|
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.TreeBuilder = exports.InMemoryDirectory = exports.InMemoryFile = void 0;
|
|
25
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
26
|
+
/**
|
|
27
|
+
* Represents a file in an in-memory file tree.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
class InMemoryFile {
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new {@link FileTree.InMemoryFile | InMemoryFile} instance.
|
|
33
|
+
* @param absolutePath - The absolute path of the file.
|
|
34
|
+
* @param contents - The contents of the file.
|
|
35
|
+
* @param contentType - Optional content type of the file.
|
|
36
|
+
*/
|
|
37
|
+
constructor(absolutePath, contents, contentType) {
|
|
38
|
+
this.absolutePath = absolutePath;
|
|
39
|
+
this.contents = contents;
|
|
40
|
+
this.contentType = contentType;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.InMemoryFile = InMemoryFile;
|
|
44
|
+
/**
|
|
45
|
+
* Represents a directory in an in-memory file tree.
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
class InMemoryDirectory {
|
|
49
|
+
/**
|
|
50
|
+
* The children of the directory.
|
|
51
|
+
*/
|
|
52
|
+
get children() {
|
|
53
|
+
return this._children;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Creates an empty new {@link FileTree.InMemoryDirectory | InMemoryDirectory} instance.
|
|
57
|
+
* @param absolutePath - The absolute path of the directory.
|
|
58
|
+
*/
|
|
59
|
+
constructor(absolutePath) {
|
|
60
|
+
this.absolutePath = absolutePath;
|
|
61
|
+
this._children = new Map();
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Gets or adds a child directory with the specified name.
|
|
65
|
+
* @param name - The name of the child directory.
|
|
66
|
+
* @returns `Success` with the child directory if successful, or
|
|
67
|
+
* `Failure` with an error message otherwise.
|
|
68
|
+
*/
|
|
69
|
+
getOrAddDirectory(name) {
|
|
70
|
+
const existing = this._children.get(name);
|
|
71
|
+
if (existing) {
|
|
72
|
+
if (existing instanceof InMemoryDirectory) {
|
|
73
|
+
return (0, ts_utils_1.succeed)(existing);
|
|
74
|
+
}
|
|
75
|
+
return (0, ts_utils_1.fail)(`${name}: not a directory`);
|
|
76
|
+
}
|
|
77
|
+
const child = new InMemoryDirectory(this.getChildPath(name));
|
|
78
|
+
this._children.set(name, child);
|
|
79
|
+
return (0, ts_utils_1.succeed)(child);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Adds a file to the directory.
|
|
83
|
+
* @param name - The name of the file.
|
|
84
|
+
* @param contents - The contents of the file.
|
|
85
|
+
* @param contentType - Optional content type of the file.
|
|
86
|
+
* @returns `Success` with the new file if successful, or
|
|
87
|
+
* `Failure` with an error message otherwise.
|
|
88
|
+
*/
|
|
89
|
+
addFile(name, contents, contentType) {
|
|
90
|
+
if (this._children.has(name)) {
|
|
91
|
+
return (0, ts_utils_1.fail)(`${name}: already exists`);
|
|
92
|
+
}
|
|
93
|
+
const child = new InMemoryFile(this.getChildPath(name), contents, contentType);
|
|
94
|
+
this._children.set(name, child);
|
|
95
|
+
return (0, ts_utils_1.succeed)(child);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Gets the absolute path for a child of this directory with the supplied
|
|
99
|
+
* name.
|
|
100
|
+
* @param name - The name of the child.
|
|
101
|
+
* @returns `Success` with the absolute path if successful, or
|
|
102
|
+
* `Failure` with an error message otherwise.
|
|
103
|
+
*/
|
|
104
|
+
getChildPath(name) {
|
|
105
|
+
if (this.absolutePath === '/') {
|
|
106
|
+
return `/${name}`;
|
|
107
|
+
}
|
|
108
|
+
return [this.absolutePath, name].join('/');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.InMemoryDirectory = InMemoryDirectory;
|
|
112
|
+
/**
|
|
113
|
+
* Helper class to build an in-memory file tree.
|
|
114
|
+
* @public
|
|
115
|
+
*/
|
|
116
|
+
class TreeBuilder {
|
|
117
|
+
/**
|
|
118
|
+
* Protected constructor for derived classes.
|
|
119
|
+
* @param prefix - The prefix for all paths in the tree.
|
|
120
|
+
* @public
|
|
121
|
+
*/
|
|
122
|
+
constructor(prefix) {
|
|
123
|
+
this.prefix = prefix !== null && prefix !== void 0 ? prefix : '/';
|
|
124
|
+
if (!this.prefix.startsWith('/')) {
|
|
125
|
+
throw new Error(`${prefix}: not an absolute path`);
|
|
126
|
+
}
|
|
127
|
+
// Normalize the prefix to remove trailing slashes (except for root)
|
|
128
|
+
/* c8 ignore next 3 - tested but code coverage has intermittent issues */
|
|
129
|
+
if (this.prefix !== '/' && this.prefix.endsWith('/')) {
|
|
130
|
+
this.prefix = this.prefix.slice(0, -1);
|
|
131
|
+
}
|
|
132
|
+
this.root = new InMemoryDirectory(this.prefix);
|
|
133
|
+
this.byAbsolutePath = new Map();
|
|
134
|
+
this.byAbsolutePath.set(this.prefix, this.root);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Creates a new {@link TreeBuilder} instance.
|
|
138
|
+
* @param prefix - The prefix for all paths in the tree.
|
|
139
|
+
* @returns `Success` with the new {@link TreeBuilder} instance if successful,
|
|
140
|
+
* or `Failure` with an error message otherwise.
|
|
141
|
+
* @public
|
|
142
|
+
*/
|
|
143
|
+
static create(prefix) {
|
|
144
|
+
return (0, ts_utils_1.captureResult)(() => new TreeBuilder(prefix));
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Adds a file to the tree.
|
|
148
|
+
* @param absolutePath - The absolute path of the file.
|
|
149
|
+
* @param contents - The contents of the file.
|
|
150
|
+
* @param contentType - The content type of the file.
|
|
151
|
+
* @returns `Success` with the new file if successful, or
|
|
152
|
+
* `Failure` with an error message otherwise.
|
|
153
|
+
* @public
|
|
154
|
+
*/
|
|
155
|
+
addFile(absolutePath, contents, contentType) {
|
|
156
|
+
const parts = absolutePath.split('/').filter((p) => p.length > 0);
|
|
157
|
+
if (parts.length === 0) {
|
|
158
|
+
return (0, ts_utils_1.fail)(`${absolutePath}: invalid file path`);
|
|
159
|
+
}
|
|
160
|
+
let dir = this.root;
|
|
161
|
+
while (parts.length > 1) {
|
|
162
|
+
const part = parts.shift();
|
|
163
|
+
const result = dir.getOrAddDirectory(part);
|
|
164
|
+
if (result.isFailure()) {
|
|
165
|
+
return (0, ts_utils_1.fail)(result.message);
|
|
166
|
+
}
|
|
167
|
+
dir = result.value;
|
|
168
|
+
if (!this.byAbsolutePath.has(dir.absolutePath)) {
|
|
169
|
+
this.byAbsolutePath.set(dir.absolutePath, dir);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return dir.addFile(parts[0], contents, contentType).onSuccess((file) => {
|
|
173
|
+
this.byAbsolutePath.set(file.absolutePath, file);
|
|
174
|
+
return (0, ts_utils_1.succeed)(file);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
exports.TreeBuilder = TreeBuilder;
|
|
179
|
+
//# sourceMappingURL=treeBuilder.js.map
|
|
@@ -0,0 +1,49 @@
|
|
|
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
|
+
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
35
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
// Browser-safe FileTree exports - excludes Node.js filesystem dependencies
|
|
39
|
+
// Export core interfaces and classes
|
|
40
|
+
__exportStar(require("./fileTreeAccessors"), exports);
|
|
41
|
+
__exportStar(require("./fileTree"), exports);
|
|
42
|
+
__exportStar(require("./directoryItem"), exports);
|
|
43
|
+
__exportStar(require("./fileItem"), exports);
|
|
44
|
+
// Export in-memory implementations for web compatibility
|
|
45
|
+
__exportStar(require("./in-memory"), exports);
|
|
46
|
+
// Exclude:
|
|
47
|
+
// - fsTree (requires Node.js fs/path)
|
|
48
|
+
// - fileTreeHelpers (imports fsTree)
|
|
49
|
+
//# sourceMappingURL=index.browser.js.map
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
35
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
// Export core interfaces and classes
|
|
39
|
+
__exportStar(require("./fileTreeAccessors"), exports);
|
|
40
|
+
__exportStar(require("./fileTree"), exports);
|
|
41
|
+
__exportStar(require("./directoryItem"), exports);
|
|
42
|
+
__exportStar(require("./fileItem"), exports);
|
|
43
|
+
// Export tree-shakeable helpers (filesystem ones will be shaken out if not used)
|
|
44
|
+
__exportStar(require("./fileTreeHelpers"), exports);
|
|
45
|
+
// Export in-memory implementations for web compatibility
|
|
46
|
+
__exportStar(require("./in-memory"), exports);
|
|
47
|
+
// Note: FsFileTreeAccessors is now only imported by fileTreeHelpers.ts
|
|
48
|
+
// Web apps that don't use forFilesystem() won't bundle fs/path dependencies
|
|
49
|
+
__exportStar(require("./fsTree"), exports);
|
|
50
|
+
//# sourceMappingURL=index.js.map
|