import spawn from "cross-spawn";
export function buildAsync(opts, cmd) {
    const proc = spawn(cmd.command, cmd.args, {
        env: {
            ...cmd.env,
            ...process.env,
        },
        cwd: opts.srcPath,
    });
    return new Promise((resolve) => {
        let buffer = "";
        proc.stdout?.on("data", (data) => (buffer += data));
        proc.stderr?.on("data", (data) => (buffer += data));
        proc.on("exit", () => {
            if (proc.exitCode === 0)
                resolve([]);
            if (proc.exitCode !== 0) {
                resolve([
                    {
                        location: {
                            file: [opts.srcPath, opts.handler].join("/"),
                        },
                        message: buffer,
                    },
                ]);
            }
        });
    });
}
export async function buildAsyncAndThrow(opts, cmd) {
    const issues = await buildAsync(opts, cmd);
    if (issues.length > 0) {
        throw new Error(issues.map((i) => i.message.toString()).join("\n"));
    }
}
