UNPKG

nativescript

Version:

Command-line interface for building NativeScript projects

82 lines 3.72 kB
"use strict"; 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], }); } async execute(args) { if (!this.$options.disableNpmInstall) { // ensure latest is installed await this.$packageManager.install(`${PREVIEW_CLI_PACKAGE}@latest`, this.$projectData.projectDir, { "save-dev": true, "save-exact": true, }); } const previewCLIPath = this.getPreviewCLIPath(); if (!previewCLIPath) { const packageManagerName = await this.$packageManager.getPackageManagerName(); let installCommand = ""; switch (packageManagerName) { 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; case constants_1.PackageManagers.bun: installCommand = "bun add --dev @nativescript/preview-cli"; case constants_1.PackageManagers.npm: default: installCommand = "npm 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", }); } async canExecute(args) { return true; } } exports.PreviewCommand = PreviewCommand; yok_1.injector.registerCommand("preview", PreviewCommand); //# sourceMappingURL=preview.js.map