"use strict";
const tslib_1 = require("tslib");
const fs_1 = tslib_1.__importDefault(require("fs"));
const path_1 = tslib_1.__importDefault(require("path"));
const command_1 = require("@oclif/command");
const chalk_1 = tslib_1.__importDefault(require("chalk"));
const dedent_1 = tslib_1.__importDefault(require("dedent"));
const enquirer_1 = require("enquirer");
const title_case_1 = require("title-case");
const debug_1 = require("./utils/debug");
const file_1 = require("./utils/file");
const slugify_1 = require("./utils/slugify");
const debug = debug_1.namespaced("new");
const TEMPLATES_DIR = path_1.default.join(__dirname, "..", "templates");
const CHOICES = fs_1.default.readdirSync(TEMPLATES_DIR).filter((name) => {
    return fs_1.default.lstatSync(path_1.default.join(TEMPLATES_DIR, name)).isDirectory();
});
class CreateFlayyerApp extends command_1.Command {
    async run() {
        debug("cli version is: %s", this.config.version);
        const { args, flags } = this.parse(CreateFlayyerApp);
        const response = await enquirer_1.prompt([
            {
                type: "input",
                name: "name",
                initial: args.name,
                skip: Boolean(args.name),
                message: "Enter project name",
                validate: Boolean,
            },
            {
                type: "autocomplete",
                name: "template",
                message: "Select the best template setup for you",
                hint: "You can customize the template later",
                initial: flags.template,
                skip: Boolean(flags.template),
                choices: CHOICES,
            },
        ]);
        const template = typeof response.template === "string" ? response.template : CHOICES[response.template];
        const slug = slugify_1.slugify(response.name);
        if (template) {
            debug("will use: %o", { slug, template });
        }
        else {
            this.error("Missing or invalid template");
        }
        const CURR_DIR = process.cwd();
        const templatePath = path_1.default.join(TEMPLATES_DIR, template);
        const destination = path_1.default.join(CURR_DIR, slug);
        debug("current directory is: %s", CURR_DIR);
        debug("templates source directory is: %s", TEMPLATES_DIR);
        debug("template source directory is: %s", templatePath);
        debug("target path is: %s", destination);
        if (fs_1.default.existsSync(destination)) {
            this.error(`Folder ${destination} exists. Delete or use another name.`);
        }
        fs_1.default.mkdirSync(destination);
        const SKIP_FILES = ["node_modules", ".flayyer-cache", ".flayyer-dist", ".flayyer-processed", ".flayyer-dev"];
        const replace = {
            "replace-slug": slug,
            "replace-title": title_case_1.titleCase(slug),
            "replace-cli-version": "^1.17.4",
            "replace-create-version": this.config.version,
        };
        const rename = { ".npmignore": ".gitignore" }; // For some (bold) reason, npm renames .gitignores to .npmignores
        debug("replacement values: %o", replace);
        debug("rename values: %o", rename);
        debug("skip files: %o", SKIP_FILES);
        debug("will copy and replace: %o", { from: templatePath, to: destination });
        file_1.recursiveCopy(templatePath, destination, SKIP_FILES, replace, rename);
        this.log(dedent_1.default `
      💫 Great!

      ${chalk_1.default.green(`Deck '${chalk_1.default.bold(slug)}' from preset '${chalk_1.default.bold(template)}' was successfully created at: ${destination}`)}

      Now go to the generated directory and install the initial dependencies:

        ${chalk_1.default.bold(`cd ${slug}`)}
        ${chalk_1.default.bold(`npm install`)}

      To start the development server run:

        ${chalk_1.default.bold("npm run start")}

      When you are ready to deploy execute:

        ${chalk_1.default.bold(`NODE_ENV=production npm run build && npm run deploy`)}

      Remember to setup your 'FLAYYER_KEY' environment variable.
      Forgot your key? Go to https://flayyer.com/dashboard/_/settings
      First time using Flayyer? Create an account at https://flayyer.com/get-started
    `);
        debug("exiting oclif");
        this.exit();
    }
}
CreateFlayyerApp.description = "create a new flayyer project";
CreateFlayyerApp.flags = {
    // add --version flag to show CLI version
    version: command_1.flags.version({ char: "v" }),
    help: command_1.flags.help({ char: "h" }),
    template: command_1.flags.string({ char: "t", description: "starting template", options: CHOICES }),
};
CreateFlayyerApp.args = [{ name: "name", description: "output directory for templates" }];
module.exports = CreateFlayyerApp;
