import type { FileSystem } from "@servicenow/sdk-build-core"; import type { IsomorphicRollupFs, Stats } from "../fs-iface"; import type { Dirent, Stats as fstats } from "fs"; /** * Filesystem wrapper around the filesystem passed in by fluent */ export class RollupFsAdapter implements IsomorphicRollupFs { promises: { lstat: (path: string) => Promise; readdir: ( path: string, options: { withFileTypes: true }, ) => Promise; readlink: (path: string) => Promise; realpath: (path: string) => Promise; }; constructor(private readonly fs: FileSystem) { // Initialized in constructor body so that arrow-function fields // (lstatPromise, readdirPromise, etc.) are already assigned. // Class field initializers run top-to-bottom before the constructor // body, so a field-level `promises = { ... }` would capture // `undefined` for fields declared below it. this.promises = { lstat: this.lstatPromise, readdir: this.readdirPromise, readlink: this.readlinkPromise, realpath: this.realpath, }; } realpath = (path: string): Promise => { return new Promise((res, rej) => { try { res(this.fs.realpathSync(path)); } catch (e) { rej(e); } }); }; realpathSync(path: string): string { return this.fs.realpathSync(path); } async access(p: string): Promise { try { const stat = this.fs.statSync(p); return !!stat; } catch (_) { return false; } } readFile(path: string, encoding?: "utf8"): Promise { return new Promise((res, rej) => { try { const options: { encoding: "utf-8" } | undefined = encoding ? { encoding: "utf-8" } : undefined; res(this.fs.readFileSync(path, options) as string | Uint8Array); } catch (e) { rej(e); } }); } stat(path: string): Promise { return new Promise((res, rej) => { try { res(this.fs.statSync(path) as unknown as Stats); } catch (e) { rej(e); } }); } lstat = ( path: string, cb: (er: NodeJS.ErrnoException | null, stats?: fstats) => void, ): void => { this.lstatPromise(path) .then((data) => cb(null, data)) .catch((err) => cb(err)); }; readdir = ( path: string, options: { withFileTypes: true }, cb: (er: NodeJS.ErrnoException | null, entries?: Dirent[]) => void, ): void => { this.readdirPromise(path, options) .then((data) => cb(null, data)) .catch((err) => cb(err)); }; // Promises-based Methods — arrow functions to preserve `this` binding // when assigned to `this.promises.*` (glob/path-scurry calls them // detached from the adapter instance). lstatPromise = (path: string): Promise => { return new Promise((res, rej) => { try { res(this.fs.statSync(path) as fstats); } catch (e) { rej(e); } }); }; readdirPromise = ( path: string, options: { withFileTypes: true }, ): Promise => { return new Promise((res, rej) => { try { const entries = this.fs.readdirSync(path, options); res(entries as Dirent[]); } catch (e) { rej(e); } }); }; readlinkPromise = async (path: string): Promise => { return this.realpath(path); }; mkdir(path: string, options?: { recursive?: boolean }): Promise { return new Promise((res, rej) => { try { if (options?.recursive) { this.fs.mkdirSync(path, { recursive: true }); } else { this.fs.mkdirSync(path); } res(); } catch (e) { rej(e); } }); } writeFile( path: string, data: string | Uint8Array, encoding?: "utf8", ): Promise { return new Promise((res, rej) => { try { const options: { encoding: "utf-8" } | undefined = encoding ? { encoding: "utf-8" } : undefined; this.fs.writeFileSync(path, data, options); res(); } catch (e) { rej(e); } }); } }