"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GradleCommandService = void 0;
const yok_1 = require("../../common/yok");
const helpers_1 = require("../../common/helpers");
class GradleCommandService {
    constructor($childProcess, $errors, $hostInfo, $logger) {
        this.$childProcess = $childProcess;
        this.$errors = $errors;
        this.$hostInfo = $hostInfo;
        this.$logger = $logger;
    }
    async executeCommand(gradleArgs, options) {
        var _a;
        const { message, cwd, stdio, spawnOptions } = options;
        this.$logger.info(message);
        const childProcessOptions = {
            cwd,
            stdio: stdio || "inherit",
            shell: this.$hostInfo.isWindows,
        };
        const gradleExecutable = (_a = options.gradlePath) !== null && _a !== void 0 ? _a : (this.$hostInfo.isWindows ? "gradlew.bat" : "./gradlew");
        const sanitizedGradleArgs = this.$hostInfo.isWindows
            ? gradleArgs.map((arg) => (0, helpers_1.quoteString)(arg))
            : gradleArgs;
        const result = await this.executeCommandSafe(gradleExecutable, sanitizedGradleArgs, childProcessOptions, spawnOptions);
        return result;
    }
    async executeCommandSafe(gradleExecutable, gradleArgs, childProcessOptions, spawnOptions) {
        try {
            const result = await this.$childProcess.spawnFromEvent(gradleExecutable, gradleArgs, "close", childProcessOptions, spawnOptions);
            return result;
        }
        catch (err) {
            this.$errors.fail(err.message);
        }
    }
}
exports.GradleCommandService = GradleCommandService;
yok_1.injector.register("gradleCommandService", GradleCommandService);
