Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Add --forwardPorts option to Capacitor CLI #5645

Merged
merged 10 commits into from Mar 15, 2023
10 changes: 9 additions & 1 deletion cli/src/android/run.ts
Expand Up @@ -12,7 +12,11 @@ const debug = Debug('capacitor:android:run');

export async function runAndroid(
config: Config,
{ target: selectedTarget, flavor: selectedFlavor }: RunCommandOptions,
{
target: selectedTarget,
flavor: selectedFlavor,
forwardPorts: selectedPorts,
}: RunCommandOptions,
): Promise<void> {
const target = await promptForPlatformTarget(
await getPlatformTargets('android'),
Expand Down Expand Up @@ -52,6 +56,10 @@ export async function runAndroid(

const nativeRunArgs = ['android', '--app', apkPath, '--target', target.id];

if (selectedPorts) {
nativeRunArgs.push('--forward', `${selectedPorts}`);
}

debug('Invoking native-run with args: %O', nativeRunArgs);

await runTask(
Expand Down
11 changes: 9 additions & 2 deletions cli/src/index.ts
Expand Up @@ -182,7 +182,6 @@ export function runProgram(config: Config): void {
),
),
);

program
.command(`run [platform]`)
.description(
Expand All @@ -195,18 +194,26 @@ export function runProgram(config: Config): void {
.allowUnknownOption(true)
.option('--target <id>', 'use a specific target')
.option('--no-sync', `do not run ${c.input('sync')}`)
.option(
'--forwardPorts <port:port>',
'Automatically run "adb reverse" for better live-reloading support',
)
.action(
wrapAction(
telemetryAction(
config,
async (platform, { scheme, flavor, list, target, sync }) => {
async (
platform,
{ scheme, flavor, list, target, sync, forwardPorts },
) => {
const { runCommand } = await import('./tasks/run');
await runCommand(config, platform, {
scheme,
flavor,
list,
target,
sync,
forwardPorts,
});
},
),
Expand Down
1 change: 1 addition & 0 deletions cli/src/tasks/run.ts
Expand Up @@ -24,6 +24,7 @@ export interface RunCommandOptions {
list?: boolean;
target?: string;
sync?: boolean;
forwardPorts?: string;
}

export async function runCommand(
Expand Down