Refactor bin/gamedig.js a bit

This commit is contained in:
CosminPerRam 2023-09-11 02:16:26 +03:00
parent 727d6327dd
commit 65dd876252

View file

@ -1,7 +1,7 @@
#!/usr/bin/env node #!/usr/bin/env node
const Minimist = require('minimist'), const Minimist = require('minimist');
Gamedig = require('..'); const GameDig = require('..');
const argv = Minimist(process.argv.slice(2), { const argv = Minimist(process.argv.slice(2), {
boolean: ['pretty','debug','givenPortOnly','requestRules'], boolean: ['pretty','debug','givenPortOnly','requestRules'],
@ -15,14 +15,13 @@ delete argv.pretty;
const givenPortOnly = argv.givenPortOnly; const givenPortOnly = argv.givenPortOnly;
delete argv.givenPortOnly; delete argv.givenPortOnly;
const options = {}; let options = {};
for(const key of Object.keys(argv)) { for(const key of Object.keys(argv)) {
const value = argv[key]; const value = argv[key];
if(
key === '_' if(key === '_' || key.charAt(0) === '$')
|| key.charAt(0) === '$'
)
continue; continue;
options[key] = value; options[key] = value;
} }
@ -41,15 +40,17 @@ if (givenPortOnly) {
options.givenPortOnly = true; options.givenPortOnly = true;
} }
const gamedig = new Gamedig(options); const printOnPretty = (object) => {
gamedig.query(options)
.then((state) => {
if(pretty) { if(pretty) {
console.log(JSON.stringify(state,null,' ')); console.log(JSON.stringify(object,null,' '));
} else { } else {
console.log(JSON.stringify(state)); console.log(JSON.stringify(object));
} }
}) }
const gamedig = new GameDig(options);
gamedig.query(options)
.then(printOnPretty)
.catch((error) => { .catch((error) => {
if (debug) { if (debug) {
if (error instanceof Error) { if (error instanceof Error) {
@ -61,10 +62,7 @@ gamedig.query(options)
if (error instanceof Error) { if (error instanceof Error) {
error = error.message; error = error.message;
} }
if (pretty) {
console.log(JSON.stringify({error: error}, null, ' ')); printOnPretty({error: error});
} else {
console.log(JSON.stringify({error: error}));
}
} }
}); });