forked from ubports/ubports-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·129 lines (115 loc) · 3.87 KB
/
cli.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env node
/*
This tool is meant to be used for testing or for being wrapped by other scripts
and programs. It is not meant to be a user-facing cli installation tool and
does *not* try to be user-friendly.
Author: Marius Gripsgard <[email protected]>
*/
const cli = require("commander");
const devices = require("./src/devices");
const adb = require("./src/adb");
const utils = require("./src/utils");
const systemImage = require("./src/system-image");
const package = require("./package.json")
const defaultChannel = "15.04/stable";
process.env.NO_GUI = 1;
const panic = (m) => {
console.log(m);
process.exit(1)
}
const setEvents = (downloadEvent) => {
downloadEvent.on("download:done", () => {
console.log("Download complete");
});
downloadEvent.on("download:error", (r) => {
console.log("Download error " + r);
});
downloadEvent.on("error", (r) => {
console.log("Error: " + r);
});
downloadEvent.on("download:checking", () => {
console.log("Download checking file");
});
downloadEvent.on("download:startCheck", () => {
utils.log.info("Download startCheck");
});
downloadEvent.on("download:start", (r) => {
console.log("Starting download of " + r + " files");
});
downloadEvent.on("download:next", (i) => {
console.log(`Downloading next file, ${i} left`);
});
downloadEvent.on("download:progress", (i) => {
process.stdout.write(`Downloading file, ${Math.ceil(i.percent*100)}% complete\r`);
});
downloadEvent.on("adbpush:done", () => {
console.log("Done pushing files");
console.log("Rebooting to recovery to flash");
adb.reboot("recovery", () => {});
});
downloadEvent.on("adbpush:error", (e) => {
console.log("Adb push error", e)
});
downloadEvent.on("adbpush:progress", (r) => {
process.stdout.write("ADB push, " + r + "%\r")
});
downloadEvent.on("adbpush:next", (r) => {
console.log("Start pushing next file, " + r + " files left")
});
downloadEvent.on("adbpush:start", (r) => {
console.log("Start pushing " + r + " files")
});
downloadEvent.on("user:reboot", (r) => {
console.log("Please reboot your device to", r.state);
});
downloadEvent.on("bootstrap:flashing", (r) => {
console.log("Flashing images");
});
}
const install = (device, ownEvent, eventSet) => {
console.log(`Installing on ${device}`);
console.log(`Using channel ${channel}`);
var downloadEvent = systemImage.installLatestVersion({
device: device,
channel: channel,
event: ownEvent
});
if (!eventSet)
setEvents(downloadEvent);
}
var getDevice = (callback) => {
adb.hasAdbAccess((adbAccess) => {
if (!adbAccess) {
if (!cli.bootstrap)
panic("I do not have adb access");
if (!cli.device)
panic("Cannot detect device, please use --device <device>");
}
if (!cli.device) {
adb.getDeviceName((device) => {
callback(device);
})
} else {
callback(cli.device);
}
})
}
var bootstrap = (device) => {
console.log(`Bootstrapping on ${device}`);
console.log(`Using channel ${channel}`);
var installEvent = devices.install(device, channel, true);
setEvents(installEvent);
}
cli
.version(package.version)
.option('-d, --device <device>', 'Specify device')
.option('-c, --channel <channel>', 'Specify channel (default: 15.04/stable)')
.option('-v, --verbose', "Verbose output")
.option('-b, --bootstrap', "Flash boot and recovery from bootloader")
.parse(process.argv);
var channel = cli.channel || defaultChannel;
if (cli.verbose) process.env.DEBUG = 1;
if (cli.bootstrap) {
getDevice(bootstrap)
utils.ensureRoot("Bootstrap requires root");
} else getDevice(install)