"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");
const path = require("path");
const config_1 = require("./config");
const command = {
    aliases: ["g"],
    command: "generate",
    desc: "generates custom template",
    templateManager: null,
    // tslint:disable:object-literal-sort-keys
    builder: yargs => {
        yargs
            .command({
            aliases: ["t"],
            command: "template [name]",
            desc: "generates custom template",
            builder: {
                "framework": {
                    alias: "f",
                    default: "jquery",
                    describe: "Framework to generate template for",
                    type: "string"
                },
                "name": {
                    alias: "n",
                    default: "custom-template",
                    describe: "Template name",
                    type: "string"
                },
                "skip-config": {
                    alias: "s",
                    default: false,
                    describe: "Runs generate command without updating the cli config",
                    type: "boolean"
                },
                "type": {
                    alias: "t",
                    describe: "Project type (depends on framework)",
                    type: "string"
                }
            }
        })
            // at least one command is required
            .demandCommand(1, "Please select command");
    },
    template(argv) {
        return __awaiter(this, void 0, void 0, function* () {
            cli_core_1.GoogleAnalytics.post({
                t: "screenview",
                cd: "Generate"
            });
            // trim
            argv.name = argv.name.trim();
            // letter+alphanumeric check
            if (!cli_core_1.Util.isAlphanumericExt(argv.name)) {
                cli_core_1.Util.error(`Name '${argv.name}' is not valid. `
                    + "Name should start with a letter and can also contain numbers, dashes and spaces.", "red");
                return;
            }
            const outDir = path.join(process.cwd(), argv.name);
            if (cli_core_1.Util.directoryExists(outDir)) {
                cli_core_1.Util.error(`Folder '${argv.name}' already exists!`, "red");
                return;
            }
            if (command.templateManager.getFrameworkById(argv.framework) === undefined) {
                return cli_core_1.Util.error("Framework not supported", "red");
            }
            let projectLib;
            if (argv.type) {
                projectLib = command.templateManager.getProjectLibrary(argv.framework, argv.type);
                if (!projectLib) {
                    return cli_core_1.Util.error(`Project type '${argv.type}' not found in framework '${argv.framework}'`);
                }
            }
            else {
                projectLib = command.templateManager.getProjectLibrary(argv.framework);
                argv.type = projectLib.projectType;
            }
            const res = yield cli_core_1.Util.processTemplates(projectLib.generateTemplateFolderPath, outDir, {
                templateFramework: argv.framework,
                templateName: argv.name,
                templateType: argv.type
            }, {});
            if (!res) {
                return cli_core_1.Util.error("Template generation failed!", "red");
            }
            if (!argv.skipConfig) {
                config_1.default.addHandler({ property: "customTemplates", value: "path:" + outDir, global: true, skipAnalytics: true });
            }
            cli_core_1.GoogleAnalytics.post({
                t: "event",
                ec: "$ig generate",
                el: "subcommand: template",
                ea: `template name: ${argv.name}; framework: ${argv.framework};` +
                    `project type: ${argv.type}; skip-config: ${argv.skipConfig}`,
                cd1: argv.framework,
                cd2: argv.type,
                cd7: argv.name,
                cd9: "template",
                cd10: !!argv.skipConfig
            });
            cli_core_1.Util.log("Template generated successfully");
        });
    }
};
exports.default = command;
