Skip to content

Commit 4967b2c

Browse files
committed
Stop execution of current command when env is not properly configured and "Sync to Playground" option is selected
1 parent a0db691 commit 4967b2c

24 files changed

+304
-174
lines changed

lib/commands/add-platform.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
1-
export class AddPlatformCommand implements ICommand {
1+
import { CommandBase } from "./command-base";
2+
3+
export class AddPlatformCommand extends CommandBase implements ICommand {
24
public allowedParameters: ICommandParameter[] = [];
35

4-
constructor(private $options: IOptions,
5-
private $platformService: IPlatformService,
6-
private $projectData: IProjectData,
7-
private $platformsData: IPlatformsData,
6+
constructor($options: IOptions,
7+
$platformService: IPlatformService,
8+
$projectData: IProjectData,
9+
$platformsData: IPlatformsData,
810
private $errors: IErrors) {
9-
this.$projectData.initializeProjectData();
11+
super($options, $platformsData, $platformService, $projectData);
12+
this.$projectData.initializeProjectData();
1013
}
1114

1215
public async execute(args: string[]): Promise<void> {
1316
await this.$platformService.addPlatforms(args, this.$options.platformTemplate, this.$projectData, this.$options, this.$options.frameworkPath);
1417
}
1518

16-
public async canExecute(args: string[]): Promise<boolean> {
19+
public async canExecute(args: string[]): Promise<ICanExecuteCommandOutput> {
1720
if (!args || args.length === 0) {
1821
this.$errors.fail("No platform specified. Please specify a platform to add");
1922
}
2023

24+
let canExecute = true;
2125
for (const arg of args) {
2226
this.$platformService.validatePlatform(arg, this.$projectData);
23-
const platformData = this.$platformsData.getPlatformData(arg, this.$projectData);
24-
const platformProjectService = platformData.platformProjectService;
25-
await platformProjectService.validate(this.$projectData);
27+
const output = await super.canExecuteCommandBase(arg);
28+
canExecute = canExecute && output.canExecute;
2629
}
2730

28-
return true;
31+
return {
32+
canExecute,
33+
suppressCommandHelp: !canExecute
34+
};
2935
}
3036
}
3137

lib/commands/build.ts

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { ANDROID_RELEASE_BUILD_ERROR_MESSAGE } from "../constants";
2+
import { CommandBase } from "./command-base";
23

3-
export class BuildCommandBase {
4-
constructor(protected $options: IOptions,
4+
export abstract class BuildCommandBase extends CommandBase {
5+
constructor($options: IOptions,
56
protected $errors: IErrors,
6-
protected $projectData: IProjectData,
7-
protected $platformsData: IPlatformsData,
7+
$projectData: IProjectData,
8+
$platformsData: IPlatformsData,
89
protected $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
9-
protected $platformService: IPlatformService,
10+
$platformService: IPlatformService,
1011
private $bundleValidatorHelper: IBundleValidatorHelper) {
12+
super($options, $platformsData, $platformService, $projectData);
1113
this.$projectData.initializeProjectData();
1214
}
1315

@@ -43,16 +45,29 @@ export class BuildCommandBase {
4345
}
4446
}
4547

46-
protected async validatePlatform(platform: string): Promise<void> {
48+
protected validatePlatform(platform: string): void {
4749
if (!this.$platformService.isPlatformSupportedForOS(platform, this.$projectData)) {
4850
this.$errors.fail(`Applications for platform ${platform} can not be built on this OS`);
4951
}
5052

5153
this.$bundleValidatorHelper.validate();
54+
}
55+
56+
protected async validateArgs(args: string[], platform: string): Promise<ICanExecuteCommandOutput> {
57+
const canExecute = await this.validateArgsCore(args, platform);
58+
return {
59+
canExecute,
60+
suppressCommandHelp: false
61+
};
62+
}
5263

53-
const platformData = this.$platformsData.getPlatformData(platform, this.$projectData);
54-
const platformProjectService = platformData.platformProjectService;
55-
await platformProjectService.validate(this.$projectData);
64+
private async validateArgsCore(args: string[], platform: string): Promise<boolean> {
65+
if (args.length !== 0) {
66+
return false;
67+
}
68+
69+
const result = await this.$platformService.validateOptions(this.$options.provision, this.$options.teamId, this.$projectData, platform);
70+
return result;
5671
}
5772
}
5873

@@ -73,9 +88,17 @@ export class BuildIosCommand extends BuildCommandBase implements ICommand {
7388
return this.executeCore([this.$platformsData.availablePlatforms.iOS]);
7489
}
7590

76-
public async canExecute(args: string[]): Promise<boolean> {
77-
await super.validatePlatform(this.$devicePlatformsConstants.iOS);
78-
return args.length === 0 && this.$platformService.validateOptions(this.$options.provision, this.$options.teamId, this.$projectData, this.$platformsData.availablePlatforms.iOS);
91+
public async canExecute(args: string[]): Promise<boolean | ICanExecuteCommandOutput> {
92+
const platform = this.$devicePlatformsConstants.iOS;
93+
94+
super.validatePlatform(platform);
95+
96+
let result = await super.canExecuteCommandBase(platform);
97+
if (result.canExecute) {
98+
result = await super.validateArgs(args, platform);
99+
}
100+
101+
return result;
79102
}
80103
}
81104

@@ -98,13 +121,20 @@ export class BuildAndroidCommand extends BuildCommandBase implements ICommand {
98121
return this.executeCore([this.$platformsData.availablePlatforms.Android]);
99122
}
100123

101-
public async canExecute(args: string[]): Promise<boolean> {
102-
await super.validatePlatform(this.$devicePlatformsConstants.Android);
103-
if (this.$options.release && (!this.$options.keyStorePath || !this.$options.keyStorePassword || !this.$options.keyStoreAlias || !this.$options.keyStoreAliasPassword)) {
104-
this.$errors.fail(ANDROID_RELEASE_BUILD_ERROR_MESSAGE);
124+
public async canExecute(args: string[]): Promise<boolean | ICanExecuteCommandOutput> {
125+
const platform = this.$devicePlatformsConstants.Android;
126+
super.validatePlatform(platform);
127+
128+
let result = await super.canExecuteCommandBase(platform);
129+
if (result.canExecute) {
130+
if (this.$options.release && (!this.$options.keyStorePath || !this.$options.keyStorePassword || !this.$options.keyStoreAlias || !this.$options.keyStoreAliasPassword)) {
131+
this.$errors.fail(ANDROID_RELEASE_BUILD_ERROR_MESSAGE);
132+
}
133+
134+
result = await super.validateArgs(args, platform);
105135
}
106136

107-
return args.length === 0 && await this.$platformService.validateOptions(this.$options.provision, this.$options.teamId, this.$projectData, this.$platformsData.availablePlatforms.Android);
137+
return result;
108138
}
109139
}
110140

lib/commands/clean-app.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
export class CleanAppCommandBase implements ICommand {
1+
import { CommandBase } from "./command-base";
2+
3+
export class CleanAppCommandBase extends CommandBase implements ICommand {
24
public allowedParameters: ICommandParameter[] = [];
35

46
protected platform: string;
57

6-
constructor(protected $options: IOptions,
7-
protected $projectData: IProjectData,
8-
protected $platformService: IPlatformService,
8+
constructor($options: IOptions,
9+
$projectData: IProjectData,
10+
$platformService: IPlatformService,
911
protected $errors: IErrors,
1012
protected $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
11-
protected $platformsData: IPlatformsData) {
12-
13-
this.$projectData.initializeProjectData();
13+
$platformsData: IPlatformsData) {
14+
super($options, $platformsData, $platformService, $projectData);
15+
this.$projectData.initializeProjectData();
1416
}
1517

1618
public async execute(args: string[]): Promise<void> {
@@ -27,15 +29,13 @@ export class CleanAppCommandBase implements ICommand {
2729
return this.$platformService.cleanDestinationApp(platformInfo);
2830
}
2931

30-
public async canExecute(args: string[]): Promise<boolean> {
32+
public async canExecute(args: string[]): Promise<ICanExecuteCommandOutput> {
3133
if (!this.$platformService.isPlatformSupportedForOS(this.platform, this.$projectData)) {
3234
this.$errors.fail(`Applications for platform ${this.platform} can not be built on this OS`);
3335
}
3436

35-
const platformData = this.$platformsData.getPlatformData(this.platform, this.$projectData);
36-
const platformProjectService = platformData.platformProjectService;
37-
await platformProjectService.validate(this.$projectData);
38-
return true;
37+
const result = await super.canExecuteCommandBase(this.platform);
38+
return result;
3939
}
4040
}
4141

lib/commands/command-base.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export abstract class CommandBase implements ICommandBase {
2+
constructor(protected $options: IOptions,
3+
protected $platformsData: IPlatformsData,
4+
protected $platformService: IPlatformService,
5+
protected $projectData: IProjectData) { }
6+
7+
abstract allowedParameters: ICommandParameter[];
8+
abstract execute(args: string[]): Promise<void>;
9+
10+
public async canExecuteCommandBase(platform: string, options?: ICanExecuteCommandOptions): Promise<ICanExecuteCommandOutput> {
11+
options = options || {};
12+
const validatePlatformOutput = await this.validatePlatformBase(platform, options.notConfiguredEnvOptions);
13+
const canExecute = this.canExecuteCommand(validatePlatformOutput);
14+
let result = { canExecute, suppressCommandHelp: !canExecute };
15+
16+
if (canExecute && options.validateOptions) {
17+
const validateOptionsOutput = await this.$platformService.validateOptions(this.$options.provision, this.$options.teamId, this.$projectData, platform);
18+
result = { canExecute: validateOptionsOutput, suppressCommandHelp: false };
19+
}
20+
21+
return result;
22+
}
23+
24+
private async validatePlatformBase(platform: string, notConfiguredEnvOptions: INotConfiguredEnvOptions): Promise<IValidatePlatformOutput> {
25+
const platformData = this.$platformsData.getPlatformData(platform, this.$projectData);
26+
const platformProjectService = platformData.platformProjectService;
27+
const result = await platformProjectService.validate(this.$projectData, this.$options, notConfiguredEnvOptions);
28+
return result;
29+
}
30+
31+
private canExecuteCommand(validatePlatformOutput: IValidatePlatformOutput): boolean {
32+
return validatePlatformOutput && validatePlatformOutput.checkEnvironmentRequirementsOutput && validatePlatformOutput.checkEnvironmentRequirementsOutput.canExecute;
33+
}
34+
}

lib/commands/debug.ts

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,35 @@ import { CONNECTED_STATUS } from "../common/constants";
22
import { isInteractive } from "../common/helpers";
33
import { cache } from "../common/decorators";
44
import { DebugCommandErrors } from "../constants";
5+
import { CommandBase } from "./command-base";
56

6-
export class DebugPlatformCommand implements ICommand {
7+
export class DebugPlatformCommand extends CommandBase implements ICommand {
78
public allowedParameters: ICommandParameter[] = [];
89

910
constructor(private platform: string,
1011
private $debugService: IDebugService,
1112
protected $devicesService: Mobile.IDevicesService,
12-
protected $platformService: IPlatformService,
13-
protected $projectData: IProjectData,
14-
protected $options: IOptions,
15-
protected $platformsData: IPlatformsData,
13+
$platformService: IPlatformService,
14+
$projectData: IProjectData,
15+
$options: IOptions,
16+
$platformsData: IPlatformsData,
1617
protected $logger: ILogger,
1718
protected $errors: IErrors,
1819
private $debugDataService: IDebugDataService,
1920
private $liveSyncService: IDebugLiveSyncService,
2021
private $prompter: IPrompter,
2122
private $liveSyncCommandHelper: ILiveSyncCommandHelper) {
23+
super($options, $platformsData, $platformService, $projectData);
2224
}
2325

2426
public async execute(args: string[]): Promise<void> {
27+
await this.$devicesService.initialize({
28+
platform: this.platform,
29+
deviceId: this.$options.device,
30+
emulator: this.$options.emulator,
31+
skipDeviceDetectionInterval: true
32+
});
33+
2534
const debugOptions = <IDebugOptions>_.cloneDeep(this.$options.argv);
2635

2736
const debugData = this.$debugDataService.createDebugData(this.$projectData, this.$options);
@@ -99,7 +108,7 @@ export class DebugPlatformCommand implements ICommand {
99108
this.$errors.failWithoutHelp(DebugCommandErrors.NO_DEVICES_EMULATORS_FOUND_FOR_OPTIONS);
100109
}
101110

102-
public async canExecute(args: string[]): Promise<boolean> {
111+
public async canExecute(args: string[]): Promise<ICanExecuteCommandOutput> {
103112
if (!this.$platformService.isPlatformSupportedForOS(this.platform, this.$projectData)) {
104113
this.$errors.fail(`Applications for platform ${this.platform} can not be built on this OS`);
105114
}
@@ -108,18 +117,8 @@ export class DebugPlatformCommand implements ICommand {
108117
this.$errors.fail("--release flag is not applicable to this command");
109118
}
110119

111-
const platformData = this.$platformsData.getPlatformData(this.platform, this.$projectData);
112-
const platformProjectService = platformData.platformProjectService;
113-
await platformProjectService.validate(this.$projectData);
114-
115-
await this.$devicesService.initialize({
116-
platform: this.platform,
117-
deviceId: this.$options.device,
118-
emulator: this.$options.emulator,
119-
skipDeviceDetectionInterval: true
120-
});
121-
122-
return true;
120+
const result = await super.canExecuteCommandBase(this.platform, { validateOptions: true, notConfiguredEnvOptions: { hideCloudBuildOption: true }});
121+
return result;
123122
}
124123
}
125124

@@ -138,7 +137,6 @@ export class DebugIOSCommand implements ICommand {
138137
private $options: IOptions,
139138
private $injector: IInjector,
140139
private $projectData: IProjectData,
141-
private $platformsData: IPlatformsData,
142140
$iosDeviceOperations: IIOSDeviceOperations,
143141
$iOSSimulatorLogProvider: Mobile.IiOSSimulatorLogProvider) {
144142
this.$projectData.initializeProjectData();
@@ -154,7 +152,7 @@ export class DebugIOSCommand implements ICommand {
154152
return this.debugPlatformCommand.execute(args);
155153
}
156154

157-
public async canExecute(args: string[]): Promise<boolean> {
155+
public async canExecute(args: string[]): Promise<ICanExecuteCommandOutput> {
158156
if (!this.$platformService.isPlatformSupportedForOS(this.$devicePlatformsConstants.iOS, this.$projectData)) {
159157
this.$errors.fail(`Applications for platform ${this.$devicePlatformsConstants.iOS} can not be built on this OS`);
160158
}
@@ -164,7 +162,8 @@ export class DebugIOSCommand implements ICommand {
164162
this.$errors.fail(`Timeout option specifies the seconds NativeScript CLI will wait to find the inspector socket port from device's logs. Must be a number.`);
165163
}
166164

167-
return await this.debugPlatformCommand.canExecute(args) && await this.$platformService.validateOptions(this.$options.provision, this.$options.teamId, this.$projectData, this.$platformsData.availablePlatforms.iOS);
165+
const result = await this.debugPlatformCommand.canExecute(args);
166+
return result;
168167
}
169168

170169
private isValidTimeoutOption() {
@@ -200,19 +199,17 @@ export class DebugAndroidCommand implements ICommand {
200199

201200
constructor(protected $errors: IErrors,
202201
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
203-
private $platformService: IPlatformService,
204-
private $options: IOptions,
205202
private $injector: IInjector,
206-
private $projectData: IProjectData,
207-
private $platformsData: IPlatformsData) {
208-
this.$projectData.initializeProjectData();
203+
private $projectData: IProjectData) {
204+
this.$projectData.initializeProjectData();
209205
}
210206

211207
public execute(args: string[]): Promise<void> {
212208
return this.debugPlatformCommand.execute(args);
213209
}
214-
public async canExecute(args: string[]): Promise<boolean> {
215-
return await this.debugPlatformCommand.canExecute(args) && await this.$platformService.validateOptions(this.$options.provision, this.$options.teamId, this.$projectData, this.$platformsData.availablePlatforms.Android);
210+
public async canExecute(args: string[]): Promise<ICanExecuteCommandOutput> {
211+
const result = await this.debugPlatformCommand.canExecute(args);
212+
return result;
216213
}
217214

218215
public platform = this.$devicePlatformsConstants.Android;

0 commit comments

Comments
 (0)