Skip to content

Commit 1b19adf

Browse files
Add debug method to the public API (#2677)
* Add debug method to the public API * Fix PR comments
1 parent dd37e55 commit 1b19adf

19 files changed

+395
-220
lines changed

lib/bootstrap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ $injector.require("tnsModulesService", "./services/tns-modules-service");
2121
$injector.require("platformsData", "./platforms-data");
2222
$injector.require("platformService", "./services/platform-service");
2323

24+
$injector.require("debugDataService", "./services/debug-data-service");
2425
$injector.require("iOSDebugService", "./services/ios-debug-service");
2526
$injector.require("androidDebugService", "./services/android-debug-service");
2627

lib/commands/debug.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
export class DebugPlatformCommand implements ICommand {
1+
import { EOL } from "os";
2+
3+
export abstract class DebugPlatformCommand implements ICommand {
24
public allowedParameters: ICommandParameter[] = [];
35

4-
constructor(private debugService: IDebugService,
6+
constructor(private debugService: IPlatformDebugService,
57
private $devicesService: Mobile.IDevicesService,
68
private $injector: IInjector,
79
private $logger: ILogger,
810
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
911
private $config: IConfiguration,
1012
private $usbLiveSyncService: ILiveSyncService,
13+
private $debugDataService: IDebugDataService,
1114
protected $platformService: IPlatformService,
1215
protected $projectData: IProjectData,
1316
protected $options: IOptions,
1417
protected $platformsData: IPlatformsData) {
15-
this.$projectData.initializeProjectData();
16-
}
18+
this.$projectData.initializeProjectData();
19+
}
1720

1821
public async execute(args: string[]): Promise<void> {
22+
const debugOptions = this.$options;
1923
const deployOptions: IDeployPlatformOptions = {
2024
clean: this.$options.clean,
2125
device: this.$options.device,
@@ -29,8 +33,12 @@
2933

3034
const buildConfig: IBuildConfig = _.merge({ buildForDevice: this.$options.forDevice }, deployOptions);
3135

36+
const debugData = this.$debugDataService.createDebugData(this.debugService, this.$options, buildConfig);
37+
38+
await this.$platformService.trackProjectType(this.$projectData);
39+
3240
if (this.$options.start) {
33-
return this.debugService.debug(this.$projectData, buildConfig);
41+
return this.printDebugInformation(await this.debugService.debug(debugData, debugOptions));
3442
}
3543

3644
const appFilesUpdaterOptions: IAppFilesUpdaterOptions = { bundle: this.$options.bundle, release: this.$options.release };
@@ -49,8 +57,9 @@
4957

5058
await deviceAppData.device.applicationManager.stopApplication(applicationId);
5159

52-
await this.debugService.debug(this.$projectData, buildConfig);
60+
this.printDebugInformation(await this.debugService.debug(debugData, debugOptions));
5361
};
62+
5463
return this.$usbLiveSyncService.liveSync(this.$devicesService.platform, this.$projectData, applicationReloadAction);
5564
}
5665

@@ -70,22 +79,29 @@
7079

7180
return true;
7281
}
82+
83+
private printDebugInformation(information: string[]): void {
84+
_.each(information, i => {
85+
this.$logger.info(`To start debugging, open the following URL in Chrome:${EOL}${i}${EOL}`.cyan);
86+
});
87+
}
7388
}
7489

7590
export class DebugIOSCommand extends DebugPlatformCommand {
76-
constructor($iOSDebugService: IDebugService,
91+
constructor($iOSDebugService: IPlatformDebugService,
7792
$devicesService: Mobile.IDevicesService,
7893
$injector: IInjector,
7994
$logger: ILogger,
8095
$devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
8196
$config: IConfiguration,
8297
$usbLiveSyncService: ILiveSyncService,
98+
$debugDataService: IDebugDataService,
8399
$platformService: IPlatformService,
84100
$options: IOptions,
85101
$projectData: IProjectData,
86102
$platformsData: IPlatformsData,
87103
$iosDeviceOperations: IIOSDeviceOperations) {
88-
super($iOSDebugService, $devicesService, $injector, $logger, $devicePlatformsConstants, $config, $usbLiveSyncService, $platformService, $projectData, $options, $platformsData);
104+
super($iOSDebugService, $devicesService, $injector, $logger, $devicePlatformsConstants, $config, $usbLiveSyncService, $debugDataService, $platformService, $projectData, $options, $platformsData);
89105
$iosDeviceOperations.setShouldDispose(this.$options.justlaunch);
90106
}
91107

@@ -97,19 +113,19 @@ export class DebugIOSCommand extends DebugPlatformCommand {
97113
$injector.registerCommand("debug|ios", DebugIOSCommand);
98114

99115
export class DebugAndroidCommand extends DebugPlatformCommand {
100-
constructor($androidDebugService: IDebugService,
116+
constructor($androidDebugService: IPlatformDebugService,
101117
$devicesService: Mobile.IDevicesService,
102118
$injector: IInjector,
103119
$logger: ILogger,
104120
$devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
105121
$config: IConfiguration,
106122
$usbLiveSyncService: ILiveSyncService,
123+
$debugDataService: IDebugDataService,
107124
$platformService: IPlatformService,
108125
$options: IOptions,
109126
$projectData: IProjectData,
110127
$platformsData: IPlatformsData) {
111-
112-
super($androidDebugService, $devicesService, $injector, $logger, $devicePlatformsConstants, $config, $usbLiveSyncService, $platformService, $projectData, $options, $platformsData);
128+
super($androidDebugService, $devicesService, $injector, $logger, $devicePlatformsConstants, $config, $usbLiveSyncService, $debugDataService, $platformService, $projectData, $options, $platformsData);
113129
}
114130

115131
public async canExecute(args: string[]): Promise<boolean> {

lib/commands/run.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export class RunCommandBase {
3838
justlaunch: this.$options.justlaunch,
3939
};
4040

41-
return this.$platformService.startApplication(args[0], deployOpts, this.$projectData);
41+
await this.$platformService.startApplication(args[0], deployOpts, this.$projectData.projectId);
42+
return this.$platformService.trackProjectType(this.$projectData);
4243
}
4344

4445
return this.$usbLiveSyncService.liveSync(args[0], this.$projectData);

lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ export const ItunesConnectApplicationTypes = new ItunesConnectApplicationTypesCl
6969
export const ANGULAR_NAME = "angular";
7070
export const TYPESCRIPT_NAME = "typescript";
7171
export const BUILD_OUTPUT_EVENT_NAME = "buildOutput";
72+
export const CONNECTION_ERROR_EVENT_NAME = "connectionError";
7273
export const VERSION_STRING = "version";

lib/declarations.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ interface IAndroidToolsInfoData {
287287
generateTypings: boolean;
288288
}
289289

290-
interface ISocketProxyFactory {
291-
createTCPSocketProxy(factory: () => any): any;
292-
createWebSocketProxy(factory: () => Promise<any>): any;
290+
interface ISocketProxyFactory extends NodeJS.EventEmitter {
291+
createTCPSocketProxy(factory: () => Promise<any>): any;
292+
createWebSocketProxy(factory: () => Promise<any>): Promise<any>;
293293
}
294294

295295
interface IiOSNotification {

lib/definitions/debug.d.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
1-
interface IDebugService {
2-
debug(projectData: IProjectData, buildConfig: IBuildConfig): Promise<void>;
3-
debugStart(projectData: IProjectData, buildConfig: IBuildConfig): Promise<void>;
1+
interface IDebugData {
2+
deviceIdentifier: string;
3+
applicationIdentifier: string;
4+
pathToAppPackage: string;
5+
projectName?: string;
6+
projectDir?: string;
7+
}
8+
9+
interface IDebugOptions {
10+
chrome?: boolean;
11+
start?: boolean;
12+
stop?: boolean;
13+
emulator?: boolean;
14+
debugBrk?: boolean;
15+
client?: boolean;
16+
justlaunch?: boolean;
17+
}
18+
19+
interface IDebugDataService {
20+
createDebugData(debugService: IPlatformDebugService, options: IOptions, buildConfig: IBuildConfig): IDebugData;
21+
}
22+
23+
interface IDebugService extends NodeJS.EventEmitter {
24+
debug(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string[]>;
25+
}
26+
27+
interface IPlatformDebugService extends IDebugService {
28+
debugStart(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void>;
429
debugStop(): Promise<void>
530
platform: string;
6-
}
31+
}

lib/definitions/platform.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ interface IPlatformService extends NodeJS.EventEmitter {
118118
* @param {IProjectData} projectData DTO with information about the project.
119119
* @returns {void}
120120
*/
121-
startApplication(platform: string, runOptions: IRunPlatformOptions, projectData: IProjectData): Promise<void>;
121+
startApplication(platform: string, runOptions: IRunPlatformOptions, projectId: string): Promise<void>;
122122

123123
/**
124124
* The emulate command. In addition to `run --emulator` command, it handles the `--available-devices` option to show the available devices.

lib/device-sockets/ios/socket-proxy-factory.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
import { EventEmitter } from "events";
2+
import { CONNECTION_ERROR_EVENT_NAME } from "../../constants";
13
import { PacketStream } from "./packet-stream";
24
import * as net from "net";
35
import * as ws from "ws";
46
import temp = require("temp");
57

6-
export class SocketProxyFactory implements ISocketProxyFactory {
8+
export class SocketProxyFactory extends EventEmitter implements ISocketProxyFactory {
79
constructor(private $logger: ILogger,
10+
private $errors: IErrors,
811
private $config: IConfiguration,
9-
private $options: IOptions) { }
12+
private $options: IOptions,
13+
private $net: INet) {
14+
super();
15+
}
1016

11-
public createTCPSocketProxy(factory: () => Promise<net.Socket>): any {
17+
public createTCPSocketProxy(factory: () => Promise<net.Socket>): net.Server {
1218
this.$logger.info("\nSetting up proxy...\nPress Ctrl + C to terminate, or disconnect.\n");
1319

1420
let server = net.createServer({
@@ -25,7 +31,7 @@ export class SocketProxyFactory implements ISocketProxyFactory {
2531
}
2632
});
2733

28-
const backendSocket: net.Socket = await factory();
34+
const backendSocket = await factory();
2935
this.$logger.info("Backend socket created.");
3036

3137
backendSocket.on("end", () => {
@@ -62,9 +68,9 @@ export class SocketProxyFactory implements ISocketProxyFactory {
6268
return server;
6369
}
6470

65-
public createWebSocketProxy(factory: () => Promise<net.Socket>): ws.Server {
71+
public async createWebSocketProxy(factory: () => Promise<net.Socket>): Promise<ws.Server> {
6672
// NOTE: We will try to provide command line options to select ports, at least on the localhost.
67-
let localPort = 8080;
73+
const localPort = await this.$net.getAvailablePortInRange(8080);
6874

6975
this.$logger.info("\nSetting up debugger proxy...\nPress Ctrl + C to terminate, or disconnect.\n");
7076

@@ -77,7 +83,15 @@ export class SocketProxyFactory implements ISocketProxyFactory {
7783
port: localPort,
7884
verifyClient: async (info: any, callback: Function) => {
7985
this.$logger.info("Frontend client connected.");
80-
const _socket = await factory();
86+
let _socket;
87+
try {
88+
_socket = await factory();
89+
} catch (err) {
90+
this.$logger.trace(err);
91+
this.emit(CONNECTION_ERROR_EVENT_NAME, err);
92+
this.$errors.failWithoutHelp("Cannot connect to device socket.");
93+
}
94+
8195
this.$logger.info("Backend socket created.");
8296
info.req["__deviceSocket"] = _socket;
8397
callback(true);

lib/nativescript-cli-lib-bootstrap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ $injector.requirePublic("companionAppsService", "./common/appbuilder/services/li
99
$injector.requirePublicClass("deviceEmitter", "./common/appbuilder/device-emitter");
1010
$injector.requirePublicClass("deviceLogProvider", "./common/appbuilder/device-log-provider");
1111
$injector.requirePublicClass("localBuildService", "./services/local-build-service");
12+
$injector.requirePublicClass("debugService", "./services/debug-service");
1213
$injector.require("iOSLogFilter", "./common/mobile/ios/ios-log-filter");
1314

1415
// We need this because some services check if (!$options.justlaunch) to start the device log after some operation.

0 commit comments

Comments
 (0)