UNPKG

yann-fs

Version:

Enhanced fs-extra with better error handling

261 lines (222 loc) 5.21 kB
import type { PathLike } from 'fs' import type { CopyOptions, MoveOptions, JsonWriteOptions, EnsureDirOptions, WriteFileOptions, ObjectEncodingOptions, Mode, FSWatcher, JsonReadOptions, JsonOutputOptions } from 'fs-extra' declare namespace YannFs { /** * 全局错误处理配置接口 */ export interface FsConfig { errHandler: (err: Error) => void } /** * 统一错误处理返回类型 */ export type ErrorResult<T> = Promise<[Error, undefined] | [null, T]> /** * 文件系统操作接口 */ export interface FileSystem { /** * 复制文件或目录 */ copy(src: string, dest: string, options?: CopyOptions): ErrorResult<void> /** * 移动文件或目录 */ move(src: string, dest: string, options?: MoveOptions): ErrorResult<void> /** * 删除文件或目录 */ remove(path: string): ErrorResult<void> /** * 清空目录但不删除目录本身 */ emptyDir(path: string): ErrorResult<void> /** * 确保目录存在,如果不存在则创建 */ ensureDir(path: string, options?: EnsureDirOptions): ErrorResult<void> /** * 确保文件存在,如果不存在则创建 */ ensureFile(path: string): ErrorResult<void> /** * 检查路径是否存在 */ pathExists(path: string): ErrorResult<boolean> /** * 读取文件内容 */ readFile(path: string, options?: WriteFileOptions): ErrorResult<Buffer> /** * 写入文件内容 */ writeFile(file: string, data: any, options?: WriteFileOptions): ErrorResult<void> /** * 追加内容到文件 */ appendFile(path: string, data: any, options?: WriteFileOptions): ErrorResult<void> /** * 读取 JSON 文件 */ readJson<T = any>(path: string, options?: JsonReadOptions): ErrorResult<T> /** * 写入 JSON 文件 */ writeJson(file: string, data: any, options?: JsonWriteOptions): ErrorResult<void> /** * 写入 JSON 文件,如果目录不存在则创建 */ outputJson(file: string, data: any, options?: JsonOutputOptions): ErrorResult<void> /** * 创建硬链接 */ ensureLink(src: string, dest: string): ErrorResult<void> /** * 创建符号链接 */ ensureSymlink(src: string, dest: string): ErrorResult<void> /** * 获取文件状态信息 */ stat(path: PathLike): ErrorResult<import('fs').Stats> /** * 检查路径是否为目录 */ isDir(path: PathLike, errHandler?: (err: Error) => void): Promise<boolean | undefined> /** * 修改文件权限 */ chmod(path: PathLike, mode: Mode): ErrorResult<void> /** * 修改文件所有者 */ chown(path: PathLike, uid: number, gid: number): ErrorResult<void> /** * 创建文件 */ createFile(file: string): ErrorResult<void> /** * 创建硬链接 */ createLink(src: string, dest: string): ErrorResult<void> /** * 创建符号链接 */ createSymlink(src: string, dest: string): ErrorResult<void> /** * 创建目录 */ mkdir(dir: string, options?: EnsureDirOptions): ErrorResult<void> /** * 递归创建目录 */ mkdirs(dir: string, options?: EnsureDirOptions): ErrorResult<void> /** * 创建临时目录 */ mkdtemp(prefix: string, options?: ObjectEncodingOptions): ErrorResult<string> /** * 读取目录内容 */ readdir( path: string, options?: ObjectEncodingOptions & { withFileTypes?: false } ): ErrorResult<string[]> /** * 获取文件状态信息(不解析符号链接) */ lstat(path: PathLike): ErrorResult<import('fs').Stats> /** * 检查文件是否存在 */ exists(path: string): Promise<boolean> /** * 截断文件 */ truncate(path: PathLike, len?: number): ErrorResult<void> /** * 删除文件 */ unlink(path: PathLike): ErrorResult<void> /** * 删除目录 */ rmdir(path: string): ErrorResult<void> /** * 检查文件访问权限 */ access(path: PathLike, mode?: number): ErrorResult<void> /** * 修改文件时间戳 */ utimes( path: PathLike, atime: string | number | Date, mtime: string | number | Date ): ErrorResult<void> /** * 监听文件或目录变化 */ watch: typeof import('fs').watch } } // 导出所有函数实现为 YannFs.FileSystem 的实例 export const { copy, move, remove, emptyDir, ensureDir, ensureFile, pathExists, readFile, writeFile, appendFile, readJson, writeJson, outputJson, ensureLink, ensureSymlink, stat, isDir, chmod, chown, createFile, createLink, createSymlink, mkdir, mkdirs, mkdtemp, readdir, lstat, exists, truncate, unlink, rmdir, access, utimes, watch }: YannFs.FileSystem // 导出类型 export type { CopyOptions, MoveOptions, JsonWriteOptions, EnsureDirOptions, JsonReadOptions, WriteFileOptions, ObjectEncodingOptions, Mode, FSWatcher }