import type { FileSystem } from "@servicenow/sdk-build-core"; import type { BufferEncoding, RollupDirectoryEntry, RollupFileStats, RollupFsModule, } from "rollup"; import { join, isAbsolute } from "path"; const resolvePath = (path: string) => { if (isAbsolute(path)) return path; return join(process.cwd(), path); }; export default (fs: FileSystem): RollupFsModule => ({ async appendFile( path: string, data: string | Uint8Array, options?: { encoding?: BufferEncoding | null; mode?: string | number; flag?: string | number; }, ): Promise { return fs.writeFileSync(resolvePath(path), data); }, async copyFile( source: string, destination: string, mode?: string | number, ): Promise { return fs.copyFileSync(source, destination); }, async mkdir( path: string, options?: { recursive?: boolean; mode?: string | number }, ): Promise { if (options?.recursive) { fs.mkdirSync(resolvePath(path), { recursive: true }); } else { fs.mkdirSync(resolvePath(path)); } }, async mkdtemp(prefix: string): Promise { const random = await crypto.randomUUID(); const path = `${prefix}${random}`; this.mkdir(resolvePath(path)); return path; }, // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore async readdir( path: string, options?: { withFileTypes: true }, ): Promise { return fs.readdirSync(resolvePath(path), options) as RollupDirectoryEntry[]; }, // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore async readFile( path: string, options?: { encoding: BufferEncoding; flag?: string | number; signal?: AbortSignal; }, ) { if (options?.encoding) { return fs.readFileSync(resolvePath(path), { encoding: "utf-8", }) as string; } return fs.readFileSync(resolvePath(path)) as Uint8Array; }, async realpath(path: string): Promise { return fs.realpathSync(resolvePath(path)); }, async rename(oldPath: string, newPath: string): Promise { return fs.renameSync(resolvePath(oldPath), resolvePath(newPath)); }, async rmdir(path: string, options?: { recursive?: boolean }): Promise { return fs.rmSync(resolvePath(path), options); }, async stat(path: string): Promise { return fs.statSync(resolvePath(path)) as unknown as RollupFileStats; }, async lstat(path: string): Promise { return fs.lstatSync(resolvePath(path)) as unknown as RollupFileStats; }, async unlink(path: string): Promise { return; }, async writeFile( path: string, data: string | Uint8Array, options?: { encoding?: BufferEncoding | null; mode?: string | number; flag?: string | number; }, ): Promise { if (options?.encoding) { fs.writeFileSync(resolvePath(path), data, { encoding: "utf-8" }); } else { fs.writeFileSync(resolvePath(path), data); } }, });