var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { exec, execFile, spawn, } from "node:child_process";
import logger from "./Logger";
import CaptureSpan from "./Telemetry/CaptureSpan";
export default class Execute {
    static executeCommand(command, options) {
        return new Promise((resolve, reject) => {
            exec(`${command}`, Object.assign({}, options), (err, stdout, stderr) => {
                if (err) {
                    logger.error(`Error executing command: ${command}`);
                    logger.error(err);
                    logger.error(stdout);
                    if (stderr) {
                        logger.error(stderr);
                    }
                    return reject(err);
                }
                if (stderr) {
                    logger.debug(stderr);
                }
                return resolve(stdout);
            });
        });
    }
    static executeCommandFile(data) {
        return new Promise((resolve, reject) => {
            execFile(data.command, data.args, {
                cwd: data.cwd,
            }, (err, stdout, stderr) => {
                if (err) {
                    logger.error(`Error executing command: ${data.command} ${data.args.join(" ")}`);
                    logger.error(err);
                    logger.error(stdout);
                    logger.error(stderr);
                    return reject(err);
                }
                if (stderr) {
                    logger.debug(stderr);
                }
                return resolve(stdout);
            });
        });
    }
    static executeCommandInheritStdio(data) {
        return new Promise((resolve, reject) => {
            var _a;
            const spawnOptions = Object.assign({ stdio: ["ignore", "inherit", "inherit"], shell: false }, data.options);
            const child = spawn(data.command, (_a = data.args) !== null && _a !== void 0 ? _a : [], spawnOptions);
            child.on("error", (err) => {
                var _a;
                logger.error(`Error executing command: ${data.command} ${((_a = data.args) !== null && _a !== void 0 ? _a : []).join(" ")}`);
                logger.error(err);
                reject(err);
            });
            child.on("close", (code) => {
                var _a;
                if (code === 0) {
                    resolve();
                    return;
                }
                const error = new Error(`Command failed: ${data.command} ${((_a = data.args) !== null && _a !== void 0 ? _a : []).join(" ")} (exit code ${code !== null && code !== void 0 ? code : "unknown"})`);
                logger.error(error);
                reject(error);
            });
        });
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], Execute, "executeCommand", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Execute, "executeCommandFile", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Execute, "executeCommandInheritStdio", null);
//# sourceMappingURL=Execute.js.map