@fgv/ts-json-base 5.1.0-3 → 5.1.0-30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.browser.js.map +1 -0
- package/dist/index.js.map +1 -0
- package/dist/packlets/converters/converters.js +11 -0
- package/dist/packlets/converters/converters.js.map +1 -0
- package/dist/packlets/converters/index.js.map +1 -0
- package/dist/packlets/file-tree/directoryItem.js.map +1 -0
- package/dist/packlets/file-tree/fileItem.js +11 -4
- package/dist/packlets/file-tree/fileItem.js.map +1 -0
- package/dist/packlets/file-tree/fileTree.js.map +1 -0
- package/dist/packlets/file-tree/fileTreeAccessors.js.map +1 -0
- package/dist/packlets/file-tree/fileTreeHelpers.inMemory.js.map +1 -0
- package/dist/packlets/file-tree/fileTreeHelpers.js.map +1 -0
- package/dist/packlets/file-tree/filterSpec.js.map +1 -0
- package/dist/packlets/file-tree/fsTree.js +44 -13
- package/dist/packlets/file-tree/fsTree.js.map +1 -0
- package/dist/packlets/file-tree/in-memory/inMemoryTree.js +44 -13
- package/dist/packlets/file-tree/in-memory/inMemoryTree.js.map +1 -0
- package/dist/packlets/file-tree/in-memory/index.js.map +1 -0
- package/dist/packlets/file-tree/in-memory/treeBuilder.js.map +1 -0
- package/dist/packlets/file-tree/index.browser.js.map +1 -0
- package/dist/packlets/file-tree/index.js.map +1 -0
- package/dist/packlets/json/common.js.map +1 -0
- package/dist/packlets/json/index.js.map +1 -0
- package/dist/packlets/json-compatible/common.js.map +1 -0
- package/dist/packlets/json-compatible/converters.js.map +1 -0
- package/dist/packlets/json-compatible/index.js.map +1 -0
- package/dist/packlets/json-compatible/validators.js.map +1 -0
- package/dist/packlets/json-file/file.js.map +1 -0
- package/dist/packlets/json-file/index.browser.js.map +1 -0
- package/dist/packlets/json-file/index.js.map +1 -0
- package/dist/packlets/json-file/jsonFsHelper.js.map +1 -0
- package/dist/packlets/json-file/jsonLike.js.map +1 -0
- package/dist/packlets/json-file/jsonTreeHelper.js.map +1 -0
- package/dist/packlets/validators/index.js.map +1 -0
- package/dist/packlets/validators/validators.js.map +1 -0
- package/dist/ts-json-base.d.ts +128 -30
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/index.browser.d.ts.map +1 -0
- package/lib/index.browser.js.map +1 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js.map +1 -0
- package/lib/packlets/converters/converters.d.ts +27 -1
- package/lib/packlets/converters/converters.d.ts.map +1 -0
- package/lib/packlets/converters/converters.js +12 -0
- package/lib/packlets/converters/converters.js.map +1 -0
- package/lib/packlets/converters/index.d.ts.map +1 -0
- package/lib/packlets/converters/index.js.map +1 -0
- package/lib/packlets/file-tree/directoryItem.d.ts.map +1 -0
- package/lib/packlets/file-tree/directoryItem.js.map +1 -0
- package/lib/packlets/file-tree/fileItem.d.ts +11 -4
- package/lib/packlets/file-tree/fileItem.d.ts.map +1 -0
- package/lib/packlets/file-tree/fileItem.js +11 -4
- package/lib/packlets/file-tree/fileItem.js.map +1 -0
- package/lib/packlets/file-tree/fileTree.d.ts.map +1 -0
- package/lib/packlets/file-tree/fileTree.js.map +1 -0
- package/lib/packlets/file-tree/fileTreeAccessors.d.ts.map +1 -0
- package/lib/packlets/file-tree/fileTreeAccessors.js.map +1 -0
- package/lib/packlets/file-tree/fileTreeHelpers.d.ts.map +1 -0
- package/lib/packlets/file-tree/fileTreeHelpers.inMemory.d.ts.map +1 -0
- package/lib/packlets/file-tree/fileTreeHelpers.inMemory.js.map +1 -0
- package/lib/packlets/file-tree/fileTreeHelpers.js.map +1 -0
- package/lib/packlets/file-tree/filterSpec.d.ts.map +1 -0
- package/lib/packlets/file-tree/filterSpec.js.map +1 -0
- package/lib/packlets/file-tree/fsTree.d.ts +44 -13
- package/lib/packlets/file-tree/fsTree.d.ts.map +1 -0
- package/lib/packlets/file-tree/fsTree.js +44 -13
- package/lib/packlets/file-tree/fsTree.js.map +1 -0
- package/lib/packlets/file-tree/in-memory/inMemoryTree.d.ts +44 -13
- package/lib/packlets/file-tree/in-memory/inMemoryTree.d.ts.map +1 -0
- package/lib/packlets/file-tree/in-memory/inMemoryTree.js +44 -13
- package/lib/packlets/file-tree/in-memory/inMemoryTree.js.map +1 -0
- package/lib/packlets/file-tree/in-memory/index.d.ts.map +1 -0
- package/lib/packlets/file-tree/in-memory/index.js.map +1 -0
- package/lib/packlets/file-tree/in-memory/treeBuilder.d.ts.map +1 -0
- package/lib/packlets/file-tree/in-memory/treeBuilder.js.map +1 -0
- package/lib/packlets/file-tree/index.browser.d.ts.map +1 -0
- package/lib/packlets/file-tree/index.browser.js.map +1 -0
- package/lib/packlets/file-tree/index.d.ts.map +1 -0
- package/lib/packlets/file-tree/index.js.map +1 -0
- package/lib/packlets/json/common.d.ts.map +1 -0
- package/lib/packlets/json/common.js.map +1 -0
- package/lib/packlets/json/index.d.ts.map +1 -0
- package/lib/packlets/json/index.js.map +1 -0
- package/lib/packlets/json-compatible/common.d.ts.map +1 -0
- package/lib/packlets/json-compatible/common.js.map +1 -0
- package/lib/packlets/json-compatible/converters.d.ts.map +1 -0
- package/lib/packlets/json-compatible/converters.js.map +1 -0
- package/lib/packlets/json-compatible/index.d.ts.map +1 -0
- package/lib/packlets/json-compatible/index.js.map +1 -0
- package/lib/packlets/json-compatible/validators.d.ts.map +1 -0
- package/lib/packlets/json-compatible/validators.js.map +1 -0
- package/lib/packlets/json-file/file.d.ts.map +1 -0
- package/lib/packlets/json-file/file.js.map +1 -0
- package/lib/packlets/json-file/index.browser.d.ts.map +1 -0
- package/lib/packlets/json-file/index.browser.js.map +1 -0
- package/lib/packlets/json-file/index.d.ts.map +1 -0
- package/lib/packlets/json-file/index.js.map +1 -0
- package/lib/packlets/json-file/jsonFsHelper.d.ts.map +1 -0
- package/lib/packlets/json-file/jsonFsHelper.js.map +1 -0
- package/lib/packlets/json-file/jsonLike.d.ts.map +1 -0
- package/lib/packlets/json-file/jsonLike.js.map +1 -0
- package/lib/packlets/json-file/jsonTreeHelper.d.ts.map +1 -0
- package/lib/packlets/json-file/jsonTreeHelper.js.map +1 -0
- package/lib/packlets/validators/index.d.ts.map +1 -0
- package/lib/packlets/validators/index.js.map +1 -0
- package/lib/packlets/validators/validators.d.ts.map +1 -0
- package/lib/packlets/validators/validators.js.map +1 -0
- package/package.json +9 -7
- package/dist/test/fixtures/file-tree/config.json +0 -1
- package/dist/test/fixtures/file-tree/data/items.json +0 -1
- package/dist/test/fixtures/file-tree/docs/api/reference.json +0 -1
- package/dist/test/unit/data/file/bad/bad3.json +0 -3
- package/dist/test/unit/data/file/bad/thing1.json +0 -4
- package/dist/test/unit/data/file/bad/thing2.json +0 -3
- package/dist/test/unit/data/file/good/thing1.json +0 -4
- package/dist/test/unit/data/file/good/thing2.json +0 -3
- package/dist/test/unit/json-compatible/helpers.js +0 -32
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fileTreeAccessors.js","sourceRoot":"","sources":["../../../src/packlets/file-tree/fileTreeAccessors.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;AA2cH,gDAWC;AAQD,sDAWC;AAQD,8CAWC;AAQD,wDAWC;AA9ED,+EAA+E;AAC/E,cAAc;AACd,+EAA+E;AAE/E;;;;;GAKG;AACH,SAAgB,kBAAkB,CAChC,SAAkC;IAElC,MAAM,OAAO,GAAG,SAA2C,CAAC;IAC5D,OAAO,CACL,OAAO,OAAO,CAAC,aAAa,KAAK,UAAU;QAC3C,OAAO,OAAO,CAAC,gBAAgB,KAAK,UAAU;QAC9C,OAAO,OAAO,CAAC,UAAU,KAAK,UAAU;QACxC,OAAO,OAAO,CAAC,eAAe,KAAK,UAAU;QAC7C,OAAO,OAAO,CAAC,eAAe,KAAK,UAAU,CAC9C,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAgB,qBAAqB,CACnC,SAAkC;IAElC,MAAM,UAAU,GAAG,SAA8C,CAAC;IAClE,oFAAoF;IACpF,OAAO,CACL,kBAAkB,CAAC,SAAS,CAAC;QAC7B,OAAO,UAAU,CAAC,UAAU,KAAK,UAAU;QAC3C,OAAO,UAAU,CAAC,OAAO,KAAK,UAAU;QACxC,OAAO,UAAU,CAAC,aAAa,KAAK,UAAU,CAC/C,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAC/B,IAAkD;IAElD,MAAM,OAAO,GAAG,IAAqC,CAAC;IACtD,OAAO,CACL,OAAO,CAAC,IAAI,KAAK,MAAM;QACvB,OAAO,OAAO,CAAC,YAAY,KAAK,UAAU;QAC1C,OAAO,OAAO,CAAC,WAAW,KAAK,UAAU;QACzC,OAAO,OAAO,CAAC,cAAc,KAAK,UAAU;QAC5C,OAAO,OAAO,CAAC,MAAM,KAAK,UAAU,CACrC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAgB,sBAAsB,CACpC,IAAuD;IAEvD,MAAM,OAAO,GAAG,IAA0C,CAAC;IAC3D,OAAO,CACL,OAAO,CAAC,IAAI,KAAK,WAAW;QAC5B,OAAO,OAAO,CAAC,eAAe,KAAK,UAAU;QAC7C,OAAO,OAAO,CAAC,oBAAoB,KAAK,UAAU;QAClD,OAAO,OAAO,CAAC,WAAW,KAAK,UAAU;QACzC,OAAO,OAAO,CAAC,MAAM,KAAK,UAAU,CACrC,CAAC;AACJ,CAAC","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 { DetailedResult, Result } from '@fgv/ts-utils';\nimport { Converter, Validator } from '@fgv/ts-utils';\nimport { JsonValue } from '../json';\n\n/**\n * Indicates the persistence capability of a save operation.\n * - `persistent`: Changes are saved to durable storage (e.g., file system).\n * - `transient`: Changes are saved in memory only and will be lost on reload.\n * @public\n */\nexport type SaveCapability = 'persistent' | 'transient';\n\n/**\n * Indicates the reason a save operation cannot be performed.\n * - `not-supported`: The accessors do not support mutation.\n * - `read-only`: The file or file system is read-only.\n * - `not-mutable`: Mutability is disabled in configuration.\n * - `path-excluded`: The path is excluded by the mutability filter.\n * - `permission-denied`: Insufficient permissions to write.\n * @public\n */\nexport type SaveFailureReason =\n | 'not-supported'\n | 'read-only'\n | 'not-mutable'\n | 'path-excluded'\n | 'permission-denied';\n\n/**\n * Detail type for getIsMutable results.\n * @public\n */\nexport type SaveDetail = SaveCapability | SaveFailureReason;\n\n/**\n * Filter specification for controlling which paths are mutable.\n * @public\n */\nexport interface IFilterSpec {\n /**\n * Paths or patterns to include. If specified, only matching paths are mutable.\n */\n include?: (string | RegExp)[];\n\n /**\n * Paths or patterns to exclude. Matching paths are not mutable.\n */\n exclude?: (string | RegExp)[];\n}\n\n/**\n * Type of item in a file tree.\n * @public\n */\nexport type FileTreeItemType = 'directory' | 'file';\n\n/**\n * Type of function to infer the content type of a file.\n * @public\n */\nexport type ContentTypeFactory<TCT extends string = string> = (\n filePath: string,\n provided?: string\n) => Result<TCT | undefined>;\n\n/**\n * Initialization parameters for a file tree.\n * @public\n */\nexport interface IFileTreeInitParams<TCT extends string = string> {\n prefix?: string;\n inferContentType?: ContentTypeFactory<TCT>;\n\n /**\n * Controls mutability of the file tree.\n * - `undefined` or `false`: No files are mutable.\n * - `true`: All files are mutable.\n * - `IFilterSpec`: Only files matching the filter are mutable.\n */\n mutable?: boolean | IFilterSpec;\n}\n\n/**\n * Options for deleting a child item from a directory.\n * @public\n */\nexport interface IDeleteChildOptions {\n /**\n * If true, recursively delete directory children and their contents.\n * Default: false (fail if directory is non-empty).\n */\n recursive?: boolean;\n}\n\n// ============================================================================\n// File Item Interfaces\n// ============================================================================\n\n/**\n * Interface for a read-only file in a file tree.\n * @public\n */\nexport interface IFileTreeFileItem<TCT extends string = string> {\n /**\n * Indicates that this {@link FileTree.FileTreeItem | file tree item} is a file.\n */\n readonly type: 'file';\n\n /**\n * The absolute path of the file.\n */\n readonly absolutePath: string;\n\n /**\n * The name of the file\n */\n readonly name: string;\n\n /**\n * The base name of the file (without extension)\n */\n readonly baseName: string;\n\n /**\n * The extension of the file\n */\n readonly extension: string;\n\n /**\n * An optional content type (e.g. mime type) for the file.\n */\n readonly contentType: TCT | undefined;\n\n /**\n * Gets the contents of the file as parsed JSON.\n * @returns `Success` with the parsed JSON-compatible contents if successful, or\n * `Failure` with an error message otherwise.\n */\n getContents(): Result<JsonValue>;\n\n /**\n * Gets the contents of the file as parsed JSON, converted to a specific type.\n * @param converter - A `Validator` or `Converter`\n * to convert the parsed JSON contents to the desired type.\n * @returns `Success` with the converted contents if successful, or\n * `Failure` with an error message otherwise.\n */\n getContents<T>(converter: Validator<T> | Converter<T>): Result<T>;\n\n /**\n * Gets the raw contents of the file as a string.\n * @returns `Success` with the raw contents if successful, or\n * `Failure` with an error message otherwise.\n */\n getRawContents(): Result<string>;\n}\n\n/**\n * Extended file item interface that supports mutation operations.\n * Use {@link FileTree.isMutableFileItem | isMutableFileItem} type guard to narrow.\n * @public\n */\nexport interface IMutableFileTreeFileItem<TCT extends string = string> extends IFileTreeFileItem<TCT> {\n /**\n * Indicates whether this file can be saved.\n * @returns `DetailedSuccess` with {@link FileTree.SaveCapability} if the file can be saved,\n * or `DetailedFailure` with {@link FileTree.SaveFailureReason} if it cannot.\n */\n getIsMutable(): DetailedResult<boolean, SaveDetail>;\n\n /**\n * Sets the contents of the file from a JSON value.\n * @param json - The JSON value to serialize and save.\n * @returns `Success` if the file was saved, or `Failure` with an error message.\n */\n setContents(json: JsonValue): Result<JsonValue>;\n\n /**\n * Sets the raw contents of the file.\n * @param contents - The string contents to save.\n * @returns `Success` if the file was saved, or `Failure` with an error message.\n */\n setRawContents(contents: string): Result<string>;\n\n /**\n * Deletes this file from its backing store.\n * @returns `Success` with `true` if the file was deleted, or `Failure` with an error message.\n */\n delete(): Result<boolean>;\n}\n\n// ============================================================================\n// Directory Item Interfaces\n// ============================================================================\n\n/**\n * Interface for a read-only directory in a file tree.\n * @public\n */\nexport interface IFileTreeDirectoryItem<TCT extends string = string> {\n /**\n * Indicates that this {@link FileTree.FileTreeItem | file tree item} is a directory\n */\n readonly type: 'directory';\n\n /**\n * The absolute path of the directory.\n */\n readonly absolutePath: string;\n\n /**\n * The name of the directory\n */\n readonly name: string;\n\n /**\n * Gets the children of the directory.\n * @returns `Success` with the children of the directory if successful,\n * or `Failure` with an error message otherwise.\n */\n getChildren(): Result<ReadonlyArray<FileTreeItem<TCT>>>;\n}\n\n/**\n * Extended directory item interface that supports mutation operations.\n * Use {@link FileTree.isMutableDirectoryItem | isMutableDirectoryItem} type guard to narrow.\n * @public\n */\nexport interface IMutableFileTreeDirectoryItem<TCT extends string = string>\n extends IFileTreeDirectoryItem<TCT> {\n /**\n * Creates a new file as a child of this directory.\n * @param name - The file name to create.\n * @param contents - The string contents to write.\n * @returns `Success` with the new file item, or `Failure` with an error message.\n */\n createChildFile(name: string, contents: string): Result<IMutableFileTreeFileItem<TCT>>;\n\n /**\n * Creates a new subdirectory as a child of this directory.\n * @param name - The directory name to create.\n * @returns `Success` with the new directory item, or `Failure` with an error message.\n */\n createChildDirectory(name: string): Result<IMutableFileTreeDirectoryItem<TCT>>;\n\n /**\n * Deletes a child item from this directory.\n * @param name - The name of the child to delete.\n * @param options - Optional {@link FileTree.IDeleteChildOptions | options} controlling deletion behavior.\n * @returns `Success` with `true` if the child was deleted, or `Failure` with an error message.\n */\n deleteChild(name: string, options?: IDeleteChildOptions): Result<boolean>;\n\n /**\n * Deletes this directory from its backing store.\n * The directory must be empty or the operation will fail.\n * @returns `Success` with `true` if the directory was deleted, or `Failure` with an error message.\n */\n delete(): Result<boolean>;\n}\n\n// ============================================================================\n// Union Types\n// ============================================================================\n\n/**\n * Type for an item in a file tree.\n * @public\n */\nexport type FileTreeItem<TCT extends string = string> = IFileTreeFileItem<TCT> | IFileTreeDirectoryItem<TCT>;\n\n/**\n * Type for a mutable item in a file tree.\n * @public\n */\nexport type MutableFileTreeItem<TCT extends string = string> =\n | IMutableFileTreeFileItem<TCT>\n | IMutableFileTreeDirectoryItem<TCT>;\n\n/**\n * A file item that may or may not be mutable at runtime.\n *\n * Use this type for parameters or fields where the code checks for mutability\n * and handles the read-only case gracefully. Use {@link FileTree.IMutableFileTreeFileItem}\n * when mutation is required.\n *\n * Narrow with {@link FileTree.isMutableFileItem} to access mutation methods.\n * @public\n */\nexport type AnyFileTreeFileItem<TCT extends string = string> =\n | IFileTreeFileItem<TCT>\n | IMutableFileTreeFileItem<TCT>;\n\n/**\n * A directory item that may or may not be mutable at runtime.\n *\n * Use this type for parameters or fields where the code checks for mutability\n * and handles the read-only case gracefully. Use {@link FileTree.IMutableFileTreeDirectoryItem}\n * when mutation is required.\n *\n * Narrow with {@link FileTree.isMutableDirectoryItem} to access mutation methods.\n * @public\n */\nexport type AnyFileTreeDirectoryItem<TCT extends string = string> =\n | IFileTreeDirectoryItem<TCT>\n | IMutableFileTreeDirectoryItem<TCT>;\n\n// ============================================================================\n// Accessor Interfaces\n// ============================================================================\n\n/**\n * Common abstraction layer interface for a tree of files\n * (e.g. a file system or a zip file).\n * @public\n */\nexport interface IFileTreeAccessors<TCT extends string = string> {\n /**\n * Resolves paths to an absolute path.\n * @param paths - Paths to resolve.\n * @returns The resolved absolute path.\n */\n resolveAbsolutePath(...paths: string[]): string;\n\n /**\n * Gets the extension of a path.\n * @param path - Path to get the extension of.\n * @returns The extension of the path.\n */\n getExtension(path: string): string;\n\n /**\n * Gets the base name of a path.\n * @param path - Path to get the base name of.\n * @param suffix - Optional suffix to remove from the base name.\n * @returns The base name of the path.\n */\n getBaseName(path: string, suffix?: string): string;\n\n /**\n * Joins paths together.\n * @param paths - Paths to join.\n * @returns The joined paths.\n */\n joinPaths(...paths: string[]): string;\n\n /**\n * Gets an item from the file tree.\n * @param path - Path of the item to get.\n * @returns The item if it exists.\n */\n getItem(path: string): Result<FileTreeItem<TCT>>;\n\n /**\n * Gets the contents of a file in the file tree.\n * @param path - Absolute path of the file.\n * @returns The contents of the file.\n */\n getFileContents(path: string): Result<string>;\n\n /**\n * Gets the content type of a file in the file tree.\n * @param path - Absolute path of the file.\n * @param provided - Optional supplied content type.\n * @returns The content type of the file.\n */\n getFileContentType(path: string, provided?: string): Result<TCT | undefined>;\n\n /**\n * Gets the children of a directory in the file tree.\n * @param path - Path of the directory.\n * @returns The children of the directory.\n */\n getChildren(path: string): Result<ReadonlyArray<FileTreeItem<TCT>>>;\n}\n\n/**\n * Extended accessors interface that supports mutation operations.\n * All mutation methods are required — use {@link FileTree.isMutableAccessors | isMutableAccessors}\n * type guard to check if an accessor supports mutation.\n * @public\n */\nexport interface IMutableFileTreeAccessors<TCT extends string = string> extends IFileTreeAccessors<TCT> {\n /**\n * Checks if a file at the given path can be saved.\n * @param path - The path to check.\n * @returns `DetailedSuccess` with {@link FileTree.SaveCapability} if the file can be saved,\n * or `DetailedFailure` with {@link FileTree.SaveFailureReason} if it cannot.\n */\n fileIsMutable(path: string): DetailedResult<boolean, SaveDetail>;\n\n /**\n * Saves the contents to a file at the given path.\n * @param path - The path of the file to save.\n * @param contents - The string contents to save.\n * @returns `Success` if the file was saved, or `Failure` with an error message.\n */\n saveFileContents(path: string, contents: string): Result<string>;\n\n /**\n * Deletes a file at the given path.\n * @param path - The path of the file to delete.\n * @returns `Success` with `true` if the file was deleted, or `Failure` with an error message.\n */\n deleteFile(path: string): Result<boolean>;\n\n /**\n * Creates a directory at the given path, including any missing parent directories.\n * @param path - The path of the directory to create.\n * @returns `Success` with the absolute path if created, or `Failure` with an error message.\n */\n createDirectory(path: string): Result<string>;\n\n /**\n * Deletes a directory at the given path.\n * The directory must be empty or the operation will fail.\n * @param path - The path of the directory to delete.\n * @returns `Success` with `true` if the directory was deleted, or `Failure` with an error message.\n */\n deleteDirectory(path: string): Result<boolean>;\n}\n\n/**\n * Extended accessors interface that supports persistence operations.\n * @public\n */\nexport interface IPersistentFileTreeAccessors<TCT extends string = string>\n extends IMutableFileTreeAccessors<TCT> {\n /**\n * Synchronize all dirty files to persistent storage.\n * @returns Promise resolving to success or failure\n */\n syncToDisk(): Promise<Result<void>>;\n\n /**\n * Check if there are unsaved changes.\n * @returns True if there are dirty files\n */\n isDirty(): boolean;\n\n /**\n * Get paths of all files with unsaved changes.\n * @returns Array of dirty file paths\n */\n getDirtyPaths(): string[];\n}\n\n// ============================================================================\n// Type Guards\n// ============================================================================\n\n/**\n * Type guard to check if accessors support mutation.\n * @param accessors - The accessors to check.\n * @returns `true` if the accessors implement {@link FileTree.IMutableFileTreeAccessors}.\n * @public\n */\nexport function isMutableAccessors<TCT extends string = string>(\n accessors: IFileTreeAccessors<TCT>\n): accessors is IMutableFileTreeAccessors<TCT> {\n const mutable = accessors as IMutableFileTreeAccessors<TCT>;\n return (\n typeof mutable.fileIsMutable === 'function' &&\n typeof mutable.saveFileContents === 'function' &&\n typeof mutable.deleteFile === 'function' &&\n typeof mutable.createDirectory === 'function' &&\n typeof mutable.deleteDirectory === 'function'\n );\n}\n\n/**\n * Type guard to check if accessors support persistence.\n * @param accessors - The accessors to check.\n * @returns `true` if the accessors implement {@link FileTree.IPersistentFileTreeAccessors}.\n * @public\n */\nexport function isPersistentAccessors<TCT extends string = string>(\n accessors: IFileTreeAccessors<TCT>\n): accessors is IPersistentFileTreeAccessors<TCT> {\n const persistent = accessors as IPersistentFileTreeAccessors<TCT>;\n /* c8 ignore next 6 - no current accessor implements IPersistentFileTreeAccessors */\n return (\n isMutableAccessors(accessors) &&\n typeof persistent.syncToDisk === 'function' &&\n typeof persistent.isDirty === 'function' &&\n typeof persistent.getDirtyPaths === 'function'\n );\n}\n\n/**\n * Type guard to check if a file item supports mutation.\n * @param item - The file item to check.\n * @returns `true` if the item implements {@link FileTree.IMutableFileTreeFileItem}.\n * @public\n */\nexport function isMutableFileItem<TCT extends string = string>(\n item: AnyFileTreeFileItem<TCT> | FileTreeItem<TCT>\n): item is IMutableFileTreeFileItem<TCT> {\n const mutable = item as IMutableFileTreeFileItem<TCT>;\n return (\n mutable.type === 'file' &&\n typeof mutable.getIsMutable === 'function' &&\n typeof mutable.setContents === 'function' &&\n typeof mutable.setRawContents === 'function' &&\n typeof mutable.delete === 'function'\n );\n}\n\n/**\n * Type guard to check if a directory item supports mutation.\n * @param item - The directory item to check.\n * @returns `true` if the item implements {@link FileTree.IMutableFileTreeDirectoryItem}.\n * @public\n */\nexport function isMutableDirectoryItem<TCT extends string = string>(\n item: AnyFileTreeDirectoryItem<TCT> | FileTreeItem<TCT>\n): item is IMutableFileTreeDirectoryItem<TCT> {\n const mutable = item as IMutableFileTreeDirectoryItem<TCT>;\n return (\n mutable.type === 'directory' &&\n typeof mutable.createChildFile === 'function' &&\n typeof mutable.createChildDirectory === 'function' &&\n typeof mutable.deleteChild === 'function' &&\n typeof mutable.delete === 'function'\n );\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fileTreeHelpers.d.ts","sourceRoot":"","sources":["../../../src/packlets/file-tree/fileTreeHelpers.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAiB,MAAM,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAE,M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|
1
|
+
{"version":3,"file":"fileTreeHelpers.inMemory.js","sourceRoot":"","sources":["../../../src/packlets/file-tree/fileTreeHelpers.inMemory.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;AAmCH,4BAQC;AAxCD,yCAAsC;AACtC,2CAAmE;AA+BnE,SAAgB,QAAQ,CACtB,KAA2B,EAC3B,MAA0C;IAE1C,MAAM,GAAG,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;IAClE,OAAO,iCAAqB,CAAC,MAAM,CAAM,KAAK,EAAE,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,GAA+B,EAAE,EAAE,CACpG,mBAAQ,CAAC,MAAM,CAAM,GAAG,CAAC,CAC1B,CAAC;AACJ,CAAC","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 { Result } from '@fgv/ts-utils';\nimport { FileTree } from './fileTree';\nimport { IInMemoryFile, InMemoryTreeAccessors } from './in-memory';\nimport { IFileTreeInitParams } from './fileTreeAccessors';\n\n/**\n * Helper function to create a new {@link FileTree.FileTree | FileTree} instance\n * with accessors for an in-memory file tree.\n * @param files - An array of File |{@link FileTree.IInMemoryFile | in-memory files} to include in the tree.\n * @param prefix - An optional prefix to add to the paths of all files in the tree.\n * @returns `Success` with the new {@link FileTree.FileTree | FileTree} instance\n * if successful, or `Failure` with an error message otherwise.\n * @public\n */\nexport function inMemory<TCT extends string = string>(\n files: IInMemoryFile<TCT>[],\n prefix?: string\n): Result<FileTree<TCT>>;\n\n/**\n * Helper function to create a new {@link FileTree.FileTree | FileTree} instance\n * with accessors for an in-memory file tree.\n * @param files - An array of File |{@link FileTree.IInMemoryFile | in-memory files} to include in the tree.\n * @param params - Optional {@link FileTree.IFileTreeInitParams | initialization parameters} for the file tree.\n * @returns `Success` with the new {@link FileTree.FileTree | FileTree} instance\n * if successful, or `Failure` with an error message otherwise.\n * @public\n */\nexport function inMemory<TCT extends string = string>(\n files: IInMemoryFile<TCT>[],\n params?: IFileTreeInitParams<TCT>\n): Result<FileTree<TCT>>;\n\nexport function inMemory<TCT extends string = string>(\n files: IInMemoryFile<TCT>[],\n params?: IFileTreeInitParams<TCT> | string\n): Result<FileTree<TCT>> {\n params = typeof params === 'string' ? { prefix: params } : params;\n return InMemoryTreeAccessors.create<TCT>(files, params).onSuccess((hal: InMemoryTreeAccessors<TCT>) =>\n FileTree.create<TCT>(hal)\n );\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fileTreeHelpers.js","sourceRoot":"","sources":["../../../src/packlets/file-tree/fileTreeHelpers.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;AA4BH,sCAKC;AA/BD,4CAAsD;AACtD,yCAAsC;AACtC,qCAA+C;AAwB/C,SAAgB,aAAa,CAC3B,MAA0C;IAE1C,MAAM,GAAG,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;IAClE,OAAO,IAAA,wBAAa,EAAC,GAAG,EAAE,CAAC,IAAI,4BAAmB,CAAM,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,mBAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;AAC5G,CAAC","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, Result } from '@fgv/ts-utils';\nimport { FileTree } from './fileTree';\nimport { FsFileTreeAccessors } from './fsTree';\nimport { IFileTreeInitParams } from './fileTreeAccessors';\n\n/**\n * Helper function to create a new {@link FileTree.FileTree | FileTree} instance\n * with accessors for the filesystem.\n * @param prefix - An optional prefix to prepended to supplied relative paths.\n * @returns `Success` with the new {@link FileTree.FileTree | FileTree} instance\n * if successful, or `Failure` with an error message otherwise.\n * @public\n */\nexport function forFilesystem<TCT extends string = string>(prefix?: string): Result<FileTree<TCT>>;\n\n/**\n * Helper function to create a new {@link FileTree.FileTree | FileTree} instance\n * with accessors for the filesystem.\n * @param params - Optional {@link FileTree.IFileTreeInitParams | initialization parameters} for the file tree.\n * @returns `Success` with the new {@link FileTree.FileTree | FileTree} instance\n * if successful, or `Failure` with an error message otherwise.\n * @public\n */\nexport function forFilesystem<TCT extends string = string>(\n params?: IFileTreeInitParams<TCT>\n): Result<FileTree<TCT>>;\nexport function forFilesystem<TCT extends string = string>(\n params?: IFileTreeInitParams<TCT> | string\n): Result<FileTree<TCT>> {\n params = typeof params === 'string' ? { prefix: params } : params;\n return captureResult(() => new FsFileTreeAccessors<TCT>(params)).onSuccess((hal) => FileTree.create(hal));\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filterSpec.d.ts","sourceRoot":"","sources":["../../../src/packlets/file-tree/filterSpec.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AA8BlD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,WAAW,GAAG,SAAS,GAAG,OAAO,CAuB/F"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filterSpec.js","sourceRoot":"","sources":["../../../src/packlets/file-tree/filterSpec.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;AAuCH,sCAuBC;AA1DD;;;;;;GAMG;AACH,SAAS,cAAc,CAAC,IAAY,EAAE,OAAwB;IAC5D,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAChC,OAAO,IAAI,KAAK,OAAO,IAAI,IAAI,CAAC,UAAU,CAAC,OAAO,GAAG,GAAG,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACtF,CAAC;IACD,OAAO,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC5B,CAAC;AAED;;;;;;GAMG;AACH,SAAS,UAAU,CAAC,IAAY,EAAE,QAAyC;IACzE,IAAI,CAAC,QAAQ,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACvC,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,cAAc,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;AACnE,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,aAAa,CAAC,IAAY,EAAE,OAA0C;IACpF,IAAI,OAAO,KAAK,SAAS,IAAI,OAAO,KAAK,KAAK,EAAE,CAAC;QAC/C,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;QACrB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IAErC,uEAAuE;IACvE,IAAI,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,kEAAkE;IAClE,IAAI,OAAO,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAClC,OAAO,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACnC,CAAC;IAED,iEAAiE;IACjE,OAAO,IAAI,CAAC;AACd,CAAC","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 { IFilterSpec } from './fileTreeAccessors';\n\n/**\n * Checks if a path matches a single pattern (string or RegExp).\n * @param path - The path to check.\n * @param pattern - The pattern to match against.\n * @returns `true` if the path matches the pattern.\n * @internal\n */\nfunction matchesPattern(path: string, pattern: string | RegExp): boolean {\n if (typeof pattern === 'string') {\n return path === pattern || path.startsWith(pattern + '/') || path.includes(pattern);\n }\n return pattern.test(path);\n}\n\n/**\n * Checks if a path matches any pattern in an array.\n * @param path - The path to check.\n * @param patterns - The patterns to match against.\n * @returns `true` if the path matches any pattern.\n * @internal\n */\nfunction matchesAny(path: string, patterns: (string | RegExp)[] | undefined): boolean {\n if (!patterns || patterns.length === 0) {\n return false;\n }\n return patterns.some((pattern) => matchesPattern(path, pattern));\n}\n\n/**\n * Checks if a path is allowed by a mutability configuration.\n * @param path - The path to check.\n * @param mutable - The mutability configuration.\n * @returns `true` if the path is mutable according to the configuration.\n * @public\n */\nexport function isPathMutable(path: string, mutable: boolean | IFilterSpec | undefined): boolean {\n if (mutable === undefined || mutable === false) {\n return false;\n }\n\n if (mutable === true) {\n return true;\n }\n\n const { include, exclude } = mutable;\n\n // If exclude patterns are specified and path matches, it's not mutable\n if (matchesAny(path, exclude)) {\n return false;\n }\n\n // If include patterns are specified, path must match at least one\n if (include && include.length > 0) {\n return matchesAny(path, include);\n }\n\n // No include patterns means all paths (not excluded) are mutable\n return true;\n}\n"]}
|
|
@@ -26,55 +26,86 @@ export declare class FsFileTreeAccessors<TCT extends string = string> implements
|
|
|
26
26
|
*/
|
|
27
27
|
constructor(params?: IFileTreeInitParams<TCT>);
|
|
28
28
|
/**
|
|
29
|
-
*
|
|
29
|
+
* Resolves paths to an absolute path.
|
|
30
|
+
* @param paths - Paths to resolve.
|
|
31
|
+
* @returns The resolved absolute path.
|
|
30
32
|
*/
|
|
31
33
|
resolveAbsolutePath(...paths: string[]): string;
|
|
32
34
|
/**
|
|
33
|
-
*
|
|
35
|
+
* Gets the extension of a path.
|
|
36
|
+
* @param itemPath - Path to get the extension of.
|
|
37
|
+
* @returns The extension of the path.
|
|
34
38
|
*/
|
|
35
39
|
getExtension(itemPath: string): string;
|
|
36
40
|
/**
|
|
37
|
-
*
|
|
41
|
+
* Gets the base name of a path.
|
|
42
|
+
* @param itemPath - Path to get the base name of.
|
|
43
|
+
* @param suffix - Optional suffix to remove from the base name.
|
|
44
|
+
* @returns The base name of the path.
|
|
38
45
|
*/
|
|
39
46
|
getBaseName(itemPath: string, suffix?: string): string;
|
|
40
47
|
/**
|
|
41
|
-
*
|
|
48
|
+
* Joins paths together.
|
|
49
|
+
* @param paths - Paths to join.
|
|
50
|
+
* @returns The joined paths.
|
|
42
51
|
*/
|
|
43
52
|
joinPaths(...paths: string[]): string;
|
|
44
53
|
/**
|
|
45
|
-
*
|
|
54
|
+
* Gets an item from the file tree.
|
|
55
|
+
* @param itemPath - Path of the item to get.
|
|
56
|
+
* @returns The item if it exists.
|
|
46
57
|
*/
|
|
47
58
|
getItem(itemPath: string): Result<FileTreeItem<TCT>>;
|
|
48
59
|
/**
|
|
49
|
-
*
|
|
60
|
+
* Gets the contents of a file in the file tree.
|
|
61
|
+
* @param filePath - Absolute path of the file.
|
|
62
|
+
* @returns The contents of the file.
|
|
50
63
|
*/
|
|
51
64
|
getFileContents(filePath: string): Result<string>;
|
|
52
65
|
/**
|
|
53
|
-
*
|
|
66
|
+
* Gets the content type of a file in the file tree.
|
|
67
|
+
* @param filePath - Absolute path of the file.
|
|
68
|
+
* @param provided - Optional supplied content type.
|
|
69
|
+
* @returns The content type of the file.
|
|
54
70
|
*/
|
|
55
71
|
getFileContentType(filePath: string, provided?: string): Result<TCT | undefined>;
|
|
56
72
|
/**
|
|
57
|
-
*
|
|
73
|
+
* Gets the children of a directory in the file tree.
|
|
74
|
+
* @param dirPath - Path of the directory.
|
|
75
|
+
* @returns The children of the directory.
|
|
58
76
|
*/
|
|
59
77
|
getChildren(dirPath: string): Result<ReadonlyArray<FileTreeItem<TCT>>>;
|
|
60
78
|
/**
|
|
61
|
-
*
|
|
79
|
+
* Checks if a file at the given path can be saved.
|
|
80
|
+
* @param path - The path to check.
|
|
81
|
+
* @returns `DetailedSuccess` with {@link FileTree.SaveCapability} if the file can be saved,
|
|
82
|
+
* or `DetailedFailure` with {@link FileTree.SaveFailureReason} if it cannot.
|
|
62
83
|
*/
|
|
63
84
|
fileIsMutable(path: string): DetailedResult<boolean, SaveDetail>;
|
|
64
85
|
/**
|
|
65
|
-
*
|
|
86
|
+
* Saves the contents to a file at the given path.
|
|
87
|
+
* @param path - The path of the file to save.
|
|
88
|
+
* @param contents - The string contents to save.
|
|
89
|
+
* @returns `Success` if the file was saved, or `Failure` with an error message.
|
|
66
90
|
*/
|
|
67
91
|
saveFileContents(path: string, contents: string): Result<string>;
|
|
68
92
|
/**
|
|
69
|
-
*
|
|
93
|
+
* Deletes a file at the given path.
|
|
94
|
+
* @param path - The path of the file to delete.
|
|
95
|
+
* @returns `Success` with `true` if the file was deleted, or `Failure` with an error message.
|
|
70
96
|
*/
|
|
71
97
|
deleteFile(path: string): Result<boolean>;
|
|
72
98
|
/**
|
|
73
|
-
*
|
|
99
|
+
* Creates a directory at the given path, including any missing parent directories.
|
|
100
|
+
* @param dirPath - The path of the directory to create.
|
|
101
|
+
* @returns `Success` with the absolute path if created, or `Failure` with an error message.
|
|
74
102
|
*/
|
|
75
103
|
createDirectory(dirPath: string): Result<string>;
|
|
76
104
|
/**
|
|
77
|
-
*
|
|
105
|
+
* Deletes a directory at the given path.
|
|
106
|
+
* The directory must be empty or the operation will fail.
|
|
107
|
+
* @param dirPath - The path of the directory to delete.
|
|
108
|
+
* @returns `Success` with `true` if the directory was deleted, or `Failure` with an error message.
|
|
78
109
|
*/
|
|
79
110
|
deleteDirectory(dirPath: string): Result<boolean>;
|
|
80
111
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fsTree.d.ts","sourceRoot":"","sources":["../../../src/packlets/file-tree/fsTree.ts"],"names":[],"mappings":"AAsBA,OAAO,EACL,YAAY,EACZ,mBAAmB,EAEnB,yBAAyB,EACzB,UAAU,EACX,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EAEL,cAAc,EAGd,MAAM,EAGP,MAAM,eAAe,CAAC;AAKvB;;;;GAIG;AACH,qBAAa,mBAAmB,CAAC,GAAG,SAAS,MAAM,GAAG,MAAM,CAAE,YAAW,yBAAyB,CAAC,GAAG,CAAC;IACrG;;OAEG;IACH,SAAgB,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAE3C;;;OAGG;IACH,SAAS,CAAC,QAAQ,CAAC,iBAAiB,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,MAAM,CAAC,GAAG,GAAG,SAAS,CAAC,CAAC;IAEpF;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAwB;IAEjD;;;;OAIG;gBACgB,MAAM,CAAC,EAAE,mBAAmB,CAAC,GAAG,CAAC;IAOpD;;;;OAIG;IACI,mBAAmB,CAAC,GAAG,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM;IAOtD;;;;OAIG;IACI,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM;IAI7C;;;;;OAKG;IACI,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM;IAI7D;;;;OAIG;IACI,SAAS,CAAC,GAAG,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM;IAI5C;;;;OAIG;IACI,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;IAa3D;;;;OAIG;IACI,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IAIxD;;;;;OAKG;IACI,kBAAkB,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,GAAG,GAAG,SAAS,CAAC;IAQvF;;;;OAIG;IACI,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,aAAa,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC;IAgB7E;;;;;OAKG;IACI,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,cAAc,CAAC,OAAO,EAAE,UAAU,CAAC;IAgCvE;;;;;OAKG;IACI,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IAUvE;;;;OAIG;IACI,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC;IAchD;;;;OAIG;IACI,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IAcvD;;;;;OAKG;IACI,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC;CAczD"}
|
|
@@ -50,7 +50,9 @@ class FsFileTreeAccessors {
|
|
|
50
50
|
this._mutable = (_b = params === null || params === void 0 ? void 0 : params.mutable) !== null && _b !== void 0 ? _b : false;
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
|
-
*
|
|
53
|
+
* Resolves paths to an absolute path.
|
|
54
|
+
* @param paths - Paths to resolve.
|
|
55
|
+
* @returns The resolved absolute path.
|
|
54
56
|
*/
|
|
55
57
|
resolveAbsolutePath(...paths) {
|
|
56
58
|
if (this.prefix && !path_1.default.isAbsolute(paths[0])) {
|
|
@@ -59,25 +61,34 @@ class FsFileTreeAccessors {
|
|
|
59
61
|
return path_1.default.resolve(...paths);
|
|
60
62
|
}
|
|
61
63
|
/**
|
|
62
|
-
*
|
|
64
|
+
* Gets the extension of a path.
|
|
65
|
+
* @param itemPath - Path to get the extension of.
|
|
66
|
+
* @returns The extension of the path.
|
|
63
67
|
*/
|
|
64
68
|
getExtension(itemPath) {
|
|
65
69
|
return path_1.default.extname(itemPath);
|
|
66
70
|
}
|
|
67
71
|
/**
|
|
68
|
-
*
|
|
72
|
+
* Gets the base name of a path.
|
|
73
|
+
* @param itemPath - Path to get the base name of.
|
|
74
|
+
* @param suffix - Optional suffix to remove from the base name.
|
|
75
|
+
* @returns The base name of the path.
|
|
69
76
|
*/
|
|
70
77
|
getBaseName(itemPath, suffix) {
|
|
71
78
|
return path_1.default.basename(itemPath, suffix);
|
|
72
79
|
}
|
|
73
80
|
/**
|
|
74
|
-
*
|
|
81
|
+
* Joins paths together.
|
|
82
|
+
* @param paths - Paths to join.
|
|
83
|
+
* @returns The joined paths.
|
|
75
84
|
*/
|
|
76
85
|
joinPaths(...paths) {
|
|
77
86
|
return path_1.default.join(...paths);
|
|
78
87
|
}
|
|
79
88
|
/**
|
|
80
|
-
*
|
|
89
|
+
* Gets an item from the file tree.
|
|
90
|
+
* @param itemPath - Path of the item to get.
|
|
91
|
+
* @returns The item if it exists.
|
|
81
92
|
*/
|
|
82
93
|
getItem(itemPath) {
|
|
83
94
|
return (0, ts_utils_1.captureResult)(() => {
|
|
@@ -93,13 +104,18 @@ class FsFileTreeAccessors {
|
|
|
93
104
|
});
|
|
94
105
|
}
|
|
95
106
|
/**
|
|
96
|
-
*
|
|
107
|
+
* Gets the contents of a file in the file tree.
|
|
108
|
+
* @param filePath - Absolute path of the file.
|
|
109
|
+
* @returns The contents of the file.
|
|
97
110
|
*/
|
|
98
111
|
getFileContents(filePath) {
|
|
99
112
|
return (0, ts_utils_1.captureResult)(() => fs_1.default.readFileSync(this.resolveAbsolutePath(filePath), 'utf8'));
|
|
100
113
|
}
|
|
101
114
|
/**
|
|
102
|
-
*
|
|
115
|
+
* Gets the content type of a file in the file tree.
|
|
116
|
+
* @param filePath - Absolute path of the file.
|
|
117
|
+
* @param provided - Optional supplied content type.
|
|
118
|
+
* @returns The content type of the file.
|
|
103
119
|
*/
|
|
104
120
|
getFileContentType(filePath, provided) {
|
|
105
121
|
if (provided !== undefined) {
|
|
@@ -109,7 +125,9 @@ class FsFileTreeAccessors {
|
|
|
109
125
|
return this._inferContentType(filePath);
|
|
110
126
|
}
|
|
111
127
|
/**
|
|
112
|
-
*
|
|
128
|
+
* Gets the children of a directory in the file tree.
|
|
129
|
+
* @param dirPath - Path of the directory.
|
|
130
|
+
* @returns The children of the directory.
|
|
113
131
|
*/
|
|
114
132
|
getChildren(dirPath) {
|
|
115
133
|
return (0, ts_utils_1.captureResult)(() => {
|
|
@@ -128,7 +146,10 @@ class FsFileTreeAccessors {
|
|
|
128
146
|
});
|
|
129
147
|
}
|
|
130
148
|
/**
|
|
131
|
-
*
|
|
149
|
+
* Checks if a file at the given path can be saved.
|
|
150
|
+
* @param path - The path to check.
|
|
151
|
+
* @returns `DetailedSuccess` with {@link FileTree.SaveCapability} if the file can be saved,
|
|
152
|
+
* or `DetailedFailure` with {@link FileTree.SaveFailureReason} if it cannot.
|
|
132
153
|
*/
|
|
133
154
|
fileIsMutable(path) {
|
|
134
155
|
const absolutePath = this.resolveAbsolutePath(path);
|
|
@@ -161,7 +182,10 @@ class FsFileTreeAccessors {
|
|
|
161
182
|
}
|
|
162
183
|
}
|
|
163
184
|
/**
|
|
164
|
-
*
|
|
185
|
+
* Saves the contents to a file at the given path.
|
|
186
|
+
* @param path - The path of the file to save.
|
|
187
|
+
* @param contents - The string contents to save.
|
|
188
|
+
* @returns `Success` if the file was saved, or `Failure` with an error message.
|
|
165
189
|
*/
|
|
166
190
|
saveFileContents(path, contents) {
|
|
167
191
|
return this.fileIsMutable(path).asResult.onSuccess(() => {
|
|
@@ -173,7 +197,9 @@ class FsFileTreeAccessors {
|
|
|
173
197
|
});
|
|
174
198
|
}
|
|
175
199
|
/**
|
|
176
|
-
*
|
|
200
|
+
* Deletes a file at the given path.
|
|
201
|
+
* @param path - The path of the file to delete.
|
|
202
|
+
* @returns `Success` with `true` if the file was deleted, or `Failure` with an error message.
|
|
177
203
|
*/
|
|
178
204
|
deleteFile(path) {
|
|
179
205
|
return this.fileIsMutable(path).asResult.onSuccess(() => {
|
|
@@ -189,7 +215,9 @@ class FsFileTreeAccessors {
|
|
|
189
215
|
});
|
|
190
216
|
}
|
|
191
217
|
/**
|
|
192
|
-
*
|
|
218
|
+
* Creates a directory at the given path, including any missing parent directories.
|
|
219
|
+
* @param dirPath - The path of the directory to create.
|
|
220
|
+
* @returns `Success` with the absolute path if created, or `Failure` with an error message.
|
|
193
221
|
*/
|
|
194
222
|
createDirectory(dirPath) {
|
|
195
223
|
const absolutePath = this.resolveAbsolutePath(dirPath);
|
|
@@ -203,7 +231,10 @@ class FsFileTreeAccessors {
|
|
|
203
231
|
});
|
|
204
232
|
}
|
|
205
233
|
/**
|
|
206
|
-
*
|
|
234
|
+
* Deletes a directory at the given path.
|
|
235
|
+
* The directory must be empty or the operation will fail.
|
|
236
|
+
* @param dirPath - The path of the directory to delete.
|
|
237
|
+
* @returns `Success` with `true` if the directory was deleted, or `Failure` with an error message.
|
|
207
238
|
*/
|
|
208
239
|
deleteDirectory(dirPath) {
|
|
209
240
|
return this.fileIsMutable(dirPath).asResult.onSuccess(() => {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fsTree.js","sourceRoot":"","sources":["../../../src/packlets/file-tree/fsTree.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;AASH,gDAAwB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C3B,OAAO,IAAI,CAAC;YACd,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAnPD,kDAmPC","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 {\n FileTreeItem,\n IFileTreeInitParams,\n IFilterSpec,\n IMutableFileTreeAccessors,\n SaveDetail\n} from './fileTreeAccessors';\nimport path from 'path';\nimport fs from 'fs';\nimport {\n captureResult,\n DetailedResult,\n fail,\n failWithDetail,\n Result,\n succeed,\n succeedWithDetail\n} from '@fgv/ts-utils';\nimport { DirectoryItem } from './directoryItem';\nimport { FileItem } from './fileItem';\nimport { isPathMutable } from './filterSpec';\n\n/**\n * Implementation of {@link FileTree.IMutableFileTreeAccessors} that uses the\n * file system to access and modify files and directories.\n * @public\n */\nexport class FsFileTreeAccessors<TCT extends string = string> implements IMutableFileTreeAccessors<TCT> {\n /**\n * Optional path prefix to prepend to all paths.\n */\n public readonly prefix: string | undefined;\n\n /**\n * Function to infer the content type of a file.\n * @public\n */\n protected readonly _inferContentType: (filePath: string) => Result<TCT | undefined>;\n\n /**\n * The mutability configuration.\n */\n private readonly _mutable: boolean | IFilterSpec;\n\n /**\n * Construct a new instance of the {@link FileTree.FsFileTreeAccessors | FsFileTreeAccessors} class.\n * @param params - Optional {@link FileTree.IFileTreeInitParams | initialization parameters}.\n * @public\n */\n public constructor(params?: IFileTreeInitParams<TCT>) {\n this.prefix = params?.prefix;\n this._inferContentType = params?.inferContentType ?? FileItem.defaultInferContentType;\n /* c8 ignore next 1 - defensive default when params is undefined */\n this._mutable = params?.mutable ?? false;\n }\n\n /**\n * Resolves paths to an absolute path.\n * @param paths - Paths to resolve.\n * @returns The resolved absolute path.\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 * Gets the extension of a path.\n * @param itemPath - Path to get the extension of.\n * @returns The extension of the path.\n */\n public getExtension(itemPath: string): string {\n return path.extname(itemPath);\n }\n\n /**\n * Gets the base name of a path.\n * @param itemPath - Path to get the base name of.\n * @param suffix - Optional suffix to remove from the base name.\n * @returns The base name of the path.\n */\n public getBaseName(itemPath: string, suffix?: string): string {\n return path.basename(itemPath, suffix);\n }\n\n /**\n * Joins paths together.\n * @param paths - Paths to join.\n * @returns The joined paths.\n */\n public joinPaths(...paths: string[]): string {\n return path.join(...paths);\n }\n\n /**\n * Gets an item from the file tree.\n * @param itemPath - Path of the item to get.\n * @returns The item if it exists.\n */\n public getItem(itemPath: string): Result<FileTreeItem<TCT>> {\n return captureResult(() => {\n const stat = fs.statSync(this.resolveAbsolutePath(itemPath));\n if (stat.isDirectory()) {\n return DirectoryItem.create<TCT>(itemPath, this).orThrow();\n } else if (stat.isFile()) {\n return FileItem.create(itemPath, this).orThrow();\n }\n /* c8 ignore next 1 - defensive coding: filesystem items should be file or directory */\n throw new Error(`${itemPath}: not a file or directory`);\n });\n }\n\n /**\n * Gets the contents of a file in the file tree.\n * @param filePath - Absolute path of the file.\n * @returns The contents of the file.\n */\n public getFileContents(filePath: string): Result<string> {\n return captureResult(() => fs.readFileSync(this.resolveAbsolutePath(filePath), 'utf8'));\n }\n\n /**\n * Gets the content type of a file in the file tree.\n * @param filePath - Absolute path of the file.\n * @param provided - Optional supplied content type.\n * @returns The content type of the file.\n */\n public getFileContentType(filePath: string, provided?: string): Result<TCT | undefined> {\n if (provided !== undefined) {\n return succeed(provided as TCT);\n }\n /* c8 ignore next 2 - coverage has intermittent issues in the build - local tests show coverage of this line */\n return this._inferContentType(filePath);\n }\n\n /**\n * Gets the children of a directory in the file tree.\n * @param dirPath - Path of the directory.\n * @returns The children of the directory.\n */\n public getChildren(dirPath: string): Result<ReadonlyArray<FileTreeItem<TCT>>> {\n return captureResult(() => {\n const children: FileTreeItem<TCT>[] = [];\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<TCT>(fullPath, this).orThrow());\n } else if (file.isFile()) {\n children.push(FileItem.create<TCT>(fullPath, this).orThrow());\n }\n });\n return children;\n });\n }\n\n /**\n * Checks if a file at the given path can be saved.\n * @param path - The path to check.\n * @returns `DetailedSuccess` with {@link FileTree.SaveCapability} if the file can be saved,\n * or `DetailedFailure` with {@link FileTree.SaveFailureReason} if it cannot.\n */\n public fileIsMutable(path: string): DetailedResult<boolean, SaveDetail> {\n const absolutePath = this.resolveAbsolutePath(path);\n\n // Check if mutability is disabled\n if (this._mutable === false) {\n return failWithDetail(`${absolutePath}: mutability is disabled`, 'not-mutable');\n }\n\n // Check if path is excluded by filter\n if (!isPathMutable(absolutePath, this._mutable)) {\n return failWithDetail(`${absolutePath}: path is excluded by filter`, 'path-excluded');\n }\n\n // Check file system permissions\n try {\n // Check if file exists\n if (fs.existsSync(absolutePath)) {\n fs.accessSync(absolutePath, fs.constants.W_OK);\n } else {\n // Check if parent directory is writable\n const parentDir = absolutePath.substring(0, absolutePath.lastIndexOf('/'));\n if (parentDir && fs.existsSync(parentDir)) {\n fs.accessSync(parentDir, fs.constants.W_OK);\n }\n }\n return succeedWithDetail(true, 'persistent');\n /* c8 ignore next 3 - unreachable when running as root (CI), tested in mutableFsTree.test.ts */\n } catch {\n return failWithDetail(`${absolutePath}: permission denied`, 'permission-denied');\n }\n }\n\n /**\n * Saves the contents to a file at the given path.\n * @param path - The path of the file to save.\n * @param contents - The string contents to save.\n * @returns `Success` if the file was saved, or `Failure` with an error message.\n */\n public saveFileContents(path: string, contents: string): Result<string> {\n return this.fileIsMutable(path).asResult.onSuccess(() => {\n const absolutePath = this.resolveAbsolutePath(path);\n return captureResult(() => {\n fs.writeFileSync(absolutePath, contents, 'utf8');\n return contents;\n });\n });\n }\n\n /**\n * Deletes a file at the given path.\n * @param path - The path of the file to delete.\n * @returns `Success` with `true` if the file was deleted, or `Failure` with an error message.\n */\n public deleteFile(path: string): Result<boolean> {\n return this.fileIsMutable(path).asResult.onSuccess(() => {\n const absolutePath = this.resolveAbsolutePath(path);\n return captureResult(() => {\n const stat = fs.statSync(absolutePath);\n if (!stat.isFile()) {\n throw new Error(`${absolutePath}: not a file`);\n }\n fs.unlinkSync(absolutePath);\n return true;\n });\n });\n }\n\n /**\n * Creates a directory at the given path, including any missing parent directories.\n * @param dirPath - The path of the directory to create.\n * @returns `Success` with the absolute path if created, or `Failure` with an error message.\n */\n public createDirectory(dirPath: string): Result<string> {\n const absolutePath = this.resolveAbsolutePath(dirPath);\n\n // Check if mutability is disabled\n if (this._mutable === false) {\n return fail(`${absolutePath}: mutability is disabled`);\n }\n\n return captureResult(() => {\n fs.mkdirSync(absolutePath, { recursive: true });\n return absolutePath;\n });\n }\n\n /**\n * Deletes a directory at the given path.\n * The directory must be empty or the operation will fail.\n * @param dirPath - The path of the directory to delete.\n * @returns `Success` with `true` if the directory was deleted, or `Failure` with an error message.\n */\n public deleteDirectory(dirPath: string): Result<boolean> {\n return this.fileIsMutable(dirPath).asResult.onSuccess(() => {\n const absolutePath = this.resolveAbsolutePath(dirPath);\n return captureResult(() => {\n const stat = fs.statSync(absolutePath);\n if (!stat.isDirectory()) {\n throw new Error(`${absolutePath}: not a directory`);\n }\n // fs.rmdirSync fails if directory is non-empty (desired behavior)\n fs.rmdirSync(absolutePath);\n return true;\n });\n });\n }\n}\n"]}
|
|
@@ -51,56 +51,87 @@ export declare class InMemoryTreeAccessors<TCT extends string = string> implemen
|
|
|
51
51
|
*/
|
|
52
52
|
static create<TCT extends string = string>(files: IInMemoryFile<TCT>[], params?: IFileTreeInitParams<TCT>): Result<InMemoryTreeAccessors<TCT>>;
|
|
53
53
|
/**
|
|
54
|
-
*
|
|
54
|
+
* Resolves paths to an absolute path.
|
|
55
|
+
* @param paths - Paths to resolve.
|
|
56
|
+
* @returns The resolved absolute path.
|
|
55
57
|
*/
|
|
56
58
|
resolveAbsolutePath(...paths: string[]): string;
|
|
57
59
|
/**
|
|
58
|
-
*
|
|
60
|
+
* Gets the extension of a path.
|
|
61
|
+
* @param path - Path to get the extension of.
|
|
62
|
+
* @returns The extension of the path.
|
|
59
63
|
*/
|
|
60
64
|
getExtension(path: string): string;
|
|
61
65
|
/**
|
|
62
|
-
*
|
|
66
|
+
* Gets the base name of a path.
|
|
67
|
+
* @param path - Path to get the base name of.
|
|
68
|
+
* @param suffix - Optional suffix to remove from the base name.
|
|
69
|
+
* @returns The base name of the path.
|
|
63
70
|
*/
|
|
64
71
|
getBaseName(path: string, suffix?: string): string;
|
|
65
72
|
/**
|
|
66
|
-
*
|
|
73
|
+
* Joins paths together.
|
|
74
|
+
* @param paths - Paths to join.
|
|
75
|
+
* @returns The joined paths.
|
|
67
76
|
*/
|
|
68
77
|
joinPaths(...paths: string[]): string;
|
|
69
78
|
/**
|
|
70
|
-
*
|
|
79
|
+
* Gets an item from the file tree.
|
|
80
|
+
* @param itemPath - Path of the item to get.
|
|
81
|
+
* @returns The item if it exists.
|
|
71
82
|
*/
|
|
72
83
|
getItem(itemPath: string): Result<FileTreeItem<TCT>>;
|
|
73
84
|
/**
|
|
74
|
-
*
|
|
85
|
+
* Gets the contents of a file in the file tree.
|
|
86
|
+
* @param path - Absolute path of the file.
|
|
87
|
+
* @returns The contents of the file.
|
|
75
88
|
*/
|
|
76
89
|
getFileContents(path: string): Result<string>;
|
|
77
90
|
/**
|
|
78
|
-
*
|
|
91
|
+
* Gets the content type of a file in the file tree.
|
|
92
|
+
* @param path - Absolute path of the file.
|
|
93
|
+
* @param provided - Optional supplied content type.
|
|
94
|
+
* @returns The content type of the file.
|
|
79
95
|
*/
|
|
80
96
|
getFileContentType(path: string, provided?: string): Result<TCT | undefined>;
|
|
81
97
|
/**
|
|
82
|
-
*
|
|
98
|
+
* Gets the children of a directory in the file tree.
|
|
99
|
+
* @param path - Path of the directory.
|
|
100
|
+
* @returns The children of the directory.
|
|
83
101
|
*/
|
|
84
102
|
getChildren(path: string): Result<ReadonlyArray<FileTreeItem<TCT>>>;
|
|
85
103
|
private _addMutableFile;
|
|
86
104
|
/**
|
|
87
|
-
*
|
|
105
|
+
* Creates a directory at the given path, including any missing parent directories.
|
|
106
|
+
* @param dirPath - The path of the directory to create.
|
|
107
|
+
* @returns `Success` with the absolute path if created, or `Failure` with an error message.
|
|
88
108
|
*/
|
|
89
109
|
createDirectory(dirPath: string): Result<string>;
|
|
90
110
|
/**
|
|
91
|
-
*
|
|
111
|
+
* Checks if a file at the given path can be saved.
|
|
112
|
+
* @param path - The path to check.
|
|
113
|
+
* @returns `DetailedSuccess` with {@link FileTree.SaveCapability} if the file can be saved,
|
|
114
|
+
* or `DetailedFailure` with {@link FileTree.SaveFailureReason} if it cannot.
|
|
92
115
|
*/
|
|
93
116
|
fileIsMutable(path: string): DetailedResult<boolean, SaveDetail>;
|
|
94
117
|
/**
|
|
95
|
-
*
|
|
118
|
+
* Deletes a file at the given path.
|
|
119
|
+
* @param path - The path of the file to delete.
|
|
120
|
+
* @returns `Success` with `true` if the file was deleted, or `Failure` with an error message.
|
|
96
121
|
*/
|
|
97
122
|
deleteFile(path: string): Result<boolean>;
|
|
98
123
|
/**
|
|
99
|
-
*
|
|
124
|
+
* Deletes a directory at the given path.
|
|
125
|
+
* The directory must be empty or the operation will fail.
|
|
126
|
+
* @param path - The path of the directory to delete.
|
|
127
|
+
* @returns `Success` with `true` if the directory was deleted, or `Failure` with an error message.
|
|
100
128
|
*/
|
|
101
129
|
deleteDirectory(path: string): Result<boolean>;
|
|
102
130
|
/**
|
|
103
|
-
*
|
|
131
|
+
* Saves the contents to a file at the given path.
|
|
132
|
+
* @param path - The path of the file to save.
|
|
133
|
+
* @param contents - The string contents to save.
|
|
134
|
+
* @returns `Success` if the file was saved, or `Failure` with an error message.
|
|
104
135
|
*/
|
|
105
136
|
saveFileContents(path: string, contents: string): Result<string>;
|
|
106
137
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inMemoryTree.d.ts","sourceRoot":"","sources":["../../../../src/packlets/file-tree/in-memory/inMemoryTree.ts"],"names":[],"mappings":"AAsBA,OAAO,EAEL,cAAc,EAGd,MAAM,EAGP,MAAM,eAAe,CAAC;AAGvB,OAAO,EACL,YAAY,EACZ,mBAAmB,EAEnB,yBAAyB,EACzB,UAAU,EACX,MAAM,sBAAsB,CAAC;AAI9B;;;GAGG;AACH,MAAM,WAAW,aAAa,CAAC,GAAG,SAAS,MAAM,GAAG,MAAM;IACxD;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAE3B;;OAEG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,GAAG,CAAC;CAC5B;AA+FD;;;;GAIG;AACH,qBAAa,qBAAqB,CAAC,GAAG,SAAS,MAAM,GAAG,MAAM,CAAE,YAAW,yBAAyB,CAAC,GAA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,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IAepD;;;;;OAKG;IACI,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,GAAG,GAAG,SAAS,CAAC;IAsBnF;;;;OAIG;IACI,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,aAAa,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC;IAsB1E,OAAO,CAAC,eAAe;IAgCvB;;;;OAIG;IACI,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;IAiCvD;;;;;OAKG;IACI,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,cAAc,CAAC,OAAO,EAAE,UAAU,CAAC;IAgBvE;;;;OAIG;IACI,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC;IAmChD;;;;;OAKG;IACI,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC;IA6CrD;;;;;OAKG;IACI,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;CAsCxE"}
|
|
@@ -138,7 +138,9 @@ class InMemoryTreeAccessors {
|
|
|
138
138
|
return (0, ts_utils_1.captureResult)(() => new InMemoryTreeAccessors(files, params));
|
|
139
139
|
}
|
|
140
140
|
/**
|
|
141
|
-
*
|
|
141
|
+
* Resolves paths to an absolute path.
|
|
142
|
+
* @param paths - Paths to resolve.
|
|
143
|
+
* @returns The resolved absolute path.
|
|
142
144
|
*/
|
|
143
145
|
resolveAbsolutePath(...paths) {
|
|
144
146
|
const parts = paths[0].startsWith('/') ? paths : [this._tree.prefix, ...paths];
|
|
@@ -146,7 +148,9 @@ class InMemoryTreeAccessors {
|
|
|
146
148
|
return `/${joined}`;
|
|
147
149
|
}
|
|
148
150
|
/**
|
|
149
|
-
*
|
|
151
|
+
* Gets the extension of a path.
|
|
152
|
+
* @param path - Path to get the extension of.
|
|
153
|
+
* @returns The extension of the path.
|
|
150
154
|
*/
|
|
151
155
|
getExtension(path) {
|
|
152
156
|
const parts = path.split('.');
|
|
@@ -156,7 +160,10 @@ class InMemoryTreeAccessors {
|
|
|
156
160
|
return `.${parts.pop()}`;
|
|
157
161
|
}
|
|
158
162
|
/**
|
|
159
|
-
*
|
|
163
|
+
* Gets the base name of a path.
|
|
164
|
+
* @param path - Path to get the base name of.
|
|
165
|
+
* @param suffix - Optional suffix to remove from the base name.
|
|
166
|
+
* @returns The base name of the path.
|
|
160
167
|
*/
|
|
161
168
|
getBaseName(path, suffix) {
|
|
162
169
|
var _a;
|
|
@@ -168,7 +175,9 @@ class InMemoryTreeAccessors {
|
|
|
168
175
|
return base;
|
|
169
176
|
}
|
|
170
177
|
/**
|
|
171
|
-
*
|
|
178
|
+
* Joins paths together.
|
|
179
|
+
* @param paths - Paths to join.
|
|
180
|
+
* @returns The joined paths.
|
|
172
181
|
*/
|
|
173
182
|
joinPaths(...paths) {
|
|
174
183
|
var _a;
|
|
@@ -176,7 +185,9 @@ class InMemoryTreeAccessors {
|
|
|
176
185
|
return ((_a = paths[0]) === null || _a === void 0 ? void 0 : _a.startsWith('/')) ? `/${joined}` : joined;
|
|
177
186
|
}
|
|
178
187
|
/**
|
|
179
|
-
*
|
|
188
|
+
* Gets an item from the file tree.
|
|
189
|
+
* @param itemPath - Path of the item to get.
|
|
190
|
+
* @returns The item if it exists.
|
|
180
191
|
*/
|
|
181
192
|
getItem(itemPath) {
|
|
182
193
|
const existing = this._tree.byAbsolutePath.get(itemPath);
|
|
@@ -191,7 +202,9 @@ class InMemoryTreeAccessors {
|
|
|
191
202
|
return (0, ts_utils_1.fail)(`${itemPath}: not found`);
|
|
192
203
|
}
|
|
193
204
|
/**
|
|
194
|
-
*
|
|
205
|
+
* Gets the contents of a file in the file tree.
|
|
206
|
+
* @param path - Absolute path of the file.
|
|
207
|
+
* @returns The contents of the file.
|
|
195
208
|
*/
|
|
196
209
|
getFileContents(path) {
|
|
197
210
|
const absolutePath = this.resolveAbsolutePath(path);
|
|
@@ -208,7 +221,10 @@ class InMemoryTreeAccessors {
|
|
|
208
221
|
return (0, ts_utils_1.captureResult)(() => JSON.stringify(item.contents));
|
|
209
222
|
}
|
|
210
223
|
/**
|
|
211
|
-
*
|
|
224
|
+
* Gets the content type of a file in the file tree.
|
|
225
|
+
* @param path - Absolute path of the file.
|
|
226
|
+
* @param provided - Optional supplied content type.
|
|
227
|
+
* @returns The content type of the file.
|
|
212
228
|
*/
|
|
213
229
|
getFileContentType(path, provided) {
|
|
214
230
|
// If provided contentType is given, use it directly (highest priority)
|
|
@@ -231,7 +247,9 @@ class InMemoryTreeAccessors {
|
|
|
231
247
|
return this._inferContentType(path);
|
|
232
248
|
}
|
|
233
249
|
/**
|
|
234
|
-
*
|
|
250
|
+
* Gets the children of a directory in the file tree.
|
|
251
|
+
* @param path - Path of the directory.
|
|
252
|
+
* @returns The children of the directory.
|
|
235
253
|
*/
|
|
236
254
|
getChildren(path) {
|
|
237
255
|
const item = this._tree.byAbsolutePath.get(path);
|
|
@@ -281,7 +299,9 @@ class InMemoryTreeAccessors {
|
|
|
281
299
|
});
|
|
282
300
|
}
|
|
283
301
|
/**
|
|
284
|
-
*
|
|
302
|
+
* Creates a directory at the given path, including any missing parent directories.
|
|
303
|
+
* @param dirPath - The path of the directory to create.
|
|
304
|
+
* @returns `Success` with the absolute path if created, or `Failure` with an error message.
|
|
285
305
|
*/
|
|
286
306
|
createDirectory(dirPath) {
|
|
287
307
|
const absolutePath = this.resolveAbsolutePath(dirPath);
|
|
@@ -312,7 +332,10 @@ class InMemoryTreeAccessors {
|
|
|
312
332
|
return (0, ts_utils_1.succeed)(absolutePath);
|
|
313
333
|
}
|
|
314
334
|
/**
|
|
315
|
-
*
|
|
335
|
+
* Checks if a file at the given path can be saved.
|
|
336
|
+
* @param path - The path to check.
|
|
337
|
+
* @returns `DetailedSuccess` with {@link FileTree.SaveCapability} if the file can be saved,
|
|
338
|
+
* or `DetailedFailure` with {@link FileTree.SaveFailureReason} if it cannot.
|
|
316
339
|
*/
|
|
317
340
|
fileIsMutable(path) {
|
|
318
341
|
const absolutePath = this.resolveAbsolutePath(path);
|
|
@@ -327,7 +350,9 @@ class InMemoryTreeAccessors {
|
|
|
327
350
|
return (0, ts_utils_1.succeedWithDetail)(true, 'transient');
|
|
328
351
|
}
|
|
329
352
|
/**
|
|
330
|
-
*
|
|
353
|
+
* Deletes a file at the given path.
|
|
354
|
+
* @param path - The path of the file to delete.
|
|
355
|
+
* @returns `Success` with `true` if the file was deleted, or `Failure` with an error message.
|
|
331
356
|
*/
|
|
332
357
|
deleteFile(path) {
|
|
333
358
|
const absolutePath = this.resolveAbsolutePath(path);
|
|
@@ -359,7 +384,10 @@ class InMemoryTreeAccessors {
|
|
|
359
384
|
return (0, ts_utils_1.succeed)(true);
|
|
360
385
|
}
|
|
361
386
|
/**
|
|
362
|
-
*
|
|
387
|
+
* Deletes a directory at the given path.
|
|
388
|
+
* The directory must be empty or the operation will fail.
|
|
389
|
+
* @param path - The path of the directory to delete.
|
|
390
|
+
* @returns `Success` with `true` if the directory was deleted, or `Failure` with an error message.
|
|
363
391
|
*/
|
|
364
392
|
deleteDirectory(path) {
|
|
365
393
|
const absolutePath = this.resolveAbsolutePath(path);
|
|
@@ -399,7 +427,10 @@ class InMemoryTreeAccessors {
|
|
|
399
427
|
return (0, ts_utils_1.succeed)(true);
|
|
400
428
|
}
|
|
401
429
|
/**
|
|
402
|
-
*
|
|
430
|
+
* Saves the contents to a file at the given path.
|
|
431
|
+
* @param path - The path of the file to save.
|
|
432
|
+
* @param contents - The string contents to save.
|
|
433
|
+
* @returns `Success` if the file was saved, or `Failure` with an error message.
|
|
403
434
|
*/
|
|
404
435
|
saveFileContents(path, contents) {
|
|
405
436
|
const isMutable = this.fileIsMutable(path);
|