"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 });
exports.PreviewCommand = void 0;
const yok_1 = require("../common/yok");
const resolve_package_path_1 = require("@rigor789/resolve-package-path");
const constants_1 = require("../constants");
const color_1 = require("../color");
const path = require("path");
const PREVIEW_CLI_PACKAGE = "@nativescript/preview-cli";
class PreviewCommand {
    constructor($logger, $errors, $projectData, $packageManager, $childProcess, $options) {
        this.$logger = $logger;
        this.$errors = $errors;
        this.$projectData = $projectData;
        this.$packageManager = $packageManager;
        this.$childProcess = $childProcess;
        this.$options = $options;
        this.allowedParameters = [];
    }
    getPreviewCLIPath() {
        return (0, resolve_package_path_1.resolvePackagePath)(PREVIEW_CLI_PACKAGE, {
            paths: [this.$projectData.projectDir],
        });
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.$options.disableNpmInstall) {
                // ensure latest is installed
                yield this.$packageManager.install(`${PREVIEW_CLI_PACKAGE}@latest`, this.$projectData.projectDir, {
                    "save-dev": true,
                    "save-exact": true,
                });
            }
            const previewCLIPath = this.getPreviewCLIPath();
            if (!previewCLIPath) {
                const packageManagerName = yield this.$packageManager.getPackageManagerName();
                let installCommand = "";
                switch (packageManagerName) {
                    case constants_1.PackageManagers.npm:
                        installCommand = "npm install --save-dev @nativescript/preview-cli";
                        break;
                    case constants_1.PackageManagers.yarn:
                    case constants_1.PackageManagers.yarn2:
                        installCommand = "yarn add -D @nativescript/preview-cli";
                        break;
                    case constants_1.PackageManagers.pnpm:
                        installCommand = "pnpm install --save-dev @nativescript/preview-cli";
                        break;
                }
                this.$logger.info([
                    `Uhh ohh, no Preview CLI found.`,
                    "",
                    `This should not happen under regular circumstances, but seems like it did somehow... :(`,
                    `Good news though, you can install the Preview CLI by running`,
                    "",
                    "  " + color_1.color.green(installCommand),
                    "",
                    "Once installed, run this command again and everything should work!",
                    "If it still fails, you can invoke the preview-cli directly as a last resort with",
                    "",
                    color_1.color.cyan("  ./node_modules/.bin/preview-cli"),
                    "",
                    "And if you are still having issues, try again - or reach out on Discord/open an issue on GitHub.",
                ].join("\n"));
                this.$errors.fail("Running preview failed.");
            }
            const previewCLIBinPath = path.resolve(previewCLIPath, "./dist/index.js");
            const commandIndex = process.argv.indexOf("preview");
            const commandArgs = process.argv.slice(commandIndex + 1);
            this.$childProcess.spawn(process.execPath, [previewCLIBinPath, ...commandArgs], {
                stdio: "inherit",
            });
        });
    }
    canExecute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            return true;
        });
    }
}
exports.PreviewCommand = PreviewCommand;
yok_1.injector.registerCommand("preview", PreviewCommand);
//# sourceMappingURL=preview.js.map