Skip to content

Commit 98124e7

Browse files
authored
feat: re-enable preview command (#5676)
* feat: enable preview command * feat: allow disabling install & pass down args + error handling * chore: use @latest * chore(release): 8.3.0-beta.0
1 parent e9d9d5a commit 98124e7

File tree

4 files changed

+94
-21
lines changed

4 files changed

+94
-21
lines changed

lib/commands/preview.ts

+79-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,90 @@
11
import { ICommandParameter, ICommand } from "../common/definitions/commands";
2-
import { IErrors } from "../common/declarations";
2+
import { IChildProcess, IErrors } from "../common/declarations";
33
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";
411

512
export class PreviewCommand implements ICommand {
613
allowedParameters: ICommandParameter[] = [];
714

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+
}
929

1030
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+
});
1588
}
1689

1790
async canExecute(args: string[]): Promise<boolean> {

package-lock.json

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nativescript",
33
"preferGlobal": true,
4-
"version": "8.2.3",
4+
"version": "8.3.0-beta.0",
55
"author": "NativeScript <[email protected]>",
66
"description": "Command-line interface for building NativeScript projects",
77
"bin": {
@@ -106,7 +106,7 @@
106106
"shelljs": "0.8.4",
107107
"simple-git": "^2.20.1",
108108
"simple-plist": "1.1.0",
109-
"source-map": "0.7.3",
109+
"source-map": "0.7.4",
110110
"stringify-package": "1.0.1",
111111
"tabtab": "https://github.com/Icenium/node-tabtab/tarball/master",
112112
"tar": "6.0.2",

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -6594,10 +6594,10 @@
65946594
"resolved" "https://registry.npmjs.org/source-map-url/-/source-map-url-0.4.0.tgz"
65956595
"version" "0.4.0"
65966596

6597-
"source-map@*", "[email protected].3":
6598-
"integrity" "sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ=="
6599-
"resolved" "https://registry.npmjs.org/source-map/-/source-map-0.7.3.tgz"
6600-
"version" "0.7.3"
6597+
"source-map@*", "[email protected].4":
6598+
"integrity" "sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA=="
6599+
"resolved" "https://registry.npmjs.org/source-map/-/source-map-0.7.4.tgz"
6600+
"version" "0.7.4"
66016601

66026602
"source-map@^0.5.6":
66036603
"integrity" "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w="

0 commit comments

Comments
 (0)