Skip to content

Commit

Permalink
Merge pull request #83 from scinscinscin/main
Browse files Browse the repository at this point in the history
Redocument the code and add dashboard to cli
  • Loading branch information
warengonzaga authored Apr 18, 2021
2 parents e294207 + ac15ff7 commit c230a9a
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 53 deletions.
77 changes: 48 additions & 29 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand All @@ -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<string> = 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);
}
}

Expand All @@ -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);
})();
37 changes: 19 additions & 18 deletions src/utils/getInformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<T extends getHistoricalMode>(
mode: T,
Expand All @@ -107,6 +106,12 @@ export async function getHistorical<T extends getHistoricalMode>(
}
>;

/**
*
* @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"
Expand Down Expand Up @@ -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,
};
}
1 change: 1 addition & 0 deletions src/utils/libs/columnizeData.ts
Original file line number Diff line number Diff line change
@@ -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: (
Expand Down
5 changes: 5 additions & 0 deletions src/utils/libs/convertCountry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> = async (
country
) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/libs/getResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/utils/routes/dashboard/dashboardHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 0 additions & 5 deletions src/utils/routes/dashboard/generateDashboardOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
};
Expand Down
17 changes: 17 additions & 0 deletions src/utils/routes/dashboard/generateWebDashboard.ts
Original file line number Diff line number Diff line change
@@ -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 = `<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="title" content="Covid-19 Tracker CLI" />
<meta name="description" content="Find information about Covid-19 in the browser and in your terminal" />
<meta name="abstract" content="Find information about Covid-19 in the browser and in your terminal" />
<meta name="keywords" content="covid19, covid, pandemic, virus, bat-soup" />
<meta name="revisit-after" content="5 days">
<meta name="language" content="EN-US" />
<meta name="robots" content="index, follow">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="google" content="notranslate" />
<meta name="google" content="nositelinkssearchbox" />
<title></title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/xterm.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/xterm-addon-fit.min.js"></script>
</head>
Expand Down
9 changes: 9 additions & 0 deletions src/utils/routes/plain/plainParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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<{
Expand Down
10 changes: 10 additions & 0 deletions src/utils/routes/regular/regularParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[])[];
Expand Down Expand Up @@ -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<{
Expand Down

0 comments on commit c230a9a

Please sign in to comment.