@@ -137,43 +137,51 @@ export class IosProject extends NSProject {
137137 return Promise . reject ( 'iOS platform is supported only on OS X.' ) ;
138138 }
139139
140+ let rebuild = ( args . request == "launch" ) ? ( args as ILaunchRequestArgs ) . rebuild : true ;
140141 // build command to execute
141142 let command = new CommandBuilder ( )
142143 . appendParam ( "debug" )
143144 . appendParam ( this . platform ( ) )
144145 . appendParamIf ( "--emulator" , args . emulator )
145146 . appendParamIf ( "--start" , args . request === "attach" )
146147 . appendParamIf ( "--debug-brk" , args . request === "launch" )
147- . appendParamIf ( "--no-rebuild" , ! args . rebuild )
148+ . appendParamIf ( "--no-rebuild" , ! rebuild )
148149 . appendParam ( "--no-client" )
149150 . appendParams ( args . tnsArgs )
150151 . build ( ) ;
151152
152153 let socketPathPrefix = 'socket-file-location: ' ;
153154 let socketPathPattern : RegExp = new RegExp ( socketPathPrefix + '.*\.sock' ) ;
154- let readyToConnect : boolean = false ;
155+
156+ let isSocketOpened = ( cliOutput : string ) : string => {
157+ let matches : RegExpMatchArray = cliOutput . match ( socketPathPattern ) ;
158+ if ( matches && matches . length > 0 ) {
159+ return matches [ 0 ] . substr ( socketPathPrefix . length ) ;
160+ }
161+ return null ;
162+ } ;
163+
164+ let isAppSynced = ( cliOutput : string ) => {
165+ return cliOutput . indexOf ( 'Successfully synced application' ) > - 1 ;
166+ } ;
155167
156168 return new Promise < string > ( ( resolve , reject ) => {
157169 // run NativeScript CLI command
158170 let child : ChildProcess = this . spawnProcess ( command . path , command . args , args . tnsOutput ) ;
159- let synced = false ;
171+
172+ let appSynced = false ;
173+ let socketPath : string = null ;
160174
161175 child . stdout . on ( 'data' , ( data ) => {
162- let strData : string = data . toString ( ) ;
163- this . emit ( 'TNS.outputMessage' , strData , 'log' ) ;
164- this . writeToTnsOutputFile ( strData ) ;
165- if ( ! synced && ! args . rebuild && strData . indexOf ( 'Successfully synced application' ) > - 1 ) {
166- synced = true ;
167- //wait a little before trying to connect, this gives a changes for adb to be able to connect to the debug socket
168- setTimeout ( ( ) => {
169- resolve ( ) ;
170- } , 500 ) ;
171- } else if ( ! readyToConnect ) {
172- let matches : RegExpMatchArray = strData . match ( socketPathPattern ) ;
173- if ( matches && matches . length > 0 ) {
174- readyToConnect = true ;
175- resolve ( matches [ 0 ] . substr ( socketPathPrefix . length ) ) ;
176- }
176+ let cliOutput : string = data . toString ( ) ;
177+ this . emit ( 'TNS.outputMessage' , cliOutput , 'log' ) ;
178+ this . writeToTnsOutputFile ( cliOutput ) ;
179+
180+ socketPath = socketPath || isSocketOpened ( cliOutput ) ;
181+ appSynced = rebuild ? false : ( appSynced || isAppSynced ( cliOutput ) ) ;
182+
183+ if ( ( rebuild && socketPath ) || ( ! rebuild && socketPath && appSynced ) ) {
184+ resolve ( socketPath ) ;
177185 }
178186 } ) ;
179187
@@ -183,13 +191,7 @@ export class IosProject extends NSProject {
183191 } ) ;
184192
185193 child . on ( 'close' , ( code , signal ) => {
186- if ( ! args . rebuild ) {
187- setTimeout ( ( ) => {
188- reject ( "The debug process exited unexpectedly code:" + code ) ;
189- } , 3000 ) ;
190- } else {
191- reject ( "The debug process exited unexpectedly code:" + code ) ;
192- }
194+ reject ( "The debug process exited unexpectedly code:" + code ) ;
193195 } ) ;
194196 } ) ;
195197 }
@@ -221,11 +223,12 @@ export class AndroidProject extends NSProject {
221223 return Promise . resolve ( child ) ;
222224 }
223225
224- public debug ( args : IAttachRequestArgs | ILaunchRequestArgs ) : Promise < void > {
225- if ( args . request === "attach" ) {
226+ public debug ( params : IAttachRequestArgs | ILaunchRequestArgs ) : Promise < void > {
227+ if ( params . request === "attach" ) {
226228 return Promise . resolve < void > ( ) ;
227229 }
228- else if ( args . request === "launch" ) {
230+ else if ( params . request === "launch" ) {
231+ let args : ILaunchRequestArgs = params as ILaunchRequestArgs ;
229232 let that = this ;
230233 let launched = false ;
231234
0 commit comments