refactoring and comments

fix(cli.js): refactor help and error handling logic for better readability and maintainability
feat(cli.js): add support for custom port and timeout options
fix(parallel.js): update list of hosts to ping
fix(bedrock.js): add comments and improve error handling in ping function
fix(java.js): add comments and improve error handling in ping function
fix(varint.js): add comments to functions and improve readability
fix(index.d.ts): export all functions from java.js and bedrock.js
fix(lib/bedrock.d.ts): update source link
fix(lib/java.d.ts): update source link
This commit is contained in:
2023-10-22 21:32:00 +03:00
parent 915edbec9c
commit 910184bf5f
8 changed files with 148 additions and 59 deletions

View File

@ -1,19 +1,15 @@
/**
* Usage examples:
* - Java (with custom timeout): node cli.js -j --host="mc.hypixel.net" --timeout 1000
* - Bedrock: node cli.js -b --host="play.timecrack.net"
*/
import { pingBedrock, pingJava } from '../index.js';
const args = getArgs();
if (args.help || args.h || Object.keys(args).length === 0) {
console.log(`node cli.js [..]
A simple to use, efficient, and full-featured Minecraft server info parser!
USAGE:
node cli.js [OPTIONS] --host <HOST> --port <PORT> --timeout <TIMEOUT>
OPTIONS:
-j Use for Minecraft Java Edition
-b Use for Minecraft Bedrock Edition
P.S. Don't use them at the same time!`);
if (shouldShowHelp(args)) {
printHelp();
process.exit(1);
}
@ -24,6 +20,42 @@ if (!args.host) {
// easter egg <3
if (args.j && args.b) {
printInterestingFacts();
process.exit(0);
}
const port = args.port || getDefaultPort(args);
const timeout = args.timeout || 500;
if (args.j) {
await pingJavaServer(args.host, port, timeout)
.catch(err => console.error(`ERROR: ${err.message}`));
} else if (args.b) {
await pingBedrockServer(args.host, port, timeout)
.catch(err => console.error(`ERROR: ${err.message}`));
} else {
console.error('ERROR: Unsupported flag passed. Use -h or --help.');
}
function shouldShowHelp(args) {
return args.help || args.h || Object.keys(args).length === 0;
}
function printHelp() {
console.log(`node cli.js [..]
A simple to use, efficient, and full-featured Minecraft server info parser!
USAGE:
node cli.js [OPTIONS] --host <HOST> --port <PORT> --timeout <TIMEOUT>
OPTIONS:
-j Use for Minecraft Java Edition
-b Use for Minecraft Bedrock Edition
P.S. Don't use them at the same time!`);
}
function printInterestingFacts() {
console.log(`Some interesting facts about MOTDs on bedrock:
- so far they seem to exclusively use legacy color codes
- the random style has a special impl for periods, they turn into animated
@ -31,25 +63,20 @@ if (args.j && args.b) {
- motd_2 is ignored? client displays "motd_1 - v{version}", where the
appended version text is considered part of motd_1 for color code processing
- motd_2 seems to mainly be used to return the server software in use (e.g. PocketMine-MP)`);
process.exit(0);
}
if (args.j) {
const data = await pingJava(args.host, {
port: args.port || 25565,
timeout: args.timeout || 500
});
function getDefaultPort(args) {
return args.j ? 25565 : 19132;
}
console.log(`host: ${args.host}\nprotocol: ${data.version.protocol}\nonline: ${data.players.online}`);
} else if (args.b) {
const data = await pingBedrock(args.host, {
port: args.port || 19132,
timeout: args.timeout || 500
});
async function pingJavaServer(host, port, timeout) {
const data = await pingJava(host, { port, timeout });
console.log(`host: ${host}\nprotocol: ${data.version?.protocol}\nonline: ${data.players?.online}`);
}
console.log(`host: ${args.host}\nprotocol: ${data.version.protocol}\nonline: ${data.players.online}`);
} else {
console.error('ERROR: Unsupported flag passed. Use -h or --help.');
async function pingBedrockServer(host, port, timeout) {
const data = await pingBedrock(host, { port, timeout });
console.log(`host: ${host}\nprotocol: ${data.version.protocol}\nonline: ${data.players.online}`);
}
// parsing command line arguments