11import * as path from "path" ;
22import * as choki from "chokidar" ;
3- import { parse } from "url" ;
43import { EOL } from "os" ;
54import { EventEmitter } from "events" ;
65import { hook } from "../../common/helpers" ;
7- import { APP_FOLDER_NAME , PACKAGE_JSON_FILE_NAME , LiveSyncTrackActionNames , USER_INTERACTION_NEEDED_EVENT_NAME , DEBUGGER_ATTACHED_EVENT_NAME } from "../../constants" ;
6+ import { APP_FOLDER_NAME , PACKAGE_JSON_FILE_NAME , LiveSyncTrackActionNames , USER_INTERACTION_NEEDED_EVENT_NAME , DEBUGGER_ATTACHED_EVENT_NAME , DEBUGGER_DETACHED_EVENT_NAME } from "../../constants" ;
87import { FileExtensions , DeviceTypes } from "../../common/constants" ;
98const deviceDescriptorPrimaryKey = "identifier" ;
109
@@ -101,7 +100,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
101100 return currentDescriptors || [ ] ;
102101 }
103102
104- private async refreshApplication ( projectData : IProjectData , liveSyncResultInfo : ILiveSyncResultInfo , debugOpts ?: IDebugOptions , outputPath ?: string ) : Promise < void > {
103+ private async refreshApplication ( projectData : IProjectData , liveSyncResultInfo : ILiveSyncResultInfo , debugOpts ?: IDebugOptions , outputPath ?: string ) : Promise < void | IDebugInformation > {
105104 const deviceDescriptor = this . getDeviceDescriptor ( liveSyncResultInfo . deviceAppData . device . deviceInfo . identifier , projectData . projectDir ) ;
106105
107106 return deviceDescriptor && deviceDescriptor . debugggingEnabled ?
@@ -138,13 +137,14 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
138137 this . $logger . info ( `Successfully synced application ${ liveSyncResultInfo . deviceAppData . appIdentifier } on device ${ liveSyncResultInfo . deviceAppData . device . deviceInfo . identifier } .` ) ;
139138 }
140139
141- private async refreshApplicationWithDebug ( projectData : IProjectData , liveSyncResultInfo : ILiveSyncResultInfo , debugOptions : IDebugOptions , outputPath ?: string ) : Promise < void > {
140+ private async refreshApplicationWithDebug ( projectData : IProjectData , liveSyncResultInfo : ILiveSyncResultInfo , debugOptions : IDebugOptions , outputPath ?: string ) : Promise < IDebugInformation > {
142141 await this . $platformService . trackProjectType ( projectData ) ;
143142
144143 const deviceAppData = liveSyncResultInfo . deviceAppData ;
145144
146145 const deviceIdentifier = liveSyncResultInfo . deviceAppData . device . deviceInfo . identifier ;
147146 await this . $debugService . debugStop ( deviceIdentifier ) ;
147+ this . emit ( DEBUGGER_DETACHED_EVENT_NAME , { deviceIdentifier } ) ;
148148
149149 const applicationId = deviceAppData . appIdentifier ;
150150 const attachDebuggerOptions : IAttachDebuggerOptions = {
@@ -181,7 +181,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
181181 return this . enableDebuggingCoreWithoutWaitingCurrentAction ( deviceOption , { projectDir : projectData . projectDir } ) ;
182182 }
183183
184- public async attachDebugger ( settings : IAttachDebuggerOptions ) : Promise < void > {
184+ public async attachDebugger ( settings : IAttachDebuggerOptions ) : Promise < IDebugInformation > {
185185 // Default values
186186 if ( settings . debugOptions ) {
187187 settings . debugOptions . chrome = settings . debugOptions . chrome === undefined ? true : settings . debugOptions . chrome ;
@@ -208,22 +208,19 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
208208 } ;
209209 debugData . pathToAppPackage = this . $platformService . lastOutputPath ( settings . platform , buildConfig , projectData , settings . outputPath ) ;
210210
211- this . printDebugInformation ( await this . $debugService . debug ( debugData , settings . debugOptions ) ) ;
211+ return this . printDebugInformation ( await this . $debugService . debug ( debugData , settings . debugOptions ) ) ;
212212 }
213213
214- public printDebugInformation ( information : string ) : void {
215- if ( ! ! information ) {
216- const wsQueryParam = parse ( information ) . query . ws ;
217- const hostPortSplit = wsQueryParam && wsQueryParam . split ( ":" ) ;
218- this . emit ( DEBUGGER_ATTACHED_EVENT_NAME , {
219- url : information ,
220- port : hostPortSplit && hostPortSplit [ 1 ]
221- } ) ;
222- this . $logger . info ( `To start debugging, open the following URL in Chrome:${ EOL } ${ information } ${ EOL } ` . cyan ) ;
214+ public printDebugInformation ( debugInformation : IDebugInformation ) : IDebugInformation {
215+ if ( ! ! debugInformation . url ) {
216+ this . emit ( DEBUGGER_ATTACHED_EVENT_NAME , debugInformation ) ;
217+ this . $logger . info ( `To start debugging, open the following URL in Chrome:${ EOL } ${ debugInformation . url } ${ EOL } ` . cyan ) ;
223218 }
219+
220+ return debugInformation ;
224221 }
225222
226- public enableDebugging ( deviceOpts : IEnableDebuggingDeviceOptions [ ] , debuggingAdditionalOptions : IDebuggingAdditionalOptions ) : Promise < void > [ ] {
223+ public enableDebugging ( deviceOpts : IEnableDebuggingDeviceOptions [ ] , debuggingAdditionalOptions : IDebuggingAdditionalOptions ) : Promise < IDebugInformation > [ ] {
227224 return _ . map ( deviceOpts , d => this . enableDebuggingCore ( d , debuggingAdditionalOptions ) ) ;
228225 }
229226
@@ -233,7 +230,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
233230 return _ . find ( deviceDescriptors , d => d . identifier === deviceIdentifier ) ;
234231 }
235232
236- private async enableDebuggingCoreWithoutWaitingCurrentAction ( deviceOption : IEnableDebuggingDeviceOptions , debuggingAdditionalOptions : IDebuggingAdditionalOptions ) : Promise < void > {
233+ private async enableDebuggingCoreWithoutWaitingCurrentAction ( deviceOption : IEnableDebuggingDeviceOptions , debuggingAdditionalOptions : IDebuggingAdditionalOptions ) : Promise < IDebugInformation > {
237234 const currentDeviceDescriptor = this . getDeviceDescriptor ( deviceOption . deviceIdentifier , debuggingAdditionalOptions . projectDir ) ;
238235 if ( ! currentDeviceDescriptor ) {
239236 this . $errors . failWithoutHelp ( `Couldn't enable debugging for ${ deviceOption . deviceIdentifier } ` ) ;
@@ -250,16 +247,19 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
250247 debugOptions : deviceOption . debugOptions
251248 } ;
252249
250+ let debugInformation : IDebugInformation ;
253251 try {
254- await this . attachDebugger ( attachDebuggerOptions ) ;
252+ debugInformation = await this . attachDebugger ( attachDebuggerOptions ) ;
255253 } catch ( err ) {
256254 this . $logger . trace ( "Couldn't attach debugger, will modify options and try again." , err ) ;
257255 attachDebuggerOptions . debugOptions . start = false ;
258- await this . attachDebugger ( attachDebuggerOptions ) ;
256+ debugInformation = await this . attachDebugger ( attachDebuggerOptions ) ;
259257 }
258+
259+ return debugInformation ;
260260 }
261261
262- private async enableDebuggingCore ( deviceOption : IEnableDebuggingDeviceOptions , debuggingAdditionalOptions : IDebuggingAdditionalOptions ) : Promise < void > {
262+ private async enableDebuggingCore ( deviceOption : IEnableDebuggingDeviceOptions , debuggingAdditionalOptions : IDebuggingAdditionalOptions ) : Promise < IDebugInformation > {
263263 const liveSyncProcessInfo : ILiveSyncProcessInfo = this . liveSyncProcessesInfo [ debuggingAdditionalOptions . projectDir ] ;
264264 if ( liveSyncProcessInfo && liveSyncProcessInfo . currentSyncAction ) {
265265 await liveSyncProcessInfo . currentSyncAction ;
@@ -290,7 +290,8 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
290290 this . $errors . failWithoutHelp ( `Couldn't disable debugging for ${ deviceOption . deviceIdentifier } . Could not find device.` ) ;
291291 }
292292
293- return this . $debugService . debugStop ( currentDevice . deviceInfo . identifier ) ;
293+ await this . $debugService . debugStop ( currentDevice . deviceInfo . identifier ) ;
294+ this . emit ( DEBUGGER_DETACHED_EVENT_NAME , { deviceIdentifier : currentDeviceDescriptor . identifier } ) ;
294295 }
295296
296297 @hook ( "liveSync" )
0 commit comments