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: wait for device before adb reverse #828

Merged
merged 9 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions .changeset/khaki-days-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": minor
---

Run adb reverse for all available devices by default
5 changes: 5 additions & 0 deletions .changeset/odd-teachers-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": minor
---

Wait for android device before running adb reverse when starting dev-server
4 changes: 2 additions & 2 deletions apps/tester-app/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- boost (1.84.0)
- callstack-repack (5.0.0-rc.2):
- callstack-repack (5.0.0-rc.3):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -1942,7 +1942,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
boost: 1dca942403ed9342f98334bf4c3621f011aa7946
callstack-repack: 3106db24c24f7a76a380230ff7794d225cecb760
callstack-repack: 5219eedfb8cb06b905edecffaecf71af4a4ecdd6
DoubleConversion: f16ae600a246532c4020132d54af21d0ddb2a385
FBLazyVector: be7314029d6ec6b90f0f75ce1195b8130ed9ac4f
fmt: 10c6e61f4be25dc963c36bd73fc7b1705fe975be
Expand Down
2 changes: 1 addition & 1 deletion apps/tester-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"get-port": "^6.1.2",
"globby": "^13.1.2",
"http-server": "^14.1.1",
"react-native-test-app": "^4.0.4",
"react-native-test-app": "^4.0.7",
"terser-webpack-plugin": "^5.3.10",
"typescript": "^5.5.3",
"vitest": "^2.0.5",
Expand Down
4 changes: 2 additions & 2 deletions apps/tester-federation-v2/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- boost (1.84.0)
- callstack-repack (5.0.0-rc.2):
- callstack-repack (5.0.0-rc.3):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -1895,7 +1895,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
boost: 1dca942403ed9342f98334bf4c3621f011aa7946
callstack-repack: 3106db24c24f7a76a380230ff7794d225cecb760
callstack-repack: 5219eedfb8cb06b905edecffaecf71af4a4ecdd6
DoubleConversion: f16ae600a246532c4020132d54af21d0ddb2a385
FBLazyVector: be7314029d6ec6b90f0f75ce1195b8130ed9ac4f
fmt: 10c6e61f4be25dc963c36bd73fc7b1705fe975be
Expand Down
2 changes: 1 addition & 1 deletion apps/tester-federation-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"get-port": "^6.1.2",
"globby": "^13.1.2",
"http-server": "^14.1.1",
"react-native-test-app": "^4.0.4",
"react-native-test-app": "^4.0.7",
"terser-webpack-plugin": "^5.3.3",
"typescript": "^5.5.3",
"webpack": "^5.91.0"
Expand Down
4 changes: 2 additions & 2 deletions apps/tester-federation/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- boost (1.84.0)
- callstack-repack (5.0.0-rc.2):
- callstack-repack (5.0.0-rc.3):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -1919,7 +1919,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
boost: 1dca942403ed9342f98334bf4c3621f011aa7946
callstack-repack: 3106db24c24f7a76a380230ff7794d225cecb760
callstack-repack: 5219eedfb8cb06b905edecffaecf71af4a4ecdd6
DoubleConversion: f16ae600a246532c4020132d54af21d0ddb2a385
FBLazyVector: be7314029d6ec6b90f0f75ce1195b8130ed9ac4f
fmt: 10c6e61f4be25dc963c36bd73fc7b1705fe975be
Expand Down
2 changes: 1 addition & 1 deletion apps/tester-federation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"get-port": "^6.1.2",
"globby": "^13.1.2",
"http-server": "^14.1.1",
"react-native-test-app": "^4.0.4",
"react-native-test-app": "^4.0.7",
"typescript": "^5.5.3"
}
}
93 changes: 78 additions & 15 deletions packages/repack/src/commands/common/runAdbReverse.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,98 @@
import path from 'node:path';
import execa from 'execa';
import type { Logger } from '../../types';

interface RunAdbReverseParams {
logger?: Logger;
port: number;
verbose?: boolean;
logger?: Logger;
wait?: boolean;
}

function getAdbPath() {
const androidHome = process.env.ANDROID_HOME;
return androidHome ? path.join(androidHome, 'platform-tools', 'adb') : 'adb';
}

function parseAdbError(error: unknown) {
const errorMessage = (error as Error).message;
const message = errorMessage.includes('error:')
? errorMessage.split('error:')[1]
: errorMessage;
return message.trim();
}

async function executeAdbCommand(command: string, logger: Logger) {
const adbPath = getAdbPath();
try {
const result = await execa.command(`${adbPath} ${command}`);
logger.debug(`[ADB] "adb ${command}" executed successfully.`);
return result;
} catch (error) {
const message = parseAdbError(error);
logger.debug(`[ADB] "adb ${command}" failed: "${message}"`);
throw new Error(message);
}
}

async function waitForDevice(logger: Logger) {
try {
await executeAdbCommand('wait-for-device', logger);
} catch (error) {
const message = (error as Error).message;
if (/more than one device\/emulator/.test(message)) {
return;
jbroma marked this conversation as resolved.
Show resolved Hide resolved
}
throw error;
}
}

async function reversePort(port: number, device: string, logger: Logger) {
await executeAdbCommand(
`-s ${device} reverse tcp:${port} tcp:${port}`,
logger
);
}

async function getDevices(logger: Logger): Promise<string[]> {
const { stdout } = await executeAdbCommand('devices', logger);
const devices = stdout
.split('\n')
.slice(1)
.map((line) => line.split('\t')[0])
.filter(Boolean);
logger.debug(`[ADB] Found ${devices.length} devices/emulators.`);
return devices;
}

/**
* Runs the adb reverse command to reverse the specified port on all available devices.
* Performs the following steps:
* 1. (Optional) Waits for the device to be available.
* 2. Get a list of all connected devices.
* 3. Attempts to reverse the specified port using adb for all devices.
*/
export async function runAdbReverse({
logger = console,
port,
verbose = false,
logger = console,
wait = false,
}: RunAdbReverseParams) {
const adbPath = process.env.ANDROID_HOME
? `${process.env.ANDROID_HOME}/platform-tools/adb`
: 'adb';
const command = `${adbPath} reverse tcp:${port} tcp:${port}`;
const info = JSON.stringify({ port, adbPath, command });

try {
await execa.command(command);
if (wait) {
await waitForDevice(logger);
}
const devices = await getDevices(logger);
for (const device of devices) {
await reversePort(port, device, logger);
}
if (verbose) {
logger.info('adb reverse success');
logger.info('[ADB] port reverse success');
}
logger.debug(`adb reverse success: ${info}`);
} catch (error) {
const message =
(error as Error).message.split('error:')[1] || (error as Error).message;
const message = (error as Error).message;
if (verbose) {
logger.warn(`adb reverse failed: "${message.trim()}"`);
logger.warn(`[ADB] port reverse failed: "${message}"`);
}
logger.debug(`adb reverse failed: "${message.trim()}" ${info}`);
}
}
2 changes: 1 addition & 1 deletion packages/repack/src/commands/rspack/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export async function start(
}

if (reversePort) {
void runAdbReverse({ port: serverPort, logger: ctx.log });
void runAdbReverse({ logger: ctx.log, port: serverPort, wait: true });
}

compiler.setDevServerContext(ctx);
Expand Down
2 changes: 1 addition & 1 deletion packages/repack/src/commands/webpack/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export async function start(_: string[], config: Config, args: StartArguments) {
}

if (reversePort) {
void runAdbReverse({ port: serverPort, logger: ctx.log });
void runAdbReverse({ logger: ctx.log, port: serverPort, wait: true });
}

const lastStats: Record<string, webpack.StatsCompilation> = {};
Expand Down
26 changes: 13 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading