chore: cleaner cli (#459)

* chore: cleaner cli

* chore: use destruction for argv
This commit is contained in:
CosminPerRam 2024-01-16 00:58:54 +02:00 committed by GitHub
parent 944716e453
commit 0a3338320e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 24 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env node
import * as process from "node:process";
import * as process from 'node:process'
import Minimist from 'minimist'
import { GameDig } from './../lib/index.js'
@ -10,17 +10,6 @@ const argv = Minimist(process.argv.slice(2), {
string: ['guildId', 'listenUdpPort', 'ipFamily']
})
const debug = argv.debug
delete argv.debug
const pretty = !!argv.pretty || debug
delete argv.pretty
const givenPortOnly = argv.givenPortOnly
delete argv.givenPortOnly
const requestRulesRequired = argv.requestRulesRequired
delete argv.requestRulesRequired
const requestPlayersRequired = argv.requestPlayersRequired
delete argv.requestPlayersRequired
const options = {}
for (const key of Object.keys(argv)) {
const value = argv[key]
@ -30,6 +19,7 @@ for (const key of Object.keys(argv)) {
options[key] = value
}
// Separate host and port
if (argv._.length >= 1) {
const target = argv._[0]
const split = target.split(':')
@ -38,21 +28,22 @@ if (argv._.length >= 1) {
options.port = split[1]
}
}
if (debug) {
options.debug = true
}
if (givenPortOnly) {
options.givenPortOnly = true
}
if (requestRulesRequired) {
options.requestRulesRequired = true
}
if (requestPlayersRequired) {
options.requestPlayersRequired = true
const applyBoolean = (value, fieldName, defaultValue = true) => {
if (value) {
options[fieldName] = defaultValue
}
}
const { debug, pretty, givenPortOnly, requestRulesRequired, requestPlayersRequired } = argv
applyBoolean(debug, 'debug')
applyBoolean(givenPortOnly, 'givenPortOnly')
applyBoolean(requestRulesRequired, 'requestRulesRequired')
applyBoolean(requestPlayersRequired, 'requestPlayersRequired')
const printOnPretty = (object) => {
if (pretty) {
if (!!pretty || debug) {
console.log(JSON.stringify(object, null, ' '))
} else {
console.log(JSON.stringify(object))