/** * File system utilities for Node.js. * * This module provides common file system operations used across the library, * including directory traversal, glob matching, and file I/O helpers. * * Supports custom file system injection via `useFs()` for Electron or testing. * * @module */ import * as nodeFs from "node:fs"; import * as nodeFsp from "node:fs/promises"; /** File system module type (sync APIs) */ export type FsModule = typeof nodeFs; /** File system promises module type (async APIs) */ export type FsPromisesModule = typeof nodeFsp; /** * Inject a custom file system module. * * Useful for: * - Electron's `original-fs` to bypass ASAR * - Virtual file systems like `memfs` for testing * * Call without arguments to reset to default Node.js fs. * * @example * ```ts * import originalFs from "original-fs"; * import { useFs } from "./fs.js"; * * // Use Electron's original-fs * useFs(originalFs); * * // Reset to default * useFs(); * ``` */ export declare function useFs(syncFs?: FsModule, asyncFs?: FsPromisesModule): void; export { globToRegex, matchGlob, matchGlobAny, createGlobMatcher, clearGlobCache, normalizePath } from "./glob.js"; /** * Information about a file system entry. */ export interface FileEntry { /** Absolute path on disk */ absolutePath: string; /** Relative path from the base directory */ relativePath: string; /** Whether this is a directory */ isDirectory: boolean; /** File size in bytes (0 for directories) */ size: number; /** Last modified time */ mtime: Date; /** Last access time */ atime: Date; /** Metadata change time */ ctime: Date; /** Creation time (when supported by the platform) */ birthTime: Date; /** Unix mode (includes file type + permissions). */ mode: number; } /** * Options for directory traversal. */ export interface TraverseOptions { /** Recursively traverse subdirectories (default: true) */ recursive?: boolean; /** Follow symbolic links (default: false) */ followSymlinks?: boolean; /** Filter function */ filter?: (entry: FileEntry) => boolean; } /** * Options for glob file matching. */ export interface GlobOptions { /** Current working directory */ cwd?: string; /** Patterns to ignore */ ignore?: string | string[]; /** Include dot files (default: false) */ dot?: boolean; /** Follow symbolic links (default: false) */ followSymlinks?: boolean; /** Filter function */ filter?: (entry: FileEntry) => boolean; } /** * Recursively traverse a directory and yield file entries. * * @param dirPath - Directory to traverse * @param options - Traversal options * @yields File entries */ export declare function traverseDirectory(dirPath: string, options?: TraverseOptions): AsyncGenerator; /** * Synchronously traverse a directory. */ export declare function traverseDirectorySync(dirPath: string, options?: TraverseOptions): FileEntry[]; /** * Find files matching a glob pattern. * * @param pattern - Glob pattern to match * @param options - Glob options * @yields Matching file entries */ export declare function glob(pattern: string, options?: GlobOptions): AsyncGenerator; /** * Synchronously find files matching a glob pattern. */ export declare function globSync(pattern: string, options?: GlobOptions): FileEntry[]; /** * Check if a file exists. */ export declare function fileExists(filePath: string): Promise; /** * Synchronously check if a file exists. */ export declare function fileExistsSync(filePath: string): boolean; /** * Ensure a directory exists, creating it recursively if needed. */ export declare function ensureDir(dirPath: string): Promise; /** * Synchronously ensure a directory exists. */ export declare function ensureDirSync(dirPath: string): void; /** * Get file stats, or null if file doesn't exist. */ export declare function safeStats(filePath: string): Promise; /** * Synchronously get file stats, or null if file doesn't exist. */ export declare function safeStatsSync(filePath: string): nodeFs.Stats | null; /** * Read a file as Uint8Array. */ export declare function readFileBytes(filePath: string): Promise; /** * Synchronously read a file as Uint8Array. */ export declare function readFileBytesSync(filePath: string): Uint8Array; /** * Write bytes to a file. */ export declare function writeFileBytes(filePath: string, data: Uint8Array): Promise; /** * Synchronously write bytes to a file. */ export declare function writeFileBytesSync(filePath: string, data: Uint8Array): void; /** * Set file modification time. */ export declare function setFileTime(filePath: string, mtime: Date): Promise; /** * Synchronously set file modification time. */ export declare function setFileTimeSync(filePath: string, mtime: Date): void; /** * Read file as text. */ export declare function readFileText(filePath: string, encoding?: BufferEncoding): Promise; /** * Synchronously read file as text. */ export declare function readFileTextSync(filePath: string, encoding?: BufferEncoding): string; /** * Write text to a file. */ export declare function writeFileText(filePath: string, content: string, encoding?: BufferEncoding): Promise; /** * Synchronously write text to a file. */ export declare function writeFileTextSync(filePath: string, content: string, encoding?: BufferEncoding): void; /** * Remove a file or directory. */ export declare function remove(targetPath: string): Promise; /** * Synchronously remove a file or directory. */ export declare function removeSync(targetPath: string): void; /** * Copy a file. */ export declare function copyFile(src: string, dest: string): Promise; /** * Synchronously copy a file. */ export declare function copyFileSync(src: string, dest: string): void; /** * Create a symbolic link. * * @param target - The path the symlink points to * @param linkPath - The path where the symlink will be created */ export declare function createSymlink(target: string, linkPath: string): Promise; /** * Synchronously create a symbolic link. * * @param target - The path the symlink points to * @param linkPath - The path where the symlink will be created */ export declare function createSymlinkSync(target: string, linkPath: string): void; /** * Change file permissions (Unix mode). * * @param filePath - Path to the file * @param mode - Unix permission mode (e.g., 0o755) */ export declare function chmod(filePath: string, mode: number): Promise; /** * Synchronously change file permissions (Unix mode). * * @param filePath - Path to the file * @param mode - Unix permission mode (e.g., 0o755) */ export declare function chmodSync(filePath: string, mode: number): void; /** * Check if the current platform supports Unix permissions. * Returns true on Unix-like systems (Linux, macOS), false on Windows. */ export declare function supportsUnixPermissions(): boolean; /** * Options for creating a read stream. */ export interface ReadStreamOptions { /** File encoding (default: none, returns Buffer) */ encoding?: BufferEncoding | null; /** High water mark for internal buffer (default: 64KB) */ highWaterMark?: number; /** Start position in bytes */ start?: number; /** End position in bytes */ end?: number; /** Auto close on end or error (default: true) */ autoClose?: boolean; } /** * Options for creating a write stream. */ export interface WriteStreamOptions { /** File encoding (default: 'utf8') */ encoding?: BufferEncoding; /** High water mark for internal buffer (default: 64KB) */ highWaterMark?: number; /** File flags (default: 'w') */ flags?: string; /** File mode (default: 0o666) */ mode?: number; /** Auto close on end or error (default: true) */ autoClose?: boolean; } /** * Create a readable stream from a file. * * @param filePath - Path to the file * @param options - Stream options * @returns A readable stream */ export declare function createReadStream(filePath: string, options?: ReadStreamOptions): nodeFs.ReadStream; /** * Create a writable stream to a file. * * @param filePath - Path to the file * @param options - Stream options * @returns A writable stream */ export declare function createWriteStream(filePath: string, options?: WriteStreamOptions): nodeFs.WriteStream; /** * Create a temporary directory. * * @param prefix - Prefix for the directory name * @returns Path to the created directory */ export declare function createTempDir(prefix?: string): Promise; /** * Synchronously create a temporary directory. * * @param prefix - Prefix for the directory name * @returns Path to the created directory */ export declare function createTempDirSync(prefix?: string): string;