"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = void 0;
const cli_core_1 = require("@igniteui/cli-core");
const yargs = require("yargs");
const add_1 = require("./commands/add");
const build_1 = require("./commands/build");
const config_1 = require("./commands/config");
const doc_1 = require("./commands/doc");
const generate_1 = require("./commands/generate");
const list_1 = require("./commands/list");
const new_1 = require("./commands/new");
const quickstart_1 = require("./commands/quickstart");
const start_1 = require("./commands/start");
const test_1 = require("./commands/test");
const upgrade_1 = require("./commands/upgrade");
const PromptSession_1 = require("./PromptSession");
const TemplateManager_1 = require("./TemplateManager");
process.title = "Ignite UI CLI";
function logHelp() {
    cli_core_1.GoogleAnalytics.post({
        t: "screenview",
        cd: "$ig help"
    });
}
function run() {
    return __awaiter(this, arguments, void 0, function* (args = null) {
        //	we are subscribing on process.exit to catch when help is executed
        process.on("exit", logHelp);
        cli_core_1.App.initialize();
        const templateManager = new TemplateManager_1.TemplateManager();
        new_1.default.template = templateManager;
        new_1.default.builder.framework.choices = templateManager.getFrameworkIds();
        add_1.default.templateManager = templateManager;
        build_1.default.templateManager = templateManager;
        start_1.default.templateManager = templateManager;
        generate_1.default.templateManager = templateManager;
        list_1.default.templateManager = templateManager;
        upgrade_1.default.templateManager = templateManager;
        const yargsModule = args ? yargs(args) : yargs;
        const argv = yargsModule
            .command(quickstart_1.default)
            .command(new_1.default)
            .command(add_1.default)
            .command(build_1.default)
            .command(start_1.default)
            .command(generate_1.default)
            .command(config_1.default)
            .command(doc_1.default)
            .command(test_1.default)
            .command(list_1.default)
            .command(upgrade_1.default)
            .options({
            version: {
                alias: "v",
                description: "Show current Ignite UI CLI version",
                global: true,
                type: "boolean"
            }
        })
            .help().alias("help", "h")
            .argv;
        //	unsubscribing from process.exit. If `help` was executed we should not reach here
        process.removeListener("exit", logHelp);
        if (argv.version) {
            cli_core_1.Util.showVersion(__dirname + "/../ignite-ui-cli.txt");
            return;
        }
        const command = argv._[0];
        // internal testing only
        /* istanbul ignore next */
        cli_core_1.App.testMode = !!argv.testMode;
        switch (command) {
            case "new":
                yield new_1.default.execute(argv);
                break;
            case "quickstart":
                yield quickstart_1.default.execute(argv);
                cli_core_1.Util.log("quickstart Created");
                break;
            case "add":
                if (add_1.default.check(argv)) {
                    yield add_1.default.execute(argv);
                }
                else {
                    yargsModule.showHelp();
                    return;
                }
                break;
            case "g":
            case "generate":
                yield generate_1.default.template(argv);
                break;
            case "build":
                yield build_1.default.execute(argv);
                break;
            case "config":
                break;
            case "doc":
                yield doc_1.default.execute(argv);
                break;
            case "test":
                yield test_1.default.execute(argv);
                break;
            case "start":
                yield start_1.default.execute(argv);
                break;
            case "l":
            case "list":
                list_1.default.execute(argv);
                break;
            case "upgrade-packages":
                yield upgrade_1.default.execute(argv);
                break;
            default:
                cli_core_1.Util.log("Starting Step by step mode.", "green");
                cli_core_1.Util.log("For available commands, stop this execution and use --help.", "green");
                const prompts = new PromptSession_1.PromptSession(templateManager);
                prompts.start();
                break;
        }
    });
}
exports.run = run;
