import type LightningFS from '@isomorphic-git/lightning-fs'; import type { JSRuntimeFS, DirectoryEntry } from './JSRuntime'; /** * LightningFS adapter that implements JSRuntimeFS interface * This makes LightningFS compatible with the unified filesystem interface */ export class LightningFSAdapter implements JSRuntimeFS { constructor(private lightningFS: LightningFS.PromisifiedFS) {} async readFile(path: string): Promise; async readFile(path: string, options: 'utf8'): Promise; async readFile(path: string, options: string): Promise; async readFile(path: string, options: { encoding: 'utf8' }): Promise; async readFile(path: string, options: { encoding: string }): Promise; async readFile(path: string, options?: string | { encoding?: string }): Promise; async readFile(path: string, options?: string | { encoding?: string }): Promise { const encoding = typeof options === 'string' ? options : options?.encoding; return this.lightningFS.readFile(path, encoding); } async writeFile(path: string, data: string | Uint8Array, options?: string | { encoding?: string }): Promise { const encoding = typeof options === 'string' ? options : options?.encoding; return this.lightningFS.writeFile(path, data, encoding); } async readdir(path: string): Promise; async readdir(path: string, options: { withFileTypes: true }): Promise; async readdir(path: string, options?: { withFileTypes?: boolean }): Promise; async readdir(path: string, options?: { withFileTypes?: boolean }): Promise { if (options?.withFileTypes) { // LightningFS doesn't support withFileTypes, so we need to simulate it const items = await this.lightningFS.readdir(path); const entries: DirectoryEntry[] = []; for (const item of items) { const itemPath = `${path}/${item}`; try { const stat = await this.lightningFS.stat(itemPath); entries.push({ name: item, isDirectory: () => stat.isDirectory(), isFile: () => stat.isFile(), }); } catch { // If stat fails, assume it's a file entries.push({ name: item, isDirectory: () => false, isFile: () => true, }); } } return entries; } else { return this.lightningFS.readdir(path); } } async mkdir(path: string, options?: { recursive?: boolean }): Promise { if (options?.recursive) { // LightningFS doesn't support recursive option, so we need to create directories one by one const parts = path.split('/').filter(Boolean); let currentPath = path.startsWith('/') ? '/' : ''; for (const part of parts) { currentPath = currentPath === '/' ? `/${part}` : `${currentPath}/${part}`; try { await this.lightningFS.mkdir(currentPath); } catch { // Directory might already exist, continue } } } else { return this.lightningFS.mkdir(path); } } async stat(path: string): Promise<{ isDirectory(): boolean; isFile(): boolean; size?: number; mtimeMs?: number; }> { return this.lightningFS.stat(path); } async lstat(path: string): Promise<{ isDirectory(): boolean; isFile(): boolean; size?: number; mtimeMs?: number; }> { return this.lightningFS.lstat(path); } async unlink(path: string): Promise { return this.lightningFS.unlink(path); } async rmdir(path: string): Promise { return this.lightningFS.rmdir(path); } async rename(oldPath: string, newPath: string): Promise { return this.lightningFS.rename(oldPath, newPath); } async readlink(path: string): Promise { return this.lightningFS.readlink(path); } async symlink(target: string, path: string): Promise { return this.lightningFS.symlink(target, path); } }