import { CliParameters } from './cli-parameters' ; import { which } from './which' ; import { ErrorCode , createError } from './errors' ; import * as execa from 'execa' ; /**
 * Finds the path to xcrun or throws an error.
 */ export async function getXcrunPath ( ) { try { return await which ( 'xcrun' ) ; } catch ( err ) { throw createError ( ErrorCode . MissingXcrun ) ; } } param xcrunPath export async function checkSimulator ( xcrunPath ) { const response = await execa ( xcrunPath , [ 'simctl' , 'list' ] ) ; const stdout = response . stdout as string ; const reducer = ( acc , line ) => ( line . indexOf ( 'Booted' ) >= 0 ? acc + 1 : acc ) ; const count = stdout . split ( '\n' ) . reduce ( reducer , 0 ) ; if ( count === 0 ) throw createError ( ErrorCode . NoRunningiOSSimulators ) ; } param xcrunPath param filename export async function saveScreenshot ( xcrunPath , filename ) { try { const response = await execa ( xcrunPath , [ 'simctl' , 'io' , 'booted' , 'screenshot' , filename ] ) ; if ( response . code !== 0 ) { throw createError ( ErrorCode . ScreenshotFail ) ; } } catch ( err ) { throw createError ( ErrorCode . ScreenshotFail ) ; } } param parameters export async function saveToFile ( parameters ) { const xcrun = await getXcrunPath ( ) ; await checkSimulator ( xcrun ) ; await saveScreenshot ( xcrun , parameters . filename ) ; }