-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4get.js
executable file
·138 lines (128 loc) · 3.8 KB
/
4get.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
130
131
132
133
134
135
136
137
138
#!/usr/bin/env node
"use strict";
console.debug = console.log;
// you gotta install those m8
require("colors");
const minimist = require("minimist");
const log = require("loglevel");
const {Runner} = require("./lib/runner");
const {ensureArray, extractThread} = require("./lib/util");
if (process.platform === "win32") {
// kek, windows
require("readline").createInterface({
input: process.stdin,
output: process.stdout
}).on("SIGINT", function() {
process.emit("SIGINT");
});
}
function usage() {
log.info("node", process.argv[1].rainbow,
"--board".yellow, "b".red, "-b".yellow, "soc".red,
"-m".yellow, "b/thread/11111111".red, "a/thread/12345".green);
const options = {
"-b, --board": "Monitor a board (multiple possible)",
"--black": "Provide your own blacklist",
"-c, --no-convert": "Do not convert pngs",
"-j, --jobs": "How many medias to get in parallel",
"-l, --loglevel": "Set a log level (silent, error, warn, info, debug)",
"-m, --monitor": "Monitor a thread (multiple possible)",
"[other]": "suck the thread one (multiple possible",
"-p, --min": "Min dimension of images",
"--version": "Print version and exit",
"-v": "Shortcut for --loglevel debug",
"-w, --win": "Force names to be valid Windows names",
};
const args = Object.keys(options);
const sk = k => k.replace(/-/g, "").replace("[", String.fromCharCode(255));
args.sort((a, b) => sk(a) > sk(b));
const max = args.reduce((p, c) => Math.max(c.length, p), 0);
log.info("");
for (const a of args) {
log.info(" ", a.yellow, " ".repeat(max - a.length + 2), options[a].bold);
}
log.info("");
log.info("For more information contact your local EFF BEE AYY agent".
magenta.bold);
}
function checkUInt(a, name) {
a = parseInt(a, 10);
if (!a || a < 1) {
throw new Error(`Invalid ${name || "argument"}`);
}
return Math.floor(a);
}
(async function main() {
const args = minimist(process.argv.slice(2), {
"--": true,
boolean: ["help", "h", "v", "win"],
alias: {
b: "board",
c: "no-convert",
h: "help",
j: "jobs",
l: "loglevel",
m: "monitor",
p: "min",
w: "win",
},
});
log.setLevel("info");
if (args.help) {
usage();
process.exit(1);
}
if (args.version) {
log.info(require("./package.json").version);
process.exit(0);
}
if (args.v) {
args.loglevel = "debug";
}
if (args.jobs) {
args.jobs = checkUInt(args.jobs, "--jobs");
}
else {
args.jobs = Math.max(1, Math.min(require("os").cpus().length * 4, 8));
}
if (args.min) {
args.min = checkUInt(args.min, "--min");
}
args.convert = !args.c;
log.setLevel(args.loglevel || "info");
log.debug("Initializing v8", process.versions);
require("v8").setFlagsFromString(
"--optimize_for_size --max_old_space_size=96");
log.debug("Spawning runner");
const runner = new Runner(args);
const waiting = [];
if (args.board && args.board.length) {
waiting.push(runner.monitorBoards(ensureArray(args.board)));
}
if (args.monitor && args.monitor.length) {
waiting.push(runner.monitorThreads(ensureArray(args.monitor)));
}
if (args._ && args._.length) {
for (const thread of ensureArray(args._).map(extractThread)) {
waiting.push(runner.process(thread.board, thread.no));
}
}
const cancel = function() {
log.warn("\rCancel requested".bold.yellow);
runner.cancel();
};
process.on("SIGINT", cancel);
process.on("SIGTERM", cancel);
process.on("SIGQUIT", cancel);
log.debug("Waiting for tasks to finish");
await Promise.all(waiting);
log.debug("Waiting for runner to go down");
await runner.close();
runner.printStats();
})().then(rv => {
log.error("kthxbai".bold.red);
process.exit(rv || 0);
}, ex => {
log.error("Shit hit the literal fan", ex);
process.exit(1);
});