"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.spawnProcess = void 0;
const child_process_1 = require("child_process");
async function spawnProcess(command, { args = [], env, cwd, onData }) {
    const nodePty = tryNodePty();
    if (nodePty) {
        const child = nodePty.spawn('bash', ['-c', `${command} $@`, '--', ...args], {
            name: 'xterm-256color',
            cols: 120,
            rows: 60,
            env: env,
            cwd,
        });
        if (onData) {
            child.onData((string) => onData(Buffer.from(string)));
        }
        return new Promise((resolve) => child.onExit(({ exitCode }) => resolve(exitCode)));
    }
    else {
        let shell = true;
        if (process.platform === 'win32') {
            shell = false;
            args = ['-c', `${command} $@`, '--', ...args];
            command = 'C:/Program Files/Git/bin/bash.exe';
        }
        const child = (0, child_process_1.spawn)(command, args, { cwd, env, shell });
        if (onData) {
            child.stdout.on('data', onData);
            child.stderr.on('data', onData);
        }
        return new Promise((resolve, reject) => child.once('close', resolve).once('error', reject));
    }
}
exports.spawnProcess = spawnProcess;
function tryNodePty() {
    try {
        return require('node-pty');
    }
    catch {
        return null;
    }
}
//# sourceMappingURL=spawn.js.map