@@ -6,18 +6,13 @@ import { ChildProcess } from "child_process";
66import { DebugServiceBase } from "./debug-service-base" ;
77import { CONNECTION_ERROR_EVENT_NAME , AWAIT_NOTIFICATION_TIMEOUT_SECONDS } from "../constants" ;
88import { getPidFromiOSSimulatorLogs } from "../common/helpers" ;
9-
10- import byline = require( "byline" ) ;
11-
12- const inspectorBackendPort = 18181 ;
139const inspectorAppName = "NativeScript Inspector.app" ;
1410const inspectorNpmPackageName = "tns-ios-inspector" ;
1511const inspectorUiDir = "WebInspectorUI/" ;
1612
1713export class IOSDebugService extends DebugServiceBase implements IPlatformDebugService {
1814 private _lldbProcess : ChildProcess ;
1915 private _sockets : net . Socket [ ] = [ ] ;
20- private _childProcess : ChildProcess ;
2116 private _socketProxy : any ;
2217
2318 constructor ( protected device : Mobile . IDevice ,
@@ -29,11 +24,11 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
2924 private $logger : ILogger ,
3025 private $errors : IErrors ,
3126 private $npmInstallationManager : INpmInstallationManager ,
27+ private $iOSDebuggerPortService : IIOSDebuggerPortService ,
3228 private $iOSNotification : IiOSNotification ,
3329 private $iOSSocketRequestExecutor : IiOSSocketRequestExecutor ,
3430 private $processService : IProcessService ,
3531 private $socketProxyFactory : ISocketProxyFactory ,
36- private $net : INet ,
3732 private $projectDataService : IProjectDataService ) {
3833 super ( device , $devicesService ) ;
3934 this . $processService . attachToProcessExitSignals ( this , this . debugStop ) ;
@@ -90,11 +85,6 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
9085 await this . killProcess ( this . _lldbProcess ) ;
9186 this . _lldbProcess = undefined ;
9287 }
93-
94- if ( this . _childProcess ) {
95- await this . killProcess ( this . _childProcess ) ;
96- this . _childProcess = undefined ;
97- }
9888 }
9989
10090 protected getChromeDebugUrl ( debugOptions : IDebugOptions , port : number ) : string {
@@ -116,38 +106,21 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
116106
117107 private async emulatorDebugBrk ( debugData : IDebugData , debugOptions : IDebugOptions ) : Promise < string > {
118108 const args = debugOptions . debugBrk ? "--nativescript-debug-brk" : "--nativescript-debug-start" ;
119- const child_process = await this . $iOSEmulatorServices . runApplicationOnEmulator ( debugData . pathToAppPackage , {
109+ const launchResult = await this . $iOSEmulatorServices . runApplicationOnEmulator ( debugData . pathToAppPackage , {
120110 waitForDebugger : true ,
121111 captureStdin : true ,
122112 args : args ,
123113 appId : debugData . applicationIdentifier ,
124114 skipInstall : true
125115 } ) ;
126116
127- const lineStream = byline ( child_process . stdout ) ;
128- this . _childProcess = child_process ;
129-
130- lineStream . on ( 'data' , ( line : NodeBuffer ) => {
131- const lineText = line . toString ( ) ;
132- if ( lineText && _ . startsWith ( lineText , debugData . applicationIdentifier ) ) {
133- const pid = getPidFromiOSSimulatorLogs ( debugData . applicationIdentifier , lineText ) ;
134- if ( ! pid ) {
135- this . $logger . trace ( `Line ${ lineText } does not contain PID of the application ${ debugData . applicationIdentifier } .` ) ;
136- return ;
137- }
138-
139- this . _lldbProcess = this . $childProcess . spawn ( "lldb" , [ "-p" , pid ] ) ;
140- if ( log4js . levels . TRACE . isGreaterThanOrEqualTo ( this . $logger . getLevel ( ) ) ) {
141- this . _lldbProcess . stdout . pipe ( process . stdout ) ;
142- }
143- this . _lldbProcess . stderr . pipe ( process . stderr ) ;
144- this . _lldbProcess . stdin . write ( "process continue\n" ) ;
145- } else {
146- process . stdout . write ( line + "\n" ) ;
147- }
148- } ) ;
149-
150- await this . waitForBackendPortToBeOpened ( debugData . deviceIdentifier ) ;
117+ const pid = getPidFromiOSSimulatorLogs ( debugData . applicationIdentifier , launchResult ) ;
118+ this . _lldbProcess = this . $childProcess . spawn ( "lldb" , [ "-p" , pid ] ) ;
119+ if ( log4js . levels . TRACE . isGreaterThanOrEqualTo ( this . $logger . getLevel ( ) ) ) {
120+ this . _lldbProcess . stdout . pipe ( process . stdout ) ;
121+ }
122+ this . _lldbProcess . stderr . pipe ( process . stderr ) ;
123+ this . _lldbProcess . stdin . write ( "process continue\n" ) ;
151124
152125 return this . wireDebuggerClient ( debugData , debugOptions ) ;
153126 }
@@ -158,20 +131,10 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
158131 const attachRequestMessage = this . $iOSNotification . getAttachRequest ( debugData . applicationIdentifier ) ;
159132
160133 const iOSEmulatorService = < Mobile . IiOSSimulatorService > this . $iOSEmulatorServices ;
161- await iOSEmulatorService . postDarwinNotification ( attachRequestMessage ) ;
162- await this . waitForBackendPortToBeOpened ( debugData . deviceIdentifier ) ;
134+ await iOSEmulatorService . postDarwinNotification ( attachRequestMessage , debugData . deviceIdentifier ) ;
163135 return result ;
164136 }
165137
166- private async waitForBackendPortToBeOpened ( deviceIdentifier : string ) : Promise < void > {
167- const portListens = await this . $net . waitForPortToListen ( { port : inspectorBackendPort , timeout : 10000 , interval : 200 } ) ;
168- if ( ! portListens ) {
169- const error = < Mobile . IDeviceError > new Error ( "Unable to connect to application. Ensure application is running on simulator." ) ;
170- error . deviceIdentifier = deviceIdentifier ;
171- throw error ;
172- }
173- }
174-
175138 private async deviceDebugBrk ( debugData : IDebugData , debugOptions : IDebugOptions ) : Promise < string > {
176139 await this . $devicesService . initialize ( { platform : this . platform , deviceId : debugData . deviceIdentifier } ) ;
177140 const projectData = this . $projectDataService . getProjectData ( debugData . projectDir ) ;
@@ -219,7 +182,7 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
219182 // the VSCode Ext starts `tns debug ios --no-client` to start/attach to debug sessions
220183 // check if --no-client is passed - default to opening a tcp socket (versus Chrome DevTools (websocket))
221184 if ( ( debugOptions . inspector || ! debugOptions . client ) && this . $hostInfo . isDarwin ) {
222- this . _socketProxy = await this . $socketProxyFactory . createTCPSocketProxy ( this . getSocketFactory ( device ) ) ;
185+ this . _socketProxy = await this . $socketProxyFactory . createTCPSocketProxy ( this . getSocketFactory ( debugData , device ) ) ;
223186 await this . openAppInspector ( this . _socketProxy . address ( ) , debugData , debugOptions ) ;
224187 return null ;
225188 } else {
@@ -228,7 +191,7 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
228191 }
229192
230193 const deviceIdentifier = device ? device . deviceInfo . identifier : debugData . deviceIdentifier ;
231- this . _socketProxy = await this . $socketProxyFactory . createWebSocketProxy ( this . getSocketFactory ( device ) , deviceIdentifier ) ;
194+ this . _socketProxy = await this . $socketProxyFactory . createWebSocketProxy ( this . getSocketFactory ( debugData , device ) , deviceIdentifier ) ;
232195 return this . getChromeDebugUrl ( debugOptions , this . _socketProxy . options . port ) ;
233196 }
234197 }
@@ -247,9 +210,13 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
247210 }
248211 }
249212
250- private getSocketFactory ( device ?: Mobile . IiOSDevice ) : ( ) => Promise < net . Socket > {
213+ private getSocketFactory ( debugData : IDebugData , device ?: Mobile . IiOSDevice ) : ( ) => Promise < net . Socket > {
251214 const factory = async ( ) => {
252- const socket = device ? await device . connectToPort ( inspectorBackendPort ) : net . connect ( inspectorBackendPort ) ;
215+ const port = await this . $iOSDebuggerPortService . getPort ( { deviceId : debugData . deviceIdentifier , appId : debugData . applicationIdentifier } ) ;
216+ if ( ! port ) {
217+ this . $errors . fail ( "NativeScript debugger was not able to get inspector socket port." ) ;
218+ }
219+ const socket = device ? await device . connectToPort ( port ) : net . connect ( port ) ;
253220 this . _sockets . push ( socket ) ;
254221 return socket ;
255222 } ;
0 commit comments