forked from jltwheeler/glasto-helper-2025
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
73 lines (63 loc) · 1.75 KB
/
main.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
#!/usr/bin/env node
const readline = require("readline");
const yargs = require("yargs");
const fs = require("fs");
const util = require("util");
const Puppets = require("./puppets");
const logger = require("./log");
const argv = yargs.argv;
const readFile = util.promisify(fs.readFile);
const parseArgs = () => {
if (!argv.site || !argv["rate-limit"] || !argv["max-tabs"]) {
logger.info(
'Usage:\nnode main.js --site="localhost:3000" --rate-limit=60 --max-tabs=10'
);
process.exit(0);
}
return argv;
};
const readFileAsString = async (filePath) => {
try {
const data = await readFile(filePath, "utf8");
return data;
} catch (error) {
logger.error(`Error reading file: ${error.message}`);
throw error;
}
};
const getRegistrationPageInnerText = async () => {
if (argv.test && argv.test !== "false") {
return await readFileAsString("resources/test.txt");
} else {
return await readFileAsString("resources/live.txt");
}
};
const setupKeyPressHandler = (tabs) => {
readline.emitKeypressEvents(process.stdin);
process.stdin.on("keypress", (str, key) => {
if (key.ctrl && key.name === "c") {
tabs
.closeTabs()
.then(() => process.exit(0))
.catch((error) => {
logger.error(`Error closing tabs: ${error.message}`);
process.exit(1);
});
} else if (key.name === "enter") {
tabs.setPaused(!tabs.getPaused());
}
});
};
const run = async () => {
parseArgs();
const registrationPageInnerText = await getRegistrationPageInnerText();
const tabs = new Puppets(
argv.site,
argv["rate-limit"],
registrationPageInnerText
);
setupKeyPressHandler(tabs);
await tabs.initializeTabs(argv["max-tabs"]);
await tabs.loadPagesAtRate();
};
run();