(
filePath: string,
options: {
stream?: boolean;
language?: "python" | "javascript" | "typescript";
timeout?: number;
} = {},
)
| 208 | * @returns The execution result |
| 209 | */ |
| 210 | export async function executeFile( |
| 211 | filePath: string, |
| 212 | options: { |
| 213 | stream?: boolean; |
| 214 | language?: "python" | "javascript" | "typescript"; |
| 215 | timeout?: number; |
| 216 | } = {}, |
| 217 | ): Promise<ExecutionResult> { |
| 218 | try { |
| 219 | // Read the file content |
| 220 | const content = await Deno.readTextFile(filePath); |
| 221 | |
| 222 | // Determine language from file extension if not specified |
| 223 | if (!options.language) { |
| 224 | const extension = filePath.split(".").pop()?.toLowerCase(); |
| 225 | if (extension === "py") { |
| 226 | options.language = "python"; |
| 227 | } else if (extension === "js") { |
| 228 | options.language = "javascript"; |
| 229 | } else if (extension === "ts") { |
| 230 | options.language = "typescript"; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // Execute the file content |
| 235 | return await executeCode(content, options); |
| 236 | } catch (error: unknown) { |
| 237 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 238 | await logMessage("error", `Failed to execute file: ${filePath}`, { error: errorMessage }); |
| 239 | |
| 240 | // Return an error result |
| 241 | return { |
| 242 | text: "", |
| 243 | results: [], |
| 244 | error: { |
| 245 | type: "error", |
| 246 | value: errorMessage, |
| 247 | }, |
| 248 | logs: { |
| 249 | stdout: [], |
| 250 | stderr: [errorMessage], |
| 251 | }, |
| 252 | }; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Write a file in the sandbox |
no test coverage detected