forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview.ts
105 lines (92 loc) · 3.13 KB
/
preview.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { ICommandParameter, ICommand } from "../common/definitions/commands";
import { IChildProcess, IErrors } from "../common/declarations";
import { injector } from "../common/yok";
import { IOptions, IPackageManager } from "../declarations";
import { IProjectData } from "../definitions/project";
import { resolvePackagePath } from "@rigor789/resolve-package-path";
import { PackageManagers } from "../constants";
import { color } from "../color";
import * as path from "path";
const PREVIEW_CLI_PACKAGE = "@nativescript/preview-cli";
export class PreviewCommand implements ICommand {
allowedParameters: ICommandParameter[] = [];
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<void> {
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.yarn:
case PackageManagers.yarn2:
installCommand = "yarn add -D @nativescript/preview-cli";
break;
case PackageManagers.pnpm:
installCommand = "pnpm install --save-dev @nativescript/preview-cli";
break;
case PackageManagers.bun:
installCommand = "bun add --dev @nativescript/preview-cli";
case 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.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.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: string[]): Promise<boolean> {
return true;
}
}
injector.registerCommand("preview", PreviewCommand);