Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions cli/src/android/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ const debug = Debug('capacitor:android:run');

export async function runAndroid(
config: Config,
{ target: selectedTarget, flavor: selectedFlavor, forwardPorts: selectedPorts }: RunCommandOptions,
{
target: selectedTarget,
targetName: selectedTargetName,
targetNameSdkVersion: selectedTargetSdkVersion,
flavor: selectedFlavor,
forwardPorts: selectedPorts,
}: RunCommandOptions,
): Promise<void> {
const target = await promptForPlatformTarget(await getPlatformTargets('android'), selectedTarget);
const target = await promptForPlatformTarget(
await getPlatformTargets('android'),
selectedTarget ?? selectedTargetName,
selectedTargetSdkVersion,
selectedTargetName !== undefined,
);

const runFlavor = selectedFlavor || config.android?.flavor || '';

Expand Down
31 changes: 29 additions & 2 deletions cli/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ export interface PlatformTarget {
export async function promptForPlatformTarget(
targets: PlatformTarget[],
selectedTarget?: string,
selectedTargetSdkVersion?: string,
selectByName?: boolean,
): Promise<PlatformTarget> {
const { prompt } = await import('prompts');
const validTargets = targets.filter((t) => t.id !== undefined);
Expand Down Expand Up @@ -405,10 +407,35 @@ export async function promptForPlatformTarget(
}

const targetID = selectedTarget.trim();
const target = targets.find((t) => t.id === targetID);
const target = targets.find((t) => {
if (selectByName === true) {
let name = t.name ?? t.model;
if (name) {
name = name.replace(/[\u2018\u2019]/g, "'").replace(/[\u201C\u201D]/g, '"');
}

if (selectedTargetSdkVersion) {
return name === targetID && t.sdkVersion === selectedTargetSdkVersion;
}

return name === targetID;
}

return t.id === targetID;
});

if (!target) {
fatal(`Invalid target ID: ${c.input(targetID)}.\n` + `Valid targets are: ${targets.map((t) => t.id).join(', ')}`);
if (selectByName) {
let invalidTargetName = targetID;
if (selectedTargetSdkVersion) {
invalidTargetName += ` [${selectedTargetSdkVersion}]`;
}
fatal(
`Invalid target name: ${c.input(invalidTargetName)}.\n` +
`Valid targets are:\n ${targets.map((t) => `${t.name} [${t.sdkVersion}]`).join('\n')}`,
);
}
fatal(`Invalid target ID: ${c.input(targetID)}.\n` + `Valid targets are:\n ${targets.map((t) => t.id).join('\n')}`);
}

return target;
Expand Down
23 changes: 22 additions & 1 deletion cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ export function runProgram(config: Config): void {
.option('--list', 'list targets, then quit')
.addOption(new Option('--json').hideHelp())
.option('--target <id>', 'use a specific target')
.option('--target-name <name>', 'use a specific target by name')
.option(
'--target-name-sdk-version <version>',
'use a specific sdk version when using --target-name, ex: 26.0 (for iOS 26) or 35 (for Android API 35)',
)
.option('--no-sync', `do not run ${c.input('sync')}`)
.option('--forwardPorts <port:port>', 'Automatically run "adb reverse" for better live-reloading support')
.option('-l, --live-reload', 'Enable Live Reload')
Expand All @@ -246,7 +251,21 @@ export function runProgram(config: Config): void {
config,
async (
platform,
{ scheme, flavor, list, json, target, sync, forwardPorts, liveReload, host, port, configuration },
{
scheme,
flavor,
list,
json,
target,
targetName,
targetNameSdkVersion,
sync,
forwardPorts,
liveReload,
host,
port,
configuration,
},
) => {
const { runCommand } = await import('./tasks/run');
await runCommand(config, platform, {
Expand All @@ -255,6 +274,8 @@ export function runProgram(config: Config): void {
list,
json,
target,
targetName,
targetNameSdkVersion,
sync,
forwardPorts,
liveReload,
Expand Down
15 changes: 13 additions & 2 deletions cli/src/ios/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@ const debug = Debug('capacitor:ios:run');

export async function runIOS(
config: Config,
{ target: selectedTarget, scheme: selectedScheme, configuration: selectedConfiguration }: RunCommandOptions,
{
target: selectedTarget,
targetName: selectedTargetName,
targetNameSdkVersion: selectedTargetSdkVersion,
scheme: selectedScheme,
configuration: selectedConfiguration,
}: RunCommandOptions,
): Promise<void> {
const target = await promptForPlatformTarget(await getPlatformTargets('ios'), selectedTarget);
const target = await promptForPlatformTarget(
await getPlatformTargets('ios'),
selectedTarget ?? selectedTargetName,
selectedTargetSdkVersion,
selectedTargetName !== undefined,
);

const runScheme = selectedScheme || config.ios.scheme;
const configuration = selectedConfiguration || 'Debug';
Expand Down
2 changes: 2 additions & 0 deletions cli/src/tasks/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export interface RunCommandOptions {
list?: boolean;
json?: boolean;
target?: string;
targetName?: string;
targetNameSdkVersion?: string;
sync?: boolean;
forwardPorts?: string;
liveReload?: boolean;
Expand Down