Fix adb path check on Windows (#3434)

Summary:
The adb path check seem to be platform dependent

Instead of pre-validating the path we can execute the intended command and retry with an alternative adb path only if it fails with file not found error

Related to https://github.com/facebook/flipper/issues/3430 (adb is not detected)

## Changelog

Fix ADB detection and startup on Windows

Pull Request resolved: https://github.com/facebook/flipper/pull/3434

Test Plan:
#### adb is detected successfully when `adb.exe` is in `platform-tools`
![image](https://user-images.githubusercontent.com/12156624/153605414-b85c1aef-1e60-457d-be90-3673900d7cc5.png)

#### Falling back to alternative path when `adb.exe` is the SDK folder
![image](https://user-images.githubusercontent.com/12156624/153605693-0adb9c1b-e77f-44a1-9d2c-00cf40f59645.png)

#### Otherwise failing if `adb.exe` is not found
![image](https://user-images.githubusercontent.com/12156624/153605797-307304d3-e513-459a-9e28-53d95ba94642.png)

Reviewed By: passy

Differential Revision: D34240518

Pulled By: aigoncharov

fbshipit-source-id: db834bbdc9815e5ad41f7a1329ec8d5869f6f24b
This commit is contained in:
Peter Velkov
2022-02-16 02:58:18 -08:00
committed by Facebook GitHub Bot
parent e52c0dfa78
commit 9d2f11f505

View File

@@ -12,7 +12,6 @@ import {execFile} from 'promisify-child-process';
import adbConfig from './adbConfig'; import adbConfig from './adbConfig';
import adbkit, {Client} from 'adbkit'; import adbkit, {Client} from 'adbkit';
import path from 'path'; import path from 'path';
import {pathExists} from 'fs-extra';
type Config = { type Config = {
androidHome: string; androidHome: string;
@@ -37,16 +36,24 @@ export async function initializeAdbClient(
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. */
async function createClient(config: Config): Promise<Client> { async function createClient(config: Config): Promise<Client> {
const androidHome = config.androidHome;
let adbPath = path.resolve(androidHome, 'platform-tools', 'adb');
if (!(await pathExists(adbPath))) {
console.info('falling back to the alternative adb path');
adbPath = path.resolve(androidHome, 'adb');
}
return reportPlatformFailures<Client>( return reportPlatformFailures<Client>(
execFile(adbPath, ['start-server']).then(() => startAdbServer(config.androidHome).then(() =>
adbkit.createClient(adbConfig()), adbkit.createClient(adbConfig()),
), ),
'createADBClient.shell', 'createADBClient.shell',
); );
} }
async function startAdbServer(androidHome: string) {
const adbPath = path.resolve(androidHome, 'platform-tools', 'adb');
const args = ['start-server'];
return execFile(adbPath, args).catch((error) => {
if (error.code == 'ENOENT') {
console.info('falling back to the alternative adb path');
return execFile(path.resolve(androidHome, 'adb'), args);
}
throw error;
});
}