import { Stats } from "@servicenow/glider-lightning-fs"; import { Dirent, Stats as fstats } from "node:fs"; import * as path from "path"; import type { IsomorphicRollupFs } from "../fs-iface"; import type fs from "@servicenow/glider-lightning-fs"; export class ExampleFS implements IsomorphicRollupFs { private dec = new TextDecoder(); promises: any; constructor(readonly nfs: fs) { // Initialize the promises interface using LightningFS this.promises = { lstat: this.lstatPromise, readdir: this.readdirPromise, readlink: this.readlinkPromise, realpath: this.realpathPromise, }; } async access(p: string): Promise { try { const stat = await this.nfs.promises.stat(p); return !!stat; } catch (e) { return false; } } async readFile( p: string, encoding?: BufferEncoding, ): Promise { return await this.nfs.promises.readFile(p, encoding); } async realpath(p: string): Promise { const normalized = path.normalize(p); return normalized; } async stat(path: string): Promise { return await this.nfs.promises.stat(path); } async mkdir(path: string, options?: { recursive?: boolean }): Promise { console.log(`mkdir ${path}`); await this.nfs.promises.mkdir(path); } async writeFile( path: string, data: string | Uint8Array, encoding?: "utf8", ): Promise { await this.nfs.promises.writeFile(path, data, encoding); } lstatSync(path: string): fstats { // return this.nfs.lstatSync(path); throw new Error("Method not implemented."); } readdir = ( path: string, options: { withFileTypes: true }, cb: (er: NodeJS.ErrnoException | null, entries?: Dirent[]) => any, ): void => { this.readdirPromise(path, options) .then((data) => cb(null, data)) .catch((err) => cb(err)); }; readdirSync(path: string, options: { withFileTypes: true }): Dirent[] { //return this.nfs.readdirSync(path, options); throw new Error("Method not implemented."); } readlinkSync(path: string): string { return ""; } realpathSync(path: string): string { return path; } // Promises-based Methods async lstatPromise(path: string): Promise { return this.nfs.promises.stat(path); } async readdirPromise( path: string, options: { withFileTypes: true }, ): Promise { const entries = await this.nfs.promises.readdir(path); if (options.withFileTypes) { return entries.map((entry) => this.createDirent(entry)); } return entries.map((entry) => this.createDirent(entry)); } async readlinkPromise(path: string): Promise { return this.nfs.promises.readlink(path); } async realpathPromise(path: string): Promise { return Promise.resolve(path); } private createDirent(entry: string): Dirent { return { name: entry, isDirectory: async () => (await this.stat(entry)).isDirectory(), isFile: async () => (await this.stat(entry)).isFile(), isSymbolicLink: () => false, // Other Dirent methods can be added as needed } as unknown as Dirent; } }