fs-syn
Version:
Lightweight purely synchronous file operation utility for Node.js, built on native fs module with zero dependencies
90 lines (86 loc) • 3.44 kB
text/typescript
import * as fs from 'fs';
/**
* 同步文件操作工具(与fs-async对应的同步版本)
* 基于Node.js原生fs模块的同步方法实现,无第三方依赖
*/
interface CopyOptions {
force?: boolean;
preserveTimestamps?: boolean;
}
interface ExpandOptions {
cwd?: string;
dot?: boolean;
onlyFiles?: boolean;
onlyDirs?: boolean;
}
interface MoveOptions {
force?: boolean;
}
interface SymlinkOptions {
type?: 'file' | 'dir' | 'junction';
}
declare let defaultEncoding: BufferEncoding;
declare function copy(from: string, to: string, options?: CopyOptions): void;
declare function move(from: string, to: string, options?: MoveOptions): void;
declare function mkdir(dirPath: string): void;
/**
* 原生实现的路径匹配(替代glob)
* 支持基本的*和**模式
*/
declare function expand(patterns: string | string[], options?: ExpandOptions): string[];
declare function write(file: string, content: string | Buffer, options?: fs.WriteFileOptions): void;
declare function read(file: string, options?: {
encoding?: BufferEncoding;
flag?: string;
}): string | Buffer;
declare function readJSON<T = any>(file: string, options?: {
encoding?: BufferEncoding;
flag?: string;
}): T;
declare function writeJSON(file: string, data: any, spaces?: number): void;
declare function remove(p: string): void;
declare function exists(...paths: string[]): boolean;
declare function isDir(p: string): boolean;
declare function isFile(p: string): boolean;
declare function isLink(p: string): boolean;
declare function isPathAbsolute(p: string): boolean;
declare function doesPathContain(ancestor: string, ...paths: string[]): boolean;
declare function readDir(dir: string, options?: {
withFileTypes?: boolean;
}): string[] | fs.Dirent[];
declare function stat(p: string): fs.Stats;
declare function chmod(p: string, mode: number | string): void;
declare function createSymlink(target: string, linkPath: string, options?: SymlinkOptions): void;
declare function realpath(p: string): string;
declare function emptyDir(dir: string): void;
declare function ensureFile(file: string): void;
declare function appendFile(file: string, content: string | Buffer, options?: fs.WriteFileOptions): void;
declare function hashFile(file: string, algorithm?: string): string;
declare const _default: {
defaultEncoding: "utf-8";
copy: typeof copy;
move: typeof move;
mkdir: typeof mkdir;
expand: typeof expand;
write: typeof write;
read: typeof read;
readJSON: typeof readJSON;
writeJSON: typeof writeJSON;
remove: typeof remove;
exists: typeof exists;
isDir: typeof isDir;
isFile: typeof isFile;
isLink: typeof isLink;
isPathAbsolute: typeof isPathAbsolute;
doesPathContain: typeof doesPathContain;
readDir: typeof readDir;
stat: typeof stat;
chmod: typeof chmod;
createSymlink: typeof createSymlink;
realpath: typeof realpath;
emptyDir: typeof emptyDir;
ensureFile: typeof ensureFile;
appendFile: typeof appendFile;
hashFile: typeof hashFile;
};
export { type CopyOptions, type ExpandOptions, type MoveOptions, type SymlinkOptions, appendFile, chmod, copy, createSymlink, _default as default, defaultEncoding, doesPathContain, emptyDir, ensureFile, exists, expand, hashFile, isDir, isFile, isLink, isPathAbsolute, mkdir, move, read, readDir, readJSON, realpath, remove, stat, write, writeJSON };