Strictify adbClient

Summary: As per title

Reviewed By: jknoxville

Differential Revision: D17284770

fbshipit-source-id: 85bc6b2b19b38f409b7eb0fda6fb94655a05c0d9
This commit is contained in:
Pritesh Nandgaonkar
2019-09-10 10:34:31 -07:00
committed by Facebook Github Bot
parent ae825db691
commit 263a738972
2 changed files with 29 additions and 13 deletions

View File

@@ -9,9 +9,10 @@ import {promisify} from 'util';
import child_process from 'child_process'; import child_process from 'child_process';
import promiseRetry from 'promise-retry'; import promiseRetry from 'promise-retry';
import adbConfig from '../utils/adbConfig'; import adbConfig from '../utils/adbConfig';
import adbkit from 'adbkit-fb'; import adbkit, {Client} from 'adbkit-fb';
let instance; const MAX_RETRIES = 5;
let instance: Promise<Client>;
export function getAdbClient(): Promise<any> { export function getAdbClient(): Promise<any> {
if (!instance) { if (!instance) {
@@ -23,11 +24,11 @@ export function getAdbClient(): Promise<any> {
/* Adbkit will attempt to start the adb server if it's not already running, /* Adbkit will attempt to start the adb server if it's not already running,
however, it sometimes fails with ENOENT errors. So instead, we start it however, it sometimes fails with ENOENT errors. So instead, we start it
manually before requesting a client. */ manually before requesting a client. */
function createClient() { function createClient(): Promise<Client> {
const adbPath = process.env.ANDROID_HOME const adbPath = process.env.ANDROID_HOME
? `${process.env.ANDROID_HOME}/platform-tools/adb` ? `${process.env.ANDROID_HOME}/platform-tools/adb`
: 'adb'; : 'adb';
return reportPlatformFailures( return reportPlatformFailures<Client>(
promisify(child_process.exec)(`${adbPath} start-server`).then(() => promisify(child_process.exec)(`${adbPath} start-server`).then(() =>
adbkit.createClient(adbConfig()), adbkit.createClient(adbConfig()),
), ),
@@ -39,26 +40,29 @@ function createClient() {
); );
/* In the event that starting adb with the above method fails, fallback /* In the event that starting adb with the above method fails, fallback
to using adbkit, though its known to be unreliable. */ to using adbkit, though its known to be unreliable. */
const unsafeClient = adbkit.createClient(adbConfig()); const unsafeClient: Client = adbkit.createClient(adbConfig());
return reportPlatformFailures( return reportPlatformFailures<Client>(
promiseRetry( promiseRetry<Client>(
(retry, number) => { (retry, attempt): Promise<Client> => {
return unsafeClient return unsafeClient
.listDevices() .listDevices()
.then(() => { .then(() => {
return unsafeClient; return unsafeClient;
}) })
.catch(e => { .catch((e: Error) => {
console.warn( console.warn(
`Failed to start adb client. Retrying. ${e.message}`, `Failed to start adb client. Retrying. ${e.message}`,
); );
retry(e); if (attempt <= MAX_RETRIES) {
retry(e);
}
throw e;
}); });
}, },
{ {
minTimeout: 200, minTimeout: 200,
retries: 5, retries: MAX_RETRIES,
}, },
), ),
'createADBClient.adbkit', 'createADBClient.adbkit',

View File

@@ -5,8 +5,20 @@
* @format * @format
*/ */
interface Device {
id: string;
type: 'emulator' | 'device' | 'offline';
}
interface Util {
readAll: (stream: NodeJS.ReadStream) => Promise<Buffer>;
}
declare module 'adbkit-fb' { declare module 'adbkit-fb' {
const util: any; const util: any;
const adbkit: any; const adbkit: any;
export function createClient({port: number, host: string}): any; export interface Client {
listDevices: () => Promise<Device[]>;
}
export function createClient(config: {port: number; host: string}): Client;
} }