( conn: DaemonConnection, clientIdOverride?: string, )
| 461 | * Priority: explicit --client flag → saved config → auto (single device) → prompt. |
| 462 | */ |
| 463 | export async function resolveClientId( |
| 464 | conn: DaemonConnection, |
| 465 | clientIdOverride?: string, |
| 466 | ): Promise<string> { |
| 467 | const clients = await conn.listConnectedClients(); |
| 468 | |
| 469 | if (clients.length === 0) { |
| 470 | throw new CliError( |
| 471 | 'No devices connected to the Valdi daemon.\n' + |
| 472 | 'Make sure the Valdi app is running and connected to the hot-reloader.', |
| 473 | ); |
| 474 | } |
| 475 | |
| 476 | if (clientIdOverride) { |
| 477 | if (!clients.some((c) => c.client_id === clientIdOverride)) { |
| 478 | throw new CliError( |
| 479 | `Client "${clientIdOverride}" not found. Run "valdi inspect devices" to see connected clients.`, |
| 480 | ); |
| 481 | } |
| 482 | return clientIdOverride; |
| 483 | } |
| 484 | |
| 485 | const config = loadInspectConfig(); |
| 486 | if (config.selectedClientId && clients.some((c) => c.client_id === config.selectedClientId)) { |
| 487 | return config.selectedClientId; |
| 488 | } |
| 489 | |
| 490 | if (clients.length === 1) { |
| 491 | return clients[0]!.client_id; |
| 492 | } |
| 493 | |
| 494 | // Multiple devices — prompt |
| 495 | return getUserChoice( |
| 496 | clients.map((c) => ({ |
| 497 | name: `${c.application_id} (${c.platform}) [${c.client_id}]`, |
| 498 | value: c.client_id, |
| 499 | })), |
| 500 | 'Multiple devices connected. Select one:', |
| 501 | ); |
| 502 | } |
no test coverage detected