forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview.ts
163 lines (150 loc) · 4.89 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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 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 $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.npm:
installCommand = "npm install --save-dev @nativescript/preview-cli";
break;
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;
}
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(
process.execPath,
[previewCLIBinPath, ...commandArgs],
{
stdio: "inherit",
}
);
}
async canExecute(args: string[]): Promise<boolean> {
return true;
}
}
// export class PreviewCommand implements ICommand {
// public allowedParameters: ICommandParameter[] = [];
//
// constructor(
// private $analyticsService: IAnalyticsService,
// private $errors: IErrors,
// private $logger: ILogger,
// private $migrateController: IMigrateController,
// private $previewAppController: IPreviewAppController,
// private $networkConnectivityValidator: INetworkConnectivityValidator,
// private $projectData: IProjectData,
// private $options: IOptions,
// private $previewAppLogProvider: IPreviewAppLogProvider,
// private $previewQrCodeService: IPreviewQrCodeService,
// $cleanupService: ICleanupService
// ) {
// this.$analyticsService.setShouldDispose(false);
// $cleanupService.setShouldDispose(false);
// }
//
// public async execute(): Promise<void> {
// this.$previewAppLogProvider.on(
// DEVICE_LOG_EVENT_NAME,
// (deviceId: string, message: string) => {
// this.$logger.info(message);
// }
// );
//
// await this.$previewAppController.startPreview({
// projectDir: this.$projectData.projectDir,
// useHotModuleReload: this.$options.hmr,
// env: this.$options.env,
// });
//
// await this.$previewQrCodeService.printLiveSyncQrCode({
// projectDir: this.$projectData.projectDir,
// useHotModuleReload: this.$options.hmr,
// link: this.$options.link,
// });
// }
//
// public async canExecute(args: string[]): Promise<boolean> {
// if (args && args.length) {
// this.$errors.failWithHelp(
// `The ${args.length > 1 ? "arguments" : "argument"} '${args.join(
// " "
// )}' ${
// args.length > 1 ? "are" : "is"
// } not valid for the preview command.`
// );
// }
//
// if (!this.$options.force) {
// await this.$migrateController.validate({
// projectDir: this.$projectData.projectDir,
// platforms: [],
// });
// }
//
// await this.$networkConnectivityValidator.validate();
// return true;
// }
// }
injector.registerCommand("preview", PreviewCommand);