import { spawn } from "node:child_process";
export async function runTask(task, route, attempt) {
    return new Promise((resolve) => {
        const isWindows = process.platform === "win32";
        let shell = isWindows ? "cmd.exe" : "/bin/bash";
        let shellArgs = [isWindows ? "/c" : "-c", `${task.command} ${task.args.join(" ")}`];
        if (isWindows && route === "wsl") {
            shell = "wsl.exe";
            const envSetup = 'source $HOME/.cargo/env 2>/dev/null; export PATH="$HOME/.local/share/solana/install/active_release/bin:$HOME/.avm/bin:$PATH"; export NVM_DIR="$HOME/.nvm"; if [ -s "$NVM_DIR/nvm.sh" ]; then . "$NVM_DIR/nvm.sh" 2>/dev/null; nvm use node 2>/dev/null; fi';
            shellArgs = ["-d", "Ubuntu", "-e", "bash", "-c", `${envSetup}; ${task.command} ${task.args.join(" ")}`];
        }
        const child = spawn(shell, shellArgs, {
            env: process.env,
        });
        let stdout = "";
        let stderr = "";
        child.stdout.on("data", (d) => {
            stdout += d.toString();
        });
        child.stderr.on("data", (d) => {
            stderr += d.toString();
        });
        child.on("close", (code) => {
            const exitCode = code ?? 1;
            resolve({
                taskId: task.id,
                attempt,
                command: `${task.command} ${task.args.join(" ")}`,
                route,
                status: exitCode === 0 ? "success" : "failed",
                stdout,
                stderr,
                exitCode,
            });
        });
    });
}
//# sourceMappingURL=runner.js.map