"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpdateCommand = void 0;
const child_process_1 = require("child_process");
const logger_1 = require("../utils/logger");
class UpdateCommand {
    async execute() {
        logger_1.logger.info("Updating mycontext CLI...");
        try {
            logger_1.logger.progress("Running update via pnpm dlx...");
            await this.runCommand("pnpm", ["dlx", "mycontext-cli@latest", "--up"]);
            logger_1.logger.success("Update command executed successfully");
        }
        catch (error) {
            logger_1.logger.error("Failed to run update command");
            logger_1.logger.info("Try manually:");
            logger_1.logger.info("  pnpm dlx mycontext-cli@latest --up");
            throw error;
        }
    }
    async runCommand(command, args) {
        return new Promise((resolve, reject) => {
            const child = (0, child_process_1.spawn)(command, args, {
                stdio: "inherit",
                timeout: 180000,
            });
            child.on("close", (code) => {
                if (code === 0) {
                    resolve();
                }
                else {
                    reject(new Error(`Command failed with code ${code}`));
                }
            });
            child.on("error", (error) => {
                reject(error);
            });
        });
    }
}
exports.UpdateCommand = UpdateCommand;
//# sourceMappingURL=update.js.map