( targets: PlatformTarget[], selectedTarget?: string, selectedTargetSdkVersion?: string, selectByName?: boolean, )
| 376 | } |
| 377 | |
| 378 | export async function promptForPlatformTarget( |
| 379 | targets: PlatformTarget[], |
| 380 | selectedTarget?: string, |
| 381 | selectedTargetSdkVersion?: string, |
| 382 | selectByName?: boolean, |
| 383 | ): Promise<PlatformTarget> { |
| 384 | const { prompt } = await import('prompts'); |
| 385 | const validTargets = targets.filter((t) => t.id !== undefined); |
| 386 | if (!selectedTarget) { |
| 387 | if (validTargets.length === 1) { |
| 388 | return validTargets[0]; |
| 389 | } else { |
| 390 | const answers = await prompt( |
| 391 | [ |
| 392 | { |
| 393 | type: 'select', |
| 394 | name: 'target', |
| 395 | message: 'Please choose a target device:', |
| 396 | choices: validTargets.map((t) => ({ |
| 397 | title: `${getPlatformTargetName(t)} (${t.id})`, |
| 398 | value: t, |
| 399 | })), |
| 400 | }, |
| 401 | ], |
| 402 | { onCancel: () => process.exit(1) }, |
| 403 | ); |
| 404 | |
| 405 | return answers.target; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | const targetID = selectedTarget.trim(); |
| 410 | const target = targets.find((t) => { |
| 411 | if (selectByName === true) { |
| 412 | let name = t.name ?? t.model; |
| 413 | if (name) { |
| 414 | // Apple device names may have "smart quotes" in the name, |
| 415 | // strip them and replace them with the "straight" versions |
| 416 | name = name.replace(/[\u2018\u2019]/g, "'").replace(/[\u201C\u201D]/g, '"'); |
| 417 | } |
| 418 | |
| 419 | if (selectedTargetSdkVersion) { |
| 420 | return name === targetID && t.sdkVersion === selectedTargetSdkVersion; |
| 421 | } |
| 422 | |
| 423 | return name === targetID; |
| 424 | } |
| 425 | |
| 426 | return t.id === targetID; |
| 427 | }); |
| 428 | |
| 429 | if (!target) { |
| 430 | if (selectByName) { |
| 431 | let invalidTargetName = targetID; |
| 432 | if (selectedTargetSdkVersion) { |
| 433 | invalidTargetName += ` [${selectedTargetSdkVersion}]`; |
| 434 | } |
| 435 | fatal( |
no test coverage detected