Skip to content

Commit 74f1be7

Browse files
Merge pull request #142 from NativeScript/vladimirov/commands-and-parameters
Add validation for command parameters
2 parents 56fef1d + 4fdafde commit 74f1be7

20 files changed

+435
-64
lines changed

lib/bootstrap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ $injector.require("analyticsSettingsService", "./services/analytics-settings-ser
1818

1919
$injector.require("emulatorSettingsService", "./services/emulator-settings-service");
2020

21+
$injector.require("platformCommandParameter", "./platform-command-param");
2122
$injector.requireCommand("create", "./commands/create-project");
2223
$injector.requireCommand("platform|*list", "./commands/list-platforms");
2324
$injector.requireCommand("platform|add", "./commands/add-platform");

lib/commands/add-platform.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
///<reference path="../.d.ts"/>
2+
"use strict";
23

34
export class AddPlatformCommand implements ICommand {
4-
constructor(private $platformService: IPlatformService) { }
5+
constructor(private $platformService: IPlatformService,
6+
private $errors: IErrors) { }
57

68
execute(args: string[]): IFuture<void> {
79
return (() => {
810
this.$platformService.addPlatforms(args).wait();
911
}).future<void>()();
1012
}
13+
14+
allowedParameters: ICommandParameter[] = [];
15+
16+
canExecute(args: string[]): IFuture<boolean> {
17+
return (() => {
18+
if(!args || args.length === 0) {
19+
this.$errors.fail("No platform specified. Please specify a platform to add");
20+
}
21+
22+
_.each(args, arg => this.$platformService.validatePlatform(arg));
23+
24+
return true;
25+
}).future<boolean>()();
26+
}
1127
}
1228
$injector.registerCommand("platform|add", AddPlatformCommand);

lib/commands/build.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
///<reference path="../.d.ts"/>
2+
"use strict";
23

34
export class BuildCommand implements ICommand {
4-
constructor(private $platformService: IPlatformService) { }
5+
constructor(private $platformService: IPlatformService,
6+
private $platformCommandParameter: ICommandParameter) { }
57

68
execute(args: string[]): IFuture<void> {
79
return (() => {
810
this.$platformService.buildPlatform(args[0]).wait();
911
}).future<void>()();
1012
}
13+
14+
allowedParameters = [this.$platformCommandParameter];
1115
}
1216
$injector.registerCommand("build", BuildCommand);

lib/commands/create-project.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
///<reference path="../.d.ts"/>
2+
"use strict";
3+
4+
export class ProjectCommandParameter implements ICommandParameter {
5+
constructor(private $errors: IErrors,
6+
private $projectNameValidator: IProjectNameValidator) { }
7+
8+
mandatory = true;
9+
validate(value: string): IFuture<boolean> {
10+
return (() => {
11+
if(!value) {
12+
this.$errors.fail("You must specify <App name> when creating a new project.");
13+
}
14+
15+
return this.$projectNameValidator.validate(value);
16+
}).future<boolean>()();
17+
}
18+
}
219

320
export class CreateProjectCommand implements ICommand {
4-
constructor(private $projectService: IProjectService) { }
21+
constructor(private $projectService: IProjectService,
22+
private $errors: IErrors,
23+
private $projectNameValidator: IProjectNameValidator) { }
524

625
public enableHooks = false;
726

@@ -10,5 +29,7 @@ export class CreateProjectCommand implements ICommand {
1029
this.$projectService.createProject(args[0]).wait();
1130
}).future<void>()();
1231
}
32+
33+
allowedParameters = [new ProjectCommandParameter(this.$errors, this.$projectNameValidator) ]
1334
}
1435
$injector.registerCommand("create", CreateProjectCommand);

lib/commands/deploy.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
///<reference path="../.d.ts"/>
2+
"use strict";
23

34
export class DeployOnDeviceCommand implements ICommand {
4-
constructor(private $platformService: IPlatformService) { }
5+
constructor(private $platformService: IPlatformService,
6+
private $platformCommandParameter: ICommandParameter) { }
57

68
execute(args: string[]): IFuture<void> {
79
return this.$platformService.deployOnDevice(args[0]);
810
}
11+
12+
allowedParameters = [this.$platformCommandParameter];
913
}
10-
$injector.registerCommand("deploy", DeployOnDeviceCommand);
14+
$injector.registerCommand("deploy", DeployOnDeviceCommand);

lib/commands/emulate.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
///<reference path="../.d.ts"/>
22

33
export class EmulateCommand implements ICommand {
4-
constructor(private $platformService: IPlatformService) { }
4+
constructor(private $platformService: IPlatformService,
5+
private $platformCommandParameter: ICommandParameter) { }
56

6-
execute(args: string[]): IFuture<void> { return this.$platformService.deployOnEmulator(args[0]);}
7+
execute(args: string[]): IFuture<void> { return this.$platformService.deployOnEmulator(args[0]); }
8+
9+
allowedParameters = [this.$platformCommandParameter];
710
}
8-
$injector.registerCommand("emulate", EmulateCommand);
11+
$injector.registerCommand("emulate", EmulateCommand);

lib/commands/list-devices.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import util = require("util")
44

55
export class ListDevicesCommand implements ICommand {
66
constructor(private $devicesServices: Mobile.IDevicesServices,
7-
private $logger: ILogger) { }
7+
private $logger: ILogger,
8+
private $stringParameter: ICommandParameter) { }
89

910
execute(args: string[]): IFuture<void> {
1011
return (() => {
@@ -17,5 +18,7 @@ export class ListDevicesCommand implements ICommand {
1718
this.$devicesServices.execute(action, undefined, {allowNoDevices: true}).wait();
1819
}).future<void>()();
1920
}
21+
22+
allowedParameters = [this.$stringParameter];
2023
}
2124
$injector.registerCommand("list-devices", ListDevicesCommand);

lib/commands/list-platforms.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ export class ListPlatformsCommand implements ICommand {
2424
}
2525
}).future<void>()();
2626
}
27+
28+
allowedParameters: ICommandParameter[] = [];
2729
}
2830
$injector.registerCommand("platform|*list", ListPlatformsCommand);

lib/commands/post-install.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ export class PostInstallCommand implements ICommand {
2121
this.$autoCompletionService.enableAutoCompletion().wait();
2222
}).future<void>()();
2323
}
24+
25+
public allowedParameters: ICommandParameter[] = [];
2426
}
2527
$injector.registerCommand("dev-post-install", PostInstallCommand);

lib/commands/prepare.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
///<reference path="../.d.ts"/>
22

33
export class PrepareCommand implements ICommand {
4-
constructor(private $platformService: IPlatformService) { }
4+
constructor(private $platformService: IPlatformService,
5+
private $platformCommandParameter: ICommandParameter) { }
56

67
execute(args: string[]): IFuture<void> {
78
return (() => {
89
this.$platformService.preparePlatform(args[0]).wait();
910
}).future<void>()();
1011
}
12+
13+
allowedParameters = [this.$platformCommandParameter];
1114
}
1215
$injector.registerCommand("prepare", PrepareCommand);

0 commit comments

Comments
 (0)