()
| 15 | } |
| 16 | |
| 17 | function loadDeviceNames(): Map<string, string> { |
| 18 | if (cachedDevices && Date.now() - cacheTimestamp < CACHE_TTL_MS) { |
| 19 | return cachedDevices; |
| 20 | } |
| 21 | |
| 22 | const map = new Map<string, string>(); |
| 23 | const tmpFile = join(tmpdir(), `devicectl-list-${process.pid}.json`); |
| 24 | |
| 25 | try { |
| 26 | execSync(`xcrun devicectl list devices --json-output ${tmpFile}`, { |
| 27 | encoding: 'utf8', |
| 28 | timeout: 10_000, |
| 29 | stdio: 'pipe', |
| 30 | }); |
| 31 | |
| 32 | const data = JSON.parse(readFileSync(tmpFile, 'utf8')) as { |
| 33 | result?: { devices?: DeviceCtlEntry[] }; |
| 34 | }; |
| 35 | |
| 36 | for (const device of data.result?.devices ?? []) { |
| 37 | const name = device.deviceProperties.name; |
| 38 | map.set(device.identifier, name); |
| 39 | if (device.hardwareProperties?.udid) { |
| 40 | map.set(device.hardwareProperties.udid, name); |
| 41 | } |
| 42 | } |
| 43 | } catch { |
| 44 | // Device list unavailable -- return empty map, will fall back to UUID only |
| 45 | } finally { |
| 46 | try { |
| 47 | unlinkSync(tmpFile); |
| 48 | } catch { |
| 49 | // ignore |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | cachedDevices = map; |
| 54 | cacheTimestamp = Date.now(); |
| 55 | return map; |
| 56 | } |
| 57 | |
| 58 | export function resolveDeviceName(deviceId: string): string | undefined { |
| 59 | const names = loadDeviceNames(); |
no test coverage detected