import type FS from "@servicenow/glider-lightning-fs"; import { OrogeneFileSystem } from "./ts-pkg-manager"; import { nfs } from "../fs-ref"; import { dirname, sep } from "path"; export class LFS implements OrogeneFileSystem { private efs: any; private encoder = new TextEncoder(); constructor() { this.efs = nfs.promises; } async readDirectory(_path: string): Promise { if (await this.directoryExists(_path)) { return await this.efs.readdir(_path); } return []; } async removeDirectory( path: string, options?: { recursive?: boolean }, ): Promise { if (await this.directoryExists(path)) { await this.efs.rmdir(path, options); } } async createDirectory(_path: string): Promise { try { await this.efs.mkdir(_path); } catch (error) { // nada } } async directoryExists(_path: string): Promise { try { const stats = await this.efs.stat(_path); return stats.type === "dir"; } catch (_) { return false; } } async readFile( _path: string, _encoding?: string, ): Promise { if (!(await this.fileExists(_path))) { return undefined; } const data = await this.efs.readFile(_path, { encoding: "utf8" }); return data; } async writeFile( path: string, content: string | Uint8Array, options?: { encoding?: "utf8"; mode?: number }, ): Promise { if (options?.encoding === "utf8") { const encoded = this.encoder.encode(content); await this.efs.writeFile(path, encoded); } else { await this.efs.writeFile(path, content); } } async deleteFile(_path: string): Promise { if (!(await this.fileExists(_path))) { return; } await this.efs.unlink(_path); } async fileExists(_path: string): Promise { try { const stat = await this.efs.stat(_path); return stat.type === "file"; } catch (error) { return false; } } }