-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wait for device before adb reverse (#828)
* chore: update Podfile locks * feat: wait-for-device before adb * feat: wait for device in start commands * chore: upgrade rnta * chore: add changeset * refactor: use path.join instead of path.resolve * feat: reverse on all available devices * chore: add 2nd changeset * chore: add comment about error case in wait-for-device
- Loading branch information
Showing
12 changed files
with
114 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,100 @@ | ||
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; | ||
// Ignore the error if there are multiple devices/emulators | ||
// we only care about about at least 1 device being online | ||
if (/more than one device\/emulator/.test(message)) { | ||
return; | ||
} | ||
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}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.