@@ -33,15 +33,15 @@ export class Simctl implements ISimctl {
33
33
return this . spawnAsync ( "boot" , [ deviceId ] ) ;
34
34
}
35
35
36
- public terminate ( deviceId : string , appIdentifier : string ) : Promise < string > {
36
+ public terminate ( deviceId : string , appIdentifier : string ) : Promise < string > {
37
37
return this . spawnAsync ( "terminate" , [ deviceId , appIdentifier ] ) ;
38
38
}
39
39
40
- public install ( deviceId : string , applicationPath : string ) : Promise < void > {
40
+ public install ( deviceId : string , applicationPath : string ) : Promise < void > {
41
41
return this . spawnAsync ( "install" , [ deviceId , applicationPath ] ) ;
42
42
}
43
43
44
- public uninstall ( deviceId : string , appIdentifier : string , opts ?: any ) : Promise < void > {
44
+ public uninstall ( deviceId : string , appIdentifier : string , opts ?: any ) : Promise < void > {
45
45
return this . spawnAsync ( "uninstall" , [ deviceId , appIdentifier ] , opts ) ;
46
46
}
47
47
@@ -138,31 +138,37 @@ export class Simctl implements ISimctl {
138
138
return this . simctlSpawn ( "spawn" , [ deviceId , "log" , "stream" , "--style" , "syslog" ] . concat ( predicateArgs ) ) ;
139
139
}
140
140
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 ;
155
148
}
156
149
}
157
150
158
- return '' ;
151
+ return childProcess . spawn ( "xcrun" , [ "simctl" , command ] . concat ( args ) , spawnOpts , opts ) ;
159
152
}
160
153
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 } ;
163
169
}
164
170
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 ) ;
167
173
}
168
174
}
0 commit comments