UNPKG

scriptable-testlab

Version:

A lightweight, efficient tool designed to manage and update scripts for Scriptable.

77 lines 2.2 kB
import path from "path"; import { ERROR_MESSAGES, FileManagerError } from "../types/errors"; function validatePath(filePath, rootPath) { if (!filePath || typeof filePath !== "string") { throw new FileManagerError(ERROR_MESSAGES.INVALID_PATH, "INVALID_PATH", filePath); } const normalizedPath = path.normalize(filePath).replace(/\\/g, "/"); const normalizedRoot = path.normalize(rootPath).replace(/\\/g, "/"); if (!normalizedPath.startsWith(normalizedRoot)) { throw new FileManagerError(ERROR_MESSAGES.OUTSIDE_ROOT, "INVALID_PATH", filePath); } } function validateNode(node, expectedType, path2) { if (!node) { throw new FileManagerError( expectedType === "file" ? ERROR_MESSAGES.FILE_NOT_FOUND : ERROR_MESSAGES.DIRECTORY_NOT_FOUND, "NOT_FOUND", path2 ); } if (node.type !== expectedType) { throw new FileManagerError( expectedType === "file" ? ERROR_MESSAGES.NOT_A_FILE : ERROR_MESSAGES.NOT_A_DIRECTORY, "INVALID_PATH", path2 ); } } function cloneNode(node) { const clonedMetadata = { ...node.metadata, creationDate: new Date(node.metadata.creationDate), modificationDate: new Date(node.metadata.modificationDate), tags: node.metadata.tags ? new Set(node.metadata.tags) : void 0, extendedAttributes: node.metadata.extendedAttributes ? new Map(node.metadata.extendedAttributes) : void 0 }; if (node.type === "file") { return { type: "file", content: node.content, metadata: clonedMetadata }; } return { type: "directory", children: new Map(node.children), metadata: clonedMetadata }; } function calculateDirectorySize(dir) { if (dir.type !== "directory") { return 0; } let totalSize = 0; for (const child of dir.children?.values() ?? []) { if (child.type === "file") { totalSize += child.metadata.size; } else { totalSize += calculateDirectorySize(child); } } return totalSize; } const FileSystemUtils = { validatePath, validateNode, cloneNode, calculateDirectorySize }; export { FileSystemUtils, calculateDirectorySize, cloneNode, validateNode, validatePath }; //# sourceMappingURL=fs.js.map