|
1 | 1 | import { ICommandParameter, ICommand } from "../common/definitions/commands";
|
2 |
| -import { IErrors } from "../common/declarations"; |
| 2 | +import { IChildProcess, IErrors } from "../common/declarations"; |
3 | 3 | import { injector } from "../common/yok";
|
| 4 | +import { IOptions, IPackageManager } from "../declarations"; |
| 5 | +import { IProjectData } from "../definitions/project"; |
| 6 | +import path = require("path"); |
| 7 | +import { resolvePackagePath } from "@rigor789/resolve-package-path"; |
| 8 | +import { PackageManagers } from "../constants"; |
| 9 | + |
| 10 | +const PREVIEW_CLI_PACKAGE = "@nativescript/preview-cli"; |
4 | 11 |
|
5 | 12 | export class PreviewCommand implements ICommand {
|
6 | 13 | allowedParameters: ICommandParameter[] = [];
|
7 | 14 |
|
8 |
| - constructor(private $errors: IErrors) {} |
| 15 | + constructor( |
| 16 | + private $logger: ILogger, |
| 17 | + private $errors: IErrors, |
| 18 | + private $projectData: IProjectData, |
| 19 | + private $packageManager: IPackageManager, |
| 20 | + private $childProcess: IChildProcess, |
| 21 | + private $options: IOptions |
| 22 | + ) {} |
| 23 | + |
| 24 | + private getPreviewCLIPath(): string { |
| 25 | + return resolvePackagePath(PREVIEW_CLI_PACKAGE, { |
| 26 | + paths: [this.$projectData.projectDir], |
| 27 | + }); |
| 28 | + } |
9 | 29 |
|
10 | 30 | async execute(args: string[]): Promise<void> {
|
11 |
| - this.$errors.fail( |
12 |
| - `The Preview service has been disabled until further notice.\n\n` + |
13 |
| - `Configure local builds and use "ns run ${args.join(" ")}" instead.` |
14 |
| - ); |
| 31 | + if (!this.$options.disableNpmInstall) { |
| 32 | + // ensure latest is installed |
| 33 | + await this.$packageManager.install( |
| 34 | + `${PREVIEW_CLI_PACKAGE}@latest`, |
| 35 | + this.$projectData.projectDir, |
| 36 | + { |
| 37 | + "save-dev": true, |
| 38 | + "save-exact": true, |
| 39 | + } as any |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + const previewCLIPath = this.getPreviewCLIPath(); |
| 44 | + |
| 45 | + if (!previewCLIPath) { |
| 46 | + const packageManagerName = await this.$packageManager.getPackageManagerName(); |
| 47 | + let installCommand = ""; |
| 48 | + |
| 49 | + switch (packageManagerName) { |
| 50 | + case PackageManagers.npm: |
| 51 | + installCommand = "npm install --save-dev @nativescript/preview-cli"; |
| 52 | + break; |
| 53 | + case PackageManagers.yarn: |
| 54 | + installCommand = "yarn add -D @nativescript/preview-cli"; |
| 55 | + break; |
| 56 | + case PackageManagers.pnpm: |
| 57 | + installCommand = "pnpm install --save-dev @nativescript/preview-cli"; |
| 58 | + break; |
| 59 | + } |
| 60 | + this.$logger.info( |
| 61 | + [ |
| 62 | + `Uhh ohh, no Preview CLI found.`, |
| 63 | + "", |
| 64 | + `This should not happen under regular circumstances, but seems like it did somehow... :(`, |
| 65 | + `Good news though, you can install the Preview CLI by running`, |
| 66 | + "", |
| 67 | + " " + installCommand.green, |
| 68 | + "", |
| 69 | + "Once installed, run this command again and everything should work!", |
| 70 | + "If it still fails, you can invoke the preview-cli directly as a last resort with", |
| 71 | + "", |
| 72 | + " ./node_modules/.bin/preview-cli".cyan, |
| 73 | + "", |
| 74 | + "And if you are still having issues, try again - or reach out on Discord/open an issue on GitHub.", |
| 75 | + ].join("\n") |
| 76 | + ); |
| 77 | + |
| 78 | + this.$errors.fail("Running preview failed."); |
| 79 | + } |
| 80 | + |
| 81 | + const previewCLIBinPath = path.resolve(previewCLIPath, "./dist/index.js"); |
| 82 | + |
| 83 | + const commandIndex = process.argv.indexOf("preview"); |
| 84 | + const commandArgs = process.argv.slice(commandIndex + 1); |
| 85 | + this.$childProcess.spawn(previewCLIBinPath, commandArgs, { |
| 86 | + stdio: "inherit", |
| 87 | + }); |
15 | 88 | }
|
16 | 89 |
|
17 | 90 | async canExecute(args: string[]): Promise<boolean> {
|
|
0 commit comments