(
filePath: string,
options: RunCodeOptions = {}
)
| 181 | * @returns The execution result |
| 182 | */ |
| 183 | export async function executeFile( |
| 184 | filePath: string, |
| 185 | options: RunCodeOptions = {} |
| 186 | ): Promise<ExecutionResult> { |
| 187 | try { |
| 188 | // Read the file content |
| 189 | const content = await Deno.readTextFile(filePath); |
| 190 | |
| 191 | // Determine language from file extension if not specified |
| 192 | if (!options.language) { |
| 193 | const extension = filePath.split('.').pop()?.toLowerCase(); |
| 194 | if (extension === 'py') { |
| 195 | options.language = 'python'; |
| 196 | } else if (extension === 'js') { |
| 197 | options.language = 'javascript'; |
| 198 | } else if (extension === 'ts') { |
| 199 | options.language = 'typescript'; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // Execute the file content |
| 204 | return await executeCode(content, options); |
| 205 | } catch (error: unknown) { |
| 206 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 207 | console.error(`Failed to execute file: ${filePath}`, errorMessage); |
| 208 | |
| 209 | // Return an error result |
| 210 | return { |
| 211 | text: "", |
| 212 | results: [], |
| 213 | error: { |
| 214 | type: "error", |
| 215 | value: errorMessage |
| 216 | }, |
| 217 | logs: { |
| 218 | stdout: [], |
| 219 | stderr: [errorMessage] |
| 220 | } |
| 221 | }; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Write a file in the sandbox |
nothing calls this directly
no test coverage detected