"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 });
const cli_core_1 = require("@igniteui/cli-core");
let command;
// tslint:disable:object-literal-sort-keys
command = {
    command: "upgrade-packages",
    desc: "upgrades Ignite UI Packages",
    templateManager: null,
    builder: {
        "skip-install": {
            alias: "si",
            default: false,
            describe: "Runs upgrade command without performing install",
            type: "boolean"
        }
    },
    execute(argv) {
        return __awaiter(this, void 0, void 0, function* () {
            cli_core_1.GoogleAnalytics.post({
                t: "screenview",
                cd: "Upgrade packages"
            });
            return command.upgrade(argv);
        });
    },
    upgrade(argv) {
        return __awaiter(this, void 0, void 0, function* () {
            const config = cli_core_1.ProjectConfig.getConfig();
            const framework = config.project.framework;
            const projectType = config.project.projectType;
            switch (framework.toLowerCase()) {
                case "jquery":
                    cli_core_1.Util.log("Upgrading packages for jQuery projects is currently not supported!");
                    return;
                case "angular":
                case "react":
                case "webcomponents":
                    if (projectType === "igx-ts" || projectType === "igr-ts" || projectType === "igc-ts") {
                        const projectLibrary = command.templateManager.getProjectLibrary(framework, projectType);
                        let project;
                        if (!config.project.projectTemplate || !projectLibrary.hasProject(config.project.projectTemplate)) {
                            // in case project template is missing from the config we provide backward.
                            project = projectLibrary.getProject(projectLibrary.projectIds[0]);
                        }
                        else {
                            project = projectLibrary.getProject(config.project.projectTemplate);
                        }
                        const success = yield project.upgradeIgniteUIPackages(process.cwd(), "");
                        if (success && !argv.skipInstall) {
                            cli_core_1.Util.log("Installing npm packages.");
                            try {
                                cli_core_1.Util.execSync("npm install --quiet");
                                cli_core_1.Util.log("npm packages installed.");
                            }
                            catch (error) {
                                cli_core_1.Util.error(`Error installing npm packages.`);
                                cli_core_1.Util.log(error.message);
                            }
                        }
                    }
                    else if (framework.toLowerCase() === "angular") {
                        cli_core_1.Util.log("Upgrading packages for Angular Wrappers projects is currently not supported!");
                        return;
                    }
                    break;
                default:
                    break;
            }
            cli_core_1.GoogleAnalytics.post({
                t: "event",
                ec: "$ig upgrade-packages",
                cd1: framework,
                cd2: projectType
            });
        });
    }
};
exports.default = command;
