@@ -33,15 +33,15 @@ export class Simctl implements ISimctl {
3333 return this . spawnAsync ( "boot" , [ deviceId ] ) ;
3434 }
3535
36- public terminate ( deviceId : string , appIdentifier : string ) : Promise < string > {
36+ public terminate ( deviceId : string , appIdentifier : string ) : Promise < string > {
3737 return this . spawnAsync ( "terminate" , [ deviceId , appIdentifier ] ) ;
3838 }
3939
40- public install ( deviceId : string , applicationPath : string ) : Promise < void > {
40+ public install ( deviceId : string , applicationPath : string ) : Promise < void > {
4141 return this . spawnAsync ( "install" , [ deviceId , applicationPath ] ) ;
4242 }
4343
44- public uninstall ( deviceId : string , appIdentifier : string , opts ?: any ) : Promise < void > {
44+ public uninstall ( deviceId : string , appIdentifier : string , opts ?: any ) : Promise < void > {
4545 return this . spawnAsync ( "uninstall" , [ deviceId , appIdentifier ] , opts ) ;
4646 }
4747
@@ -138,31 +138,37 @@ export class Simctl implements ISimctl {
138138 return this . simctlSpawn ( "spawn" , [ deviceId , "log" , "stream" , "--style" , "syslog" ] . concat ( predicateArgs ) ) ;
139139 }
140140
141- private simctlExec ( command : string , args : string [ ] , opts ?: any ) : string {
142- const result = childProcess . spawnSync ( "xcrun" , [ "simctl" , command ] . concat ( args ) , opts ) ;
143- if ( result && result . stderr && ! _ . isEmpty ( result . stderr ) ) {
144- throw new Error ( `Error while executing command '${ [ "xcrun" , "simctl" , command ] . concat ( args ) . join ( " " ) } '. Please ensure your tools are configured correctly. More info: ${ result . stderr . toString ( ) } ` ) ;
145- }
146- if ( result ) {
147- if ( result . signal ) {
148- // In some cases, sending Ctrl + C (SIGINT) is handled by the simctl itself and spawnSync finishes, but the SIGINT does not stop current process.
149- // In order to ensure the same signal is sent to the caller (CLI for example), send the signal manually:
150- process . send ( result . signal ) ;
151- }
152-
153- if ( result . stdout ) {
154- return result . stdout . toString ( ) . trim ( ) ;
141+ private async spawnAsync ( command : string , args : string [ ] , spawnOpts ?: child_process . SpawnOptions , opts ?: ISkipErrorComposition ) : Promise < any > {
142+ const { canExecuteXcrun, xcodeToolsError } = this . verifyXcodeCommandLineToolsAreInstalled ( ) ;
143+ if ( ! canExecuteXcrun ) {
144+ if ( opts . skipError ) {
145+ return null ;
146+ } else {
147+ throw xcodeToolsError ;
155148 }
156149 }
157150
158- return '' ;
151+ return childProcess . spawn ( "xcrun" , [ "simctl" , command ] . concat ( args ) , spawnOpts , opts ) ;
159152 }
160153
161- private simctlSpawn ( command : string , args : string [ ] , opts ?: child_process . SpawnOptions ) : child_process . ChildProcess {
162- return child_process . spawn ( "xcrun" , [ "simctl" , command ] . concat ( args ) , opts ) ;
154+ private verifyXcodeCommandLineToolsAreInstalled ( ) : { canExecuteXcrun : boolean , xcodeToolsError : Error } {
155+ let canExecuteXcrun = false ;
156+ let xcodeToolsError : Error = null ;
157+
158+ try {
159+ const result = childProcess . execSync ( "xcode-select -p" , { stdio : "pipe" } ) ;
160+ canExecuteXcrun = ! ! ( result && result . toString ( ) . trim ( ) ) ;
161+ if ( ! canExecuteXcrun ) {
162+ xcodeToolsError = new Error ( "Unable to work with iOS Simulator as Xcode Command Line Tools cannot be found." ) ;
163+ }
164+ } catch ( err ) {
165+ xcodeToolsError = err
166+ }
167+
168+ return { canExecuteXcrun, xcodeToolsError } ;
163169 }
164170
165- private spawnAsync ( command : string , args : string [ ] , opts ?: child_process . SpawnOptions ) : Promise < any > {
166- return childProcess . spawn ( "xcrun" , [ "simctl" , command ] . concat ( args ) , opts ) ;
171+ private simctlSpawn ( command : string , args : string [ ] , spawnOpts ?: child_process . SpawnOptions ) : child_process . ChildProcess {
172+ return child_process . spawn ( "xcrun" , [ "simctl" , command ] . concat ( args ) , spawnOpts ) ;
167173 }
168174}
0 commit comments