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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
/**
 * Copyright (C) 2024 Hedera Hashgraph, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the ""License"");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an ""AS IS"" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import { spawn } from 'child_process';
import chalk from 'chalk';
import { SoloLogger } from './logging.js';
import { inject, injectable } from 'tsyringe-neo';
import { patchInject } from './container_helper.js';
let ShellRunner = class ShellRunner {
    logger;
    constructor(logger) {
        this.logger = logger;
        this.logger = patchInject(logger, SoloLogger, this.constructor.name);
    }
    /** Returns a promise that invokes the shell command */
    run(cmd, verbose = false) {
        const self = this;
        const callStack = new Error().stack; // capture the callstack to be included in error
        self.logger.info(`Executing command: '${cmd}'`);
        return new Promise((resolve, reject) => {
            const child = spawn(cmd, {
                shell: true,
            });
            const output = [];
            child.stdout.on('data', d => {
                const items = d.toString().split(/\r?\n/);
                items.forEach(item => {
                    if (item) {
                        output.push(item);
                    }
                });
            });
            const errOutput = [];
            child.stderr.on('data', d => {
                const items = d.toString().split(/\r?\n/);
                items.forEach(item => {
                    if (item) {
                        errOutput.push(item.trim());
                    }
                });
            });
            child.on('exit', (code, signal) => {
                if (code) {
                    const err = new Error(`Command exit with error code ${code}: ${cmd}`);
                    // include the callStack to the parent run() instead of from inside this handler.
                    // this is needed to ensure we capture the proper callstack for easier debugging.
                    err.stack = callStack;
                    if (verbose) {
                        errOutput.forEach(m => self.logger.showUser(chalk.red(m)));
                    }
                    self.logger.error(`Error executing: '${cmd}'`, {
                        commandExitCode: code,
                        commandExitSignal: signal,
                        commandOutput: output,
                        errOutput,
                        error: { message: err.message, stack: err.stack },
                    });
                    reject(err);
                }
                self.logger.debug(`Finished executing: '${cmd}'`, {
                    commandExitCode: code,
                    commandExitSignal: signal,
                    commandOutput: output,
                    errOutput,
                });
                resolve(output);
            });
        });
    }
};
ShellRunner = __decorate([
    injectable(),
    __param(0, inject(SoloLogger)),
    __metadata("design:paramtypes", [SoloLogger])
], ShellRunner);
export { ShellRunner };
//# sourceMappingURL=shell_runner.js.map