Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
@sviande/adb kill (#2064)
Browse files Browse the repository at this point in the history
* Fix: ADB kill fix ordering

* fix: stop ABD only when expo launched it.
  • Loading branch information
sviande authored Jul 24, 2020
1 parent 0435457 commit 398af44
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
25 changes: 25 additions & 0 deletions packages/xdl/src/Android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { getUrlAsync as getWebpackUrlAsync } from './Webpack';
import { getImageDimensionsAsync } from './tools/ImageUtils';

let _lastUrl: string | null = null;
let _isAdbOwner: boolean | null = null;

const BEGINNING_OF_ADB_ERROR_MESSAGE = 'error: ';
const CANT_START_ACTIVITY_ERROR = 'Activity not started, unable to resolve Intent';

Expand Down Expand Up @@ -115,10 +117,29 @@ export function isPlatformSupported(): boolean {
);
}

async function adbAlreadyRunning(adb: string): Promise<boolean> {
try {
let result = await spawnAsync(adb, ['start-server']);
const lines = _.trim(result.stderr).split(/\r?\n/);
return lines.includes('* daemon started successfully') === false;
} catch (e) {
let errorMessage = _.trim(e.stderr || e.stdout);
if (errorMessage.startsWith(BEGINNING_OF_ADB_ERROR_MESSAGE)) {
errorMessage = errorMessage.substring(BEGINNING_OF_ADB_ERROR_MESSAGE.length);
}
throw new Error(errorMessage);
}
}

export async function getAdbOutputAsync(args: string[]): Promise<string> {
await Binaries.addToPathAsync('adb');
const adb = whichADB();

if (_isAdbOwner === null) {
const alreadyRunning = await adbAlreadyRunning(adb);
_isAdbOwner = alreadyRunning === false;
}

try {
const result = await spawnAsync(adb, args);
return result.stdout;
Expand Down Expand Up @@ -626,6 +647,10 @@ See https://docs.expo.io/guides/splash-screens/#splash-screen-api-limitations-on
}

export async function maybeStopAdbDaemonAsync() {
if (_isAdbOwner !== true) {
return false;
}

try {
await getAdbOutputAsync(['kill-server']);
return true;
Expand Down
3 changes: 2 additions & 1 deletion packages/xdl/src/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2416,14 +2416,15 @@ async function _stopInternalAsync(projectRoot: string): Promise<void> {
await stopExpoServerAsync(projectRoot);
ProjectUtils.logInfo(projectRoot, 'expo', '\u203A Stopping Metro bundler');
await stopReactNativeServerAsync(projectRoot);
await Android.maybeStopAdbDaemonAsync();
if (!Config.offline) {
try {
await stopTunnelsAsync(projectRoot);
} catch (e) {
ProjectUtils.logDebug(projectRoot, 'expo', `Error stopping ngrok ${e.message}`);
}
}

await Android.maybeStopAdbDaemonAsync();
}

export async function stopWebOnlyAsync(projectDir: string): Promise<void> {
Expand Down

0 comments on commit 398af44

Please sign in to comment.