"use strict";
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 CommandService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandService = void 0;
const common_1 = require("@nestjs/common");
const child_process_1 = require("child_process");
let CommandService = CommandService_1 = class CommandService {
    constructor() {
        this.logger = new common_1.Logger(CommandService_1.name);
    }
    escapeArgForWindows(arg) {
        if (/[{}\s"^&|<>]/.test(arg)) {
            const escaped = arg.replace(/"/g, '\\"');
            return `"${escaped}"`;
        }
        return arg;
    }
    async executeCommandWithArgs(commandWithArgs) {
        const { command, args } = commandWithArgs;
        this.logger.debug(`Executing command: ${command} ${args.join(' ')}`);
        return new Promise((resolve, reject) => {
            const isWindows = process.platform === 'win32';
            const spawnArgs = isWindows
                ? args.map(arg => this.escapeArgForWindows(arg))
                : args;
            const child = (0, child_process_1.spawn)(command, spawnArgs, {
                shell: isWindows,
                stdio: ['pipe', 'pipe', 'pipe'],
                cwd: commandWithArgs.cwd,
            });
            let stdout = '';
            let stderr = '';
            child.stdout.on('data', (data) => {
                stdout += data.toString();
            });
            child.stderr.on('data', (data) => {
                stderr += data.toString();
            });
            child.on('error', (error) => {
                this.logger.error(`Error executing command: ${command}`, error);
                reject(error);
            });
            child.on('close', (code) => {
                if (code !== 0) {
                    this.logger.error(`Command failed with code ${code}: ${command}`, stderr);
                    reject(new Error(stderr || `Command failed with exit code ${code}`));
                    return;
                }
                resolve(stdout);
            });
        });
    }
};
exports.CommandService = CommandService;
exports.CommandService = CommandService = CommandService_1 = __decorate([
    (0, common_1.Injectable)()
], CommandService);
//# sourceMappingURL=command.service.js.map