/** * Directory builder for fluent directory operations */ import type { ISmartFsProvider } from '../interfaces/mod.provider.js'; import type { TFileMode, IFileStats, IDirectoryEntry, IListOptions, } from '../interfaces/mod.types.js'; /** * Directory builder class for fluent directory operations * Configuration methods return `this` for chaining * Action methods return Promises for execution */ export class SmartFsDirectory { private provider: ISmartFsProvider; private path: string; // Configuration options private options: { recursive?: boolean; mode?: TFileMode; filter?: string | RegExp | ((entry: IDirectoryEntry) => boolean); includeStats?: boolean; } = {}; constructor(provider: ISmartFsProvider, path: string) { this.provider = provider; this.path = this.provider.normalizePath(path); } // --- Configuration Methods (return this for chaining) --- /** * Enable recursive operations (for list, create, delete) */ public recursive(): this { this.options.recursive = true; return this; } /** * Set directory permissions/mode * @param mode - Directory mode (e.g., 0o755) */ public mode(mode: TFileMode): this { this.options.mode = mode; return this; } /** * Filter directory entries * @param filter - String pattern, RegExp, or filter function * * @example * ```typescript * // String pattern (glob-like) * .filter('*.ts') * * // RegExp * .filter(/\.ts$/) * * // Function * .filter(entry => entry.isFile && entry.name.endsWith('.ts')) * ``` */ public filter(filter: string | RegExp | ((entry: IDirectoryEntry) => boolean)): this { this.options.filter = filter; return this; } /** * Include file statistics in directory listings */ public includeStats(): this { this.options.includeStats = true; return this; } // --- Action Methods (return Promises) --- /** * List directory contents * @returns Array of directory entries */ public async list(): Promise { const listOptions: IListOptions = { recursive: this.options.recursive, filter: this.options.filter, includeStats: this.options.includeStats, }; return this.provider.listDirectory(this.path, listOptions); } /** * Create the directory */ public async create(): Promise { return this.provider.createDirectory(this.path, { recursive: this.options.recursive, mode: this.options.mode, }); } /** * Delete the directory */ public async delete(): Promise { return this.provider.deleteDirectory(this.path, { recursive: this.options.recursive, }); } /** * Check if the directory exists * @returns True if directory exists */ public async exists(): Promise { return this.provider.directoryExists(this.path); } /** * Get directory statistics * @returns Directory stats */ public async stat(): Promise { return this.provider.directoryStat(this.path); } /** * Get the directory path */ public getPath(): string { return this.path; } }