Skip to content

Commit 8c80d72

Browse files
committed
Hide "Sync to Playground" option when env is not properly configured and tns test command is executed
1 parent 01288cc commit 8c80d72

File tree

5 files changed

+79
-37
lines changed

5 files changed

+79
-37
lines changed

lib/commands/test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ function RunTestCommandFactory(platform: string) {
55
$options: IOptions,
66
$testExecutionService: ITestExecutionService,
77
$projectData: IProjectData,
8-
$analyticsService: IAnalyticsService) {
8+
$analyticsService: IAnalyticsService,
9+
$platformEnvironmentRequirements: IPlatformEnvironmentRequirements) {
910
$projectData.initializeProjectData();
1011
$analyticsService.setShouldDispose($options.justlaunch || !$options.watch);
1112
const projectFilesConfig = helpers.getProjectFilesConfig({ isReleaseBuild: $options.release });
1213
this.execute = (args: string[]): Promise<void> => $testExecutionService.startTestRunner(platform, $projectData, projectFilesConfig);
14+
this.canExecute = (args: string[]): Promise<boolean> => canExecute({ $platformEnvironmentRequirements, $projectData, $options, platform });
1315
this.allowedParameters = [];
1416
};
1517
}
@@ -18,14 +20,27 @@ $injector.registerCommand("dev-test|android", RunTestCommandFactory('android'));
1820
$injector.registerCommand("dev-test|ios", RunTestCommandFactory('iOS'));
1921

2022
function RunKarmaTestCommandFactory(platform: string) {
21-
return function RunKarmaTestCommand($options: IOptions, $testExecutionService: ITestExecutionService, $projectData: IProjectData, $analyticsService: IAnalyticsService) {
23+
return function RunKarmaTestCommand($options: IOptions, $testExecutionService: ITestExecutionService, $projectData: IProjectData, $analyticsService: IAnalyticsService, $platformEnvironmentRequirements: IPlatformEnvironmentRequirements) {
2224
$projectData.initializeProjectData();
2325
$analyticsService.setShouldDispose($options.justlaunch || !$options.watch);
2426
const projectFilesConfig = helpers.getProjectFilesConfig({ isReleaseBuild: $options.release });
2527
this.execute = (args: string[]): Promise<void> => $testExecutionService.startKarmaServer(platform, $projectData, projectFilesConfig);
28+
this.canExecute = (args: string[]): Promise<boolean> => canExecute({ $platformEnvironmentRequirements, $projectData, $options, platform });
2629
this.allowedParameters = [];
2730
};
2831
}
2932

33+
async function canExecute(input: { $platformEnvironmentRequirements: IPlatformEnvironmentRequirements, $projectData: IProjectData, $options: IOptions, platform: string }): Promise<boolean> {
34+
const { $platformEnvironmentRequirements, $projectData, $options, platform } = input;
35+
const output = await $platformEnvironmentRequirements.checkEnvironmentRequirements({
36+
platform,
37+
projectDir: $projectData.projectDir,
38+
options: $options,
39+
hideSyncToPreviewAppOption: true
40+
});
41+
42+
return output.canExecute;
43+
}
44+
3045
$injector.registerCommand("test|android", RunKarmaTestCommandFactory('android'));
3146
$injector.registerCommand("test|ios", RunKarmaTestCommandFactory('iOS'));

lib/definitions/platform.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ interface ICheckEnvironmentRequirementsInput {
399399
projectDir?: string;
400400
runtimeVersion?: string;
401401
options?: IOptions;
402+
hideSyncToPreviewAppOption?: boolean;
402403
}
403404

404405
interface ICheckEnvironmentRequirementsOutput {

lib/helpers/livesync-command-helper.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ export class LiveSyncCommandHelper implements ILiveSyncCommandHelper {
2222
}
2323

2424
public async executeCommandLiveSync(platform?: string, additionalOptions?: ILiveSyncCommandHelperAdditionalOptions) {
25-
if (!this.$options.syncAllFiles) {
26-
this.$logger.info("Skipping node_modules folder! Use the syncAllFiles option to sync files from this folder.");
27-
}
28-
2925
if (additionalOptions && additionalOptions.syncToPreviewApp) {
3026
return;
3127
}
3228

29+
if (!this.$options.syncAllFiles) {
30+
this.$logger.info("Skipping node_modules folder! Use the syncAllFiles option to sync files from this folder.");
31+
}
32+
3333
const emulator = this.$options.emulator;
3434
await this.$devicesService.initialize({
3535
deviceId: this.$options.device,

lib/services/platform-environment-requirements.ts

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
4141
};
4242

4343
public async checkEnvironmentRequirements(input: ICheckEnvironmentRequirementsInput): Promise<ICheckEnvironmentRequirementsOutput> {
44-
const { platform, projectDir, runtimeVersion, options } = input;
44+
const { platform, projectDir, runtimeVersion, hideSyncToPreviewAppOption } = input;
45+
const options = input.options || <IOptions>{ };
46+
4547
let selectedOption = null;
4648

4749
if (process.env.NS_SKIP_ENV_CHECK) {
@@ -65,21 +67,9 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
6567
this.fail(this.getNonInteractiveConsoleMessage(platform));
6668
}
6769

68-
const infoMessage = this.getInteractiveConsoleMessage(platform);
69-
this.$logger.info(infoMessage);
70+
const infoMessage = this.getInteractiveConsoleMessage({ hideSyncToPreviewAppOption });
7071

71-
const choices = this.$nativeScriptCloudExtensionService.isInstalled() ? [
72-
PlatformEnvironmentRequirements.SYNC_TO_PREVIEW_APP_OPTION_NAME,
73-
PlatformEnvironmentRequirements.TRY_CLOUD_OPERATION_OPTION_NAME,
74-
PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME,
75-
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME,
76-
] : [
77-
PlatformEnvironmentRequirements.SYNC_TO_PREVIEW_APP_OPTION_NAME,
78-
PlatformEnvironmentRequirements.CLOUD_SETUP_OPTION_NAME,
79-
PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME,
80-
PlatformEnvironmentRequirements.BOTH_CLOUD_SETUP_AND_LOCAL_SETUP_OPTION_NAME,
81-
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME,
82-
];
72+
const choices = this.getChoices({ hideSyncToPreviewAppOption });
8373

8474
selectedOption = await this.promptForChoice({ infoMessage, choices });
8575

@@ -236,22 +226,28 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
236226
]);
237227
}
238228

239-
private getInteractiveConsoleMessage(platform: string) {
240-
return this.$nativeScriptCloudExtensionService.isInstalled() ?
241-
this.buildMultilineMessage([
242-
`${PlatformEnvironmentRequirements.MISSING_LOCAL_BUT_CLOUD_SETUP_MESSAGE} ${PlatformEnvironmentRequirements.CHOOSE_OPTIONS_MESSAGE}`,
243-
PlatformEnvironmentRequirements.SYNC_TO_PREVIEW_APP_MESSAGE,
244-
`Select "Configure for Local Builds" to run the setup script and automatically configure your environment for local builds.`,
245-
`Select "Skip Step and Configure Manually" to disregard this option and install any required components manually.`
246-
]) :
247-
this.buildMultilineMessage([
248-
PlatformEnvironmentRequirements.MISSING_LOCAL_AND_CLOUD_SETUP_MESSAGE,
249-
PlatformEnvironmentRequirements.SYNC_TO_PREVIEW_APP_MESSAGE,
250-
`Select "Configure for Cloud Builds" to install the ${NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension and automatically configure your environment for cloud builds.`,
251-
`Select "Configure for Local Builds" to run the setup script and automatically configure your environment for local builds.`,
252-
`Select "Configure for Both Local and Cloud Builds" to automatically configure your environment for both options.`,
253-
`Select "Configure for Both Local and Cloud Builds" to automatically configure your environment for both options.`
254-
]);
229+
private getInteractiveConsoleMessage(options: { hideSyncToPreviewAppOption: boolean }) {
230+
const isNativeScriptCloudExtensionInstalled = this.$nativeScriptCloudExtensionService.isInstalled();
231+
const message = isNativeScriptCloudExtensionInstalled ?
232+
`${PlatformEnvironmentRequirements.MISSING_LOCAL_BUT_CLOUD_SETUP_MESSAGE} ${PlatformEnvironmentRequirements.CHOOSE_OPTIONS_MESSAGE}` :
233+
PlatformEnvironmentRequirements.MISSING_LOCAL_AND_CLOUD_SETUP_MESSAGE;
234+
const choices = isNativeScriptCloudExtensionInstalled ? [
235+
`Select "Configure for Local Builds" to run the setup script and automatically configure your environment for local builds.`,
236+
`Select "Skip Step and Configure Manually" to disregard this option and install any required components manually.`
237+
] : [
238+
`Select "Configure for Cloud Builds" to install the ${NATIVESCRIPT_CLOUD_EXTENSION_NAME} extension and automatically configure your environment for cloud builds.`,
239+
`Select "Configure for Local Builds" to run the setup script and automatically configure your environment for local builds.`,
240+
`Select "Configure for Both Local and Cloud Builds" to automatically configure your environment for both options.`,
241+
`Select "Configure for Both Local and Cloud Builds" to automatically configure your environment for both options.`
242+
];
243+
244+
if (!options.hideSyncToPreviewAppOption) {
245+
choices.unshift(PlatformEnvironmentRequirements.SYNC_TO_PREVIEW_APP_MESSAGE);
246+
}
247+
248+
const lines = [message].concat(choices);
249+
const result = this.buildMultilineMessage(lines);
250+
return result;
255251
}
256252

257253
private async promptForChoice(opts: { infoMessage: string, choices: string[], }): Promise<string> {
@@ -279,5 +275,24 @@ export class PlatformEnvironmentRequirements implements IPlatformEnvironmentRequ
279275
private buildMultilineMessage(parts: string[]): string {
280276
return parts.join(EOL);
281277
}
278+
279+
private getChoices(options: { hideSyncToPreviewAppOption: boolean }): string[] {
280+
const choices = this.$nativeScriptCloudExtensionService.isInstalled() ? [
281+
PlatformEnvironmentRequirements.TRY_CLOUD_OPERATION_OPTION_NAME,
282+
PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME,
283+
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME,
284+
] : [
285+
PlatformEnvironmentRequirements.CLOUD_SETUP_OPTION_NAME,
286+
PlatformEnvironmentRequirements.LOCAL_SETUP_OPTION_NAME,
287+
PlatformEnvironmentRequirements.BOTH_CLOUD_SETUP_AND_LOCAL_SETUP_OPTION_NAME,
288+
PlatformEnvironmentRequirements.MANUALLY_SETUP_OPTION_NAME,
289+
];
290+
291+
if (!options.hideSyncToPreviewAppOption) {
292+
choices.unshift(PlatformEnvironmentRequirements.SYNC_TO_PREVIEW_APP_OPTION_NAME);
293+
}
294+
295+
return choices;
296+
}
282297
}
283298
$injector.register("platformEnvironmentRequirements", PlatformEnvironmentRequirements);

test/services/platform-environment-requirements.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ describe("platformEnvironmentRequirements ", () => {
108108
assert.deepEqual("To continue, choose one of the following options: ", promptForChoiceData[0].message);
109109
assert.deepEqual(['Sync to Playground', 'Try Cloud Operation', 'Configure for Local Builds', 'Skip Step and Configure Manually'], promptForChoiceData[0].choices);
110110
});
111+
it("should not show 'Sync to Playground' option when hideSyncToPreviewAppOption is provided", async () => {
112+
mockDoctorService({ canExecuteLocalBuild: false });
113+
mockPrompter({ firstCallOptionName: PlatformEnvironmentRequirements.CLOUD_SETUP_OPTION_NAME });
114+
mockNativeScriptCloudExtensionService({ isInstalled: true });
115+
116+
await assert.isRejected(platformEnvironmentRequirements.checkEnvironmentRequirements({ platform, hideSyncToPreviewAppOption: true }));
117+
assert.isTrue(promptForChoiceData.length === 1);
118+
assert.isTrue(isExtensionInstallCalled);
119+
assert.deepEqual("To continue, choose one of the following options: ", promptForChoiceData[0].message);
120+
assert.deepEqual(['Try Cloud Operation', 'Configure for Local Builds', 'Skip Step and Configure Manually'], promptForChoiceData[0].choices);
121+
});
111122
it("should skip env check when NS_SKIP_ENV_CHECK environment variable is passed", async() => {
112123
process.env.NS_SKIP_ENV_CHECK = true;
113124

0 commit comments

Comments
 (0)