( conn: DaemonConnection, clientId: string, contextIdOverride?: string, )
| 423 | * Priority: explicit contextId argument → auto (single context) → prompt. |
| 424 | */ |
| 425 | export async function resolveContextId( |
| 426 | conn: DaemonConnection, |
| 427 | clientId: string, |
| 428 | contextIdOverride?: string, |
| 429 | ): Promise<string> { |
| 430 | const contexts = await conn.listContexts(clientId); |
| 431 | |
| 432 | if (contexts.length === 0) { |
| 433 | throw new CliError('No contexts found on the connected device.'); |
| 434 | } |
| 435 | |
| 436 | if (contextIdOverride) { |
| 437 | if (!contexts.some((c) => c.id === contextIdOverride)) { |
| 438 | throw new CliError( |
| 439 | `Context "${contextIdOverride}" not found. Run "valdi inspect contexts" to see available contexts.`, |
| 440 | ); |
| 441 | } |
| 442 | return contextIdOverride; |
| 443 | } |
| 444 | |
| 445 | if (contexts.length === 1) { |
| 446 | return contexts[0]!.id; |
| 447 | } |
| 448 | |
| 449 | // Multiple contexts — prompt |
| 450 | return getUserChoice( |
| 451 | contexts.map((c) => ({ |
| 452 | name: `${c.rootComponentName} [${c.id}]`, |
| 453 | value: c.id, |
| 454 | })), |
| 455 | 'Multiple contexts found. Select one:', |
| 456 | ); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Resolve which connected client to target. |
no test coverage detected