Skip to content

Commit e345c5c

Browse files
committed
feat: add key commands
1 parent ba3293c commit e345c5c

27 files changed

+1687
-831
lines changed

lib/bootstrap.ts

+77-68
Large diffs are not rendered by default.

lib/color.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// using chalk as some of our other dependencies are already using it...
22
// exporting from here so we can easily refactor to a different color library if needed
3+
import * as ansi from "ansi-colors";
34
import * as chalk from "chalk";
45

56
export type Color = typeof chalk.Color;
67

8+
export function stripColors(formatStr: string) {
9+
return ansi.stripColor(formatStr);
10+
}
11+
712
export const color = chalk;

lib/commands/run.ts

+32-23
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import { ERROR_NO_VALID_SUBCOMMAND_FORMAT } from "../common/constants";
2-
import {
3-
ANDROID_RELEASE_BUILD_ERROR_MESSAGE,
4-
ANDROID_APP_BUNDLE_SIGNING_ERROR_MESSAGE,
5-
} from "../constants";
2+
import { IErrors, IHostInfo } from "../common/declarations";
63
import { cache } from "../common/decorators";
7-
import { hasValidAndroidSigning } from "../common/helpers";
8-
import { IProjectData, IProjectDataService } from "../definitions/project";
9-
import { IMigrateController } from "../definitions/migrate";
10-
import { IOptions, IPlatformValidationService } from "../declarations";
114
import { ICommand, ICommandParameter } from "../common/definitions/commands";
12-
import { IErrors, IHostInfo } from "../common/declarations";
5+
import {
6+
IKeyCommandHelper,
7+
IKeyCommandPlatform,
8+
} from "../common/definitions/key-commands";
139
import { IInjector } from "../common/definitions/yok";
10+
import { hasValidAndroidSigning } from "../common/helpers";
1411
import { injector } from "../common/yok";
12+
import {
13+
ANDROID_APP_BUNDLE_SIGNING_ERROR_MESSAGE,
14+
ANDROID_RELEASE_BUILD_ERROR_MESSAGE,
15+
} from "../constants";
16+
import { IOptions, IPlatformValidationService } from "../declarations";
17+
import { IMigrateController } from "../definitions/migrate";
18+
import { IProjectData, IProjectDataService } from "../definitions/project";
1519

1620
export class RunCommandBase implements ICommand {
17-
private liveSyncCommandHelperAdditionalOptions: ILiveSyncCommandHelperAdditionalOptions = <
18-
ILiveSyncCommandHelperAdditionalOptions
19-
>{};
21+
private liveSyncCommandHelperAdditionalOptions: ILiveSyncCommandHelperAdditionalOptions =
22+
<ILiveSyncCommandHelperAdditionalOptions>{};
2023

2124
public platform: string;
2225
constructor(
@@ -26,14 +29,20 @@ export class RunCommandBase implements ICommand {
2629
private $liveSyncCommandHelper: ILiveSyncCommandHelper,
2730
private $migrateController: IMigrateController,
2831
private $options: IOptions,
29-
private $projectData: IProjectData
32+
private $projectData: IProjectData,
33+
private $keyCommandHelper: IKeyCommandHelper,
3034
) {}
3135

3236
public allowedParameters: ICommandParameter[] = [];
3337
public async execute(args: string[]): Promise<void> {
34-
return this.$liveSyncCommandHelper.executeCommandLiveSync(
38+
await this.$liveSyncCommandHelper.executeCommandLiveSync(
3539
this.platform,
36-
this.liveSyncCommandHelperAdditionalOptions
40+
this.liveSyncCommandHelperAdditionalOptions,
41+
);
42+
43+
this.$keyCommandHelper.attachKeyCommands(
44+
this.platform as IKeyCommandPlatform,
45+
"run",
3746
);
3847
}
3948

@@ -89,7 +98,7 @@ export class RunIosCommand implements ICommand {
8998
private $injector: IInjector,
9099
private $options: IOptions,
91100
private $platformValidationService: IPlatformValidationService,
92-
private $projectDataService: IProjectDataService
101+
private $projectDataService: IProjectDataService,
93102
) {}
94103

95104
public async execute(args: string[]): Promise<void> {
@@ -102,11 +111,11 @@ export class RunIosCommand implements ICommand {
102111
if (
103112
!this.$platformValidationService.isPlatformSupportedForOS(
104113
this.$devicePlatformsConstants.iOS,
105-
projectData
114+
projectData,
106115
)
107116
) {
108117
this.$errors.fail(
109-
`Applications for platform ${this.$devicePlatformsConstants.iOS} can not be built on this OS`
118+
`Applications for platform ${this.$devicePlatformsConstants.iOS} can not be built on this OS`,
110119
);
111120
}
112121

@@ -116,7 +125,7 @@ export class RunIosCommand implements ICommand {
116125
this.$options.provision,
117126
this.$options.teamId,
118127
projectData,
119-
this.$devicePlatformsConstants.iOS.toLowerCase()
128+
this.$devicePlatformsConstants.iOS.toLowerCase(),
120129
));
121130
return result;
122131
}
@@ -143,7 +152,7 @@ export class RunAndroidCommand implements ICommand {
143152
private $injector: IInjector,
144153
private $options: IOptions,
145154
private $platformValidationService: IPlatformValidationService,
146-
private $projectData: IProjectData
155+
private $projectData: IProjectData,
147156
) {}
148157

149158
public async execute(args: string[]): Promise<void> {
@@ -156,11 +165,11 @@ export class RunAndroidCommand implements ICommand {
156165
if (
157166
!this.$platformValidationService.isPlatformSupportedForOS(
158167
this.$devicePlatformsConstants.Android,
159-
this.$projectData
168+
this.$projectData,
160169
)
161170
) {
162171
this.$errors.fail(
163-
`Applications for platform ${this.$devicePlatformsConstants.Android} can not be built on this OS`
172+
`Applications for platform ${this.$devicePlatformsConstants.Android} can not be built on this OS`,
164173
);
165174
}
166175

@@ -179,7 +188,7 @@ export class RunAndroidCommand implements ICommand {
179188
this.$options.provision,
180189
this.$options.teamId,
181190
this.$projectData,
182-
this.$devicePlatformsConstants.Android.toLowerCase()
191+
this.$devicePlatformsConstants.Android.toLowerCase(),
183192
);
184193
}
185194
}

lib/commands/start.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { ICommand, ICommandParameter } from "../common/definitions/commands";
2+
import { injector } from "../common/yok";
3+
import { IStartService } from "../definitions/start-service";
4+
5+
export class StartCommand implements ICommand {
6+
constructor(private $startService: IStartService) {}
7+
async execute(args: string[]): Promise<void> {
8+
this.$startService.start();
9+
return;
10+
}
11+
allowedParameters: ICommandParameter[];
12+
async canExecute?(args: string[]): Promise<boolean> {
13+
return true;
14+
}
15+
}
16+
17+
injector.registerCommand("start", StartCommand);
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
export type IKeyCommandPlatform = "Android" | "iOS" | "all";
2+
export type IKeysLowerCase =
3+
| "a"
4+
| "b"
5+
| "c"
6+
| "d"
7+
| "e"
8+
| "f"
9+
| "g"
10+
| "h"
11+
| "i"
12+
| "j"
13+
| "k"
14+
| "l"
15+
| "m"
16+
| "n"
17+
| "o"
18+
| "p"
19+
| "q"
20+
| "r"
21+
| "s"
22+
| "t"
23+
| "u"
24+
| "v"
25+
| "w"
26+
| "x"
27+
| "y"
28+
| "z";
29+
30+
export type IValidKeyCommands = IKeysLowerCase | `${Uppercase<IKeysLowerCase>}`;
31+
32+
export interface IKeyCommandHelper {
33+
attachKeyCommands: (
34+
platform: IKeyCommandPlatform,
35+
processType: SupportedProcessType,
36+
) => void;
37+
38+
addOverride(key: IValidKeyCommands, execute: () => Promise<boolean>);
39+
removeOverride(key: IValidKeyCommands);
40+
printCommands(platform: IKeyCommandPlatform): void;
41+
}
42+
43+
export type SupportedProcessType = "start" | "run";
44+
45+
export interface IKeyCommand {
46+
key: IValidKeyCommands;
47+
platform: IKeyCommandPlatform;
48+
description: string;
49+
willBlockKeyCommandExecution?: boolean;
50+
execute(platform: string): Promise<void>;
51+
canExecute?: (processType: SupportedProcessType) => boolean;
52+
}

lib/common/definitions/yok.d.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { IDisposable, IDictionary } from "../declarations";
22
import { ICommand } from "./commands";
3+
import { IKeyCommand, IValidKeyCommands } from "./key-commands";
34

45
interface IInjector extends IDisposable {
56
require(name: string, file: string): void;
@@ -8,6 +9,7 @@ interface IInjector extends IDisposable {
89
requirePublicClass(names: string | string[], file: string): void;
910
requireCommand(name: string, file: string): void;
1011
requireCommand(names: string[], file: string): void;
12+
requireKeyCommand(name: string, file: string): void;
1113
/**
1214
* Resolves an implementation by constructor function.
1315
* The injector will create new instances for every call.
@@ -22,21 +24,24 @@ interface IInjector extends IDisposable {
2224
resolve<T>(name: string, ctorArguments?: IDictionary<any>): T;
2325

2426
resolveCommand(name: string): ICommand;
27+
resolveKeyCommand(key: string): IKeyCommand;
2528
register(name: string, resolver: any, shared?: boolean): void;
2629
registerCommand(name: string, resolver: any): void;
2730
registerCommand(names: string[], resolver: any): void;
31+
registerKeyCommand(key: IValidKeyCommands, resolver: any): void;
2832
getRegisteredCommandsNames(includeDev: boolean): string[];
33+
getRegisteredKeyCommandsNames(): string[];
2934
dynamicCallRegex: RegExp;
3035
dynamicCall(call: string, args?: any[]): Promise<any>;
3136
isDefaultCommand(commandName: string): boolean;
3237
isValidHierarchicalCommand(
3338
commandName: string,
34-
commandArguments: string[]
39+
commandArguments: string[],
3540
): Promise<boolean>;
3641
getChildrenCommandsNames(commandName: string): string[];
3742
buildHierarchicalCommand(
3843
parentCommandName: string,
39-
commandLineArguments: string[]
44+
commandLineArguments: string[],
4045
): any;
4146
publicApi: any;
4247

lib/common/header.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { color, stripColors } from "../color";
2+
3+
export function printHeader() {
4+
if (process.env.HIDE_HEADER) return;
5+
const version = "8.5.3";
6+
const middle = [
7+
color.dim("│ "),
8+
color.cyanBright.bold(" {N} NativeScript "),
9+
color.whiteBright.bold("CLI"),
10+
color.dim(` [v${version}] `),
11+
color.dim(" │"),
12+
].join("");
13+
const middle2 = [
14+
color.dim("│ "),
15+
color.whiteBright.bold(" Empowering JS with Native APIs "),
16+
color.dim(" │"),
17+
].join("");
18+
19+
const end = [color.dim("─┘")].join("");
20+
21+
const width = stripColors(middle).length;
22+
const endWidth = stripColors(end).length;
23+
console.info(" ");
24+
console.info(" " + color.dim("┌" + "─".repeat(width - 2) + "┐"));
25+
console.info(" " + middle);
26+
console.info(" " + middle2);
27+
console.info(" " + color.dim("└" + "─".repeat(width - endWidth - 1)) + end);
28+
}

lib/common/mobile/mobile-core/android-device-discovery.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,26 @@ interface IAdbAndroidDeviceInfo {
1111

1212
export class AndroidDeviceDiscovery
1313
extends DeviceDiscovery
14-
implements Mobile.IAndroidDeviceDiscovery {
14+
implements Mobile.IAndroidDeviceDiscovery
15+
{
1516
private _devices: IAdbAndroidDeviceInfo[] = [];
1617
private isStarted: boolean;
1718

1819
constructor(
1920
private $injector: IInjector,
2021
private $adb: Mobile.IAndroidDebugBridge,
21-
private $mobileHelper: Mobile.IMobileHelper
22+
private $mobileHelper: Mobile.IMobileHelper,
2223
) {
2324
super();
2425
}
2526

2627
private async createAndAddDevice(
27-
adbDeviceInfo: IAdbAndroidDeviceInfo
28+
adbDeviceInfo: IAdbAndroidDeviceInfo,
2829
): Promise<void> {
2930
this._devices.push(adbDeviceInfo);
3031
const device: Mobile.IAndroidDevice = this.$injector.resolve(
3132
AndroidDevice,
32-
{ identifier: adbDeviceInfo.identifier, status: adbDeviceInfo.status }
33+
{ identifier: adbDeviceInfo.identifier, status: adbDeviceInfo.status },
3334
);
3435
await device.init();
3536
this.addDevice(device);
@@ -41,7 +42,7 @@ export class AndroidDeviceDiscovery
4142
}
4243

4344
public async startLookingForDevices(
44-
options?: Mobile.IDeviceLookingOptions
45+
options?: Mobile.IDeviceLookingOptions,
4546
): Promise<void> {
4647
if (
4748
options &&
@@ -56,7 +57,6 @@ export class AndroidDeviceDiscovery
5657

5758
private async checkForDevices(): Promise<void> {
5859
const devices = await this.$adb.getDevices();
59-
6060
await this.checkCurrentData(devices);
6161
}
6262

@@ -72,7 +72,7 @@ export class AndroidDeviceDiscovery
7272
identifier: identifier,
7373
status: status,
7474
};
75-
}
75+
},
7676
);
7777

7878
_(this._devices)
@@ -82,12 +82,13 @@ export class AndroidDeviceDiscovery
8282
_.find(
8383
currentDevices,
8484
(device) =>
85-
device.identifier === d.identifier && device.status === d.status
85+
device.identifier === d.identifier &&
86+
device.status === d.status,
8687
)
87-
)
88+
),
8889
)
8990
.each((d: IAdbAndroidDeviceInfo) =>
90-
this.deleteAndRemoveDevice(d.identifier)
91+
this.deleteAndRemoveDevice(d.identifier),
9192
);
9293

9394
await Promise.all(
@@ -99,12 +100,12 @@ export class AndroidDeviceDiscovery
99100
this._devices,
100101
(device) =>
101102
device.identifier === d.identifier &&
102-
device.status === d.status
103+
device.status === d.status,
103104
)
104-
)
105+
),
105106
)
106107
.map((d: IAdbAndroidDeviceInfo) => this.createAndAddDevice(d))
107-
.value()
108+
.value(),
108109
);
109110
}
110111

lib/common/mobile/mobile-core/device-discovery.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { injector } from "../../yok";
66

77
export class DeviceDiscovery
88
extends EventEmitter
9-
implements Mobile.IDeviceDiscovery {
9+
implements Mobile.IDeviceDiscovery
10+
{
1011
private devices: IDictionary<Mobile.IDevice> = {};
1112

1213
public async startLookingForDevices(): Promise<void> {

0 commit comments

Comments
 (0)