|
| 1 | +import { AppError } from '../../utils/errors.ts'; |
| 2 | +import { runCmd } from '../../utils/exec.ts'; |
| 3 | +import type { DeviceInfo } from '../../utils/device.ts'; |
| 4 | +import { buildSimctlArgs } from './simctl.ts'; |
| 5 | +import { IOS_SIMCTL_LIST_TIMEOUT_MS } from './config.ts'; |
| 6 | + |
| 7 | +type SimctlDeviceRecord = { |
| 8 | + name: string; |
| 9 | + udid: string; |
| 10 | + state: string; |
| 11 | + isAvailable: boolean; |
| 12 | +}; |
| 13 | + |
| 14 | +type SimctlListPayload = { |
| 15 | + devices: Record<string, SimctlDeviceRecord[]>; |
| 16 | +}; |
| 17 | + |
| 18 | +export type EnsureSimulatorResult = { |
| 19 | + udid: string; |
| 20 | + device: string; |
| 21 | + runtime: string; |
| 22 | + created: boolean; |
| 23 | + booted: boolean; |
| 24 | +}; |
| 25 | + |
| 26 | +type EnsureSimulatorOptions = { |
| 27 | + deviceName: string; |
| 28 | + runtime?: string; |
| 29 | + simulatorSetPath?: string | null; |
| 30 | + reuseExisting: boolean; |
| 31 | + boot: boolean; |
| 32 | + ensureReady: (device: DeviceInfo) => Promise<void>; |
| 33 | +}; |
| 34 | + |
| 35 | +export async function ensureSimulatorExists(options: EnsureSimulatorOptions): Promise<EnsureSimulatorResult> { |
| 36 | + const { deviceName, runtime, simulatorSetPath, reuseExisting, boot, ensureReady } = options; |
| 37 | + |
| 38 | + if (process.platform !== 'darwin') { |
| 39 | + throw new AppError('UNSUPPORTED_PLATFORM', 'ensure-simulator is only available on macOS'); |
| 40 | + } |
| 41 | + |
| 42 | + const simctlOpts = { simulatorSetPath: simulatorSetPath ?? undefined }; |
| 43 | + let udid: string; |
| 44 | + let resolvedRuntime: string; |
| 45 | + let created: boolean; |
| 46 | + |
| 47 | + if (reuseExisting) { |
| 48 | + const existing = await findExistingSimulator({ deviceName, runtime, simctlOpts }); |
| 49 | + if (existing) { |
| 50 | + udid = existing.udid; |
| 51 | + resolvedRuntime = existing.runtime; |
| 52 | + created = false; |
| 53 | + } else { |
| 54 | + const result = await createSimulator({ deviceName, runtime, simctlOpts }); |
| 55 | + udid = result.udid; |
| 56 | + resolvedRuntime = await resolveSimulatorRuntime(udid, simctlOpts); |
| 57 | + created = true; |
| 58 | + } |
| 59 | + } else { |
| 60 | + const result = await createSimulator({ deviceName, runtime, simctlOpts }); |
| 61 | + udid = result.udid; |
| 62 | + resolvedRuntime = await resolveSimulatorRuntime(udid, simctlOpts); |
| 63 | + created = true; |
| 64 | + } |
| 65 | + |
| 66 | + let booted = false; |
| 67 | + if (boot) { |
| 68 | + const device: DeviceInfo = { |
| 69 | + platform: 'ios', |
| 70 | + id: udid, |
| 71 | + name: deviceName, |
| 72 | + kind: 'simulator', |
| 73 | + target: 'mobile', |
| 74 | + ...(simulatorSetPath ? { simulatorSetPath } : {}), |
| 75 | + }; |
| 76 | + await ensureReady(device); |
| 77 | + booted = true; |
| 78 | + } |
| 79 | + |
| 80 | + return { udid, device: deviceName, runtime: resolvedRuntime, created, booted }; |
| 81 | +} |
| 82 | + |
| 83 | +type FindOptions = { |
| 84 | + deviceName: string; |
| 85 | + runtime?: string; |
| 86 | + simctlOpts: { simulatorSetPath?: string }; |
| 87 | +}; |
| 88 | + |
| 89 | +async function findExistingSimulator( |
| 90 | + options: FindOptions, |
| 91 | +): Promise<{ udid: string; runtime: string } | null> { |
| 92 | + const { deviceName, runtime, simctlOpts } = options; |
| 93 | + const result = await runCmd('xcrun', buildSimctlArgs(['list', 'devices', '-j'], simctlOpts), { |
| 94 | + allowFailure: true, |
| 95 | + timeoutMs: IOS_SIMCTL_LIST_TIMEOUT_MS, |
| 96 | + }); |
| 97 | + if (result.exitCode !== 0) return null; |
| 98 | + |
| 99 | + try { |
| 100 | + const payload = JSON.parse(String(result.stdout ?? '')) as SimctlListPayload; |
| 101 | + for (const [runtimeKey, devices] of Object.entries(payload.devices ?? {})) { |
| 102 | + if (runtime && !normalizeRuntime(runtimeKey).includes(normalizeRuntime(runtime))) continue; |
| 103 | + for (const device of devices) { |
| 104 | + if (!device.isAvailable) continue; |
| 105 | + if (device.name.toLowerCase() === deviceName.toLowerCase()) { |
| 106 | + return { udid: device.udid, runtime: runtimeKey }; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return null; |
| 111 | + } catch { |
| 112 | + return null; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +type CreateOptions = { |
| 117 | + deviceName: string; |
| 118 | + runtime?: string; |
| 119 | + simctlOpts: { simulatorSetPath?: string }; |
| 120 | +}; |
| 121 | + |
| 122 | +async function createSimulator(options: CreateOptions): Promise<{ udid: string }> { |
| 123 | + const { deviceName, runtime, simctlOpts } = options; |
| 124 | + const createArgs = runtime |
| 125 | + ? ['create', deviceName, deviceName, runtime] |
| 126 | + : ['create', deviceName, deviceName]; |
| 127 | + |
| 128 | + const result = await runCmd('xcrun', buildSimctlArgs(createArgs, simctlOpts), { |
| 129 | + allowFailure: true, |
| 130 | + }); |
| 131 | + |
| 132 | + if (result.exitCode !== 0) { |
| 133 | + throw new AppError('COMMAND_FAILED', 'Failed to create iOS simulator', { |
| 134 | + deviceName, |
| 135 | + runtime, |
| 136 | + stdout: String(result.stdout ?? ''), |
| 137 | + stderr: String(result.stderr ?? ''), |
| 138 | + exitCode: result.exitCode, |
| 139 | + hint: 'Ensure the device type and runtime identifiers are valid. Run `xcrun simctl list devicetypes` and `xcrun simctl list runtimes` to see available options.', |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + const udid = String(result.stdout ?? '').trim(); |
| 144 | + if (!udid) { |
| 145 | + throw new AppError('COMMAND_FAILED', 'simctl create returned no UDID', { |
| 146 | + deviceName, |
| 147 | + runtime, |
| 148 | + stdout: String(result.stdout ?? ''), |
| 149 | + stderr: String(result.stderr ?? ''), |
| 150 | + }); |
| 151 | + } |
| 152 | + return { udid }; |
| 153 | +} |
| 154 | + |
| 155 | +async function resolveSimulatorRuntime( |
| 156 | + udid: string, |
| 157 | + simctlOpts: { simulatorSetPath?: string }, |
| 158 | +): Promise<string> { |
| 159 | + const result = await runCmd('xcrun', buildSimctlArgs(['list', 'devices', '-j'], simctlOpts), { |
| 160 | + allowFailure: true, |
| 161 | + timeoutMs: IOS_SIMCTL_LIST_TIMEOUT_MS, |
| 162 | + }); |
| 163 | + if (result.exitCode !== 0) return ''; |
| 164 | + try { |
| 165 | + const payload = JSON.parse(String(result.stdout ?? '')) as SimctlListPayload; |
| 166 | + for (const [runtimeKey, devices] of Object.entries(payload.devices ?? {})) { |
| 167 | + if (devices.some((d) => d.udid === udid)) return runtimeKey; |
| 168 | + } |
| 169 | + return ''; |
| 170 | + } catch { |
| 171 | + return ''; |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +function normalizeRuntime(runtime: string): string { |
| 176 | + return runtime.toLowerCase().replace(/[._-]/g, ''); |
| 177 | +} |
0 commit comments