UNPKG

alacritty-theme-switch

Version:
54 lines (53 loc) 2.02 kB
import * as dntShim from "../../_dnt.shims.js"; import { ensureDir } from "../../deps/jsr.io/@std/fs/1.0.21/ensure_dir.js"; import { walk } from "../../deps/jsr.io/@std/fs/1.0.21/walk.js"; import { fromPromise } from "neverthrow"; import { DirectoryNotAccessibleError, FileDeletionError, FileNotFoundError, WriteError, } from "./fs-errors.js"; /** * Safely walks a directory and returns all entries. * * @param root - Root directory to walk * @param options - Optional walk options * @returns A ResultAsync containing an array of all entries or an error */ export function safeWalkAll(root, options) { return fromPromise(Array.fromAsync(walk(root, options)), (error) => new DirectoryNotAccessibleError(root, { cause: error })); } /** * Safely deletes a file. * * @param path - Path to the file * @param options - Optional remove options * @returns A ResultAsync containing void or an error */ export function safeDeleteFile(path, options) { return fromPromise(dntShim.Deno.remove(path, options), (error) => new FileDeletionError(path, { cause: error })); } /** * Safely ensures a directory exists, creating it if necessary. * * @param dirPath - Path to the directory * @returns A ResultAsync containing void or an error */ export function safeEnsureDir(dirPath) { return fromPromise(ensureDir(dirPath), (error) => new DirectoryNotAccessibleError(dirPath, { cause: error })); } /** * Safely writes a file. * * @param path - Path to the file * @param content - Content to write to the file * @returns A ResultAsync containing void or an error */ export function safeWriteFile(path, content) { return fromPromise(dntShim.Deno.writeTextFile(path, content), (error) => new WriteError(path, { cause: error })); } /** * Safely stats a file. * * @param path - Path to the file * @returns A ResultAsync containing the file stats or an error */ export function safeStat(path) { return fromPromise(dntShim.Deno.stat(path), (error) => new FileNotFoundError(path, { cause: error })); }