import * as osascript from '@expo/osascript' ; import spawnAsync from '@expo/spawn-async' ; import { exec } from 'child_process' ; import { promisify } from 'util' ; import { Logger } from '../../internal' ; import { TimeoutError , waitForActionAsync } from './waitForActionAsync' ; const execAsync = promisify ( exec ) ; export async function waitForSimulatorAppToStart ( ) { return waitForActionAsync < boolean > ( { interval : 50 , action : isSimulatorAppRunningAsync } ) ; } /**
 * I think the app can be open while no simulators are booted.
 */ export async function isSimulatorAppRunningAsync ( ) { try { const zeroMeansNo = ( await osascript . execAsync ( 'tell app "System Events" to count processes whose name is "Simulator"' ) ) . trim ( ) ; if ( zeroMeansNo === '0' ) { return false ; } } catch ( error ) { if ( error . message . includes ( 'Application isn’t running' ) ) { return false ; } throw error ; } return true ; } export async function ensureSimulatorAppRunningAsync ( { udid } ) { if ( ! ( await isSimulatorAppRunningAsync ( ) ) ) { Logger . global . info ( `\u203A Opening the iOS simulator, this might take a moment.` ) ; await openSimulatorAppAsync ( { udid } ) ; if ( ! ( await waitForSimulatorAppToStart ( ) ) ) { throw new TimeoutError ( `Simulator app did not open fast enough. Try opening Simulator first, then running your app.` ) ; } } } export async function openSimulatorAppAsync ( { udid } ) { const args = [ 'open' , '-a' , 'Simulator' ] ; if ( udid ) { args . push ( '--args' , '-CurrentDeviceUDID' , udid ) ; } await execAsync ( args . join ( ' ' ) ) ; } export async function killAllAsync ( ) { return await spawnAsync ( 'killAll' , [ 'Simulator' ] ) ; }