Skip to content

Commit 66193a9

Browse files
committed
use ANDROID_HOME and ANDROID_SDK_ROOT
1 parent 2958eb5 commit 66193a9

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/adb.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,47 @@
11
const { spawn } = require("child_process")
2-
const { gray } = require("kleur")
2+
const { gray, bold } = require("kleur")
33
const { getDevice } = require("./getDevice")
4-
const { verbose, formatSpawnArgs } = require("./log")
4+
const { verbose, formatSpawnArgs, fail } = require("./log")
55
const { spawnSafeSync } = require("./spawnSafeSync")
6+
const { join } = require("path")
67

78
let device = ""
9+
let adbPath = "adb"
810

911
module.exports.adb = (/** @type {string[]} */ ...args) =>
10-
spawnSafeSync("adb", ["-s", device, ...args])
12+
spawnSafeSync(adbPath, ["-s", device, ...args])
1113

1214
module.exports.adbAsync = (/** @type {string[]} */ ...args) => {
1315
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
1537
}
1638

1739
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+
}
1846
device = await getDevice()
1947
}

src/getDevice.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ const { selectOption } = require("./selectOption")
55
const { spawnSafeSync } = require("./spawnSafeSync")
66

77
module.exports.getDevice = async () => {
8-
const res = spawnSafeSync("which", ["adb"], { failOnError: false })
9-
if (res.status !== 0) {
10-
fail(
11-
`${bold("adb")} not found`,
12-
"Please install and configure the android dev tools.",
13-
)
14-
}
15-
168
const result = spawnSafeSync("adb", ["devices", "-l"]).stdout.toString()
179
const deviceLines = result
1810
.replace("List of devices attached", "")

0 commit comments

Comments
 (0)