Skip to content
Prev Previous commit
Next Next commit
Add plain mode to CLI
  • Loading branch information
scinorandex committed Mar 31, 2021
commit f5bf1e86bc588c95fab710ff579fd6683fce674e
47 changes: 35 additions & 12 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import argv from "minimist";
import { generateColorTable } from "./utils/generateTable";
import {
globalHistory,
globalInformation,
historyPerCountry,
informationPerCountry,
} from "./utils/handlers";
import {
globalHistoryPlain,
globalInformationPlain,
historyPerCountryPlain,
informationPerCountryPlain,
} from "./utils/plainHandlers";

const args = argv(process.argv.slice(2));
let { history, mode, help, quiet } = args;
let { history, mode, help, quiet, plain } = args;
const country = args._[0];

const { version } = require("../package.json");
Expand All @@ -23,30 +28,48 @@ Country: Can be a country name or ISO 3166-1 alpha-2 country code
Options:
--history Show a chart of country's cases of world's cases
--mode Use with --history to make show a chart of cases, deaths, or recovered
--quiet Only show necessary information`;
--quiet Only show necessary information
--plain Enable plain mode`;

let output: string = "";
const main = async () => {
if (help) return console.log(helpMessage);
quiet = quiet === undefined || typeof quiet === "undefined" ? false : quiet;
quiet = quiet === undefined ? false : quiet;

if (history === undefined || typeof history === "undefined") {
if (country === undefined) output = await globalInformation(quiet);
else output = await informationPerCountry(country, quiet);
if (history === undefined) {
if (country === undefined) {
output =
plain === true
? await globalInformationPlain(quiet)
: await globalInformation(quiet);
} else {
output =
plain === true
? await informationPerCountryPlain(country, quiet)
: await informationPerCountry(country, quiet);
}
}

mode = mode === undefined || typeof mode === "undefined" ? "cases" : mode; // defauilt to cases if mode is not present
mode = mode === undefined ? "cases" : mode; // default to cases if mode is not present
if (!["cases", "deaths", "recovered"].includes(mode)) mode === "cases"; // default to cases if mode is not cases | deaths | recovered

if (history) {
if (country === undefined) output = await globalHistory(mode, quiet);
else output = await historyPerCountry(country, mode, quiet);
if (country === undefined) {
output =
plain === true
? await globalHistoryPlain(mode, quiet)
: await globalHistory(mode, quiet);
} else {
output =
plain === true
? await historyPerCountryPlain(country, mode, quiet)
: await historyPerCountry(country, mode, quiet);
}
}

console.log(output);
};

main().catch((err) => {
let errorTable = generateColorTable([err.message], "red");
console.log(errorTable);
console.log(err.message + "\n");
});