diff --git a/src/lib/JSRuntime.ts b/src/lib/JSRuntime.ts index 349d789c..54aefebd 100644 --- a/src/lib/JSRuntime.ts +++ b/src/lib/JSRuntime.ts @@ -5,11 +5,13 @@ export interface JSRuntimeFS { // Core file operations with proper overloads for type safety readFile(path: string): Promise; - readFile(path: string, encoding: 'utf8'): Promise; - readFile(path: string, encoding: string): Promise; - readFile(path: string, encoding?: string): Promise; + readFile(path: string, options: 'utf8'): Promise; + readFile(path: string, options: string): Promise; + readFile(path: string, options: { encoding: 'utf8' }): Promise; + readFile(path: string, options: { encoding: string }): Promise; + readFile(path: string, options?: string | { encoding?: string }): Promise; - writeFile(path: string, data: string | Uint8Array, encoding?: string): Promise; + writeFile(path: string, data: string | Uint8Array, options?: string | { encoding?: string }): Promise; // readdir overloads for different return types readdir(path: string): Promise; diff --git a/src/lib/LightningFSAdapter.ts b/src/lib/LightningFSAdapter.ts index 77dfedfb..413944b6 100644 --- a/src/lib/LightningFSAdapter.ts +++ b/src/lib/LightningFSAdapter.ts @@ -9,14 +9,18 @@ export class LightningFSAdapter implements JSRuntimeFS { constructor(private lightningFS: LightningFS.PromisifiedFS) {} async readFile(path: string): Promise; - async readFile(path: string, encoding: 'utf8'): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: string): Promise; - async readFile(path: string, encoding?: 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, encoding?: string): Promise { + 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); } diff --git a/src/lib/ProjectsManager.test.ts b/src/lib/ProjectsManager.test.ts index 5e43cf2a..22b59ea6 100644 --- a/src/lib/ProjectsManager.test.ts +++ b/src/lib/ProjectsManager.test.ts @@ -41,15 +41,19 @@ class MockFS implements JSRuntimeFS { private dirs: Set = new Set(); async readFile(path: string): Promise; - async readFile(path: string, encoding: "utf8"): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: 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 content = this.files.get(path); if (content === undefined) { const error = new Error(`ENOENT: no such file or directory, open '${path}'`) as NodeJS.ErrnoException; error.code = 'ENOENT'; throw error; } + const encoding = typeof options === 'string' ? options : options?.encoding; if (encoding === 'utf8') { if (typeof content === 'string') { return content; diff --git a/src/lib/fileUtils.test.ts b/src/lib/fileUtils.test.ts index e75495e1..801fe30d 100644 --- a/src/lib/fileUtils.test.ts +++ b/src/lib/fileUtils.test.ts @@ -13,12 +13,16 @@ class MockFS implements JSRuntimeFS { } async readFile(path: string): Promise; - async readFile(path: string, encoding: 'utf8'): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: 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 data = this.files.get(path); if (!data) throw new Error(`File not found: ${path}`); + const encoding = typeof options === 'string' ? options : options?.encoding; if (encoding === 'utf8') { return typeof data === 'string' ? data : new TextDecoder().decode(data); } @@ -26,7 +30,7 @@ class MockFS implements JSRuntimeFS { return data; } - async writeFile(path: string, data: string | Uint8Array): Promise { + async writeFile(path: string, data: string | Uint8Array, _options?: string | { encoding?: string }): Promise { this.files.set(path, data); } diff --git a/src/lib/tmpCleanup.test.ts b/src/lib/tmpCleanup.test.ts index 9ba27c96..eabf2f42 100644 --- a/src/lib/tmpCleanup.test.ts +++ b/src/lib/tmpCleanup.test.ts @@ -26,15 +26,19 @@ class MockFS implements JSRuntimeFS { } async readFile(path: string): Promise; - async readFile(path: string, encoding: 'utf8'): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: 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 file = this.files.get(path); if (!file || file.isDir) throw new Error(`File not found: ${path}`); + const encoding = typeof options === 'string' ? options : options?.encoding; return encoding === 'utf8' ? file.content.toString() : new Uint8Array(); } - async writeFile(path: string, data: string | Uint8Array): Promise { + async writeFile(path: string, data: string | Uint8Array, _options?: string | { encoding?: string }): Promise { this.files.set(path, { content: data, mtimeMs: Date.now(), isDir: false }); } diff --git a/src/lib/tools/TextEditorStrReplaceTool.test.ts b/src/lib/tools/TextEditorStrReplaceTool.test.ts index 3b7df782..b439282d 100644 --- a/src/lib/tools/TextEditorStrReplaceTool.test.ts +++ b/src/lib/tools/TextEditorStrReplaceTool.test.ts @@ -14,18 +14,21 @@ class MockFS implements JSRuntimeFS { } async readFile(path: string): Promise; - async readFile(path: string, encoding: 'utf8'): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: string): Promise; - async readFile(path: string, encoding?: 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 content = this.files.get(path); if (content === undefined) { throw new Error(`File not found: ${path}`); } + const encoding = typeof options === 'string' ? options : options?.encoding; return encoding ? content : new TextEncoder().encode(content); } - async writeFile(path: string, data: string | Uint8Array, _encoding?: string): Promise { + async writeFile(path: string, data: string | Uint8Array, _options?: string | { encoding?: string }): Promise { const content = typeof data === 'string' ? data : new TextDecoder().decode(data); this.files.set(path, content); } diff --git a/src/lib/tools/TextEditorViewTool.test.ts b/src/lib/tools/TextEditorViewTool.test.ts index 03e81b73..eddabc93 100644 --- a/src/lib/tools/TextEditorViewTool.test.ts +++ b/src/lib/tools/TextEditorViewTool.test.ts @@ -41,15 +41,18 @@ class MockFS implements JSRuntimeFS { } async readFile(path: string): Promise; - async readFile(path: string, encoding: 'utf8'): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: string): Promise; - async readFile(path: string, encoding?: 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 content = this.files.get(path); if (content === undefined) { throw new Error(`File not found: ${path}`); } + const encoding = typeof options === 'string' ? options : options?.encoding; // If no encoding specified, return Uint8Array if (!encoding) { // For binary files, return mock binary data with null bytes @@ -70,7 +73,7 @@ class MockFS implements JSRuntimeFS { return content; } - async writeFile(path: string, data: string | Uint8Array, _encoding?: string): Promise { + async writeFile(path: string, data: string | Uint8Array, _options?: string | { encoding?: string }): Promise { const content = typeof data === 'string' ? data : new TextDecoder().decode(data); this.files.set(path, content); } diff --git a/src/lib/tools/TextEditorWriteTool.test.ts b/src/lib/tools/TextEditorWriteTool.test.ts index 34c1ffc9..17169b4f 100644 --- a/src/lib/tools/TextEditorWriteTool.test.ts +++ b/src/lib/tools/TextEditorWriteTool.test.ts @@ -13,18 +13,21 @@ class MockFS implements JSRuntimeFS { } async readFile(path: string): Promise; - async readFile(path: string, encoding: 'utf8'): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: string): Promise; - async readFile(path: string, encoding?: 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 content = this.files.get(path); if (content === undefined) { throw new Error(`File not found: ${path}`); } + const encoding = typeof options === 'string' ? options : options?.encoding; return encoding ? content : new TextEncoder().encode(content); } - async writeFile(path: string, data: string | Uint8Array, _encoding?: string): Promise { + async writeFile(path: string, data: string | Uint8Array, _options?: string | { encoding?: string }): Promise { const content = typeof data === 'string' ? data : new TextDecoder().decode(data); this.files.set(path, content); }