"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_native_1 = require("../tools/react-native");
var packager_1 = require("../tools/packager");
var pretty_1 = require("../tools/pretty");
var flag_1 = require("../tools/flag");
var cache_1 = require("../tools/cache");
var os_1 = require("os");
exports.default = {
    run: function (toolbox) { return __awaiter(void 0, void 0, void 0, function () {
        var print, filesystem, system, meta, parameters, strings, prompt, kebabCase, exists, path, removeAsync, copy, read, write, homedir, info, colors, warning, gray, cyan, yellow, underline, white, options, yname, useDefault, CMD_INDENT, command, debug, log, validateProjectName, projectName, projectNameKebab, bname, defaultBundleIdentifier, bundleIdentifier, bundleIdentifierResponse, validateBundleIdentifier, defaultTargetPath, targetPath, targetPathResponse, handleHomePrefix, defaultOverwrite, overwrite, overwriteResponse, alreadyExists, defaultGit, git, gitResponse, defaultRemoveDemo, removeDemo, removeDemoResponse, availablePackagers, defaultPackagerName, packagerName, validatePackagerName, initial_1, NOT_FOUND, packagerNameResponse, packagerOptions, isWindows, ignitePath, boilerplatePath, boilerplate, defaultInstallDeps, installDeps, installDepsResponse, expo, perfStart, terminalWidth, logo, pkg, msg, boilerplateIgnorePath, targetIgnorePath, cwd, packageJsonRaw, packageJson, boilerplatePackageJsonHash, cachePath, cacheExists, defaultUseCache, useCache, shouldUseCache, msg, renameSpinnerMsg, boilerplateBundleIdentifier, shouldFreshInstallDeps, unboxingMessage, msg, IGNITE, e_1, warnAboutEOL, unixToDosPath, templates, unixToDosCmd_1, results, e_2, perfDuration, p2, cliCommand, prettyCliCommand, isMac;
        var _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    print = toolbox.print, filesystem = toolbox.filesystem, system = toolbox.system, meta = toolbox.meta, parameters = toolbox.parameters, strings = toolbox.strings, prompt = toolbox.prompt;
                    kebabCase = strings.kebabCase;
                    exists = filesystem.exists, path = filesystem.path, removeAsync = filesystem.removeAsync, copy = filesystem.copy, read = filesystem.read, write = filesystem.write, homedir = filesystem.homedir;
                    info = print.info, colors = print.colors, warning = print.warning;
                    gray = colors.gray, cyan = colors.cyan, yellow = colors.yellow, underline = colors.underline, white = colors.white;
                    options = parameters.options;
                    yname = (0, flag_1.boolFlag)(options.y) || (0, flag_1.boolFlag)(options.yes);
                    useDefault = function (option) { return yname && option === undefined; };
                    CMD_INDENT = "  ";
                    command = function (cmd) { return p2(white(CMD_INDENT + cmd)); };
                    debug = (0, flag_1.boolFlag)(options.debug);
                    log = function (m) {
                        debug && info(" ".concat(m));
                        return m;
                    };
                    // log raw parameters for debugging
                    log("ignite command: ".concat(parameters.argv.join(" ")));
                    // #endregion
                    // #region Project Name
                    // retrieve project name from toolbox
                    (0, pretty_1.p)();
                    validateProjectName = require("../tools/validations").validateProjectName;
                    return [4 /*yield*/, validateProjectName(toolbox)];
                case 1:
                    projectName = _b.sent();
                    projectNameKebab = kebabCase(projectName);
                    bname = options.b || options.boilerplate;
                    if (bname) {
                        (0, pretty_1.p)();
                        (0, pretty_1.p)(yellow("Different boilerplates are no longer supported in Ignite v4+."));
                        (0, pretty_1.p)(gray("To use the old CLI to support different boilerplates, try:"));
                        (0, pretty_1.p)(cyan("npx ignite-cli@3 new ".concat(projectName, " --boilerplate ").concat(bname)));
                        process.exit(1);
                    }
                    defaultBundleIdentifier = "com.".concat(projectName.toLowerCase());
                    bundleIdentifier = useDefault(options.bundle) ? defaultBundleIdentifier : options.bundle;
                    if (!(bundleIdentifier === undefined)) return [3 /*break*/, 3];
                    return [4 /*yield*/, prompt.ask(function () { return ({
                            type: "input",
                            name: "bundleIdentifier",
                            message: "What bundle identifier?",
                            initial: defaultBundleIdentifier,
                            prefix: pretty_1.prefix,
                        }); })];
                case 2:
                    bundleIdentifierResponse = _b.sent();
                    bundleIdentifier = bundleIdentifierResponse.bundleIdentifier;
                    _b.label = 3;
                case 3:
                    validateBundleIdentifier = require("../tools/validations").validateBundleIdentifier;
                    validateBundleIdentifier(toolbox, bundleIdentifier);
                    defaultTargetPath = path(projectName);
                    targetPath = useDefault(options.targetPath) ? defaultTargetPath : options.targetPath;
                    if (!(targetPath === undefined)) return [3 /*break*/, 5];
                    return [4 /*yield*/, prompt.ask(function () { return ({
                            type: "input",
                            name: "targetPath",
                            message: "Where do you want to start your project?",
                            initial: defaultTargetPath,
                            prefix: pretty_1.prefix,
                        }); })];
                case 4:
                    targetPathResponse = _b.sent();
                    targetPath = targetPathResponse.targetPath;
                    _b.label = 5;
                case 5:
                    handleHomePrefix = function (p) {
                        return (p === null || p === void 0 ? void 0 : p.startsWith("~")) ? p.replace("~", homedir()) : p;
                    };
                    targetPath = path(handleHomePrefix(targetPath));
                    defaultOverwrite = false;
                    overwrite = useDefault(options.overwrite) ? defaultOverwrite : (0, flag_1.boolFlag)(options.overwrite);
                    if (!(exists(targetPath) && overwrite === undefined)) return [3 /*break*/, 7];
                    return [4 /*yield*/, prompt.ask(function () { return ({
                            type: "confirm",
                            name: "overwrite",
                            message: "Directory ".concat(targetPath, " already exists. Do you want to overwrite it?"),
                            initial: defaultOverwrite,
                            format: pretty_1.prettyPrompt.format.boolean,
                            prefix: pretty_1.prefix,
                        }); })];
                case 6:
                    overwriteResponse = _b.sent();
                    overwrite = overwriteResponse.overwrite;
                    _b.label = 7;
                case 7:
                    if (exists(targetPath) && overwrite === false) {
                        alreadyExists = "Error: There's already a folder at ".concat(targetPath, ". To force overwriting that folder, run with --overwrite or say yes.");
                        (0, pretty_1.p)();
                        (0, pretty_1.p)(yellow(alreadyExists));
                        process.exit(1);
                    }
                    defaultGit = true;
                    git = useDefault(options.git) ? defaultGit : (0, flag_1.boolFlag)(options.git);
                    if (!(git === undefined)) return [3 /*break*/, 9];
                    return [4 /*yield*/, prompt.ask(function () { return ({
                            type: "confirm",
                            name: "git",
                            message: "Do you want to initialize a git repository?",
                            initial: defaultGit,
                            format: pretty_1.prettyPrompt.format.boolean,
                            prefix: pretty_1.prefix,
                        }); })];
                case 8:
                    gitResponse = _b.sent();
                    git = gitResponse.git;
                    _b.label = 9;
                case 9:
                    defaultRemoveDemo = false;
                    removeDemo = useDefault(options.removeDemo)
                        ? defaultRemoveDemo
                        : (0, flag_1.boolFlag)(options.removeDemo);
                    if (!(removeDemo === undefined)) return [3 /*break*/, 11];
                    return [4 /*yield*/, prompt.ask(function () { return ({
                            type: "confirm",
                            name: "removeDemo",
                            message: "Remove demo code? We recommend leaving it in if it's your first time using Ignite",
                            initial: defaultRemoveDemo,
                            format: pretty_1.prettyPrompt.format.boolean,
                            prefix: pretty_1.prefix,
                        }); })];
                case 10:
                    removeDemoResponse = _b.sent();
                    removeDemo = removeDemoResponse.removeDemo;
                    _b.label = 11;
                case 11:
                    availablePackagers = packager_1.packager.availablePackagers();
                    defaultPackagerName = availablePackagers.includes("yarn") ? "yarn" : "npm";
                    packagerName = useDefault(options.packager) ? defaultPackagerName : options.packager;
                    validatePackagerName = function (input) {
                        return typeof input === "string" && ["npm", "yarn", "pnpm"].includes(input);
                    };
                    if (packagerName !== undefined && validatePackagerName(packagerName) === false) {
                        (0, pretty_1.p)();
                        (0, pretty_1.p)(yellow("Error: Invalid packager: \"".concat(packagerName, "\". Valid packagers are npm, yarn, pnpm.")));
                        process.exit(1);
                    }
                    if (packagerName !== undefined && availablePackagers.includes(packagerName) === false) {
                        (0, pretty_1.p)();
                        (0, pretty_1.p)(yellow("Error: selected \"".concat(packagerName, "\" but packager was not available on system")));
                        process.exit(1);
                    }
                    if (!(packagerName === undefined)) return [3 /*break*/, 13];
                    initial_1 = availablePackagers.findIndex(function (p) { return p === defaultPackagerName; });
                    NOT_FOUND = -1;
                    if (initial_1 === NOT_FOUND) {
                        (0, pretty_1.p)();
                        (0, pretty_1.p)(yellow("Error: Default packager \"".concat(defaultPackagerName, "\" was not available on system")));
                        process.exit(1);
                    }
                    return [4 /*yield*/, prompt.ask(function () { return ({
                            type: "select",
                            name: "packagerName",
                            message: "Which package manager do you want to use?",
                            choices: availablePackagers,
                            initial: initial_1,
                            prefix: pretty_1.prefix,
                        }); })];
                case 12:
                    packagerNameResponse = _b.sent();
                    packagerName = packagerNameResponse.packagerName;
                    _b.label = 13;
                case 13:
                    packagerOptions = { packagerName: packagerName };
                    isWindows = process.platform === "win32";
                    ignitePath = path("".concat(meta.src), "..");
                    boilerplatePath = path(ignitePath, "boilerplate");
                    boilerplate = function () {
                        var pathParts = [];
                        for (var _i = 0; _i < arguments.length; _i++) {
                            pathParts[_i] = arguments[_i];
                        }
                        return path.apply(void 0, __spreadArray([boilerplatePath], pathParts, false));
                    };
                    log("ignitePath: ".concat(ignitePath));
                    log("boilerplatePath: ".concat(boilerplatePath));
                    defaultInstallDeps = true;
                    installDeps = useDefault(options.installDeps)
                        ? defaultInstallDeps
                        : (0, flag_1.boolFlag)(options.installDeps);
                    if (!(installDeps === undefined)) return [3 /*break*/, 15];
                    return [4 /*yield*/, prompt.ask(function () { return ({
                            type: "confirm",
                            name: "installDeps",
                            message: "Do you want to install dependencies?",
                            initial: defaultInstallDeps,
                            format: pretty_1.prettyPrompt.format.boolean,
                            prefix: pretty_1.prefix,
                        }); })];
                case 14:
                    installDepsResponse = _b.sent();
                    installDeps = installDepsResponse.installDeps;
                    _b.label = 15;
                case 15:
                    expo = (0, flag_1.boolFlag)(options.expo);
                    if (expo) {
                        warning(" Detected --expo, this option is deprecated. Ignite sets you up to run native or Expo!");
                        (0, pretty_1.p)();
                    }
                    perfStart = new Date().getTime();
                    terminalWidth = (_a = process.stdout.columns) !== null && _a !== void 0 ? _a : 80;
                    logo = terminalWidth > 80 ? function () { return (0, pretty_1.ascii)("logo.ascii.txt"); } : function () { return (0, pretty_1.ascii)("logo-sm.ascii.txt"); };
                    (0, pretty_1.p)();
                    (0, pretty_1.p)();
                    (0, pretty_1.p)();
                    (0, pretty_1.p)();
                    logo();
                    (0, pretty_1.p)();
                    (0, pretty_1.p)();
                    pkg = (0, pretty_1.pkgColor)(packagerName);
                    (0, pretty_1.p)(" \u2588 Creating ".concat((0, pretty_1.em)(projectName), " using ").concat((0, pretty_1.em)("Ignite ".concat(meta.version()))));
                    (0, pretty_1.p)(" \u2588 Powered by ".concat((0, pretty_1.ir)(" ∞ Infinite Red "), " (").concat((0, pretty_1.link)("https://infinite.red"), ")"));
                    (0, pretty_1.p)(" \u2588 Package Manager: ".concat(pkg(print.colors.bold(packagerName))));
                    (0, pretty_1.p)(" \u2588 Bundle identifier: ".concat((0, pretty_1.em)(bundleIdentifier)));
                    (0, pretty_1.p)(" \u2588 Path: ".concat(underline(targetPath)));
                    (0, pretty_1.hr)();
                    (0, pretty_1.p)();
                    if (!(exists(targetPath) === "dir" && overwrite === true)) return [3 /*break*/, 17];
                    msg = " Tossing that old app like it's hot";
                    (0, pretty_1.startSpinner)(msg);
                    return [4 /*yield*/, removeAsync(targetPath)];
                case 16:
                    _b.sent();
                    (0, pretty_1.stopSpinner)(msg, "🗑️");
                    _b.label = 17;
                case 17: 
                // #endregion
                // #region Local build folder clean
                // Remove some folders that we don't want to copy over
                // This mostly only applies when you're developing locally
                return [4 /*yield*/, Promise.all([
                        removeAsync(path(boilerplatePath, "node_modules")),
                        removeAsync(path(boilerplatePath, "ios", "Pods")),
                        removeAsync(path(boilerplatePath, "ios", "build")),
                        removeAsync(path(boilerplatePath, "android", ".idea")),
                        removeAsync(path(boilerplatePath, "android", ".gradle")),
                        removeAsync(path(boilerplatePath, "android", "build")),
                    ])
                    // #endregion
                    // #region Copy Boilerplate Files
                ];
                case 18:
                    // #endregion
                    // #region Local build folder clean
                    // Remove some folders that we don't want to copy over
                    // This mostly only applies when you're developing locally
                    _b.sent();
                    // #endregion
                    // #region Copy Boilerplate Files
                    (0, pretty_1.startSpinner)(" 3D-printing a new React Native app");
                    return [4 /*yield*/, (0, react_native_1.copyBoilerplate)(toolbox, {
                            boilerplatePath: boilerplatePath,
                            targetPath: targetPath,
                            excluded: [".vscode", "node_modules", "yarn.lock"],
                            overwrite: overwrite,
                        })];
                case 19:
                    _b.sent();
                    (0, pretty_1.stopSpinner)(" 3D-printing a new React Native app", "🖨");
                    boilerplateIgnorePath = exists(boilerplate(".gitignore.template"))
                        ? boilerplate(".gitignore.template")
                        : boilerplate(".gitignore");
                    targetIgnorePath = log(path(targetPath, ".gitignore"));
                    copy(log(boilerplateIgnorePath), targetIgnorePath, { overwrite: true });
                    if (exists(targetIgnorePath) === false) {
                        warning("  Unable to copy ".concat(boilerplateIgnorePath, " to ").concat(targetIgnorePath));
                    }
                    cwd = log(process.cwd());
                    // jump into the project to do additional tasks
                    process.chdir(targetPath);
                    packageJsonRaw = read("package.json");
                    packageJsonRaw = packageJsonRaw
                        .replace(/HelloWorld/g, projectName)
                        .replace(/hello-world/g, projectNameKebab);
                    packageJson = JSON.parse(packageJsonRaw);
                    write("./package.json", packageJson);
                    boilerplatePackageJsonHash = cache_1.cache.hash(read(path(boilerplatePath, "package.json")));
                    cachePath = path(cache_1.cache.rootdir(), boilerplatePackageJsonHash, packagerName);
                    cacheExists = exists(cachePath) === "dir";
                    log("".concat(!cacheExists ? "expected " : "", "cachePath: ").concat(cachePath));
                    log("cacheExists: ".concat(cacheExists));
                    defaultUseCache = false;
                    useCache = options.useCache === undefined ? defaultUseCache : (0, flag_1.boolFlag)(options.useCache);
                    shouldUseCache = installDeps && cacheExists && useCache;
                    if (!shouldUseCache) return [3 /*break*/, 21];
                    msg = "Grabbing those ".concat(packagerName, " dependencies from the back");
                    (0, pretty_1.startSpinner)(msg);
                    return [4 /*yield*/, cache_1.cache.copy({
                            fromRootDir: cachePath,
                            toRootDir: targetPath,
                            packagerName: packagerName,
                        })];
                case 20:
                    _b.sent();
                    (0, pretty_1.stopSpinner)(msg, "📦");
                    _b.label = 21;
                case 21:
                    renameSpinnerMsg = "Getting those last few details perfect";
                    (0, pretty_1.startSpinner)(renameSpinnerMsg);
                    boilerplateBundleIdentifier = "com.helloworld";
                    return [4 /*yield*/, (0, react_native_1.renameReactNativeApp)(toolbox, "HelloWorld", projectName, boilerplateBundleIdentifier, bundleIdentifier)];
                case 22:
                    _b.sent();
                    return [4 /*yield*/, (0, react_native_1.replaceMaestroBundleIds)(toolbox, boilerplateBundleIdentifier, bundleIdentifier)];
                case 23:
                    _b.sent();
                    (0, pretty_1.stopSpinner)(renameSpinnerMsg, "🎨");
                    shouldFreshInstallDeps = installDeps && shouldUseCache === false;
                    if (!shouldFreshInstallDeps) return [3 /*break*/, 25];
                    unboxingMessage = "Installing ".concat(packagerName, " dependencies (wow these are heavy)");
                    (0, pretty_1.startSpinner)(unboxingMessage);
                    return [4 /*yield*/, packager_1.packager.install(__assign(__assign({}, packagerOptions), { onProgress: log }))];
                case 24:
                    _b.sent();
                    (0, pretty_1.stopSpinner)(unboxingMessage, "🧶");
                    _b.label = 25;
                case 25: 
                // remove the gitignore template
                return [4 /*yield*/, removeAsync(".gitignore.template")
                    // #endregion
                    // #region Cache dependencies
                ];
                case 26:
                    // remove the gitignore template
                    _b.sent();
                    if (!(shouldFreshInstallDeps && cacheExists === false && useCache)) return [3 /*break*/, 28];
                    msg = "Saving ".concat(packagerName, " dependencies for next time");
                    (0, pretty_1.startSpinner)(msg);
                    log(targetPath);
                    return [4 /*yield*/, cache_1.cache.copy({
                            fromRootDir: targetPath,
                            toRootDir: cachePath,
                            packagerName: packagerName,
                        })];
                case 27:
                    _b.sent();
                    (0, pretty_1.stopSpinner)(msg, "📦");
                    _b.label = 28;
                case 28:
                    if (!(installDeps === true)) return [3 /*break*/, 30];
                    // Make sure all our modifications are formatted nicely
                    return [4 /*yield*/, packager_1.packager.run("format", __assign(__assign({}, packagerOptions), { silent: !debug }))];
                case 29:
                    // Make sure all our modifications are formatted nicely
                    _b.sent();
                    _b.label = 30;
                case 30:
                    if (!(removeDemo === true)) return [3 /*break*/, 35];
                    (0, pretty_1.startSpinner)(" Removing fancy demo code");
                    _b.label = 31;
                case 31:
                    _b.trys.push([31, 33, , 34]);
                    IGNITE = "node " + filesystem.path(__dirname, "..", "..", "bin", "ignite");
                    log("Ignite bin path: ".concat(IGNITE));
                    return [4 /*yield*/, system.run("".concat(IGNITE, " remove-demo ").concat(targetPath), {
                            onProgress: log,
                        })];
                case 32:
                    _b.sent();
                    return [3 /*break*/, 34];
                case 33:
                    e_1 = _b.sent();
                    log(e_1);
                    (0, pretty_1.p)(yellow("Unable to remove demo code."));
                    return [3 /*break*/, 34];
                case 34:
                    (0, pretty_1.stopSpinner)(" Removing fancy demo code", "🛠️");
                    _b.label = 35;
                case 35:
                    warnAboutEOL = false;
                    if (!isWindows) return [3 /*break*/, 39];
                    return [4 /*yield*/, system.exec("which unix2dos")
                        // find all templates Ignite provides by default
                    ];
                case 36:
                    unixToDosPath = _b.sent();
                    templates = filesystem.find("".concat(targetPath, "/ignite/templates"), {
                        directories: false,
                        files: true,
                        matching: "*.ejs",
                    });
                    log("unix2dos path: ".concat(unixToDosPath));
                    log("templates to change EOL: ".concat(templates));
                    if (!unixToDosPath) return [3 /*break*/, 38];
                    unixToDosCmd_1 = unixToDosPath.split("/").pop();
                    return [4 /*yield*/, Promise.allSettled(templates.map(function (file) { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0:
                                        log("Converting EOL for ".concat(file));
                                        return [4 /*yield*/, system.run("".concat(unixToDosCmd_1, " ").concat(file))];
                                    case 1:
                                        _a.sent();
                                        return [2 /*return*/];
                                }
                            });
                        }); }))
                        // inspect the results of conversion and log
                    ];
                case 37:
                    results = _b.sent();
                    // inspect the results of conversion and log
                    results.forEach(function (result) {
                        if (result.status === "rejected") {
                            warnAboutEOL = true;
                            log("Error converting EOL: ".concat(JSON.stringify(result.reason)));
                        }
                    });
                    return [3 /*break*/, 39];
                case 38:
                    warnAboutEOL = true;
                    _b.label = 39;
                case 39:
                    if (!(git === true)) return [3 /*break*/, 49];
                    (0, pretty_1.startSpinner)(" Backing everything up in source control");
                    _b.label = 40;
                case 40:
                    _b.trys.push([40, 47, , 48]);
                    if (!isWindows) return [3 /*break*/, 44];
                    return [4 /*yield*/, system.run(log("git init"))];
                case 41:
                    _b.sent();
                    return [4 /*yield*/, system.run(log("git add -A"))];
                case 42:
                    _b.sent();
                    return [4 /*yield*/, system.run(log("git commit -m \"New Ignite ".concat(meta.version(), " app")))];
                case 43:
                    _b.sent();
                    return [3 /*break*/, 46];
                case 44: return [4 /*yield*/, system.run(log("\n              \\rm -rf ./.git\n              git init;\n              git add -A;\n              git commit -m \"New Ignite ".concat(meta.version(), " app\";\n            ")))];
                case 45:
                    _b.sent();
                    _b.label = 46;
                case 46: return [3 /*break*/, 48];
                case 47:
                    e_2 = _b.sent();
                    (0, pretty_1.p)(yellow("Unable to commit the initial changes. Please check your git username and email."));
                    return [3 /*break*/, 48];
                case 48:
                    (0, pretty_1.stopSpinner)(" Backing everything up in source control", "🗄");
                    _b.label = 49;
                case 49:
                    // back to the original directory
                    process.chdir(log(cwd));
                    // #endregion
                    // #region Print Finish
                    // clean up any spinners we forgot to clear
                    (0, pretty_1.p)();
                    (0, pretty_1.hr)();
                    (0, pretty_1.p)();
                    (0, pretty_1.clearSpinners)();
                    perfDuration = Math.round((new Date().getTime() - perfStart) / 10) / 100;
                    p2 = function (m) {
                        if (m === void 0) { m = ""; }
                        return (0, pretty_1.p)(" ".concat(m));
                    };
                    p2("Ignited ".concat((0, pretty_1.em)("".concat(projectName)), " in ").concat(gray("".concat(perfDuration, "s")), "  \uD83D\uDE80 "));
                    p2();
                    cliCommand = buildCliCommand({
                        flags: {
                            b: bname,
                            boilerplate: bname,
                            bundle: bundleIdentifier,
                            debug: debug,
                            git: git,
                            installDeps: installDeps,
                            overwrite: overwrite,
                            expo: expo,
                            packager: packagerName,
                            targetPath: targetPath,
                            removeDemo: removeDemo,
                            useCache: useCache,
                            y: yname,
                            yes: yname,
                        },
                        projectName: projectName,
                        toolbox: toolbox,
                    });
                    p2("For next time: here are the Ignite options you picked!");
                    prettyCliCommand = cliCommand
                        .split(" ")
                        .map(function (c) { return (c === projectName || (c === null || c === void 0 ? void 0 : c.startsWith("--")) ? "".concat(c, " \\").concat(os_1.EOL) : c); }) // add a line break after the project name and each flag
                        .map(function (c, i, a) { return (i === a.length - 1 ? c.replace("\\".concat(os_1.EOL), "") : c); }) // remove the line break after the last flag
                        .map(function (c) { return (c.startsWith("--") ? pretty_1.INDENT + CMD_INDENT + CMD_INDENT + c : c); }) // add whitespace to the flags so it looks nice
                        .join(" ");
                    command("".concat(prettyCliCommand));
                    p2();
                    if (!(0, react_native_1.isAndroidInstalled)(toolbox)) {
                        (0, pretty_1.hr)();
                        p2();
                        p2("To run in Android, make sure you've followed the latest");
                        p2("react-native setup instructions. You reference them at:");
                        p2("".concat((0, pretty_1.link)("https://reactnative.dev/docs/environment-setup")));
                        p2();
                    }
                    if (warnAboutEOL) {
                        (0, pretty_1.hr)();
                        p2();
                        p2(yellow("Generator templates could not be converted to Windows EOL."));
                        p2(yellow("You may want to update these manually with your code editor, more info at:"));
                        p2("".concat((0, pretty_1.link)("https://github.com/infinitered/ignite/blob/master/docs/Generators.md#windows")));
                        p2();
                    }
                    (0, pretty_1.hr)();
                    p2();
                    p2("Need additional help?");
                    p2();
                    p2("Join our Slack community at ".concat((0, pretty_1.link)("http://community.infinite.red.")));
                    p2();
                    (0, pretty_1.hr)();
                    p2();
                    p2("Now get cooking! 🍽");
                    command("cd ".concat(targetPath));
                    if (!installDeps)
                        command(packager_1.packager.installCmd({ packagerName: packagerName }));
                    command("".concat(packagerName, " start"));
                    isMac = process.platform === "darwin";
                    if (isMac) {
                        command("".concat(packager_1.packager.runCmd("ios", packagerOptions)));
                    }
                    else {
                        command("".concat(packager_1.packager.runCmd("android", packagerOptions)));
                    }
                    p2();
                    p2("With Expo:");
                    command("cd ".concat(projectName));
                    if (!installDeps)
                        command(packager_1.packager.installCmd({ packagerName: packagerName }));
                    command("".concat(packager_1.packager.runCmd("expo:start", packagerOptions)));
                    p2();
                    p2();
                    // #endregion
                    // this is a hack to prevent the process from hanging
                    // if there are any tasks left in the event loop
                    // like I/O operations to process.stdout and process.stderr
                    // see https://github.com/infinitered/ignite/issues/2084
                    process.exit(0);
                    return [2 /*return*/];
            }
        });
    }); },
};
function buildCliCommand(args) {
    var flags = args.flags, toolbox = args.toolbox, projectName = args.projectName;
    var strings = toolbox.strings;
    var kebabCase = strings.kebabCase;
    var privateFlags = ["b", "boilerplate", "debug", "expo", "useCache", "y", "yes"];
    var stringFlag = function (_a) {
        var key = _a[0], value = _a[1];
        return "--".concat(kebabCase(key), "=").concat(value);
    };
    var booleanFlag = function (_a) {
        var key = _a[0], value = _a[1];
        return value ? "--".concat(kebabCase(key)) : "--".concat(kebabCase(key), "=").concat(value);
    };
    var cliCommand = "npx ignite-cli new ".concat(projectName, " ").concat(Object.entries(flags)
        .filter(function (_a) {
        var key = _a[0];
        return privateFlags.includes(key) === false;
    })
        .filter(function (_a) {
        var value = _a[1];
        return value !== undefined;
    })
        .map(function (_a) {
        var key = _a[0], value = _a[1];
        return typeof value === "boolean" ? booleanFlag([key, value]) : stringFlag([key, value]);
    })
        .join(" "));
    return cliCommand;
}
//# sourceMappingURL=new.js.map