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

@sviande/adb kill #2064

Merged
merged 2 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions packages/xdl/src/Android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import * as Versions from './Versions';
import { getUrlAsync as getWebpackUrlAsync } from './Webpack';

let _lastUrl: string | null = null;
let _isAdbOwner: boolean | null = null;
sviande marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down Expand Up @@ -116,10 +118,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 {
let result = await spawnAsync(adb, args);
return result.stdout;
Expand Down Expand Up @@ -623,6 +644,10 @@ See https://docs.expo.io/versions/latest/guides/splash-screens/#differences-betw
}

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