From 65dd876252879913811efdb1d5e9ad6a9f818945 Mon Sep 17 00:00:00 2001 From: CosminPerRam Date: Mon, 11 Sep 2023 02:16:26 +0300 Subject: [PATCH] Refactor bin/gamedig.js a bit --- bin/gamedig.js | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/bin/gamedig.js b/bin/gamedig.js index e85e3f2..0b0d0ee 100755 --- a/bin/gamedig.js +++ b/bin/gamedig.js @@ -1,7 +1,7 @@ #!/usr/bin/env node -const Minimist = require('minimist'), - Gamedig = require('..'); +const Minimist = require('minimist'); +const GameDig = require('..'); const argv = Minimist(process.argv.slice(2), { boolean: ['pretty','debug','givenPortOnly','requestRules'], @@ -15,14 +15,13 @@ delete argv.pretty; const givenPortOnly = argv.givenPortOnly; delete argv.givenPortOnly; -const options = {}; +let options = {}; for(const key of Object.keys(argv)) { const value = argv[key]; - if( - key === '_' - || key.charAt(0) === '$' - ) + + if(key === '_' || key.charAt(0) === '$') continue; + options[key] = value; } @@ -41,15 +40,17 @@ if (givenPortOnly) { options.givenPortOnly = true; } -const gamedig = new Gamedig(options); +const printOnPretty = (object) => { + if(pretty) { + console.log(JSON.stringify(object,null,' ')); + } else { + console.log(JSON.stringify(object)); + } +} + +const gamedig = new GameDig(options); gamedig.query(options) - .then((state) => { - if(pretty) { - console.log(JSON.stringify(state,null,' ')); - } else { - console.log(JSON.stringify(state)); - } - }) + .then(printOnPretty) .catch((error) => { if (debug) { if (error instanceof Error) { @@ -61,10 +62,7 @@ gamedig.query(options) if (error instanceof Error) { error = error.message; } - if (pretty) { - console.log(JSON.stringify({error: error}, null, ' ')); - } else { - console.log(JSON.stringify({error: error})); - } + + printOnPretty({error: error}); } });