"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());
    });
};
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 (_) 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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.initHandler = void 0;
var support_1 = require("./support");
var path_1 = __importDefault(require("path"));
var prompts_1 = __importDefault(require("prompts"));
var constants_1 = require("./constants");
var fs_extra_1 = require("fs-extra");
var version = require('../package.json').version;
var command = {
    command: ['init'],
    describe: 'Creates a new vitro app',
    builder: function (argv) {
        argv.option('skip-install', {
            type: 'boolean',
            boolean: true,
            "default": false
        });
        return argv;
    },
    handler: function (argv) { return __awaiter(void 0, void 0, void 0, function () {
        var packageManagerChoice;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    support_1.debug('argv', argv);
                    support_1.debug('cwd', process.cwd());
                    support_1.printGreen('if you use workspaces remember to add .vitro to your packages globs first', true);
                    return [4 /*yield*/, prompts_1["default"]({
                            type: 'select',
                            name: 'value',
                            message: 'What package manager do you use?',
                            choices: [
                                {
                                    title: 'Npm',
                                    value: 'npm'
                                },
                                {
                                    title: 'Yarn',
                                    value: 'yarn'
                                },
                                {
                                    title: 'Pnpm',
                                    value: 'pnpm'
                                },
                            ],
                            initial: 0
                        })
                        // tell user to add the .vitro folder inside the workspace packages if using workspaces
                    ];
                case 1:
                    packageManagerChoice = _a.sent();
                    // tell user to add the .vitro folder inside the workspace packages if using workspaces
                    return [4 /*yield*/, initHandler({
                            skipInstall: Boolean(argv['skip-install']),
                            packageManager: packageManagerChoice.value
                        })];
                case 2:
                    // tell user to add the .vitro folder inside the workspace packages if using workspaces
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }); }
}; // as CommandModule
function initHandler(_a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.skipInstall, skipInstall = _c === void 0 ? false : _c, _d = _b.packageManager, packageManager = _d === void 0 ? 'npm' : _d;
    return __awaiter(this, void 0, void 0, function () {
        return __generator(this, function (_e) {
            switch (_e.label) {
                case 0:
                    support_1.printGreen("creating " + constants_1.NEXT_APP_PATH, true);
                    return [4 /*yield*/, fs_extra_1.remove(constants_1.NEXT_APP_PATH)];
                case 1:
                    _e.sent();
                    return [4 /*yield*/, fs_extra_1.copy(constants_1.TEMPLATE_PATH, constants_1.NEXT_APP_PATH, {
                            overwrite: true,
                            recursive: true,
                            filter: function (src) {
                                {
                                    // debug(src)
                                    return true;
                                    // return !src.includes('node_modules')
                                }
                            }
                        })];
                case 2:
                    _e.sent();
                    fs_extra_1.writeFileSync(path_1["default"].join(constants_1.NEXT_APP_PATH, constants_1.VERSION_FILE_PATH), "module.exports = '" + version + "'");
                    return [4 /*yield*/, addVitroToGitignore()];
                case 3:
                    _e.sent();
                    if (!!skipInstall) return [3 /*break*/, 5];
                    support_1.printGreen("installing dependencies inside " + constants_1.NEXT_APP_PATH + " with " + packageManager, true);
                    return [4 /*yield*/, support_1.runCommand({
                            command: getInstallCommand(packageManager),
                            env: {
                            // npm_config_loglevel: 'silent',
                            },
                            cwd: path_1["default"].resolve('.', constants_1.NEXT_APP_PATH)
                        })];
                case 4:
                    _e.sent();
                    _e.label = 5;
                case 5:
                    if (!!fs_extra_1.existsSync(constants_1.CONFIG_PATH)) return [3 /*break*/, 7];
                    support_1.printGreen("creating default " + constants_1.CONFIG_PATH, true);
                    return [4 /*yield*/, fs_extra_1.writeFile(constants_1.CONFIG_PATH, constants_1.getDefaultConfig({ packageManager: packageManager }))];
                case 6:
                    _e.sent();
                    _e.label = 7;
                case 7:
                    support_1.printGreen('created vitro app successfully!', true);
                    return [2 /*return*/];
            }
        });
    });
}
exports.initHandler = initHandler;
function addVitroToGitignore() {
    return __awaiter(this, void 0, void 0, function () {
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (!fs_extra_1.existsSync('.gitignore')) return [3 /*break*/, 3];
                    if (!!fs_extra_1.readFileSync('.gitignore').toString().includes(constants_1.NEXT_APP_PATH)) return [3 /*break*/, 2];
                    support_1.printGreen("adding " + constants_1.NEXT_APP_PATH + " to .gitignore", true);
                    return [4 /*yield*/, fs_extra_1.appendFile('.gitignore', '\n' + constants_1.NEXT_APP_PATH + '\n')];
                case 1:
                    _a.sent();
                    _a.label = 2;
                case 2: return [3 /*break*/, 5];
                case 3: return [4 /*yield*/, fs_extra_1.writeFileSync('.gitignore', '\n' + constants_1.NEXT_APP_PATH + '\n')];
                case 4:
                    _a.sent();
                    _a.label = 5;
                case 5: return [2 /*return*/];
            }
        });
    });
}
function getInstallCommand(packageManager) {
    if (packageManager === 'npm') {
        return 'npm i --no-audit --quiet --ignore-scripts --no-fund';
    }
    else if (packageManager === 'yarn') {
        return 'yarn install';
    }
    else if (packageManager == 'pnpm') {
        return 'pnpm i --ignore-scripts';
    }
}
exports["default"] = command;
//# sourceMappingURL=init.js.map