-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathplatform-environment-requirements.ts
312 lines (261 loc) · 13.8 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import { NATIVESCRIPT_CLOUD_EXTENSION_NAME, TrackActionNames } from "../constants";
import { isInteractive } from "../common/helpers";
import { EOL } from "os";
import { cache } from "../common/decorators";
export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequirements {
constructor(private $commandsService: ICommandsService,
private $doctorService: IDoctorService,
private $errors: IErrors,
private $logger: ILogger,
private $nativeScriptCloudExtensionService: INativeScriptCloudExtensionService,
private $prompter: IPrompter,
private $staticConfig: IStaticConfig,
private $analyticsService: IAnalyticsService,
private $injector: IInjector,
private $previewQrCodeService: IPreviewQrCodeService) { }
@cache()
private get $liveSyncService(): ILiveSyncService {
return this.$injector.resolve("liveSyncService");
}
public static CLOUD_SETUP_OPTION_NAME = "Configure for Cloud Builds";
public static LOCAL_SETUP_OPTION_NAME = "Configure for Local Builds";
public static TRY_CLOUD_OPERATION_OPTION_NAME = "Try Cloud Operation";
public static SYNC_TO_PREVIEW_APP_OPTION_NAME = "Sync to Playground";
public static MANUALLY_SETUP_OPTION_NAME = "Skip Step and Configure Manually";
private static BOTH_CLOUD_SETUP_AND_LOCAL_SETUP_OPTION_NAME = "Configure for Both Local and Cloud Builds";
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 MISSING_LOCAL_AND_CLOUD_SETUP_MESSAGE = `You are missing the ${NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension and you will not be able to execute cloud builds. ${PlatformEnvironmentRequirements.MISSING_LOCAL_SETUP_MESSAGE} ${PlatformEnvironmentRequirements.CHOOSE_OPTIONS_MESSAGE} `;
private static MISSING_LOCAL_BUT_CLOUD_SETUP_MESSAGE = `You have ${NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension installed, so you can execute cloud builds, but ${_.lowerFirst(PlatformEnvironmentRequirements.MISSING_LOCAL_SETUP_MESSAGE)}`;
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.`;
private cliCommandToCloudCommandName: IStringDictionary = {
"build": "tns cloud build",
"run": "tns cloud run",
"deploy": "tns cloud deploy"
};
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);
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 });
await this.processCloudBuildsIfNeeded(selectedOption, platform);
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)) {
return {
canExecute: true,
selectedOption
};
}
if (this.$nativeScriptCloudExtensionService.isInstalled()) {
const option = await this.promptForChoice({
infoMessage: PlatformEnvironmentRequirements.NOT_CONFIGURED_ENV_AFTER_SETUP_SCRIPT_MESSAGE,
choices: [
PlatformEnvironmentRequirements.TRY_CLOUD_OPERATION_OPTION_NAME,
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME
]
});
this.processTryCloudSetupIfNeeded(option, platform);
this.processManuallySetupIfNeeded(option, platform);
} else {
const option = await this.promptForChoice({
infoMessage: PlatformEnvironmentRequirements.NOT_CONFIGURED_ENV_AFTER_SETUP_SCRIPT_MESSAGE,
choices: [
PlatformEnvironmentRequirements.CLOUD_SETUP_OPTION_NAME,
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME
]
});
await this.processCloudBuildsIfNeeded(option, platform);
this.processManuallySetupIfNeeded(option, platform);
}
}
if (selectedOption === PlatformEnvironmentRequirements.BOTH_CLOUD_SETUP_AND_LOCAL_SETUP_OPTION_NAME) {
await this.processBothCloudBuildsAndSetupScript();
if (await this.$doctorService.canExecuteLocalBuild(platform, projectDir, runtimeVersion)) {
return {
canExecute: true,
selectedOption
};
}
this.processManuallySetup(platform);
}
this.processTryCloudSetupIfNeeded(selectedOption, platform);
}
return {
canExecute,
selectedOption
};
}
private async processCloudBuildsIfNeeded(selectedOption: string, platform?: string): Promise<void> {
if (selectedOption === PlatformEnvironmentRequirements.CLOUD_SETUP_OPTION_NAME) {
await this.processCloudBuilds(platform);
}
}
private async processCloudBuilds(platform: string): Promise<void> {
await this.processCloudBuildsCore();
this.fail(this.getCloudBuildsMessage(platform));
}
private processCloudBuildsCore(): Promise<IExtensionData> {
return this.$nativeScriptCloudExtensionService.install();
}
private getCloudBuildsMessage(platform?: string): string {
const cloudCommandName = this.cliCommandToCloudCommandName[(this.$commandsService.currentCommandData || <any>{}).commandName];
if (!cloudCommandName) {
return `In order to test your application use the $ tns login command to log in with your account and then $ tns cloud build command to build your app in the cloud.`;
}
if (!platform) {
return `Use the $ tns login command to log in with your account and then $ ${cloudCommandName.toLowerCase()} command.`;
}
return `Use the $ tns login command to log in with your account and then $ ${cloudCommandName.toLowerCase()} ${platform.toLowerCase()} command.`;
}
private processTryCloudSetupIfNeeded(selectedOption: string, platform?: string) {
if (selectedOption === PlatformEnvironmentRequirements.TRY_CLOUD_OPERATION_OPTION_NAME) {
this.fail(this.getCloudBuildsMessage(platform));
}
}
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.failWithoutHelp(`No project found. In order to sync to playground you need to go to project directory or specify --path option.`);
}
await this.$liveSyncService.liveSync([], {
syncToPreviewApp: true,
projectDir,
skipWatcher: !options.watch,
watchAllFiles: options.syncAllFiles,
clean: options.clean,
bundle: !!options.bundle,
release: options.release,
env: options.env,
timeout: options.timeout,
useHotModuleReload: options.hmr
});
await this.$previewQrCodeService.printLiveSyncQrCode({ 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 async processBothCloudBuildsAndSetupScript(): Promise<void> {
try {
await this.processCloudBuildsCore();
} catch (e) {
this.$logger.trace(`Error while installing ${NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension. ${e.message}.`);
}
await this.$doctorService.runSetupScript();
}
private fail(message: string): void {
this.$errors.fail({ formatStr: message, suppressCommandHelp: true, printOnStdout: true });
}
private getNonInteractiveConsoleMessage(platform: string) {
return this.$nativeScriptCloudExtensionService.isInstalled() ?
this.buildMultilineMessage([
`${PlatformEnvironmentRequirements.MISSING_LOCAL_SETUP_MESSAGE} ${PlatformEnvironmentRequirements.CHOOSE_OPTIONS_MESSAGE}`,
PlatformEnvironmentRequirements.RUN_PREVIEW_COMMAND_MESSAGE,
PlatformEnvironmentRequirements.RUN_TNS_SETUP_MESSAGE,
this.getCloudBuildsMessage(platform),
this.getEnvVerificationMessage()
]) :
this.buildMultilineMessage([
PlatformEnvironmentRequirements.MISSING_LOCAL_AND_CLOUD_SETUP_MESSAGE,
PlatformEnvironmentRequirements.RUN_PREVIEW_COMMAND_MESSAGE,
PlatformEnvironmentRequirements.RUN_TNS_SETUP_MESSAGE,
`Run $ tns cloud setup command to install the ${NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension to configure your environment for cloud builds.`,
this.getEnvVerificationMessage()
]);
}
private getInteractiveConsoleMessage(options: INotConfiguredEnvOptions) {
const isNativeScriptCloudExtensionInstalled = this.$nativeScriptCloudExtensionService.isInstalled();
const message = isNativeScriptCloudExtensionInstalled ?
`${PlatformEnvironmentRequirements.MISSING_LOCAL_BUT_CLOUD_SETUP_MESSAGE} ${PlatformEnvironmentRequirements.CHOOSE_OPTIONS_MESSAGE}` :
PlatformEnvironmentRequirements.MISSING_LOCAL_AND_CLOUD_SETUP_MESSAGE;
const choices = isNativeScriptCloudExtensionInstalled ? [
`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.`
] : [
`Select "Configure for Cloud Builds" to install the ${NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension and automatically configure your environment for cloud builds.`,
`Select "Configure for Local Builds" to run the setup script and automatically configure your environment for local builds.`,
`Select "Configure for Both Local and Cloud Builds" to automatically configure your environment for both options.`,
`Select "Configure for Both Local and Cloud Builds" to automatically configure your environment for both options.`
];
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[] = [];
if (this.$nativeScriptCloudExtensionService.isInstalled()) {
choices.push(...[PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME,
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME]);
if (!options.hideCloudBuildOption) {
choices.unshift(PlatformEnvironmentRequirements.TRY_CLOUD_OPERATION_OPTION_NAME);
}
} else {
choices.push(...[
PlatformEnvironmentRequirements.CLOUD_SETUP_OPTION_NAME,
PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME,
PlatformEnvironmentRequirements.BOTH_CLOUD_SETUP_AND_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);