diff --git a/lib/commands/preview.ts b/lib/commands/preview.ts index 088217f2da..5440ee4d81 100644 --- a/lib/commands/preview.ts +++ b/lib/commands/preview.ts @@ -1,17 +1,90 @@ import { ICommandParameter, ICommand } from "../common/definitions/commands"; -import { IErrors } from "../common/declarations"; +import { IChildProcess, IErrors } from "../common/declarations"; import { injector } from "../common/yok"; +import { IOptions, IPackageManager } from "../declarations"; +import { IProjectData } from "../definitions/project"; +import path = require("path"); +import { resolvePackagePath } from "@rigor789/resolve-package-path"; +import { PackageManagers } from "../constants"; + +const PREVIEW_CLI_PACKAGE = "@nativescript/preview-cli"; export class PreviewCommand implements ICommand { allowedParameters: ICommandParameter[] = []; - constructor(private $errors: IErrors) {} + constructor( + private $logger: ILogger, + private $errors: IErrors, + private $projectData: IProjectData, + private $packageManager: IPackageManager, + private $childProcess: IChildProcess, + private $options: IOptions + ) {} + + private getPreviewCLIPath(): string { + return resolvePackagePath(PREVIEW_CLI_PACKAGE, { + paths: [this.$projectData.projectDir], + }); + } async execute(args: string[]): Promise { - this.$errors.fail( - `The Preview service has been disabled until further notice.\n\n` + - `Configure local builds and use "ns run ${args.join(" ")}" instead.` - ); + 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, + } as any + ); + } + + const previewCLIPath = this.getPreviewCLIPath(); + + if (!previewCLIPath) { + const packageManagerName = await this.$packageManager.getPackageManagerName(); + let installCommand = ""; + + switch (packageManagerName) { + case PackageManagers.npm: + installCommand = "npm install --save-dev @nativescript/preview-cli"; + break; + case PackageManagers.yarn: + installCommand = "yarn add -D @nativescript/preview-cli"; + break; + case 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`, + "", + " " + installCommand.green, + "", + "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", + "", + " ./node_modules/.bin/preview-cli".cyan, + "", + "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(previewCLIBinPath, commandArgs, { + stdio: "inherit", + }); } async canExecute(args: string[]): Promise { diff --git a/package-lock.json b/package-lock.json index f794a0db27..b78910b3e6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "nativescript", - "version": "8.2.3", + "version": "8.3.0-beta.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "nativescript", - "version": "8.2.3", + "version": "8.3.0-beta.0", "hasInstallScript": true, "license": "Apache-2.0", "dependencies": { @@ -59,7 +59,7 @@ "shelljs": "0.8.4", "simple-git": "^2.20.1", "simple-plist": "1.1.0", - "source-map": "0.7.3", + "source-map": "0.7.4", "stringify-package": "1.0.1", "tabtab": "https://github.com/Icenium/node-tabtab/tarball/master", "tar": "6.0.2", @@ -11384,9 +11384,9 @@ } }, "node_modules/source-map": { - "version": "0.7.3", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.3.tgz", - "integrity": "sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==", + "version": "0.7.4", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.4.tgz", + "integrity": "sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==", "engines": { "node": ">= 8" } @@ -22154,9 +22154,9 @@ } }, "source-map": { - "version": "0.7.3", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.3.tgz", - "integrity": "sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==" + "version": "0.7.4", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.4.tgz", + "integrity": "sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==" }, "source-map-resolve": { "version": "0.5.3", diff --git a/package.json b/package.json index a0020fe2ad..a24844e1ea 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "nativescript", "preferGlobal": true, - "version": "8.2.3", + "version": "8.3.0-beta.0", "author": "NativeScript ", "description": "Command-line interface for building NativeScript projects", "bin": { @@ -106,7 +106,7 @@ "shelljs": "0.8.4", "simple-git": "^2.20.1", "simple-plist": "1.1.0", - "source-map": "0.7.3", + "source-map": "0.7.4", "stringify-package": "1.0.1", "tabtab": "https://github.com/Icenium/node-tabtab/tarball/master", "tar": "6.0.2", diff --git a/yarn.lock b/yarn.lock index 43b1af8262..fb036e6eac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6594,10 +6594,10 @@ "resolved" "https://registry.npmjs.org/source-map-url/-/source-map-url-0.4.0.tgz" "version" "0.4.0" -"source-map@*", "source-map@0.7.3": - "integrity" "sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==" - "resolved" "https://registry.npmjs.org/source-map/-/source-map-0.7.3.tgz" - "version" "0.7.3" +"source-map@*", "source-map@0.7.4": + "integrity" "sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==" + "resolved" "https://registry.npmjs.org/source-map/-/source-map-0.7.4.tgz" + "version" "0.7.4" "source-map@^0.5.6": "integrity" "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w="