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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/index.browser.js +31 -0
  2. package/dist/index.js +29 -0
  3. package/dist/packlets/converters/converters.js +187 -0
  4. package/dist/packlets/converters/index.js +23 -0
  5. package/dist/packlets/file-tree/directoryItem.js +67 -0
  6. package/dist/packlets/file-tree/fileItem.js +126 -0
  7. package/dist/packlets/file-tree/fileTree.js +85 -0
  8. package/dist/packlets/file-tree/fileTreeAccessors.js +23 -0
  9. package/dist/packlets/file-tree/fileTreeHelpers.inMemory.js +28 -0
  10. package/dist/packlets/file-tree/fileTreeHelpers.js +29 -0
  11. package/dist/packlets/file-tree/fsTree.js +122 -0
  12. package/dist/packlets/file-tree/in-memory/inMemoryTree.js +177 -0
  13. package/dist/packlets/file-tree/in-memory/index.js +23 -0
  14. package/dist/packlets/file-tree/in-memory/treeBuilder.js +173 -0
  15. package/dist/packlets/file-tree/index.browser.js +34 -0
  16. package/dist/packlets/file-tree/index.js +35 -0
  17. package/dist/packlets/json/common.js +145 -0
  18. package/dist/packlets/json/index.js +23 -0
  19. package/dist/packlets/json-compatible/common.js +23 -0
  20. package/dist/packlets/json-compatible/converters.js +90 -0
  21. package/dist/packlets/json-compatible/index.js +26 -0
  22. package/dist/packlets/json-compatible/validators.js +54 -0
  23. package/dist/packlets/json-file/file.js +74 -0
  24. package/dist/packlets/json-file/index.browser.js +30 -0
  25. package/dist/packlets/json-file/index.js +30 -0
  26. package/dist/packlets/json-file/jsonFsHelper.js +166 -0
  27. package/dist/packlets/json-file/jsonLike.js +26 -0
  28. package/dist/packlets/json-file/jsonTreeHelper.js +116 -0
  29. package/dist/packlets/validators/index.js +23 -0
  30. package/dist/packlets/validators/validators.js +179 -0
  31. package/dist/test/fixtures/file-tree/config.json +1 -0
  32. package/dist/test/fixtures/file-tree/data/items.json +1 -0
  33. package/dist/test/fixtures/file-tree/docs/api/reference.json +1 -0
  34. package/dist/test/unit/data/file/bad/bad3.json +3 -0
  35. package/dist/test/unit/data/file/bad/thing1.json +4 -0
  36. package/dist/test/unit/data/file/bad/thing2.json +3 -0
  37. package/dist/test/unit/data/file/good/thing1.json +4 -0
  38. package/dist/test/unit/data/file/good/thing2.json +3 -0
  39. package/dist/test/unit/json-compatible/helpers.js +32 -0
  40. package/dist/tsdoc-metadata.json +1 -1
  41. package/lib/packlets/json-file/index.browser.d.ts +1 -1
  42. package/lib/packlets/json-file/index.browser.js +2 -2
  43. package/package.json +7 -5
@@ -0,0 +1,122 @@
1
+ /*
2
+ * Copyright (c) 2025 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ import path from 'path';
23
+ import fs from 'fs';
24
+ import { captureResult, succeed } from '@fgv/ts-utils';
25
+ import { DirectoryItem } from './directoryItem';
26
+ import { FileItem } from './fileItem';
27
+ /**
28
+ * Implementation of {@link FileTree.IFileTreeAccessors} that uses the
29
+ * file system to access files and directories.
30
+ * @public
31
+ */
32
+ export class FsFileTreeAccessors {
33
+ /**
34
+ * Construct a new instance of the {@link FileTree.FsFileTreeAccessors | FsFileTreeAccessors} class.
35
+ * @param params - Optional {@link FileTree.IFileTreeInitParams | initialization parameters}.
36
+ * @public
37
+ */
38
+ constructor(params) {
39
+ var _a;
40
+ this.prefix = params === null || params === void 0 ? void 0 : params.prefix;
41
+ this._inferContentType = (_a = params === null || params === void 0 ? void 0 : params.inferContentType) !== null && _a !== void 0 ? _a : FileItem.defaultInferContentType;
42
+ }
43
+ /**
44
+ * {@inheritdoc FileTree.IFileTreeAccessors.resolveAbsolutePath}
45
+ */
46
+ resolveAbsolutePath(...paths) {
47
+ if (this.prefix && !path.isAbsolute(paths[0])) {
48
+ return path.resolve(this.prefix, ...paths);
49
+ }
50
+ return path.resolve(...paths);
51
+ }
52
+ /**
53
+ * {@inheritdoc FileTree.IFileTreeAccessors.getExtension}
54
+ */
55
+ getExtension(itemPath) {
56
+ return path.extname(itemPath);
57
+ }
58
+ /**
59
+ * {@inheritdoc FileTree.IFileTreeAccessors.getBaseName}
60
+ */
61
+ getBaseName(itemPath, suffix) {
62
+ return path.basename(itemPath, suffix);
63
+ }
64
+ /**
65
+ * {@inheritdoc FileTree.IFileTreeAccessors.joinPaths}
66
+ */
67
+ joinPaths(...paths) {
68
+ return path.join(...paths);
69
+ }
70
+ /**
71
+ * {@inheritdoc FileTree.IFileTreeAccessors.getItem}
72
+ */
73
+ getItem(itemPath) {
74
+ return captureResult(() => {
75
+ const stat = fs.statSync(this.resolveAbsolutePath(itemPath));
76
+ if (stat.isDirectory()) {
77
+ return DirectoryItem.create(itemPath, this).orThrow();
78
+ }
79
+ else if (stat.isFile()) {
80
+ return FileItem.create(itemPath, this).orThrow();
81
+ }
82
+ /* c8 ignore next 1 - defensive coding: filesystem items should be file or directory */
83
+ throw new Error(`${itemPath}: not a file or directory`);
84
+ });
85
+ }
86
+ /**
87
+ * {@inheritdoc FileTree.IFileTreeAccessors.getFileContents}
88
+ */
89
+ getFileContents(filePath) {
90
+ return captureResult(() => fs.readFileSync(this.resolveAbsolutePath(filePath), 'utf8'));
91
+ }
92
+ /**
93
+ * {@inheritdoc FileTree.IFileTreeAccessors.getFileContentType}
94
+ */
95
+ getFileContentType(filePath, provided) {
96
+ if (provided !== undefined) {
97
+ return succeed(provided);
98
+ }
99
+ /* c8 ignore next 2 - coverage has intermittent issues in the build - local tests show coverage of this line */
100
+ return this._inferContentType(filePath);
101
+ }
102
+ /**
103
+ * {@inheritdoc FileTree.IFileTreeAccessors.getChildren}
104
+ */
105
+ getChildren(dirPath) {
106
+ return captureResult(() => {
107
+ const children = [];
108
+ const files = fs.readdirSync(this.resolveAbsolutePath(dirPath), { withFileTypes: true });
109
+ files.forEach((file) => {
110
+ const fullPath = this.resolveAbsolutePath(dirPath, file.name);
111
+ if (file.isDirectory()) {
112
+ children.push(DirectoryItem.create(fullPath, this).orThrow());
113
+ }
114
+ else if (file.isFile()) {
115
+ children.push(FileItem.create(fullPath, this).orThrow());
116
+ }
117
+ });
118
+ return children;
119
+ });
120
+ }
121
+ }
122
+ //# sourceMappingURL=fsTree.js.map
@@ -0,0 +1,177 @@
1
+ /*
2
+ * Copyright (c) 2025 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ import { captureResult, fail, succeed } from '@fgv/ts-utils';
23
+ import { DirectoryItem } from '../directoryItem';
24
+ import { FileItem } from '../fileItem';
25
+ import { InMemoryDirectory, InMemoryFile, TreeBuilder } from './treeBuilder';
26
+ /**
27
+ * Implementation of {@link FileTree.IFileTreeAccessors} that uses an in-memory
28
+ * tree to access files and directories.
29
+ * @public
30
+ */
31
+ export class InMemoryTreeAccessors {
32
+ /**
33
+ * Protected constructor for derived classes.
34
+ * @param files - An array of {@link FileTree.IInMemoryFile | in-memory files} to include in the tree.
35
+ * @param params - Optional params for the tree.
36
+ * @public
37
+ */
38
+ constructor(files, params) {
39
+ var _a, _b;
40
+ this._tree = TreeBuilder.create(params === null || params === void 0 ? void 0 : params.prefix).orThrow();
41
+ this._inferContentType = (_a = params === null || params === void 0 ? void 0 : params.inferContentType) !== null && _a !== void 0 ? _a : FileItem.defaultInferContentType;
42
+ for (const file of files) {
43
+ const contentType = (_b = file.contentType) !== null && _b !== void 0 ? _b : this._inferContentType(file.path).orDefault();
44
+ this._tree.addFile(file.path, file.contents, contentType).orThrow();
45
+ }
46
+ }
47
+ /**
48
+ * Creates a new {@link FileTree.InMemoryTreeAccessors | InMemoryTreeAccessors} instance with the supplied
49
+ * in-memory files.
50
+ * @param files - An array of {@link FileTree.IInMemoryFile | in-memory files} to include in the tree.
51
+ * @param params - Optional params for the tree.
52
+ */
53
+ static create(files, params) {
54
+ /* c8 ignore next 2 - tested but code coverage has intermittent issues */
55
+ params = typeof params === 'string' ? { prefix: params } : params;
56
+ return captureResult(() => new InMemoryTreeAccessors(files, params));
57
+ }
58
+ /**
59
+ * {@inheritdoc FileTree.IFileTreeAccessors.resolveAbsolutePath}
60
+ */
61
+ resolveAbsolutePath(...paths) {
62
+ const parts = paths[0].startsWith('/') ? paths : [this._tree.prefix, ...paths];
63
+ const joined = parts.flatMap((p) => p.split('/').filter((s) => s.length > 0)).join('/');
64
+ return `/${joined}`;
65
+ }
66
+ /**
67
+ * {@inheritdoc FileTree.IFileTreeAccessors.getExtension}
68
+ */
69
+ getExtension(path) {
70
+ const parts = path.split('.');
71
+ if (parts.length === 1) {
72
+ return '';
73
+ }
74
+ return `.${parts.pop()}`;
75
+ }
76
+ /**
77
+ * {@inheritdoc FileTree.IFileTreeAccessors.getBaseName}
78
+ */
79
+ getBaseName(path, suffix) {
80
+ var _a;
81
+ /* c8 ignore next 1 - ?? is defense in depth should never happen */
82
+ const base = (_a = path.split('/').pop()) !== null && _a !== void 0 ? _a : '';
83
+ if (suffix && base.endsWith(suffix)) {
84
+ return base.slice(0, base.length - suffix.length);
85
+ }
86
+ return base;
87
+ }
88
+ /**
89
+ * {@inheritdoc FileTree.IFileTreeAccessors.joinPaths}
90
+ */
91
+ joinPaths(...paths) {
92
+ return paths.join('/');
93
+ }
94
+ /**
95
+ * {@inheritdoc FileTree.IFileTreeAccessors.getItem}
96
+ */
97
+ getItem(itemPath) {
98
+ const existing = this._tree.byAbsolutePath.get(itemPath);
99
+ if (existing) {
100
+ if (existing instanceof InMemoryFile) {
101
+ return FileItem.create(existing.absolutePath, this);
102
+ }
103
+ else if (existing instanceof InMemoryDirectory) {
104
+ return DirectoryItem.create(existing.absolutePath, this);
105
+ }
106
+ }
107
+ return fail(`${itemPath}: not found`);
108
+ }
109
+ /**
110
+ * {@inheritdoc FileTree.IFileTreeAccessors.getFileContents}
111
+ */
112
+ getFileContents(path) {
113
+ const item = this._tree.byAbsolutePath.get(path);
114
+ if (item === undefined) {
115
+ return fail(`${path}: not found`);
116
+ }
117
+ /* c8 ignore next 3 - local coverage is 100% but build coverage has intermittent issues */
118
+ if (!(item instanceof InMemoryFile)) {
119
+ return fail(`${path}: not a file`);
120
+ }
121
+ // if the body is a string we don't want to add quotes
122
+ if (typeof item.contents === 'string') {
123
+ return succeed(item.contents);
124
+ }
125
+ /* c8 ignore next 2 - local coverage is 100% but build coverage has intermittent issues */
126
+ return captureResult(() => JSON.stringify(item.contents));
127
+ }
128
+ /**
129
+ * {@inheritdoc FileTree.IFileTreeAccessors.getFileContentType}
130
+ */
131
+ getFileContentType(path, provided) {
132
+ // If provided contentType is given, use it directly (highest priority)
133
+ if (provided !== undefined) {
134
+ return succeed(provided);
135
+ }
136
+ const item = this._tree.byAbsolutePath.get(path);
137
+ if (item === undefined) {
138
+ // If file doesn't exist, still try to infer content type from path
139
+ return this._inferContentType(path);
140
+ }
141
+ if (!(item instanceof InMemoryFile)) {
142
+ // For directories, return undefined
143
+ return succeed(undefined);
144
+ }
145
+ // Return stored contentType if it exists, otherwise infer
146
+ if (item.contentType !== undefined) {
147
+ return succeed(item.contentType);
148
+ }
149
+ return this._inferContentType(path);
150
+ }
151
+ /**
152
+ * {@inheritdoc FileTree.IFileTreeAccessors.getChildren}
153
+ */
154
+ getChildren(path) {
155
+ const item = this._tree.byAbsolutePath.get(path);
156
+ if (item === undefined) {
157
+ return fail(`${path}: not found`);
158
+ }
159
+ /* c8 ignore next 3 - local coverage is 100% but build coverage has intermittent issues */
160
+ if (!(item instanceof InMemoryDirectory)) {
161
+ return fail(`${path}: not a directory`);
162
+ }
163
+ return captureResult(() => {
164
+ const children = [];
165
+ for (const child of item.children.values()) {
166
+ if (child instanceof InMemoryFile) {
167
+ children.push(FileItem.create(child.absolutePath, this).orThrow());
168
+ }
169
+ else if (child instanceof InMemoryDirectory) {
170
+ children.push(DirectoryItem.create(child.absolutePath, this).orThrow());
171
+ }
172
+ }
173
+ return children;
174
+ });
175
+ }
176
+ }
177
+ //# sourceMappingURL=inMemoryTree.js.map
@@ -0,0 +1,23 @@
1
+ /*
2
+ * Copyright (c) 2025 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ export * from './inMemoryTree';
23
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,173 @@
1
+ /*
2
+ * Copyright (c) 2025 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ import { captureResult, fail, succeed } from '@fgv/ts-utils';
23
+ /**
24
+ * Represents a file in an in-memory file tree.
25
+ * @public
26
+ */
27
+ export class InMemoryFile {
28
+ /**
29
+ * Creates a new {@link FileTree.InMemoryFile | InMemoryFile} instance.
30
+ * @param absolutePath - The absolute path of the file.
31
+ * @param contents - The contents of the file.
32
+ * @param contentType - Optional content type of the file.
33
+ */
34
+ constructor(absolutePath, contents, contentType) {
35
+ this.absolutePath = absolutePath;
36
+ this.contents = contents;
37
+ this.contentType = contentType;
38
+ }
39
+ }
40
+ /**
41
+ * Represents a directory in an in-memory file tree.
42
+ * @public
43
+ */
44
+ export class InMemoryDirectory {
45
+ /**
46
+ * The children of the directory.
47
+ */
48
+ get children() {
49
+ return this._children;
50
+ }
51
+ /**
52
+ * Creates an empty new {@link FileTree.InMemoryDirectory | InMemoryDirectory} instance.
53
+ * @param absolutePath - The absolute path of the directory.
54
+ */
55
+ constructor(absolutePath) {
56
+ this.absolutePath = absolutePath;
57
+ this._children = new Map();
58
+ }
59
+ /**
60
+ * Gets or adds a child directory with the specified name.
61
+ * @param name - The name of the child directory.
62
+ * @returns `Success` with the child directory if successful, or
63
+ * `Failure` with an error message otherwise.
64
+ */
65
+ getOrAddDirectory(name) {
66
+ const existing = this._children.get(name);
67
+ if (existing) {
68
+ if (existing instanceof InMemoryDirectory) {
69
+ return succeed(existing);
70
+ }
71
+ return fail(`${name}: not a directory`);
72
+ }
73
+ const child = new InMemoryDirectory(this.getChildPath(name));
74
+ this._children.set(name, child);
75
+ return succeed(child);
76
+ }
77
+ /**
78
+ * Adds a file to the directory.
79
+ * @param name - The name of the file.
80
+ * @param contents - The contents of the file.
81
+ * @param contentType - Optional content type of the file.
82
+ * @returns `Success` with the new file if successful, or
83
+ * `Failure` with an error message otherwise.
84
+ */
85
+ addFile(name, contents, contentType) {
86
+ if (this._children.has(name)) {
87
+ return fail(`${name}: already exists`);
88
+ }
89
+ const child = new InMemoryFile(this.getChildPath(name), contents, contentType);
90
+ this._children.set(name, child);
91
+ return succeed(child);
92
+ }
93
+ /**
94
+ * Gets the absolute path for a child of this directory with the supplied
95
+ * name.
96
+ * @param name - The name of the child.
97
+ * @returns `Success` with the absolute path if successful, or
98
+ * `Failure` with an error message otherwise.
99
+ */
100
+ getChildPath(name) {
101
+ if (this.absolutePath === '/') {
102
+ return `/${name}`;
103
+ }
104
+ return [this.absolutePath, name].join('/');
105
+ }
106
+ }
107
+ /**
108
+ * Helper class to build an in-memory file tree.
109
+ * @public
110
+ */
111
+ export class TreeBuilder {
112
+ /**
113
+ * Protected constructor for derived classes.
114
+ * @param prefix - The prefix for all paths in the tree.
115
+ * @public
116
+ */
117
+ constructor(prefix) {
118
+ this.prefix = prefix !== null && prefix !== void 0 ? prefix : '/';
119
+ if (!this.prefix.startsWith('/')) {
120
+ throw new Error(`${prefix}: not an absolute path`);
121
+ }
122
+ // Normalize the prefix to remove trailing slashes (except for root)
123
+ /* c8 ignore next 3 - tested but code coverage has intermittent issues */
124
+ if (this.prefix !== '/' && this.prefix.endsWith('/')) {
125
+ this.prefix = this.prefix.slice(0, -1);
126
+ }
127
+ this.root = new InMemoryDirectory(this.prefix);
128
+ this.byAbsolutePath = new Map();
129
+ this.byAbsolutePath.set(this.prefix, this.root);
130
+ }
131
+ /**
132
+ * Creates a new {@link TreeBuilder} instance.
133
+ * @param prefix - The prefix for all paths in the tree.
134
+ * @returns `Success` with the new {@link TreeBuilder} instance if successful,
135
+ * or `Failure` with an error message otherwise.
136
+ * @public
137
+ */
138
+ static create(prefix) {
139
+ return captureResult(() => new TreeBuilder(prefix));
140
+ }
141
+ /**
142
+ * Adds a file to the tree.
143
+ * @param absolutePath - The absolute path of the file.
144
+ * @param contents - The contents of the file.
145
+ * @param contentType - The content type of the file.
146
+ * @returns `Success` with the new file if successful, or
147
+ * `Failure` with an error message otherwise.
148
+ * @public
149
+ */
150
+ addFile(absolutePath, contents, contentType) {
151
+ const parts = absolutePath.split('/').filter((p) => p.length > 0);
152
+ if (parts.length === 0) {
153
+ return fail(`${absolutePath}: invalid file path`);
154
+ }
155
+ let dir = this.root;
156
+ while (parts.length > 1) {
157
+ const part = parts.shift();
158
+ const result = dir.getOrAddDirectory(part);
159
+ if (result.isFailure()) {
160
+ return fail(result.message);
161
+ }
162
+ dir = result.value;
163
+ if (!this.byAbsolutePath.has(dir.absolutePath)) {
164
+ this.byAbsolutePath.set(dir.absolutePath, dir);
165
+ }
166
+ }
167
+ return dir.addFile(parts[0], contents, contentType).onSuccess((file) => {
168
+ this.byAbsolutePath.set(file.absolutePath, file);
169
+ return succeed(file);
170
+ });
171
+ }
172
+ }
173
+ //# sourceMappingURL=treeBuilder.js.map
@@ -0,0 +1,34 @@
1
+ /*
2
+ * Copyright (c) 2025 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ // Browser-safe FileTree exports - excludes Node.js filesystem dependencies
23
+ // Export core interfaces and classes
24
+ export * from './fileTreeAccessors';
25
+ export * from './fileTree';
26
+ export * from './directoryItem';
27
+ export * from './fileItem';
28
+ // Export in-memory implementations for web compatibility
29
+ export * from './in-memory';
30
+ export { inMemory } from './fileTreeHelpers.inMemory';
31
+ // Exclude:
32
+ // - fsTree (requires Node.js fs/path)
33
+ // - fileTreeHelpers (imports fsTree)
34
+ //# sourceMappingURL=index.browser.js.map
@@ -0,0 +1,35 @@
1
+ /*
2
+ * Copyright (c) 2025 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+ // Export core interfaces and classes
23
+ export * from './fileTreeAccessors';
24
+ export * from './fileTree';
25
+ export * from './directoryItem';
26
+ export * from './fileItem';
27
+ // Export tree-shakeable helpers (filesystem ones will be shaken out if not used)
28
+ export * from './fileTreeHelpers';
29
+ export { inMemory } from './fileTreeHelpers.inMemory';
30
+ // Export in-memory implementations for web compatibility
31
+ export * from './in-memory';
32
+ // Note: FsFileTreeAccessors is now only imported by fileTreeHelpers.ts
33
+ // Web apps that don't use forFilesystem() won't bundle fs/path dependencies
34
+ export * from './fsTree';
35
+ //# sourceMappingURL=index.js.map