-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathplatform-environment-requirements.ts
182 lines (145 loc) · 7.66 KB
/
platform-environment-requirements.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { TrackActionNames } from "../constants";
import { isInteractive, hook } from "../common/helpers";
import { EOL } from "os";
export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequirements {
constructor(private $doctorService: IDoctorService,
private $errors: IErrors,
private $logger: ILogger,
private $prompter: IPrompter,
private $staticConfig: IStaticConfig,
private $analyticsService: IAnalyticsService,
private $injector: IInjector,
private $previewQrCodeService: IPreviewQrCodeService) { }
public get $previewAppController(): IPreviewAppController {
return this.$injector.resolve("previewAppController");
}
public static LOCAL_SETUP_OPTION_NAME = "Configure for Local Builds";
public static SYNC_TO_PREVIEW_APP_OPTION_NAME = "Sync to Playground";
public static MANUALLY_SETUP_OPTION_NAME = "Skip Step and Configure Manually";
private static CHOOSE_OPTIONS_MESSAGE = "To continue, choose one of the following options: ";
private static NOT_CONFIGURED_ENV_AFTER_SETUP_SCRIPT_MESSAGE = `The setup script was not able to configure your environment for local builds. To execute local builds, you have to set up your environment manually. Please consult our setup instructions here 'https://docs.nativescript.org/start/quick-setup'.`;
private static MISSING_LOCAL_SETUP_MESSAGE = "Your environment is not configured properly and you will not be able to execute local builds.";
private static RUN_TNS_SETUP_MESSAGE = 'Run $ tns setup command to run the setup script to try to automatically configure your environment for local builds.';
private static SYNC_TO_PREVIEW_APP_MESSAGE = `Select "Sync to Playground" to enjoy NativeScript without any local setup. All you need is a couple of companion apps installed on your devices.`;
private static RUN_PREVIEW_COMMAND_MESSAGE = `Run $ tns preview command to enjoy NativeScript without any local setup.`;
@hook("checkEnvironment")
public async checkEnvironmentRequirements(input: ICheckEnvironmentRequirementsInput): Promise<ICheckEnvironmentRequirementsOutput> {
const { platform, projectDir, runtimeVersion } = input;
const notConfiguredEnvOptions = input.notConfiguredEnvOptions || {};
const options = input.options || <IOptions>{};
let selectedOption = null;
if (process.env.NS_SKIP_ENV_CHECK) {
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: TrackActionNames.CheckEnvironmentRequirements,
additionalData: "Skipped: NS_SKIP_ENV_CHECK is set"
});
return {
canExecute: true,
selectedOption
};
}
const canExecute = await this.$doctorService.canExecuteLocalBuild({ platform, projectDir, runtimeVersion, forceCheck: input.forceCheck });
if (!canExecute) {
if (!isInteractive()) {
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: TrackActionNames.CheckEnvironmentRequirements,
additionalData: "Non-interactive terminal, unable to execute local builds."
});
this.fail(this.getNonInteractiveConsoleMessage(platform));
}
const infoMessage = this.getInteractiveConsoleMessage(notConfiguredEnvOptions);
const choices = this.getChoices(notConfiguredEnvOptions);
selectedOption = await this.promptForChoice({ infoMessage, choices });
this.processManuallySetupIfNeeded(selectedOption, platform);
await this.processSyncToPreviewAppIfNeeded(selectedOption, projectDir, options);
if (selectedOption === PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME) {
await this.$doctorService.runSetupScript();
if (await this.$doctorService.canExecuteLocalBuild({ platform, projectDir, runtimeVersion, forceCheck: input.forceCheck })) {
return {
canExecute: true,
selectedOption
};
}
this.fail(PlatformEnvironmentRequirements.NOT_CONFIGURED_ENV_AFTER_SETUP_SCRIPT_MESSAGE);
}
}
return {
canExecute,
selectedOption
};
}
private processManuallySetupIfNeeded(selectedOption: string, platform?: string) {
if (selectedOption === PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME) {
this.processManuallySetup(platform);
}
}
private async processSyncToPreviewAppIfNeeded(selectedOption: string, projectDir: string, options: IOptions) {
if (selectedOption === PlatformEnvironmentRequirements.SYNC_TO_PREVIEW_APP_OPTION_NAME) {
if (!projectDir) {
this.$errors.fail(`No project found. In order to sync to playground you need to go to project directory or specify --path option.`);
}
await this.$previewAppController.startPreview({
projectDir,
env: options.env,
useHotModuleReload: options.hmr,
});
await this.$previewQrCodeService.printLiveSyncQrCode({ projectDir, useHotModuleReload: options.hmr, link: options.link });
}
}
private processManuallySetup(platform?: string): void {
this.fail(`To be able to ${platform ? `build for ${platform}` : 'build'}, verify that your environment is configured according to the system requirements described at ${this.$staticConfig.SYS_REQUIREMENTS_LINK}. If you have any questions, check Stack Overflow: 'https://stackoverflow.com/questions/tagged/nativescript' and our public Slack channel: 'https://nativescriptcommunity.slack.com/'`);
}
private fail(message: string): void {
this.$errors.fail({ formatStr: message, printOnStdout: true });
}
private getNonInteractiveConsoleMessage(platform: string) {
return this.buildMultilineMessage([
`${PlatformEnvironmentRequirements.MISSING_LOCAL_SETUP_MESSAGE} ${PlatformEnvironmentRequirements.CHOOSE_OPTIONS_MESSAGE}`,
PlatformEnvironmentRequirements.RUN_PREVIEW_COMMAND_MESSAGE,
PlatformEnvironmentRequirements.RUN_TNS_SETUP_MESSAGE,
this.getEnvVerificationMessage()
]);
}
private getInteractiveConsoleMessage(options: INotConfiguredEnvOptions) {
const message = PlatformEnvironmentRequirements.MISSING_LOCAL_SETUP_MESSAGE;
const choices = [
`Select "Configure for Local Builds" to run the setup script and automatically configure your environment for local builds.`,
`Select "Skip Step and Configure Manually" to disregard this option and install any required components manually.`
];
if (!options.hideSyncToPreviewAppOption) {
choices.unshift(PlatformEnvironmentRequirements.SYNC_TO_PREVIEW_APP_MESSAGE);
}
const lines = [message].concat(choices);
const result = this.buildMultilineMessage(lines);
return result;
}
private async promptForChoice(opts: { infoMessage: string, choices: string[], }): Promise<string> {
this.$logger.info(opts.infoMessage);
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: TrackActionNames.CheckEnvironmentRequirements,
additionalData: `User should select: ${opts.infoMessage}`
});
const selection = await this.$prompter.promptForChoice(PlatformEnvironmentRequirements.CHOOSE_OPTIONS_MESSAGE, opts.choices);
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: TrackActionNames.CheckEnvironmentRequirements,
additionalData: `User selected: ${selection}`
});
return selection;
}
private getEnvVerificationMessage() {
return `Verify that your environment is configured according to the system requirements described at ${this.$staticConfig.SYS_REQUIREMENTS_LINK}.`;
}
private buildMultilineMessage(parts: string[]): string {
return parts.join(EOL);
}
private getChoices(options: INotConfiguredEnvOptions): string[] {
const choices: string[] = [];
choices.push(...[PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME,
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME]);
if (!options.hideSyncToPreviewAppOption) {
choices.unshift(PlatformEnvironmentRequirements.SYNC_TO_PREVIEW_APP_OPTION_NAME);
}
return choices;
}
}
$injector.register("platformEnvironmentRequirements", PlatformEnvironmentRequirements);