"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.execProcess = execProcess;
const __ΩParameters = ['T', 'args', '', 'Parameters', 'l>e"!R!RPde#!Ph"!@2""/#qk#\'QRb!Pde"!p)w$y'];
const __ΩReturnType = ['T', 'args', '', 'ReturnType', 'l>e"!R"RPde#!P"@2"h"!/#qk#\'QRb!Pde"!p)w$y'];
function __assignType(fn, args) {
    fn.__type = args;
    return fn;
}
const child_process_1 = require("child_process");
const telemetry_1 = require("../../telemetry");
const error_1 = require("../utils/error");
const __ΩIExecOptions = ['cwd', 'errorOnNonZero', () => __ΩParameters, () => child_process_1.spawn, 2, "stdio", 'stdio', () => __ΩReturnType, () => child_process_1.spawn, 'proc', '', 'onSpawn', 'shell', 'IExecOptions', 'P&4!8)4"8i$o#".%f.&f4\'8Pi)o("2*$/+4,8)4-8Mw.y'];
const __ΩIExecResult = ['code', 'stdout', 'stderr', 'IExecResult', 'PP\',J4!!4"!4#Mw$y'];
async function execProcess(cmd, args, options) {
    return (0, telemetry_1.withSpan)('execProcess', { cmd, args }, async () => {
        try {
            return await (Promise.Ω = [[() => __ΩIExecResult, 'n!']], new Promise(__assignType((resolve, reject) => {
                const stdout = [];
                const stderr = [];
                const proc = (0, child_process_1.spawn)(cmd, args, { cwd: options?.cwd, stdio: options?.stdio, shell: options?.shell });
                proc.on('error', reject);
                proc.on('spawn', () => options?.onSpawn?.(proc));
                proc.stdout?.on('data', __assignType(data => stdout.push(data), ['data', '', 'P"2!"/"']));
                proc.stderr?.on('data', __assignType(data => stderr.push(data), ['data', '', 'P"2!"/"']));
                proc.on('exit', __assignType(code => {
                    if (options?.errorOnNonZero !== false && code !== 0) {
                        reject(new Error(`Process exited with code ${code}`));
                    }
                    resolve({
                        code,
                        stdout: Buffer.concat(stdout),
                        stderr: Buffer.concat(stderr)
                    });
                }, ['code', '', 'P"2!"/"']));
            }, ['resolve', 'reject', '', 'P"2!"2""/#'])));
        }
        catch (err) {
            throw (0, error_1.toError)(`Failure during execution of process with command: ${cmd} ${args.map(__assignType(a => (a.includes(' ') ? `"${a}"` : a), ['a', '', 'P"2!"/"'])).join(' ')}`, err);
        }
    });
}
execProcess.__type = ['cmd', 'args', () => __ΩIExecOptions, 'options', () => __ΩIExecResult, 'execProcess', 'P&2!&F2"n#2$8n%`/&'];
//# sourceMappingURL=process.js.map