From 263a738972abd06a338a266cf429f5168f90900b Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Tue, 10 Sep 2019 10:34:31 -0700 Subject: [PATCH] Strictify adbClient Summary: As per title Reviewed By: jknoxville Differential Revision: D17284770 fbshipit-source-id: 85bc6b2b19b38f409b7eb0fda6fb94655a05c0d9 --- src/utils/adbClient.tsx | 28 ++++++++++++++++------------ types/adbkit-fb.d.tsx | 14 +++++++++++++- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/utils/adbClient.tsx b/src/utils/adbClient.tsx index 1a7300580..12f7f2d10 100644 --- a/src/utils/adbClient.tsx +++ b/src/utils/adbClient.tsx @@ -9,9 +9,10 @@ import {promisify} from 'util'; import child_process from 'child_process'; import promiseRetry from 'promise-retry'; 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; export function getAdbClient(): Promise { if (!instance) { @@ -23,11 +24,11 @@ export function getAdbClient(): Promise { /* 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 manually before requesting a client. */ -function createClient() { +function createClient(): Promise { const adbPath = process.env.ANDROID_HOME ? `${process.env.ANDROID_HOME}/platform-tools/adb` : 'adb'; - return reportPlatformFailures( + return reportPlatformFailures( promisify(child_process.exec)(`${adbPath} start-server`).then(() => adbkit.createClient(adbConfig()), ), @@ -39,26 +40,29 @@ function createClient() { ); /* In the event that starting adb with the above method fails, fallback - to using adbkit, though its known to be unreliable. */ - const unsafeClient = adbkit.createClient(adbConfig()); - return reportPlatformFailures( - promiseRetry( - (retry, number) => { + to using adbkit, though its known to be unreliable. */ + const unsafeClient: Client = adbkit.createClient(adbConfig()); + return reportPlatformFailures( + promiseRetry( + (retry, attempt): Promise => { return unsafeClient .listDevices() .then(() => { return unsafeClient; }) - .catch(e => { + .catch((e: Error) => { console.warn( `Failed to start adb client. Retrying. ${e.message}`, ); - retry(e); + if (attempt <= MAX_RETRIES) { + retry(e); + } + throw e; }); }, { minTimeout: 200, - retries: 5, + retries: MAX_RETRIES, }, ), 'createADBClient.adbkit', diff --git a/types/adbkit-fb.d.tsx b/types/adbkit-fb.d.tsx index e7fc98f12..4277f33b5 100644 --- a/types/adbkit-fb.d.tsx +++ b/types/adbkit-fb.d.tsx @@ -5,8 +5,20 @@ * @format */ +interface Device { + id: string; + type: 'emulator' | 'device' | 'offline'; +} + +interface Util { + readAll: (stream: NodeJS.ReadStream) => Promise; +} + declare module 'adbkit-fb' { const util: any; const adbkit: any; - export function createClient({port: number, host: string}): any; + export interface Client { + listDevices: () => Promise; + } + export function createClient(config: {port: number; host: string}): Client; }