-
Notifications
You must be signed in to change notification settings - Fork 4
/
adb.js
60 lines (45 loc) · 1.45 KB
/
adb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* eslint-disable import/no-commonjs */
const path = require("path");
const util = require("util");
async function adbExec(options) {
const exec = util.promisify(require("child_process").exec);
let adbBinary = "adb";
if (process.platform === "linux") {
adbBinary = path.resolve("linux", "adb");
} else if (process.platform === "win32") {
adbBinary = path.resolve("windows", "adb.exe");
}
adbBinary = "\"" + adbBinary + "\"";
const { stdout, stderr } = await exec(adbBinary + " " + options.cmd);
if (stderr) {
return stderr;
}
if (options.trim) {
return stdout.trim();
}
return stdout;
}
async function hasAdbConnectedDevice() {
const deviceOutput = await adbExec({ cmd: "devices" });
const outputLength = deviceOutput.split("\n").length;
return outputLength === 4;
}
async function loadProperties() {
const getprop = await adbExec({ cmd: "shell getprop" });
const lines = getprop.split("\n");
const properties = [];
lines.forEach((line) => {
try {
const prop = line.split(":")[0].trim();
const value = line.split(":")[1].trim();
if (prop.match(/\[(.*?)\]/) && value.match(/\[(.*?)\]/)) {
const propName = prop.match(/\[(.*?)\]/)[1];
properties[propName] = value.match(/\[(.*?)\]/)[1];
}
} catch (e) {
//term.bold.red("Unable to load property : " + line);
}
});
return properties;
}
module.exports = { adbExec, hasAdbConnectedDevice, loadProperties };