Skip to content

Commit f1a2995

Browse files
author
Fatme
authored
Merge pull request #3495 from NativeScript/fatme/fix-env-messages
fix (getting started): Improve behaviour when nativescript-cloud is installed
2 parents 51ba9d2 + 50bbfb6 commit f1a2995

File tree

6 files changed

+249
-160
lines changed

6 files changed

+249
-160
lines changed

lib/bootstrap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ $injector.require("terminalSpinnerService", "./services/terminal-spinner-service
160160

161161
$injector.require('playgroundService', './services/playground-service');
162162
$injector.require("platformEnvironmentRequirements", "./services/platform-environment-requirements");
163-
$injector.require("nativescriptCloudExtensionService", "./services/nativescript-cloud-extension-service");
163+
$injector.require("nativeScriptCloudExtensionService", "./services/nativescript-cloud-extension-service");
164164

165165
$injector.requireCommand("resources|generate|icons", "./commands/generate-assets");
166166
$injector.requireCommand("resources|generate|splashes", "./commands/generate-assets");

lib/commands/setup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ $injector.registerCommand("setup|*", SetupCommand);
1212
export class CloudSetupCommand implements ICommand {
1313
public allowedParameters: ICommandParameter[] = [];
1414

15-
constructor(private $nativescriptCloudExtensionService: INativescriptCloudExtensionService) { }
15+
constructor(private $nativeScriptCloudExtensionService: INativeScriptCloudExtensionService) { }
1616

1717
public execute(args: string[]): Promise<any> {
18-
return this.$nativescriptCloudExtensionService.install();
18+
return this.$nativeScriptCloudExtensionService.install();
1919
}
2020
}
2121
$injector.registerCommand(["setup|cloud", "cloud|setup"], CloudSetupCommand);

lib/declarations.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,12 +788,17 @@ interface IBundleValidatorHelper {
788788
validate(): void;
789789
}
790790

791-
interface INativescriptCloudExtensionService {
791+
interface INativeScriptCloudExtensionService {
792792
/**
793793
* Installs nativescript-cloud extension
794794
* @return {Promise<IExtensionData>} returns the extension data
795795
*/
796796
install(): Promise<IExtensionData>;
797+
/**
798+
* Checks if nativescript-cloud extension is installed
799+
* @return {boolean} returns true in case when nativescript-cloud extension is installed, false otherwise
800+
*/
801+
isInstalled(): boolean
797802
}
798803

799804
/**
Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
import * as constants from "../constants";
2+
import * as semver from "semver";
23

3-
export class NativescriptCloudExtensionService implements INativescriptCloudExtensionService {
4-
4+
export class NativeScriptCloudExtensionService implements INativeScriptCloudExtensionService {
55
constructor(private $extensibilityService: IExtensibilityService,
6-
private $logger: ILogger) { }
6+
private $logger: ILogger,
7+
private $npmInstallationManager: INpmInstallationManager) { }
78

89
public install(): Promise<IExtensionData> {
9-
const installedExtensions = this.$extensibilityService.getInstalledExtensions() || {};
10-
if (!installedExtensions[constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME]) {
10+
if (!this.isInstalled()) {
1111
return this.$extensibilityService.installExtension(constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME);
1212
}
1313

1414
this.$logger.out(`Extension ${constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME} is already installed.`);
1515
}
16+
17+
public isInstalled(): boolean {
18+
return !!this.getExtensionData();
19+
}
20+
21+
public async isLatestVersionInstalled(): Promise<boolean> {
22+
const extensionData = this.getExtensionData();
23+
if (extensionData) {
24+
const latestVersion = await this.$npmInstallationManager.getLatestVersion(constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME);
25+
return semver.eq(latestVersion, extensionData.version);
26+
}
27+
28+
return false;
29+
}
30+
31+
private getExtensionData(): IExtensionData {
32+
return _.find(this.$extensibilityService.getInstalledExtensionsData(), extensionData => extensionData.extensionName === constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME);
33+
}
1634
}
17-
$injector.register("nativescriptCloudExtensionService", NativescriptCloudExtensionService);
35+
$injector.register("nativeScriptCloudExtensionService", NativeScriptCloudExtensionService);

lib/services/platform-environment-requirements.ts

Lines changed: 85 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
66
constructor(private $commandsService: ICommandsService,
77
private $doctorService: IDoctorService,
88
private $errors: IErrors,
9-
private $nativescriptCloudExtensionService: INativescriptCloudExtensionService,
109
private $logger: ILogger,
10+
private $nativeScriptCloudExtensionService: INativeScriptCloudExtensionService,
1111
private $prompter: IPrompter,
1212
private $staticConfig: IStaticConfig) { }
1313

14-
public static CLOUD_BUILDS_OPTION_NAME = "Configure for Cloud Builds";
15-
public static SETUP_SCRIPT_OPTION_NAME = "Configure for Local Builds";
14+
public static CLOUD_SETUP_OPTION_NAME = "Configure for Cloud Builds";
15+
public static LOCAL_SETUP_OPTION_NAME = "Configure for Local Builds";
1616
public static MANUALLY_SETUP_OPTION_NAME = "Skip Step and Configure Manually";
17-
private static BOTH_CLOUD_BUILDS_AND_SETUP_SCRIPT_OPTION_NAME = "Configure for Both Local and Cloud Builds";
18-
private static NOT_CONFIGURED_ENV_MESSAGE = "To continue, choose one of the following options: ";
19-
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. To continue, choose one of the following options:";
17+
private static BOTH_CLOUD_SETUP_AND_LOCAL_SETUP_OPTION_NAME = "Configure for Both Local and Cloud Builds";
18+
private static CHOOSE_OPTIONS_MESSAGE = "To continue, choose one of the following options: ";
19+
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. In case you have any questions, you can check our forum: 'http://forum.nativescript.org' and our public Slack channel: 'https://nativescriptcommunity.slack.com/'. ${PlatformEnvironmentRequirements.CHOOSE_OPTIONS_MESSAGE}`;
20+
private static MISSING_LOCAL_SETUP_MESSAGE = "Your environment is not configured properly and you will not be able to execute local builds.";
21+
private static MISSING_LOCAL_AND_CLOUD_SETUP_MESSAGE = `You are missing the ${constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension and you will not be able to execute cloud builds. ${PlatformEnvironmentRequirements.MISSING_LOCAL_SETUP_MESSAGE} ${PlatformEnvironmentRequirements.CHOOSE_OPTIONS_MESSAGE} `;
22+
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.';
2023

2124
private cliCommandToCloudCommandName: IStringDictionary = {
2225
"build": "tns cloud build",
@@ -32,47 +35,38 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
3235
const canExecute = await this.$doctorService.canExecuteLocalBuild(platform);
3336
if (!canExecute) {
3437
if (!isInteractive()) {
35-
this.fail(`You are missing the ${constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension and you will not be able to execute cloud builds. Your environment is not configured properly and you will not be able to execute local builds. To continue, choose one of the following options: ` + EOL
36-
+ "Run $ tns setup command to run the setup script to try to automatically configure your environment for local builds." + EOL
37-
+ `Run $ tns cloud setup command to install the ${constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension to configure your environment for cloud builds.` + EOL
38-
+ `Verify that your environment is configured according to the system requirements described at ${this.$staticConfig.SYS_REQUIREMENTS_LINK}`);
38+
this.fail(this.getNonInteractiveConsoleMessage(platform));
3939
}
4040

41-
this.$logger.info(`You are missing the ${constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension and you will not be able to execute cloud builds. Your environment is not configured properly and you will not be able to execute local builds. ` + EOL
42-
+ `Select "Configure for Cloud Builds" to install the ${constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension and automatically configure your environment for cloud builds.` + EOL
43-
+ `Select "Configure for Local Builds" to run the setup script and automatically configure your environment for local builds.`
44-
+ `Select "Configure for Both Local and Cloud Builds" to automatically configure your environment for both options.`
45-
+ `Select "Skip Step and Configure Manually" to disregard these options and install any required components manually.`);
46-
47-
const selectedOption = await this.$prompter.promptForChoice(PlatformEnvironmentRequirements.NOT_CONFIGURED_ENV_MESSAGE, [
48-
PlatformEnvironmentRequirements.CLOUD_BUILDS_OPTION_NAME,
49-
PlatformEnvironmentRequirements.SETUP_SCRIPT_OPTION_NAME,
50-
PlatformEnvironmentRequirements.BOTH_CLOUD_BUILDS_AND_SETUP_SCRIPT_OPTION_NAME,
51-
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME,
52-
]);
41+
this.$logger.info(this.getInteractiveConsoleMessage(platform));
5342

54-
await this.processCloudBuildsIfNeeded(platform, selectedOption);
43+
const selectedOption = await this.promptForChoice();
5544

45+
await this.processCloudBuildsIfNeeded(platform, selectedOption);
5646
this.processManuallySetupIfNeeded(platform, selectedOption);
5747

58-
if (selectedOption === PlatformEnvironmentRequirements.SETUP_SCRIPT_OPTION_NAME) {
48+
if (selectedOption === PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME) {
5949
await this.$doctorService.runSetupScript();
6050

6151
if (await this.$doctorService.canExecuteLocalBuild(platform)) {
6252
return true;
6353
}
6454

65-
const option = await this.$prompter.promptForChoice(PlatformEnvironmentRequirements.NOT_CONFIGURED_ENV_AFTER_SETUP_SCRIPT_MESSAGE, [
66-
PlatformEnvironmentRequirements.CLOUD_BUILDS_OPTION_NAME,
67-
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME
68-
]);
55+
if (this.$nativeScriptCloudExtensionService.isInstalled()) {
56+
this.processManuallySetup(platform);
57+
} else {
58+
const option = await this.$prompter.promptForChoice(PlatformEnvironmentRequirements.NOT_CONFIGURED_ENV_AFTER_SETUP_SCRIPT_MESSAGE, [
59+
PlatformEnvironmentRequirements.CLOUD_SETUP_OPTION_NAME,
60+
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME
61+
]);
6962

70-
await this.processCloudBuildsIfNeeded(platform, option);
63+
await this.processCloudBuildsIfNeeded(platform, option);
7164

72-
this.processManuallySetupIfNeeded(platform, option);
65+
this.processManuallySetupIfNeeded(platform, option);
66+
}
7367
}
7468

75-
if (selectedOption === PlatformEnvironmentRequirements.BOTH_CLOUD_BUILDS_AND_SETUP_SCRIPT_OPTION_NAME) {
69+
if (selectedOption === PlatformEnvironmentRequirements.BOTH_CLOUD_SETUP_AND_LOCAL_SETUP_OPTION_NAME) {
7670
await this.processBothCloudBuildsAndSetupScript(platform);
7771
if (await this.$doctorService.canExecuteLocalBuild(platform)) {
7872
return true;
@@ -86,7 +80,7 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
8680
}
8781

8882
private async processCloudBuildsIfNeeded(platform: string, selectedOption: string): Promise<void> {
89-
if (selectedOption === PlatformEnvironmentRequirements.CLOUD_BUILDS_OPTION_NAME) {
83+
if (selectedOption === PlatformEnvironmentRequirements.CLOUD_SETUP_OPTION_NAME) {
9084
await this.processCloudBuilds(platform);
9185
}
9286
}
@@ -97,10 +91,10 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
9791
}
9892

9993
private processCloudBuildsCore(platform: string): Promise<IExtensionData> {
100-
return this.$nativescriptCloudExtensionService.install();
94+
return this.$nativeScriptCloudExtensionService.install();
10195
}
10296

103-
private getCloudBuildsMessage(platform: string): string {
97+
private getCloudBuildsMessage(platform: string): string {
10498
const cloudCommandName = this.cliCommandToCloudCommandName[this.$commandsService.currentCommandData.commandName];
10599
if (!cloudCommandName) {
106100
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.`;
@@ -136,5 +130,62 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
136130
private fail(message: string): void {
137131
this.$errors.fail({ formatStr: message, suppressCommandHelp: true, printOnStdout: true });
138132
}
133+
134+
private getNonInteractiveConsoleMessage(platform: string) {
135+
return this.$nativeScriptCloudExtensionService.isInstalled() ?
136+
this.buildMultilineMessage([
137+
`${PlatformEnvironmentRequirements.MISSING_LOCAL_SETUP_MESSAGE} ${PlatformEnvironmentRequirements.CHOOSE_OPTIONS_MESSAGE}`,
138+
PlatformEnvironmentRequirements.RUN_TNS_SETUP_MESSAGE,
139+
this.getCloudBuildsMessage(platform),
140+
this.getEnvVerificationMessage()
141+
]) :
142+
this.buildMultilineMessage([
143+
PlatformEnvironmentRequirements.MISSING_LOCAL_AND_CLOUD_SETUP_MESSAGE,
144+
PlatformEnvironmentRequirements.RUN_TNS_SETUP_MESSAGE,
145+
`Run $ tns cloud setup command to install the ${constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension to configure your environment for cloud builds.`,
146+
this.getEnvVerificationMessage()
147+
]);
148+
}
149+
150+
private getInteractiveConsoleMessage(platform: string) {
151+
const message = `The ${constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension is installed and you can ${_.lowerFirst(this.getCloudBuildsMessage(platform))}`;
152+
153+
return this.$nativeScriptCloudExtensionService.isInstalled() ?
154+
this.buildMultilineMessage([
155+
`${message.bold}`,
156+
`${PlatformEnvironmentRequirements.MISSING_LOCAL_SETUP_MESSAGE} ${PlatformEnvironmentRequirements.CHOOSE_OPTIONS_MESSAGE}`,
157+
`Select "Configure for Local Builds" to run the setup script and automatically configure your environment for local builds.`,
158+
`Select "Skip Step and Configure Manually" to disregard this option and install any required components manually.`
159+
]) :
160+
this.buildMultilineMessage([
161+
PlatformEnvironmentRequirements.MISSING_LOCAL_AND_CLOUD_SETUP_MESSAGE,
162+
`Select "Configure for Cloud Builds" to install the ${constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension and automatically configure your environment for cloud builds.`,
163+
`Select "Configure for Local Builds" to run the setup script and automatically configure your environment for local builds.`,
164+
`Select "Configure for Both Local and Cloud Builds" to automatically configure your environment for both options.`,
165+
`Select "Configure for Both Local and Cloud Builds" to automatically configure your environment for both options.`
166+
]);
167+
}
168+
169+
private promptForChoice(): Promise<string> {
170+
const choices = this.$nativeScriptCloudExtensionService.isInstalled() ? [
171+
PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME,
172+
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME,
173+
] : [
174+
PlatformEnvironmentRequirements.CLOUD_SETUP_OPTION_NAME,
175+
PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME,
176+
PlatformEnvironmentRequirements.BOTH_CLOUD_SETUP_AND_LOCAL_SETUP_OPTION_NAME,
177+
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME,
178+
];
179+
180+
return this.$prompter.promptForChoice(PlatformEnvironmentRequirements.CHOOSE_OPTIONS_MESSAGE, choices);
181+
}
182+
183+
private getEnvVerificationMessage() {
184+
return `Verify that your environment is configured according to the system requirements described at ${this.$staticConfig.SYS_REQUIREMENTS_LINK}.`;
185+
}
186+
187+
private buildMultilineMessage(parts: string[]): string {
188+
return parts.join(EOL);
189+
}
139190
}
140191
$injector.register("platformEnvironmentRequirements", PlatformEnvironmentRequirements);

0 commit comments

Comments
 (0)