import { execFile, spawn } from "node:child_process";
import { promisify } from "node:util";
import { isWindowsCommandScript, quoteWindowsArgument, quoteWindowsCommand } from "./executable.js";
const execFileAsync = promisify(execFile);
export function spawnProcess(command, args, options) {
    const isWindows = process.platform === "win32";
    const shell = isWindowsCommandScript(command) ? true : (options?.shell ?? isWindows);
    const shouldQuoteForShell = isWindows && shell !== false;
    const resolvedCommand = shouldQuoteForShell ? quoteWindowsCommand(command) : command;
    const resolvedArgs = shouldQuoteForShell ? args.map(quoteWindowsArgument) : args;
    return spawn(resolvedCommand, resolvedArgs, {
        ...options,
        shell,
        windowsHide: true,
    });
}
export async function execCommand(command, args, options) {
    const isWindows = process.platform === "win32";
    const shell = isWindowsCommandScript(command) ? true : isWindows;
    const shouldQuoteForShell = isWindows && shell !== false;
    const resolvedCommand = shouldQuoteForShell ? quoteWindowsCommand(command) : command;
    const resolvedArgs = shouldQuoteForShell ? args.map(quoteWindowsArgument) : args;
    return execFileAsync(resolvedCommand, resolvedArgs, {
        cwd: options?.cwd,
        env: options?.env,
        encoding: options?.encoding ?? "utf8",
        timeout: options?.timeout,
        maxBuffer: options?.maxBuffer,
        shell,
        windowsHide: true,
    });
}
export function platformShell() {
    if (process.platform === "win32") {
        return { command: "cmd.exe", flag: ["/c"] };
    }
    return { command: "/bin/sh", flag: ["-lc"] };
}
export function platformBash() {
    if (process.platform === "win32") {
        return { command: "cmd.exe", flag: ["/c"] };
    }
    return { command: "/bin/bash", flag: ["-lc"] };
}
//# sourceMappingURL=spawn.js.map