2023-11-12 12:14:43 +01:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2024-01-15 23:58:54 +01:00
|
|
|
import * as process from 'node:process'
|
2023-11-12 12:14:43 +01:00
|
|
|
|
|
|
|
import Minimist from 'minimist'
|
2023-11-28 11:59:13 +01:00
|
|
|
import { GameDig } from './../lib/index.js'
|
2023-11-12 12:14:43 +01:00
|
|
|
|
|
|
|
const argv = Minimist(process.argv.slice(2), {
|
2024-01-16 00:39:07 +01:00
|
|
|
boolean: ['pretty', 'debug', 'givenPortOnly', 'requestRules', 'requestRulesRequired', 'requestPlayersRequired', 'stripColors'],
|
|
|
|
string: ['guildId', 'listenUdpPort', 'ipFamily'],
|
|
|
|
default: {
|
|
|
|
stripColors: true
|
|
|
|
}
|
2023-11-12 12:14:43 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const options = {}
|
|
|
|
for (const key of Object.keys(argv)) {
|
|
|
|
const value = argv[key]
|
|
|
|
|
|
|
|
if (key === '_' || key.charAt(0) === '$') { continue }
|
|
|
|
|
|
|
|
options[key] = value
|
|
|
|
}
|
|
|
|
|
2024-01-15 23:58:54 +01:00
|
|
|
// Separate host and port
|
2023-11-12 12:14:43 +01:00
|
|
|
if (argv._.length >= 1) {
|
|
|
|
const target = argv._[0]
|
|
|
|
const split = target.split(':')
|
|
|
|
options.host = split[0]
|
2024-01-16 01:14:02 +01:00
|
|
|
if (split.length > 1) {
|
2023-11-12 12:14:43 +01:00
|
|
|
options.port = split[1]
|
|
|
|
}
|
|
|
|
}
|
2024-01-15 23:58:54 +01:00
|
|
|
|
2024-01-16 01:14:02 +01:00
|
|
|
const { debug, pretty } = options
|
2024-01-15 23:58:54 +01:00
|
|
|
|
2023-11-12 12:14:43 +01:00
|
|
|
const printOnPretty = (object) => {
|
2024-01-15 23:58:54 +01:00
|
|
|
if (!!pretty || debug) {
|
2023-11-12 12:14:43 +01:00
|
|
|
console.log(JSON.stringify(object, null, ' '))
|
|
|
|
} else {
|
|
|
|
console.log(JSON.stringify(object))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const gamedig = new GameDig(options)
|
|
|
|
gamedig.query(options)
|
|
|
|
.then(printOnPretty)
|
|
|
|
.catch((error) => {
|
|
|
|
if (debug) {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
console.log(error.stack)
|
|
|
|
} else {
|
|
|
|
console.log(error)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
error = error.message
|
|
|
|
}
|
|
|
|
|
|
|
|
printOnPretty({ error })
|
|
|
|
}
|
|
|
|
})
|