-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfastboot.js
68 lines (53 loc) · 1.54 KB
/
fastboot.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
61
62
63
64
65
66
67
68
/* eslint-disable import/no-commonjs */
const path = require("path");
const util = require("util");
async function fastbootExec(options) {
const exec = util.promisify(require("child_process").exec);
let fastbootBinary = "fastboot";
if (process.platform === "linux") {
fastbootBinary = path.resolve("linux", "fastboot");
} else if (process.platform === "win32") {
fastbootBinary = path.resolve("windows", "fastboot.exe");
}
fastbootBinary = "\"" + fastbootBinary + "\"";
const { stdout, stderr } = await exec(fastbootBinary + " " + options.cmd);
if (stderr) {
if (options.trim) {
return stderr.split("\n")[0].trim();
}
return stderr;
}
if (options.trim) {
return stdout.split("\n")[0].trim();
}
return stdout;
}
async function isUserspace() {
const userpace = await fastbootExec({
cmd: "getvar is-userspace",
trim: true,
});
return userpace.includes("yes");
}
async function loadVariables() {
const variables = await fastbootExec({
cmd: "getvar all"
});
const lines = variables.split("\n");
const properties = [];
lines.forEach((line) => {
try {
line = line.replace("(bootloader) ", "");
const variable = line.split(":")[0].trim();
const value = line.split(":")[1].trim();
if (variable && value) {
const propName = variable;
properties[propName] = value;
}
} catch (e) {
//term.bold.red("Unable to load property : " + line);
}
});
return properties;
}
module.exports = { fastbootExec, isUserspace, loadVariables };