@fgv/ts-utils 4.3.0 → 4.5.0-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 +6 -0
- package/CHANGELOG.md +6 -1
- package/dist/ts-utils.d.ts +4248 -3531
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/index.d.ts +3 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +7 -3
- package/lib/index.js.map +1 -1
- package/lib/packlets/base/utils.d.ts.map +1 -1
- package/lib/packlets/base/utils.js +0 -1
- package/lib/packlets/base/utils.js.map +1 -1
- package/lib/packlets/collections/collectible.d.ts +82 -8
- package/lib/packlets/collections/collectible.d.ts.map +1 -1
- package/lib/packlets/collections/collectible.js +36 -11
- package/lib/packlets/collections/collectible.js.map +1 -1
- package/lib/packlets/collections/collector.d.ts +78 -79
- package/lib/packlets/collections/collector.d.ts.map +1 -1
- package/lib/packlets/collections/collector.js +83 -52
- package/lib/packlets/collections/collector.js.map +1 -1
- package/lib/packlets/collections/collectorValidator.d.ts +89 -0
- package/lib/packlets/collections/collectorValidator.d.ts.map +1 -0
- package/lib/packlets/collections/collectorValidator.js +98 -0
- package/lib/packlets/collections/collectorValidator.js.map +1 -0
- package/lib/packlets/collections/convertingCollector.d.ts +77 -36
- package/lib/packlets/collections/convertingCollector.d.ts.map +1 -1
- package/lib/packlets/collections/convertingCollector.js +71 -26
- package/lib/packlets/collections/convertingCollector.js.map +1 -1
- package/lib/packlets/collections/convertingCollectorValidator.d.ts +69 -0
- package/lib/packlets/collections/convertingCollectorValidator.d.ts.map +1 -0
- package/lib/packlets/collections/{collectorConverter.js → convertingCollectorValidator.js} +28 -15
- package/lib/packlets/collections/convertingCollectorValidator.js.map +1 -0
- package/lib/packlets/collections/index.d.ts +7 -5
- package/lib/packlets/collections/index.d.ts.map +1 -1
- package/lib/packlets/collections/index.js +7 -5
- package/lib/packlets/collections/index.js.map +1 -1
- package/lib/packlets/collections/keyValueConverters.d.ts +1 -17
- package/lib/packlets/collections/keyValueConverters.d.ts.map +1 -1
- package/lib/packlets/collections/keyValueConverters.js +1 -8
- package/lib/packlets/collections/keyValueConverters.js.map +1 -1
- package/lib/packlets/collections/readonlyResultMap.d.ts +0 -4
- package/lib/packlets/collections/readonlyResultMap.d.ts.map +1 -1
- package/lib/packlets/collections/readonlyResultMap.js.map +1 -1
- package/lib/packlets/collections/resultMap.d.ts +0 -4
- package/lib/packlets/collections/resultMap.d.ts.map +1 -1
- package/lib/packlets/collections/resultMap.js +0 -6
- package/lib/packlets/collections/resultMap.js.map +1 -1
- package/lib/packlets/collections/{resultMapConverter.d.ts → resultMapValidator.d.ts} +11 -11
- package/lib/packlets/collections/{resultMapConverter.d.ts.map → resultMapValidator.d.ts.map} +1 -1
- package/lib/packlets/collections/{resultMapConverter.js → resultMapValidator.js} +6 -6
- package/lib/packlets/collections/{resultMapConverter.js.map → resultMapValidator.js.map} +1 -1
- package/lib/packlets/collections/validatingCollector.d.ts +69 -0
- package/lib/packlets/collections/validatingCollector.d.ts.map +1 -0
- package/lib/packlets/collections/validatingCollector.js +67 -0
- package/lib/packlets/collections/validatingCollector.js.map +1 -0
- package/lib/packlets/collections/validatingConvertingCollector.d.ts +59 -0
- package/lib/packlets/collections/validatingConvertingCollector.d.ts.map +1 -0
- package/lib/packlets/collections/validatingConvertingCollector.js +68 -0
- package/lib/packlets/collections/validatingConvertingCollector.js.map +1 -0
- package/lib/packlets/collections/validatingResultMap.d.ts +53 -0
- package/lib/packlets/collections/validatingResultMap.d.ts.map +1 -0
- package/lib/packlets/collections/validatingResultMap.js +61 -0
- package/lib/packlets/collections/validatingResultMap.js.map +1 -0
- package/lib/packlets/file-tree/directoryItem.d.ts +47 -0
- package/lib/packlets/file-tree/directoryItem.d.ts.map +1 -0
- package/lib/packlets/file-tree/directoryItem.js +71 -0
- package/lib/packlets/file-tree/directoryItem.js.map +1 -0
- package/lib/packlets/file-tree/fileItem.d.ts +64 -0
- package/lib/packlets/file-tree/fileItem.d.ts.map +1 -0
- package/lib/packlets/file-tree/fileItem.js +93 -0
- package/lib/packlets/file-tree/fileItem.js.map +1 -0
- package/lib/packlets/file-tree/fileTree.d.ts +84 -0
- package/lib/packlets/file-tree/fileTree.d.ts.map +1 -0
- package/lib/packlets/file-tree/fileTree.js +135 -0
- package/lib/packlets/file-tree/fileTree.js.map +1 -0
- package/lib/packlets/file-tree/fileTreeAccessors.d.ts +134 -0
- package/lib/packlets/file-tree/fileTreeAccessors.d.ts.map +1 -0
- package/lib/packlets/file-tree/fileTreeAccessors.js +24 -0
- package/lib/packlets/file-tree/fileTreeAccessors.js.map +1 -0
- package/lib/packlets/file-tree/fsTree.d.ts +45 -0
- package/lib/packlets/file-tree/fsTree.d.ts.map +1 -0
- package/lib/packlets/file-tree/fsTree.js +116 -0
- package/lib/packlets/file-tree/fsTree.js.map +1 -0
- package/lib/packlets/file-tree/in-memory/inMemoryTree.d.ts +67 -0
- package/lib/packlets/file-tree/in-memory/inMemoryTree.d.ts.map +1 -0
- package/lib/packlets/file-tree/in-memory/inMemoryTree.js +150 -0
- package/lib/packlets/file-tree/in-memory/inMemoryTree.js.map +1 -0
- package/lib/packlets/file-tree/in-memory/index.d.ts +2 -0
- package/lib/packlets/file-tree/in-memory/index.d.ts.map +1 -0
- package/lib/packlets/file-tree/in-memory/index.js +39 -0
- package/lib/packlets/file-tree/in-memory/index.js.map +1 -0
- package/lib/packlets/file-tree/in-memory/treeBuilder.d.ts +106 -0
- package/lib/packlets/file-tree/in-memory/treeBuilder.d.ts.map +1 -0
- package/lib/packlets/file-tree/in-memory/treeBuilder.js +170 -0
- package/lib/packlets/file-tree/in-memory/treeBuilder.js.map +1 -0
- package/lib/packlets/file-tree/index.d.ts +8 -0
- package/lib/packlets/file-tree/index.d.ts.map +1 -0
- package/lib/packlets/file-tree/index.js +46 -0
- package/lib/packlets/file-tree/index.js.map +1 -0
- package/lib/packlets/hash/crcNormalizer.js +1 -1
- package/lib/packlets/hash/crcNormalizer.js.map +1 -1
- package/package.json +15 -15
- package/lib/packlets/collections/collectorConverter.d.ts +0 -59
- package/lib/packlets/collections/collectorConverter.d.ts.map +0 -1
- package/lib/packlets/collections/collectorConverter.js.map +0 -1
- package/lib/packlets/collections/collectors.d.ts +0 -32
- package/lib/packlets/collections/collectors.d.ts.map +0 -1
- package/lib/packlets/collections/collectors.js +0 -57
- package/lib/packlets/collections/collectors.js.map +0 -1
- package/lib/packlets/collections/convertingResultMap.d.ts +0 -73
- package/lib/packlets/collections/convertingResultMap.d.ts.map +0 -1
- package/lib/packlets/collections/convertingResultMap.js +0 -102
- package/lib/packlets/collections/convertingResultMap.js.map +0 -1
|
@@ -0,0 +1,116 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
24
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
25
|
+
};
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.FsFileTreeAccessors = void 0;
|
|
28
|
+
const path_1 = __importDefault(require("path"));
|
|
29
|
+
const fs_1 = __importDefault(require("fs"));
|
|
30
|
+
const base_1 = require("../base");
|
|
31
|
+
const directoryItem_1 = require("./directoryItem");
|
|
32
|
+
const fileItem_1 = require("./fileItem");
|
|
33
|
+
/**
|
|
34
|
+
* Implementation of {@link FileTree.IFileTreeAccessors | IFileTreeAccessors} that uses the
|
|
35
|
+
* file system to access files and directories.
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
class FsFileTreeAccessors {
|
|
39
|
+
/**
|
|
40
|
+
* Protected constructor for derived classes.
|
|
41
|
+
* @param prefix - Optional prefix for the tree.
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
constructor(prefix) {
|
|
45
|
+
this.prefix = prefix;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.resolveAbsolutePath}
|
|
49
|
+
*/
|
|
50
|
+
resolveAbsolutePath(...paths) {
|
|
51
|
+
if (this.prefix && !path_1.default.isAbsolute(paths[0])) {
|
|
52
|
+
return path_1.default.resolve(this.prefix, ...paths);
|
|
53
|
+
}
|
|
54
|
+
return path_1.default.resolve(...paths);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.getExtension}
|
|
58
|
+
*/
|
|
59
|
+
getExtension(itemPath) {
|
|
60
|
+
return path_1.default.extname(itemPath);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.getBaseName}
|
|
64
|
+
*/
|
|
65
|
+
getBaseName(itemPath, suffix) {
|
|
66
|
+
return path_1.default.basename(itemPath, suffix);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.joinPaths}
|
|
70
|
+
*/
|
|
71
|
+
joinPaths(...paths) {
|
|
72
|
+
return path_1.default.join(...paths);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.getItem}
|
|
76
|
+
*/
|
|
77
|
+
getItem(itemPath) {
|
|
78
|
+
return (0, base_1.captureResult)(() => {
|
|
79
|
+
const stat = fs_1.default.statSync(this.resolveAbsolutePath(itemPath));
|
|
80
|
+
if (stat.isDirectory()) {
|
|
81
|
+
return directoryItem_1.DirectoryItem.create(itemPath, this).orThrow();
|
|
82
|
+
}
|
|
83
|
+
else if (stat.isFile()) {
|
|
84
|
+
return fileItem_1.FileItem.create(itemPath, this).orThrow();
|
|
85
|
+
}
|
|
86
|
+
throw new Error(`${itemPath}: not a file or directory`);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.getFileContents}
|
|
91
|
+
*/
|
|
92
|
+
getFileContents(filePath) {
|
|
93
|
+
return (0, base_1.captureResult)(() => fs_1.default.readFileSync(this.resolveAbsolutePath(filePath), 'utf8'));
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.getChildren}
|
|
97
|
+
*/
|
|
98
|
+
getChildren(dirPath) {
|
|
99
|
+
return (0, base_1.captureResult)(() => {
|
|
100
|
+
const children = [];
|
|
101
|
+
const files = fs_1.default.readdirSync(this.resolveAbsolutePath(dirPath), { withFileTypes: true });
|
|
102
|
+
files.forEach((file) => {
|
|
103
|
+
const fullPath = this.resolveAbsolutePath(dirPath, file.name);
|
|
104
|
+
if (file.isDirectory()) {
|
|
105
|
+
children.push(directoryItem_1.DirectoryItem.create(fullPath, this).orThrow());
|
|
106
|
+
}
|
|
107
|
+
else if (file.isFile()) {
|
|
108
|
+
children.push(fileItem_1.FileItem.create(fullPath, this).orThrow());
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
return children;
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.FsFileTreeAccessors = FsFileTreeAccessors;
|
|
116
|
+
//# sourceMappingURL=fsTree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fsTree.js","sourceRoot":"","sources":["../../../src/packlets/file-tree/fsTree.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;AAGH,gDAAwB;AACxB,4CAAoB;AACpB,kCAAgD;AAChD,mDAAgD;AAChD,yCAAsC;AAEtC;;;;GAIG;AACH,MAAa,mBAAmB;IAG9B;;;;OAIG;IACH,YAAmB,MAAe;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED;;OAEG;IACI,mBAAmB,CAAC,GAAG,KAAe;QAC3C,IAAI,IAAI,CAAC,MAAM,IAAI,CAAC,cAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YAC9C,OAAO,cAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC;QAC7C,CAAC;QACD,OAAO,cAAI,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC,CAAC;IAChC,CAAC;IAED;;OAEG;IACI,YAAY,CAAC,QAAgB;QAClC,OAAO,cAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAChC,CAAC;IAED;;OAEG;IACI,WAAW,CAAC,QAAgB,EAAE,MAAe;QAClD,OAAO,cAAI,CAAC,QAAQ,CAAC,QAA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gBACrB,MAAM,QAAQ,GAAG,IAAI,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;gBAC9D,IAAI,IAAI,CAAC,WAAW,EAAE,EAAE,CAAC;oBACvB,QAAQ,CAAC,IAAI,CAAC,6BAAa,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;gBAChE,CAAC;qBAAM,IAAI,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC;oBACzB,QAAQ,CAAC,IAAI,CAAC,mBAAQ,CAAC,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;gBAC3D,CAAC;YACH,CAAC,CAAC,CAAC;YACH,OAAO,QAAQ,CAAC;QAClB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAnFD,kDAmFC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { FileTreeItem, IFileTreeAccessors } from './fileTreeAccessors';\nimport path from 'path';\nimport fs from 'fs';\nimport { captureResult, Result } from '../base';\nimport { DirectoryItem } from './directoryItem';\nimport { FileItem } from './fileItem';\n\n/**\n * Implementation of {@link FileTree.IFileTreeAccessors | IFileTreeAccessors} that uses the\n * file system to access files and directories.\n * @public\n */\nexport class FsFileTreeAccessors implements IFileTreeAccessors {\n public readonly prefix: string | undefined;\n\n /**\n * Protected constructor for derived classes.\n * @param prefix - Optional prefix for the tree.\n * @public\n */\n public constructor(prefix?: string) {\n this.prefix = prefix;\n }\n\n /**\n * {@inheritDoc FileTree.IFileTreeAccessors.resolveAbsolutePath}\n */\n public resolveAbsolutePath(...paths: string[]): string {\n if (this.prefix && !path.isAbsolute(paths[0])) {\n return path.resolve(this.prefix, ...paths);\n }\n return path.resolve(...paths);\n }\n\n /**\n * {@inheritDoc FileTree.IFileTreeAccessors.getExtension}\n */\n public getExtension(itemPath: string): string {\n return path.extname(itemPath);\n }\n\n /**\n * {@inheritDoc FileTree.IFileTreeAccessors.getBaseName}\n */\n public getBaseName(itemPath: string, suffix?: string): string {\n return path.basename(itemPath, suffix);\n }\n\n /**\n * {@inheritDoc FileTree.IFileTreeAccessors.joinPaths}\n */\n public joinPaths(...paths: string[]): string {\n return path.join(...paths);\n }\n\n /**\n * {@inheritDoc FileTree.IFileTreeAccessors.getItem}\n */\n public getItem(itemPath: string): Result<FileTreeItem> {\n return captureResult(() => {\n const stat = fs.statSync(this.resolveAbsolutePath(itemPath));\n if (stat.isDirectory()) {\n return DirectoryItem.create(itemPath, this).orThrow();\n } else if (stat.isFile()) {\n return FileItem.create(itemPath, this).orThrow();\n }\n throw new Error(`${itemPath}: not a file or directory`);\n });\n }\n\n /**\n * {@inheritDoc FileTree.IFileTreeAccessors.getFileContents}\n */\n public getFileContents(filePath: string): Result<string> {\n return captureResult(() => fs.readFileSync(this.resolveAbsolutePath(filePath), 'utf8'));\n }\n\n /**\n * {@inheritDoc FileTree.IFileTreeAccessors.getChildren}\n */\n public getChildren(dirPath: string): Result<ReadonlyArray<FileTreeItem>> {\n return captureResult(() => {\n const children: FileTreeItem[] = [];\n const files = fs.readdirSync(this.resolveAbsolutePath(dirPath), { withFileTypes: true });\n files.forEach((file) => {\n const fullPath = this.resolveAbsolutePath(dirPath, file.name);\n if (file.isDirectory()) {\n children.push(DirectoryItem.create(fullPath, this).orThrow());\n } else if (file.isFile()) {\n children.push(FileItem.create(fullPath, this).orThrow());\n }\n });\n return children;\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Result } from '../../base';
|
|
2
|
+
import { FileTreeItem, IFileTreeAccessors } from '../fileTreeAccessors';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a single file in an in-memory {@link FileTree.FileTree | file tree}.
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export interface IInMemoryFile {
|
|
8
|
+
/**
|
|
9
|
+
* The absolute path of the file in the tree.
|
|
10
|
+
*/
|
|
11
|
+
readonly path: string;
|
|
12
|
+
/**
|
|
13
|
+
* The contents of the file
|
|
14
|
+
*/
|
|
15
|
+
readonly contents: unknown;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Implementation of {@link FileTree.IFileTreeAccessors | IFileTreeAccessors} that uses an in-memory
|
|
19
|
+
* tree to access files and directories.
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export declare class InMemoryTreeAccessors implements IFileTreeAccessors {
|
|
23
|
+
private readonly _tree;
|
|
24
|
+
/**
|
|
25
|
+
* Protected constructor for derived classes.
|
|
26
|
+
* @param files - An array of {@link FileTree.IInMemoryFile | in-memory files} to include in the tree.
|
|
27
|
+
* @param prefix - Optional prefix for the tree.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
protected constructor(files: IInMemoryFile[], prefix?: string);
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new {@link FileTree.InMemoryTreeAccessors | InMemoryTreeAccessors} instance with the supplied
|
|
33
|
+
* in-memory files.
|
|
34
|
+
* @param files - An array of {@link FileTree.IInMemoryFile | in-memory files} to include in the tree.
|
|
35
|
+
* @param prefix - Optional prefix for the tree.
|
|
36
|
+
*/
|
|
37
|
+
static create(files: IInMemoryFile[], prefix?: string): Result<InMemoryTreeAccessors>;
|
|
38
|
+
/**
|
|
39
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.resolveAbsolutePath}
|
|
40
|
+
*/
|
|
41
|
+
resolveAbsolutePath(...paths: string[]): string;
|
|
42
|
+
/**
|
|
43
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getExtension}
|
|
44
|
+
*/
|
|
45
|
+
getExtension(path: string): string;
|
|
46
|
+
/**
|
|
47
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getBaseName}
|
|
48
|
+
*/
|
|
49
|
+
getBaseName(path: string, suffix?: string): string;
|
|
50
|
+
/**
|
|
51
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.joinPaths}
|
|
52
|
+
*/
|
|
53
|
+
joinPaths(...paths: string[]): string;
|
|
54
|
+
/**
|
|
55
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.getItem}
|
|
56
|
+
*/
|
|
57
|
+
getItem(itemPath: string): Result<FileTreeItem>;
|
|
58
|
+
/**
|
|
59
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.getFileContents}
|
|
60
|
+
*/
|
|
61
|
+
getFileContents(path: string): Result<string>;
|
|
62
|
+
/**
|
|
63
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.getChildren}
|
|
64
|
+
*/
|
|
65
|
+
getChildren(path: string): Result<ReadonlyArray<FileTreeItem>>;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=inMemoryTree.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inMemoryTree.d.ts","sourceRoot":"","sources":["../../../../src/packlets/file-tree/in-memory/inMemoryTree.ts"],"names":[],"mappings":"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 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 base_1 = require("../../base");
|
|
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 | 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 prefix - Optional prefix for the tree.
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
constructor(files, prefix) {
|
|
42
|
+
this._tree = treeBuilder_1.TreeBuilder.create(prefix).orThrow();
|
|
43
|
+
for (const file of files) {
|
|
44
|
+
this._tree.addFile(file.path, file.contents).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 prefix - Optional prefix for the tree.
|
|
52
|
+
*/
|
|
53
|
+
static create(files, prefix) {
|
|
54
|
+
return (0, base_1.captureResult)(() => new InMemoryTreeAccessors(files, prefix));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.resolveAbsolutePath}
|
|
58
|
+
*/
|
|
59
|
+
resolveAbsolutePath(...paths) {
|
|
60
|
+
const parts = paths[0].startsWith('/') ? paths : [this._tree.prefix, ...paths];
|
|
61
|
+
const joined = parts.flatMap((p) => p.split('/').filter((s) => s.length > 0)).join('/');
|
|
62
|
+
return `/${joined}`;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getExtension}
|
|
66
|
+
*/
|
|
67
|
+
getExtension(path) {
|
|
68
|
+
const parts = path.split('.');
|
|
69
|
+
if (parts.length === 1) {
|
|
70
|
+
return '';
|
|
71
|
+
}
|
|
72
|
+
return `.${parts.pop()}`;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.getBaseName}
|
|
76
|
+
*/
|
|
77
|
+
getBaseName(path, suffix) {
|
|
78
|
+
var _a;
|
|
79
|
+
/* c8 ignore next 1 - ?? is defense in depth should never happen */
|
|
80
|
+
const base = (_a = path.split('/').pop()) !== null && _a !== void 0 ? _a : '';
|
|
81
|
+
if (suffix && base.endsWith(suffix)) {
|
|
82
|
+
return base.slice(0, base.length - suffix.length);
|
|
83
|
+
}
|
|
84
|
+
return base;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* {@inheritdoc FileTree.IFileTreeAccessors.joinPaths}
|
|
88
|
+
*/
|
|
89
|
+
joinPaths(...paths) {
|
|
90
|
+
return paths.join('/');
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.getItem}
|
|
94
|
+
*/
|
|
95
|
+
getItem(itemPath) {
|
|
96
|
+
const existing = this._tree.byAbsolutePath.get(itemPath);
|
|
97
|
+
if (existing) {
|
|
98
|
+
if (existing instanceof treeBuilder_1.InMemoryFile) {
|
|
99
|
+
return fileItem_1.FileItem.create(existing.absolutePath, this);
|
|
100
|
+
}
|
|
101
|
+
else if (existing instanceof treeBuilder_1.InMemoryDirectory) {
|
|
102
|
+
return directoryItem_1.DirectoryItem.create(existing.absolutePath, this);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return (0, base_1.fail)(`${itemPath}: not found`);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.getFileContents}
|
|
109
|
+
*/
|
|
110
|
+
getFileContents(path) {
|
|
111
|
+
const item = this._tree.byAbsolutePath.get(path);
|
|
112
|
+
if (item === undefined) {
|
|
113
|
+
return (0, base_1.fail)(`${path}: not found`);
|
|
114
|
+
}
|
|
115
|
+
if (!(item instanceof treeBuilder_1.InMemoryFile)) {
|
|
116
|
+
return (0, base_1.fail)(`${path}: not a file`);
|
|
117
|
+
}
|
|
118
|
+
// if the body is a string we don't want to add quotes
|
|
119
|
+
if (typeof item.contents === 'string') {
|
|
120
|
+
return (0, base_1.succeed)(item.contents);
|
|
121
|
+
}
|
|
122
|
+
return (0, base_1.captureResult)(() => JSON.stringify(item.contents));
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* {@inheritDoc FileTree.IFileTreeAccessors.getChildren}
|
|
126
|
+
*/
|
|
127
|
+
getChildren(path) {
|
|
128
|
+
const item = this._tree.byAbsolutePath.get(path);
|
|
129
|
+
if (item === undefined) {
|
|
130
|
+
return (0, base_1.fail)(`${path}: not found`);
|
|
131
|
+
}
|
|
132
|
+
if (!(item instanceof treeBuilder_1.InMemoryDirectory)) {
|
|
133
|
+
return (0, base_1.fail)(`${path}: not a directory`);
|
|
134
|
+
}
|
|
135
|
+
return (0, base_1.captureResult)(() => {
|
|
136
|
+
const children = [];
|
|
137
|
+
for (const child of item.children.values()) {
|
|
138
|
+
if (child instanceof treeBuilder_1.InMemoryFile) {
|
|
139
|
+
children.push(fileItem_1.FileItem.create(child.absolutePath, this).orThrow());
|
|
140
|
+
}
|
|
141
|
+
else if (child instanceof treeBuilder_1.InMemoryDirectory) {
|
|
142
|
+
children.push(directoryItem_1.DirectoryItem.create(child.absolutePath, this).orThrow());
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return children;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
exports.InMemoryTreeAccessors = InMemoryTreeAccessors;
|
|
150
|
+
//# sourceMappingURL=inMemoryTree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inMemoryTree.js","sourceRoot":"","sources":["../../../../src/packlets/file-tree/in-memory/inMemoryTree.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,qCAAkE;AAClE,oDAAiD;AACjD,0CAAuC;AAEvC,+CAA6E;AAkB7E;;;;GAIG;AACH,MAAa,qBAAqB;IAGhC;;;;;OAKG;IACH,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AAC;oBAC9C,QAAQ,CAAC,IAAI,CAAC,6BAAa,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;gBAC1E,CAAC;YACH,CAAC;YACD,OAAO,QAAQ,CAAC;QAClB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAzHD,sDAyHC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { captureResult, fail, Result, succeed } from '../../base';\nimport { DirectoryItem } from '../directoryItem';\nimport { FileItem } from '../fileItem';\nimport { FileTreeItem, IFileTreeAccessors } from '../fileTreeAccessors';\nimport { InMemoryDirectory, InMemoryFile, TreeBuilder } from './treeBuilder';\n\n/**\n * Represents a single file in an in-memory {@link FileTree.FileTree | file tree}.\n * @public\n */\nexport interface IInMemoryFile {\n /**\n * The absolute path of the file in the tree.\n */\n readonly path: string;\n\n /**\n * The contents of the file\n */\n readonly contents: unknown;\n}\n\n/**\n * Implementation of {@link FileTree.IFileTreeAccessors | IFileTreeAccessors} that uses an in-memory\n * tree to access files and directories.\n * @public\n */\nexport class InMemoryTreeAccessors implements IFileTreeAccessors {\n private readonly _tree: TreeBuilder;\n\n /**\n * Protected constructor for derived classes.\n * @param files - An array of {@link FileTree.IInMemoryFile | in-memory files} to include in the tree.\n * @param prefix - Optional prefix for the tree.\n * @public\n */\n protected constructor(files: IInMemoryFile[], prefix?: string) {\n this._tree = TreeBuilder.create(prefix).orThrow();\n for (const file of files) {\n this._tree.addFile(file.path, file.contents).orThrow();\n }\n }\n\n /**\n * Creates a new {@link FileTree.InMemoryTreeAccessors | InMemoryTreeAccessors} instance with the supplied\n * in-memory files.\n * @param files - An array of {@link FileTree.IInMemoryFile | in-memory files} to include in the tree.\n * @param prefix - Optional prefix for the tree.\n */\n public static create(files: IInMemoryFile[], prefix?: string): Result<InMemoryTreeAccessors> {\n return captureResult(() => new InMemoryTreeAccessors(files, prefix));\n }\n\n /**\n * {@inheritDoc FileTree.IFileTreeAccessors.resolveAbsolutePath}\n */\n public resolveAbsolutePath(...paths: string[]): string {\n const parts = paths[0].startsWith('/') ? paths : [this._tree.prefix, ...paths];\n const joined = parts.flatMap((p) => p.split('/').filter((s) => s.length > 0)).join('/');\n return `/${joined}`;\n }\n\n /**\n * {@inheritdoc FileTree.IFileTreeAccessors.getExtension}\n */\n public getExtension(path: string): string {\n const parts = path.split('.');\n if (parts.length === 1) {\n return '';\n }\n return `.${parts.pop()}`;\n }\n\n /**\n * {@inheritdoc FileTree.IFileTreeAccessors.getBaseName}\n */\n public getBaseName(path: string, suffix?: string): string {\n /* c8 ignore next 1 - ?? is defense in depth should never happen */\n const base = path.split('/').pop() ?? '';\n if (suffix && base.endsWith(suffix)) {\n return base.slice(0, base.length - suffix.length);\n }\n return base;\n }\n\n /**\n * {@inheritdoc FileTree.IFileTreeAccessors.joinPaths}\n */\n public joinPaths(...paths: string[]): string {\n return paths.join('/');\n }\n\n /**\n * {@inheritDoc FileTree.IFileTreeAccessors.getItem}\n */\n public getItem(itemPath: string): Result<FileTreeItem> {\n const existing = this._tree.byAbsolutePath.get(itemPath);\n if (existing) {\n if (existing instanceof InMemoryFile) {\n return FileItem.create(existing.absolutePath, this);\n } else if (existing instanceof InMemoryDirectory) {\n return DirectoryItem.create(existing.absolutePath, this);\n }\n }\n return fail(`${itemPath}: not found`);\n }\n\n /**\n * {@inheritDoc FileTree.IFileTreeAccessors.getFileContents}\n */\n public getFileContents(path: string): Result<string> {\n const item = this._tree.byAbsolutePath.get(path);\n if (item === undefined) {\n return fail(`${path}: not found`);\n }\n if (!(item instanceof InMemoryFile)) {\n return fail(`${path}: not a file`);\n }\n // if the body is a string we don't want to add quotes\n if (typeof item.contents === 'string') {\n return succeed(item.contents);\n }\n return captureResult(() => JSON.stringify(item.contents));\n }\n\n /**\n * {@inheritDoc FileTree.IFileTreeAccessors.getChildren}\n */\n public getChildren(path: string): Result<ReadonlyArray<FileTreeItem>> {\n const item = this._tree.byAbsolutePath.get(path);\n if (item === undefined) {\n return fail(`${path}: not found`);\n }\n if (!(item instanceof InMemoryDirectory)) {\n return fail(`${path}: not a directory`);\n }\n return captureResult(() => {\n const children: FileTreeItem[] = [];\n for (const child of item.children.values()) {\n if (child instanceof InMemoryFile) {\n children.push(FileItem.create(child.absolutePath, this).orThrow());\n } else if (child instanceof InMemoryDirectory) {\n children.push(DirectoryItem.create(child.absolutePath, this).orThrow());\n }\n }\n return children;\n });\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/packlets/file-tree/in-memory/index.ts"],"names":[],"mappings":"AAsBA,cAAc,gBAAgB,CAAC"}
|
|
@@ -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 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/packlets/file-tree/in-memory/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;AAEH,iDAA+B","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nexport * from './inMemoryTree';\n"]}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Result } from '../../base';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a file in an in-memory file tree.
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export declare class InMemoryFile {
|
|
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
|
+
* Creates a new {@link InMemoryFile | InMemoryFile} instance.
|
|
17
|
+
* @param absolutePath - The absolute path of the file.
|
|
18
|
+
* @param contents - The contents of the file.
|
|
19
|
+
*/
|
|
20
|
+
constructor(absolutePath: string, contents: unknown);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Represents a directory in an in-memory file tree.
|
|
24
|
+
* @public
|
|
25
|
+
*/
|
|
26
|
+
export declare class InMemoryDirectory {
|
|
27
|
+
/**
|
|
28
|
+
* The absolute path of the directory.
|
|
29
|
+
*/
|
|
30
|
+
readonly absolutePath: string;
|
|
31
|
+
protected _children: Map<string, InMemoryDirectory | InMemoryFile>;
|
|
32
|
+
/**
|
|
33
|
+
* The children of the directory.
|
|
34
|
+
*/
|
|
35
|
+
get children(): ReadonlyMap<string, InMemoryDirectory | InMemoryFile>;
|
|
36
|
+
/**
|
|
37
|
+
* Creates an empty new {@link InMemoryDirectory | InMemoryDirectory} instance.
|
|
38
|
+
* @param absolutePath - The absolute path of the directory.
|
|
39
|
+
*/
|
|
40
|
+
constructor(absolutePath: string);
|
|
41
|
+
/**
|
|
42
|
+
* Gets or adds a child directory with the specified name.
|
|
43
|
+
* @param name - The name of the child directory.
|
|
44
|
+
* @returns {@link Success | Success} with the child directory if successful, or
|
|
45
|
+
* {@link Failure | Failure} with an error message otherwise.
|
|
46
|
+
*/
|
|
47
|
+
getOrAddDirectory(name: string): Result<InMemoryDirectory>;
|
|
48
|
+
/**
|
|
49
|
+
* Adds a file to the directory.
|
|
50
|
+
* @param name - The name of the file.
|
|
51
|
+
* @param contents - The contents of the file.
|
|
52
|
+
* @returns {@link Success | Success} with the new file if successful, or
|
|
53
|
+
* {@link Failure | Failure} with an error message otherwise.
|
|
54
|
+
*/
|
|
55
|
+
addFile(name: string, contents: unknown): Result<InMemoryFile>;
|
|
56
|
+
/**
|
|
57
|
+
* Gets the absolute path for a child of this directory with the supplied
|
|
58
|
+
* name.
|
|
59
|
+
* @param name - The name of the child.
|
|
60
|
+
* @returns {@link Success | Success} with the absolute path if successful, or
|
|
61
|
+
* {@link Failure | Failure} with an error message otherwise.
|
|
62
|
+
*/
|
|
63
|
+
getChildPath(name: string): string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Helper class to build an in-memory file tree.
|
|
67
|
+
* @public
|
|
68
|
+
*/
|
|
69
|
+
export declare class TreeBuilder {
|
|
70
|
+
/**
|
|
71
|
+
* The prefix for all paths in the tree.
|
|
72
|
+
*/
|
|
73
|
+
readonly prefix: string;
|
|
74
|
+
/**
|
|
75
|
+
* The root directory of the tree.
|
|
76
|
+
*/
|
|
77
|
+
readonly root: InMemoryDirectory;
|
|
78
|
+
/**
|
|
79
|
+
* A map of all directories and files in the tree by absolute path.
|
|
80
|
+
*/
|
|
81
|
+
readonly byAbsolutePath: Map<string, InMemoryDirectory | InMemoryFile>;
|
|
82
|
+
/**
|
|
83
|
+
* Protected constructor for derived classes.
|
|
84
|
+
* @param prefix - The prefix for all paths in the tree.
|
|
85
|
+
* @public
|
|
86
|
+
*/
|
|
87
|
+
protected constructor(prefix?: string);
|
|
88
|
+
/**
|
|
89
|
+
* Creates a new {@link TreeBuilder | TreeBuilder} instance.
|
|
90
|
+
* @param prefix - The prefix for all paths in the tree.
|
|
91
|
+
* @returns {@link Success | Success} with the new {@link TreeBuilder | TreeBuilder} instance if successful,
|
|
92
|
+
* or {@link Failure | Failure} with an error message otherwise.
|
|
93
|
+
* @public
|
|
94
|
+
*/
|
|
95
|
+
static create(prefix?: string): Result<TreeBuilder>;
|
|
96
|
+
/**
|
|
97
|
+
* Adds a file to the tree.
|
|
98
|
+
* @param absolutePath - The absolute path of the file.
|
|
99
|
+
* @param contents - The contents of the file.
|
|
100
|
+
* @returns {@link Success | Success} with the new file if successful, or
|
|
101
|
+
* {@link Failure | Failure} with an error message otherwise.
|
|
102
|
+
* @public
|
|
103
|
+
*/
|
|
104
|
+
addFile(absolutePath: string, contents: unknown): Result<InMemoryFile>;
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=treeBuilder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"treeBuilder.d.ts","sourceRoot":"","sources":["../../../../src/packlets/file-tree/in-memory/treeBuilder.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,MAAM,EAAgC,MAAM,YAAY,CAAC;AAElE;;;GAGG;AACH,qBAAa,YAAY;IACvB;;OAEG;IACH,SAAgB,YAAY,EAAE,MAAM,CAAC;IACrC;;OAEG;IACH,SAAgB,QAAQ,EAAE,OAAO,CAAC;IAElC;;;;OAIG;gBACgB,YAAY,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO;CAI3D;AAED;;;GAGG;AACH,qBAAa,iBAAiB;IAC5B;;OAEG;IACH,SAAgB,YAAY,EAAE,MAAM,CAAC;IACrC,SAAS,CAAC,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,iBAAiB,GAAG,YAAY,CAAC,CAAC;IAEnE;;OAEG;IACH,IAAW,QAAQ,IAAI,WAAW,CAAC,MAAM,EAAE,iBAAiB,GAAG,YAAY,CAAC,CAE3E;IAED;;;OAGG;gBACgB,YAAY,EAAE,MAAM;IAKvC;;;;;OAKG;IACI,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,iBAAiB,CAAC;IAajE;;;;;;OAMG;IACI,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,GAAG,MAAM,CAAC,YAAY,CAAC;IASrE;;;;;;OAMG;IACI,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;CAM1C;AAED;;;GAGG;AACH,qBAAa,WAAW;I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