Skip to content

Commit 91eda56

Browse files
committed
Fixes the behaviour of getting started when nativescript-cloud is already installed.
Fixes messages when console is non-interactive. Add `In case you have any issues, you can ask in our forum.` message when env is not configured after setup script. Refactor unit tests
1 parent 5c6d12c commit 91eda56

File tree

3 files changed

+205
-152
lines changed

3 files changed

+205
-152
lines changed
Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as constants from "../constants";
2+
import * as semver from "semver";
23

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

89
public install(): Promise<IExtensionData> {
910
if (!this.isInstalled()) {
@@ -14,11 +15,22 @@ export class NativescriptCloudExtensionService implements INativescriptCloudExte
1415
}
1516

1617
public isInstalled(): boolean {
17-
return !!this.getInstalledExtensions()[constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME];
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;
1829
}
1930

20-
private getInstalledExtensions(): IStringDictionary {
21-
return this.$extensibilityService.getInstalledExtensions() || {};
31+
private getExtensionData(): IExtensionData {
32+
return this.$extensibilityService.getInstalledExtensionsData()
33+
.find(extensionData => extensionData.extensionName === constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME);
2234
}
2335
}
2436
$injector.register("nativescriptCloudExtensionService", NativescriptCloudExtensionService);

lib/services/platform-environment-requirements.ts

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@ 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";
17+
private static BOTH_CLOUD_SETUP_AND_LOCAL_SETUP_OPTION_NAME = "Configure for Both Local and Cloud Builds";
1818
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:";
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 issues, you can ask in our forum. To continue, choose one of the following options:";
20+
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. Your environment is not configured properly and you will not be able to execute local builds. To continue, choose one of the following options: `;
21+
private static MISSING_LOCAL_SETUP_MESSAGE = 'Your environment is not configured properly and you will not be able to execute local builds.';
2022

2123
private cliCommandToCloudCommandName: IStringDictionary = {
2224
"build": "tns cloud build",
@@ -35,41 +37,35 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
3537
this.fail(this.getNonInteractiveConsoleMessage(platform));
3638
}
3739

38-
this.$logger.info(`` + EOL
39-
+ `Select "Configure for Cloud Builds" to install the ${constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension and automatically configure your environment for cloud builds.` + EOL
40-
+ `Select "Configure for Local Builds" to run the setup script and automatically configure your environment for local builds.`
41-
+ `Select "Configure for Both Local and Cloud Builds" to automatically configure your environment for both options.`
42-
+ `Select "Skip Step and Configure Manually" to disregard these options and install any required components manually.`);
40+
this.$logger.info(this.getInteractiveConsoleMessage(platform));
4341

44-
const selectedOption = await this.$prompter.promptForChoice(PlatformEnvironmentRequirements.NOT_CONFIGURED_ENV_MESSAGE, [
45-
PlatformEnvironmentRequirements.CLOUD_BUILDS_OPTION_NAME,
46-
PlatformEnvironmentRequirements.SETUP_SCRIPT_OPTION_NAME,
47-
PlatformEnvironmentRequirements.BOTH_CLOUD_BUILDS_AND_SETUP_SCRIPT_OPTION_NAME,
48-
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME,
49-
]);
42+
const selectedOption = await this.promptForChoice();
5043

5144
await this.processCloudBuildsIfNeeded(platform, selectedOption);
52-
5345
this.processManuallySetupIfNeeded(platform, selectedOption);
5446

55-
if (selectedOption === PlatformEnvironmentRequirements.SETUP_SCRIPT_OPTION_NAME) {
47+
if (selectedOption === PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME) {
5648
await this.$doctorService.runSetupScript();
5749

5850
if (await this.$doctorService.canExecuteLocalBuild(platform)) {
5951
return true;
6052
}
6153

62-
const option = await this.$prompter.promptForChoice(PlatformEnvironmentRequirements.NOT_CONFIGURED_ENV_AFTER_SETUP_SCRIPT_MESSAGE, [
63-
PlatformEnvironmentRequirements.CLOUD_BUILDS_OPTION_NAME,
64-
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME
65-
]);
54+
if (this.$nativescriptCloudExtensionService.isInstalled()) {
55+
this.processManuallySetup(platform);
56+
} else {
57+
const option = await this.$prompter.promptForChoice(PlatformEnvironmentRequirements.NOT_CONFIGURED_ENV_AFTER_SETUP_SCRIPT_MESSAGE, [
58+
PlatformEnvironmentRequirements.CLOUD_SETUP_OPTION_NAME,
59+
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME
60+
]);
6661

67-
await this.processCloudBuildsIfNeeded(platform, option);
62+
await this.processCloudBuildsIfNeeded(platform, option);
6863

69-
this.processManuallySetupIfNeeded(platform, option);
64+
this.processManuallySetupIfNeeded(platform, option);
65+
}
7066
}
7167

72-
if (selectedOption === PlatformEnvironmentRequirements.BOTH_CLOUD_BUILDS_AND_SETUP_SCRIPT_OPTION_NAME) {
68+
if (selectedOption === PlatformEnvironmentRequirements.BOTH_CLOUD_SETUP_AND_LOCAL_SETUP_OPTION_NAME) {
7369
await this.processBothCloudBuildsAndSetupScript(platform);
7470
if (await this.$doctorService.canExecuteLocalBuild(platform)) {
7571
return true;
@@ -83,7 +79,7 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
8379
}
8480

8581
private async processCloudBuildsIfNeeded(platform: string, selectedOption: string): Promise<void> {
86-
if (selectedOption === PlatformEnvironmentRequirements.CLOUD_BUILDS_OPTION_NAME) {
82+
if (selectedOption === PlatformEnvironmentRequirements.CLOUD_SETUP_OPTION_NAME) {
8783
await this.processCloudBuilds(platform);
8884
}
8985
}
@@ -135,17 +131,48 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
135131
}
136132

137133
private getNonInteractiveConsoleMessage(platform: string) {
138-
if (!this.$nativescriptCloudExtensionService.isInstalled()) {
139-
return `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
134+
if (this.$nativescriptCloudExtensionService.isInstalled()) {
135+
return `${PlatformEnvironmentRequirements.MISSING_LOCAL_SETUP_MESSAGE} To continue, choose one of the following options: ` + EOL
140136
+ "Run $ tns setup command to run the setup script to try to automatically configure your environment for local builds." + EOL
141-
+ `Run $ tns cloud setup command to install the ${constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension to configure your environment for cloud builds.` + EOL
142-
+ `Verify that your environment is configured according to the system requirements described at ${this.$staticConfig.SYS_REQUIREMENTS_LINK}.`
137+
+ `${this.getCloudBuildsMessage(platform)}` + EOL
138+
+ `Verify that your environment is configured according to the system requirements described at ${this.$staticConfig.SYS_REQUIREMENTS_LINK}.`;
143139
}
144140

145-
return `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
141+
return `${PlatformEnvironmentRequirements.MISSING_LOCAL_AND_CLOUD_SETUP_MESSAGE}` + EOL
146142
+ "Run $ tns setup command to run the setup script to try to automatically configure your environment for local builds." + EOL
147-
+ `${this.getCloudBuildsMessage(platform)}` + EOL
143+
+ `Run $ tns cloud setup command to install the ${constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension to configure your environment for cloud builds.` + EOL
148144
+ `Verify that your environment is configured according to the system requirements described at ${this.$staticConfig.SYS_REQUIREMENTS_LINK}.`;
149145
}
146+
147+
private getInteractiveConsoleMessage(platform: string) {
148+
if (this.$nativescriptCloudExtensionService.isInstalled()) {
149+
const message = `The ${constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension is installed and you can ${_.lowerFirst(this.getCloudBuildsMessage(platform))}`;
150+
151+
return `${message.bold}` + EOL
152+
+ `${PlatformEnvironmentRequirements.MISSING_LOCAL_SETUP_MESSAGE} To continue, choose one of the following options: ` + EOL
153+
+ `Select "Configure for Local Builds" to run the setup script and automatically configure your environment for local builds.` + EOL
154+
+ `Select "Skip Step and Configure Manually" to disregard this option and install any required components manually.`;
155+
}
156+
157+
return `${PlatformEnvironmentRequirements.MISSING_LOCAL_AND_CLOUD_SETUP_MESSAGE}` + EOL
158+
+ `Select "Configure for Cloud Builds" to install the ${constants.NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension and automatically configure your environment for cloud builds.` + EOL
159+
+ `Select "Configure for Local Builds" to run the setup script and automatically configure your environment for local builds.`
160+
+ `Select "Configure for Both Local and Cloud Builds" to automatically configure your environment for both options.`
161+
+ `Select "Skip Step and Configure Manually" to disregard these options and install any required components manually.`;
162+
}
163+
164+
private promptForChoice(): Promise<string> {
165+
const choices = this.$nativescriptCloudExtensionService.isInstalled() ? [
166+
PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME,
167+
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME,
168+
] : [
169+
PlatformEnvironmentRequirements.CLOUD_SETUP_OPTION_NAME,
170+
PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME,
171+
PlatformEnvironmentRequirements.BOTH_CLOUD_SETUP_AND_LOCAL_SETUP_OPTION_NAME,
172+
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME,
173+
];
174+
175+
return this.$prompter.promptForChoice(PlatformEnvironmentRequirements.NOT_CONFIGURED_ENV_MESSAGE, choices);
176+
}
150177
}
151178
$injector.register("platformEnvironmentRequirements", PlatformEnvironmentRequirements);

0 commit comments

Comments
 (0)