|
1 | 1 | const { spawn } = require("child_process") |
2 | | -const { gray } = require("kleur") |
| 2 | +const { gray, bold } = require("kleur") |
3 | 3 | const { getDevice } = require("./getDevice") |
4 | | -const { verbose, formatSpawnArgs } = require("./log") |
| 4 | +const { verbose, formatSpawnArgs, fail } = require("./log") |
5 | 5 | const { spawnSafeSync } = require("./spawnSafeSync") |
| 6 | +const { join } = require("path") |
6 | 7 |
|
7 | 8 | let device = "" |
| 9 | +let adbPath = "adb" |
8 | 10 |
|
9 | 11 | module.exports.adb = (/** @type {string[]} */ ...args) => |
10 | | - spawnSafeSync("adb", ["-s", device, ...args]) |
| 12 | + spawnSafeSync(adbPath, ["-s", device, ...args]) |
11 | 13 |
|
12 | 14 | module.exports.adbAsync = (/** @type {string[]} */ ...args) => { |
13 | 15 | verbose(gray("$"), "adb", formatSpawnArgs(["-s", device, ...args])) |
14 | | - return spawn("adb", ["-s", device, ...args]) |
| 16 | + return spawn(adbPath, ["-s", device, ...args]) |
| 17 | +} |
| 18 | + |
| 19 | +function setupAdbPath() { |
| 20 | + const adbs = [ |
| 21 | + process.env["ANDROID_HOME"] || "", |
| 22 | + process.env["ANDROID_SDK_ROOT"] || "", |
| 23 | + ] |
| 24 | + .filter(Boolean) |
| 25 | + .map((dir) => join(dir, "adb")) |
| 26 | + .concat(["adb"]) |
| 27 | + for (const _adbPath of adbs) { |
| 28 | + verbose("Trying", bold("adb"), "at path", bold(_adbPath)) |
| 29 | + adbPath = adbPath |
| 30 | + const result = spawnSafeSync(adbPath, ["devices"]) |
| 31 | + if (!result.error && result.status === 0) { |
| 32 | + verbose("It worked!") |
| 33 | + return true |
| 34 | + } |
| 35 | + } |
| 36 | + return false |
15 | 37 | } |
16 | 38 |
|
17 | 39 | module.exports.init = async () => { |
| 40 | + if (!setupAdbPath()) { |
| 41 | + fail( |
| 42 | + `${bold("adb")} not found`, |
| 43 | + "Please install and configure the android dev tools.", |
| 44 | + ) |
| 45 | + } |
18 | 46 | device = await getDevice() |
19 | 47 | } |
0 commit comments