From d6145642481b934b2990446751bfbb783036a775 Mon Sep 17 00:00:00 2001 From: scinorandex Date: Sat, 17 Apr 2021 23:44:47 +0800 Subject: [PATCH 1/2] Redocument the code and add dashboard to cli --- src/cli.ts | 77 ++++++++++++------- src/utils/getInformation.ts | 37 ++++----- src/utils/libs/columnizeData.ts | 1 + src/utils/libs/convertCountry.ts | 5 ++ .../routes/dashboard/dashboardHandlers.ts | 2 + .../routes/dashboard/generateWebDashboard.ts | 17 ++++ src/utils/routes/plain/plainParser.ts | 9 +++ src/utils/routes/regular/regularParser.ts | 10 +++ 8 files changed, 111 insertions(+), 47 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 6d56d4a..a701f2a 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -13,9 +13,13 @@ import { historyPerCountryPlain, informationPerCountryPlain, } from "./utils/routes/plain/plainHandlers"; +import { + countryDashboard, + globalDashboard, +} from "./utils/routes/dashboard/dashboardHandlers"; const args = argv(process.argv.slice(2)); -let { history, mode, help, quiet, plain } = args; +let { dashboard, history, mode, help, quiet, plain, size } = args; const country = args._[0]; const helpMessage = `${welcomeMessage} @@ -26,8 +30,10 @@ Country: Can be a country name or ISO 3166-1 alpha-2 country code Leave empty to show global data Options: + --dashboard Show a dashboard + --size Use with --dashboard to control the size of the output --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 + --mode Use with --history to show a chart of cases, deaths, or recovered --quiet Only show necessary information --plain Enable plain mode @@ -36,22 +42,28 @@ Useful Links: ${lines.WNrepoLink} ${lines.WNDonateLink}`; -let output: string = ""; -const main = async () => { - if (help) return console.log(helpMessage); +const main: () => Promise = async () => { + if (help) return helpMessage; quiet = quiet === undefined ? false : quiet; + if (dashboard) { + if (size === undefined) size = "sm"; + if (!["sm", "md", "lg"].includes(size)) size = "sm"; + + return country === undefined + ? await globalDashboard(size, false) + : await countryDashboard(country, size, false); + } + if (history === undefined) { if (country === undefined) { - output = - plain === true - ? await globalInformationPlain(quiet) - : await globalInformation(quiet); + return plain === true + ? await globalInformationPlain(quiet) + : await globalInformation(quiet); } else { - output = - plain === true - ? await informationPerCountryPlain(country, quiet) - : await informationPerCountry(country, quiet); + return plain === true + ? await informationPerCountryPlain(country, quiet) + : await informationPerCountry(country, quiet); } } @@ -60,25 +72,32 @@ const main = async () => { if (history) { if (country === undefined) { - output = - plain === true - ? await globalHistoryPlain(mode, quiet) - : await globalHistory(mode, quiet); + return plain === true + ? await globalHistoryPlain(mode, quiet) + : await globalHistory(mode, quiet); } else { - output = - plain === true - ? await historyPerCountryPlain(country, mode, quiet) - : await historyPerCountry(country, mode, quiet); + return plain === true + ? await historyPerCountryPlain(country, mode, quiet) + : await historyPerCountry(country, mode, quiet); } } - // remove magic? newline - let response = output.split("\n"); - response.pop(); - - console.log(response.join("\n")); + return ""; }; -main().catch((err) => { - console.log(err.message + "\n"); -}); +(async () => { + let response = await main().catch((err) => { + // Log error and exit out + console.log(err.message + "\n"); + process.exit(); + }); + + //Remove magic new lines + let responseArray = response.split("\n"); + while (!/\S/.test(responseArray[responseArray.length - 1])) { + responseArray.pop(); + } + response = responseArray.join("\n") + "\n"; + + console.log(response); +})(); diff --git a/src/utils/getInformation.ts b/src/utils/getInformation.ts index 188cf1c..939d2fd 100644 --- a/src/utils/getInformation.ts +++ b/src/utils/getInformation.ts @@ -4,9 +4,7 @@ import { capitalizeFirstLetter } from "../utils/libs/capitalizeFirstLetter"; axios.defaults.baseURL = "https://disease.sh/v3/covid-19"; /** - * @param isPlain Set to true to recieve an object containing the responses instead of the rows - * @returns an object containing the data and metainfo **if isPlain is set to true** - * @returns an array in the format of [timestamp, rows] **if isPlain is set to false** + * @returns An object containing the epoch timestamp of when the data was updated, and the raw data from the API */ export const getAllInfo: () => Promise<{ updated: number; @@ -38,10 +36,8 @@ export const getAllInfo: () => Promise<{ }; /** - * @param country the country code or string that the user provides from req.params or CLI - * @param isPlain Set to true to recieve an object containing the responses instead of the rows - * @returns an object containing the data and metainfo **if isPlain is set to true** - * @returns an array in the format of [timestamp, API countryname, formal countryname, rows[]] **if isPlain is false** + * @param country Country string + * @returns Object containing the time when the data was updated, API countryname, formal countryname, and the raw data from the API */ export const getCountryInfo: ( country: string @@ -89,6 +85,9 @@ export const getCountryInfo: ( type getHistoricalMode = "cases" | "deaths" | "recovered" | "all"; +// This is a way of setting conditional types depending on what was passed to the mode parameter +// If the mode parameter receives "all" then the type will be different because instead of only receiving one +// set of data, it will be receiving everything // prettier-ignore export async function getHistorical( mode: T, @@ -107,6 +106,12 @@ export async function getHistorical( } >; +/** + * + * @param mode What data the user wants to receive, if all, then the user will be receiving everything but it can be specified to one type of data + * @param country Country string + * @returns Object containing date already formatted, and the data which is either a number[] or the raw data in another object *if* mode is set to all + */ export async function getHistorical( mode: getHistoricalMode, country = "all" @@ -138,15 +143,11 @@ export async function getHistorical( const date = `${informationType} from ${dates.shift()} to ${dates.pop()}`; - if (mode === "all") { - return { - date, - chartData, - }; - } else { - return { - date, - chartData: Object.values(chartData) as number[], - }; - } + // If mode is not all then set the chartData to the values of the dates from the API + if (mode !== "all") chartData = Object.values(chartData) as number[]; + + return { + date, + chartData, + }; } diff --git a/src/utils/libs/columnizeData.ts b/src/utils/libs/columnizeData.ts index c1a3112..d869e6d 100644 --- a/src/utils/libs/columnizeData.ts +++ b/src/utils/libs/columnizeData.ts @@ -1,6 +1,7 @@ /** * * @param data An object containing your keys and values + * @param padding Option parameter that controls the amount of padding on the left side * @returns A 2 row column containing your keys and values */ export const columnizeData: ( diff --git a/src/utils/libs/convertCountry.ts b/src/utils/libs/convertCountry.ts index 2551097..3c1985b 100644 --- a/src/utils/libs/convertCountry.ts +++ b/src/utils/libs/convertCountry.ts @@ -4,6 +4,11 @@ let countryCodes: { [key: string]: string } = {}; countryCodes = (await axios.get("http://country.io/names.json")).data; })(); +/** + * + * @param country A string that is either an 2 digit ISO country code or a full length countryname + * @returns Full version of the country name + */ export const convertCountryCode: (country: string) => Promise = async ( country ) => { diff --git a/src/utils/routes/dashboard/dashboardHandlers.ts b/src/utils/routes/dashboard/dashboardHandlers.ts index 1c06730..daba88c 100644 --- a/src/utils/routes/dashboard/dashboardHandlers.ts +++ b/src/utils/routes/dashboard/dashboardHandlers.ts @@ -55,6 +55,7 @@ const convertHistoricalDataToChart: (historical: { * * @param country Country that the user requested * @param size Size that the user requested + * @param isWeb Boolean that states if the output will be run through the html template */ export const countryDashboard = async ( country: string, @@ -155,6 +156,7 @@ export const countryDashboard = async ( /** * * @param size Size that the user requested + * @param isWeb Boolean that states if the output will be run through the html template */ export const globalDashboard = async (size: DashboardSize, isWeb: boolean) => { let { data, updated } = await getAllInfo(); diff --git a/src/utils/routes/dashboard/generateWebDashboard.ts b/src/utils/routes/dashboard/generateWebDashboard.ts index 5e2388d..51e2bc0 100644 --- a/src/utils/routes/dashboard/generateWebDashboard.ts +++ b/src/utils/routes/dashboard/generateWebDashboard.ts @@ -1,8 +1,25 @@ +/** + * + * @param data Data meant to be rendered to terminals + * @returns Web safe version of the data using Xterm.js + */ export const generateWebDashboard: (data: string) => string = (data) => { data = data.replace(/\n/g, "\\r\\n"); let response = ` + + + + + + + + + + + + diff --git a/src/utils/routes/plain/plainParser.ts b/src/utils/routes/plain/plainParser.ts index 516e93a..231144f 100644 --- a/src/utils/routes/plain/plainParser.ts +++ b/src/utils/routes/plain/plainParser.ts @@ -4,6 +4,10 @@ import { normalizeNumbers, } from "../../libs/numberNormalizers"; +/** + * + * @returns An object containing the data and the unix epoch timestamp of when the data was updated + */ export const globalInfoPlain: () => Promise<{ timeUpdated: number; data: { @@ -41,6 +45,11 @@ export const globalInfoPlain: () => Promise<{ }; }; +/** + * + * @param country A country string + * @returns An object containing the data, API countryname, the formal countryname, and the unix epoch timestamp of when the data was updated + */ export const countryInfoPlain: ( country: string ) => Promise<{ diff --git a/src/utils/routes/regular/regularParser.ts b/src/utils/routes/regular/regularParser.ts index e13731b..fb2f231 100644 --- a/src/utils/routes/regular/regularParser.ts +++ b/src/utils/routes/regular/regularParser.ts @@ -4,6 +4,10 @@ import { normalizeNumbers, } from "../../libs/numberNormalizers"; +/** + * + * @returns Object containing epoch timestamp of when the data was updated, and an array containing the rows for the table + */ export const globalInfo: () => Promise<{ timeUpdated: number; rowsOfData: (string | string[])[]; @@ -35,6 +39,12 @@ export const globalInfo: () => Promise<{ return { timeUpdated: updated, rowsOfData }; }; +/** + * + * @param country Country string + * @returns An object containing the rows for the table, + * API countryname, the formal countryname, and the unix epoch timestamp of when the data was updated + */ export const countryInfo: ( country: string ) => Promise<{ From ac15ff70036d44367a8400bec0ea51e44b570c55 Mon Sep 17 00:00:00 2001 From: scinorandex Date: Sun, 18 Apr 2021 23:36:29 +0800 Subject: [PATCH 2/2] Update BMC info and make dashboard quiet --- src/utils/libs/getResponses.ts | 2 +- src/utils/routes/dashboard/generateDashboardOutput.ts | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/utils/libs/getResponses.ts b/src/utils/libs/getResponses.ts index 0b0e682..4282b07 100644 --- a/src/utils/libs/getResponses.ts +++ b/src/utils/libs/getResponses.ts @@ -9,7 +9,7 @@ export const lines = { defaultHeader: `COVID-19 Tracker & CLI v${version}`, helpMessage: `Help: Try to append the URL with /help to learn more...`, sponsorMessage: `Love this project? Help us to help others by means of coffee!\n`, - BMCLink: `(Buy Us a Coffee) wareneutron.com/donate`, + BMCLink: `Buy Us a Coffee - wareneutron.com/donate`, twitterPlug: `Follow the lead dev on twitter for more updates!\n`, handleHashtag: ["@warengonzaga", "#covid19trackercli"], docsLink: "Docs: docs.wareneutron.com/covid19-tracker-cli", diff --git a/src/utils/routes/dashboard/generateDashboardOutput.ts b/src/utils/routes/dashboard/generateDashboardOutput.ts index 16b845b..dc21b2e 100644 --- a/src/utils/routes/dashboard/generateDashboardOutput.ts +++ b/src/utils/routes/dashboard/generateDashboardOutput.ts @@ -2,7 +2,6 @@ import blessed from "blessed"; import contrib from "blessed-contrib"; import { removeANSI } from "../../libs/generateTable"; import { lines, welcomeMessage } from "../../libs/getResponses"; -import { getSaying } from "../../libs/getSaying"; import { blessedConfig } from "./blessedConfig"; import { DashboardSize } from "./dashboardHandlers"; @@ -206,12 +205,8 @@ export const generateDashboardOutput: ( response = removeUnneededLines(response); response += "\n"; - response += getSaying() + "\n"; - response += lines.WNrepoLink + "\n\n"; response += lines.BMCLink + "\n"; response += lines.sponsorMessage + "\n"; - response += lines.twitterPlug; - response += lines.handleHashtag.join(" ") + "\n"; return response; };