-
Notifications
You must be signed in to change notification settings - Fork 4
/
actions.js
97 lines (82 loc) · 2.5 KB
/
actions.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
/* eslint-disable import/no-commonjs */
const colors = require("colors");
const inquirer = require("inquirer");
const flash_firmware = require("./flash");
const util = require("util");
const { Separator } = require("inquirer");
const readdir = util.promisify(require("fs").readdir);
async function flash() {
const firmwareContent = await readdir("firmware", { withFileTypes: true });
const dirs = firmwareContent.filter((dir) => dir.isDirectory());
const files = firmwareContent.filter((file) => file.isFile());
if (files.length === 0 && dirs.length === 0) {
console.log("\nNo firmwares found in the firmware folder".brightRed);
console.log(
"Put one manually or use Check firmwares from main menu\n".brightRed
);
require("./prompt").show_menu(true);
return;
}
const menu = {
type: "list",
message: "Select a file/folder",
name: "firmware",
pageSize: 500,
choices: [],
};
if (dirs.length > 0) {
menu.choices.push(new Separator("Folders"));
dirs.forEach((dir) => {
menu.choices.push({
name: dir.name,
});
});
}
if (files.length > 0) {
menu.choices.push(new Separator("Files"));
files.forEach((file) => {
menu.choices.push({
name: file.name,
});
});
}
menu.choices.push(new Separator());
menu.choices.push({
name: "Back to main menu\n",
value: "main_menu",
});
inquirer.prompt([menu]).then(async (answers) => {
if (answers.firmware === "main_menu") {
require("./prompt").show_menu(true);
} else if (answers.firmware.includes(".zip")) {
const extractMenu = {
type: "confirm",
message: "Extract only ?",
name: "extract",
pageSize: 500,
};
inquirer.prompt([extractMenu]).then((extract) => {
flash_firmware(answers.firmware, extract.extract);
});
} else {
flash_firmware(answers.firmware, false);
}
});
}
async function check_firmware(sku, carrier) {
const { show_menu, show_firmwares } = require("./prompt");
const firmware_request = require("./firmware_check");
if (!sku || !carrier) {
console.log("Unable to check for firmwares. Missing SKU and/or carrier.".brightRed);
process.exit(0);
}
firmware_request("sku", sku, carrier, (firmwares) => {
if (firmwares && firmwares.length > 0) {
show_firmwares(firmwares, sku, carrier);
} else {
console.log("No firmwares found.");
show_menu(true, sku, carrier);
}
});
}
module.exports = { flash, check_firmware };