Skip to content

Commit

Permalink
feat: adb reverse interaction & run adb reverse by default (#824)
Browse files Browse the repository at this point in the history
* feat: ivnert the reverse port CLI option

* feat: run adb reverse by default

* refactor: log adb reverse only in debug, more verbose logging

* feat: add adb reverse interaction

* fix: run adb reverse on watchRun when using rspack

* test: align setupInteractions tests

* refactor: use lowercase adb

* chore: add changeset

* refactor: run when bundling for Android

* refactor: undo cli flag changes
  • Loading branch information
jbroma authored Dec 16, 2024
1 parent 312108e commit 8cf7cc3
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-carrots-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": minor
---

Added `adb reverse` interaction & `adb reverse` command is now run by default when bundling for Android
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ describe('setupInteractions', () => {
onOpenDevTools: debuggerSupport ? jest.fn() : undefined,
onOpenDevMenu() {},
onReload() {},
onAdbReverse() {},
},
{
logger: mockLogger,
Expand All @@ -261,6 +262,10 @@ describe('setupInteractions', () => {
);
expect(mockProcess.stdout.write).toHaveBeenNthCalledWith(
4,
' a: Run adb reverse\n'
);
expect(mockProcess.stdout.write).toHaveBeenNthCalledWith(
5,
'\nPress Ctrl+c or Ctrl+z to quit the dev server\n\n'
);
});
Expand Down
25 changes: 21 additions & 4 deletions packages/repack/src/commands/common/runAdbReverse.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import execa from 'execa';
import type { Logger } from '../../types';

export async function runAdbReverse(port: number, logger: Logger = console) {
interface RunAdbReverseParams {
port: number;
verbose?: boolean;
logger?: Logger;
}

export async function runAdbReverse({
port,
verbose = false,
logger = console,
}: 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);
logger.info(`Successfully run: ${command}`);
if (verbose) {
logger.info('adb reverse success');
}
logger.debug(`adb reverse success: ${info}`);
} catch (error) {
// Get just the error message
const message =
(error as Error).message.split('error:')[1] || (error as Error).message;
logger.warn(`Failed to run: ${command} - ${message.trim()}`);
if (verbose) {
logger.warn(`adb reverse failed: "${message.trim()}"`);
}
logger.debug(`adb reverse failed: "${message.trim()}" ${info}`);
}
}
6 changes: 6 additions & 0 deletions packages/repack/src/commands/common/setupInteractions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function setupInteractions(
onReload?: () => void;
onOpenDevMenu?: () => void;
onOpenDevTools?: () => void;
onAdbReverse?: () => void;
},
options?: {
logger?: Logger;
Expand Down Expand Up @@ -92,6 +93,11 @@ export function setupInteractions(
postPerformMessage: 'Opening debugger',
helpName: 'Open debugger',
},
a: {
action: handlers.onAdbReverse,
postPerformMessage: 'Running adb reverse',
helpName: 'Run adb reverse',
},
};

// use process.stdout for sync output at startup
Expand Down
13 changes: 12 additions & 1 deletion packages/repack/src/commands/rspack/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import type {
import memfs from 'memfs';
import type { Reporter } from '../../logging';
import type { HMRMessageBody } from '../../types';
import { adaptFilenameToPlatform, getEnvOptions, loadConfig } from '../common';
import {
adaptFilenameToPlatform,
getEnvOptions,
loadConfig,
runAdbReverse,
} from '../common';
import { DEV_SERVER_ASSET_TYPES } from '../consts';
import type { StartCliOptions } from '../types';
import type { CompilerAsset, MultiWatching } from './types';
Expand Down Expand Up @@ -73,6 +78,12 @@ export class Compiler {
this.compiler.hooks.watchRun.tap('repack:watch', () => {
this.isCompilationInProgress = true;
this.platforms.forEach((platform) => {
if (platform === 'android') {
void runAdbReverse({
port: this.cliOptions.arguments.start.port!,
logger: this.devServerContext.log,
});
}
this.devServerContext.notifyBuildStart(platform);
});
});
Expand Down
31 changes: 23 additions & 8 deletions packages/repack/src/commands/rspack/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ export async function start(
args.config ?? args.webpackConfig
);
const { reversePort, ...restArgs } = args;

const serverProtocol = args.https ? 'https' : 'http';
const serverHost = args.host || DEFAULT_HOSTNAME;
const serverPort = args.port ?? DEFAULT_PORT;
const serverURL = `${serverProtocol}://${serverHost}:${serverPort}`;
const showHttpRequests = args.verbose || args.logRequests;

const cliOptions: StartCliOptions = {
config: {
root: cliConfig.root,
Expand All @@ -50,7 +57,9 @@ export async function start(
reactNativePath: cliConfig.reactNativePath,
},
command: 'start',
arguments: { start: { ...restArgs } },
arguments: {
start: { ...restArgs, host: serverHost, port: serverPort },
},
};

if (args.platform && !cliOptions.config.platforms.includes(args.platform)) {
Expand All @@ -76,11 +85,6 @@ export async function start(
// @ts-ignore
const compiler = new Compiler(cliOptions, reporter);

const serverHost = args.host || DEFAULT_HOSTNAME;
const serverPort = args.port ?? DEFAULT_PORT;
const serverURL = `${args.https === true ? 'https' : 'http'}://${serverHost}:${serverPort}`;
const showHttpRequests = args.verbose || args.logRequests;

const { createServer } = await import('@callstack/repack-dev-server');
const { start, stop } = await createServer({
options: {
Expand Down Expand Up @@ -110,13 +114,24 @@ export async function start(
method: 'POST',
});
},
onAdbReverse() {
void runAdbReverse({
port: serverPort,
logger: ctx.log,
verbose: true,
});
},
},
{ logger: ctx.log }
);
}

if (reversePort && args.port) {
void runAdbReverse(args.port, ctx.log);
if (
reversePort ||
args.platform === undefined ||
args.platform === 'android'
) {
void runAdbReverse({ port: serverPort, logger: ctx.log });
}

compiler.setDevServerContext(ctx);
Expand Down
33 changes: 24 additions & 9 deletions packages/repack/src/commands/webpack/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export async function start(_: string[], config: Config, args: StartArguments) {
args.config ?? args.webpackConfig
);
const { reversePort, ...restArgs } = args;

const serverProtocol = args.https ? 'https' : 'http';
const serverHost = args.host || DEFAULT_HOSTNAME;
const serverPort = args.port ?? DEFAULT_PORT;
const serverURL = `${serverProtocol}://${serverHost}:${serverPort}`;
const showHttpRequests = args.verbose || args.logRequests;

const cliOptions: StartCliOptions = {
config: {
root: config.root,
Expand All @@ -49,7 +56,9 @@ export async function start(_: string[], config: Config, args: StartArguments) {
reactNativePath: config.reactNativePath,
},
command: 'start',
arguments: { start: { ...restArgs } },
arguments: {
start: { ...restArgs, host: serverHost, port: serverPort },
},
};

if (args.platform && !cliOptions.config.platforms.includes(args.platform)) {
Expand All @@ -74,11 +83,6 @@ export async function start(_: string[], config: Config, args: StartArguments) {

const compiler = new Compiler(cliOptions, reporter);

const serverHost = args.host || DEFAULT_HOSTNAME;
const serverPort = args.port ?? DEFAULT_PORT;
const serverURL = `${args.https === true ? 'https' : 'http'}://${serverHost}:${serverPort}`;
const showHttpRequests = args.verbose || args.logRequests;

const { createServer } = await import('@callstack/repack-dev-server');
const { start, stop } = await createServer({
options: {
Expand Down Expand Up @@ -108,21 +112,32 @@ export async function start(_: string[], config: Config, args: StartArguments) {
method: 'POST',
});
},
onAdbReverse() {
void runAdbReverse({
port: serverPort,
logger: ctx.log,
verbose: true,
});
},
},
{ logger: ctx.log }
);
}

if (reversePort && args.port) {
void runAdbReverse(args.port, ctx.log);
if (
reversePort ||
args.platform === undefined ||
args.platform === 'android'
) {
void runAdbReverse({ port: serverPort, logger: ctx.log });
}

const lastStats: Record<string, webpack.StatsCompilation> = {};

compiler.on('watchRun', ({ platform }) => {
ctx.notifyBuildStart(platform);
if (platform === 'android') {
void runAdbReverse(args.port ?? DEFAULT_PORT, ctx.log);
void runAdbReverse({ port: serverPort, logger: ctx.log });
}
});

Expand Down

0 comments on commit 8cf7cc3

Please sign in to comment.