1import chalk from 'chalk'; 2 3import * as SimControl from '../../../start/platforms/ios/simctl'; 4import prompt from '../../../utils/prompts'; 5import { ConnectedDevice } from '../appleDevice/AppleDevice'; 6 7type AnyDevice = SimControl.Device | ConnectedDevice; 8 9function isConnectedDevice(item: AnyDevice): item is ConnectedDevice { 10 return 'deviceType' in item; 11} 12 13function isSimControlDevice(item: AnyDevice): item is SimControl.Device { 14 return 'state' in item; 15} 16 17/** Format a device for the prompt list. Exposed for testing. */ 18export function formatDeviceChoice(item: AnyDevice): { title: string; value: string } { 19 const isConnected = isConnectedDevice(item) && item.deviceType === 'device'; 20 const isActive = isSimControlDevice(item) && item.state === 'Booted'; 21 const symbol = isConnected ? ' ' : ''; 22 const format = isActive ? chalk.bold : (text: string) => text; 23 return { 24 title: `${symbol}${format(item.name)}${ 25 item.osVersion ? chalk.dim(` (${item.osVersion})`) : '' 26 }`, 27 value: item.udid, 28 }; 29} 30 31/** Prompt to select a device from a searchable list of devices. */ 32export async function promptDeviceAsync(devices: AnyDevice[]): Promise<AnyDevice> { 33 // --device with no props after 34 const { value } = await prompt({ 35 type: 'autocomplete', 36 name: 'value', 37 limit: 11, 38 message: 'Select a device', 39 choices: devices.map((item) => formatDeviceChoice(item)), 40 suggest: (input: any, choices: any) => { 41 const regex = new RegExp(input, 'i'); 42 return choices.filter((choice: any) => regex.test(choice.title)); 43 }, 44 }); 45 return devices.find((device) => device.udid === value)!; 46} 47