Skip to content

Commit c230a9a

Browse files
authored
Merge pull request #83 from scinscinscin/main
Redocument the code and add dashboard to cli
2 parents e294207 + ac15ff7 commit c230a9a

File tree

10 files changed

+112
-53
lines changed

10 files changed

+112
-53
lines changed

src/cli.ts

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ import {
1313
historyPerCountryPlain,
1414
informationPerCountryPlain,
1515
} from "./utils/routes/plain/plainHandlers";
16+
import {
17+
countryDashboard,
18+
globalDashboard,
19+
} from "./utils/routes/dashboard/dashboardHandlers";
1620

1721
const args = argv(process.argv.slice(2));
18-
let { history, mode, help, quiet, plain } = args;
22+
let { dashboard, history, mode, help, quiet, plain, size } = args;
1923
const country = args._[0];
2024

2125
const helpMessage = `${welcomeMessage}
@@ -26,8 +30,10 @@ Country: Can be a country name or ISO 3166-1 alpha-2 country code
2630
Leave empty to show global data
2731
2832
Options:
33+
--dashboard Show a dashboard
34+
--size Use with --dashboard to control the size of the output
2935
--history Show a chart of country's cases of world's cases
30-
--mode Use with --history to make show a chart of cases, deaths, or recovered
36+
--mode Use with --history to show a chart of cases, deaths, or recovered
3137
--quiet Only show necessary information
3238
--plain Enable plain mode
3339
@@ -36,22 +42,28 @@ Useful Links:
3642
${lines.WNrepoLink}
3743
${lines.WNDonateLink}`;
3844

39-
let output: string = "";
40-
const main = async () => {
41-
if (help) return console.log(helpMessage);
45+
const main: () => Promise<string> = async () => {
46+
if (help) return helpMessage;
4247
quiet = quiet === undefined ? false : quiet;
4348

49+
if (dashboard) {
50+
if (size === undefined) size = "sm";
51+
if (!["sm", "md", "lg"].includes(size)) size = "sm";
52+
53+
return country === undefined
54+
? await globalDashboard(size, false)
55+
: await countryDashboard(country, size, false);
56+
}
57+
4458
if (history === undefined) {
4559
if (country === undefined) {
46-
output =
47-
plain === true
48-
? await globalInformationPlain(quiet)
49-
: await globalInformation(quiet);
60+
return plain === true
61+
? await globalInformationPlain(quiet)
62+
: await globalInformation(quiet);
5063
} else {
51-
output =
52-
plain === true
53-
? await informationPerCountryPlain(country, quiet)
54-
: await informationPerCountry(country, quiet);
64+
return plain === true
65+
? await informationPerCountryPlain(country, quiet)
66+
: await informationPerCountry(country, quiet);
5567
}
5668
}
5769

@@ -60,25 +72,32 @@ const main = async () => {
6072

6173
if (history) {
6274
if (country === undefined) {
63-
output =
64-
plain === true
65-
? await globalHistoryPlain(mode, quiet)
66-
: await globalHistory(mode, quiet);
75+
return plain === true
76+
? await globalHistoryPlain(mode, quiet)
77+
: await globalHistory(mode, quiet);
6778
} else {
68-
output =
69-
plain === true
70-
? await historyPerCountryPlain(country, mode, quiet)
71-
: await historyPerCountry(country, mode, quiet);
79+
return plain === true
80+
? await historyPerCountryPlain(country, mode, quiet)
81+
: await historyPerCountry(country, mode, quiet);
7282
}
7383
}
7484

75-
// remove magic? newline
76-
let response = output.split("\n");
77-
response.pop();
78-
79-
console.log(response.join("\n"));
85+
return "";
8086
};
8187

82-
main().catch((err) => {
83-
console.log(err.message + "\n");
84-
});
88+
(async () => {
89+
let response = await main().catch((err) => {
90+
// Log error and exit out
91+
console.log(err.message + "\n");
92+
process.exit();
93+
});
94+
95+
//Remove magic new lines
96+
let responseArray = response.split("\n");
97+
while (!/\S/.test(responseArray[responseArray.length - 1])) {
98+
responseArray.pop();
99+
}
100+
response = responseArray.join("\n") + "\n";
101+
102+
console.log(response);
103+
})();

src/utils/getInformation.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { capitalizeFirstLetter } from "../utils/libs/capitalizeFirstLetter";
44
axios.defaults.baseURL = "https://disease.sh/v3/covid-19";
55

66
/**
7-
* @param isPlain Set to true to recieve an object containing the responses instead of the rows
8-
* @returns an object containing the data and metainfo **if isPlain is set to true**
9-
* @returns an array in the format of [timestamp, rows] **if isPlain is set to false**
7+
* @returns An object containing the epoch timestamp of when the data was updated, and the raw data from the API
108
*/
119
export const getAllInfo: () => Promise<{
1210
updated: number;
@@ -38,10 +36,8 @@ export const getAllInfo: () => Promise<{
3836
};
3937

4038
/**
41-
* @param country the country code or string that the user provides from req.params or CLI
42-
* @param isPlain Set to true to recieve an object containing the responses instead of the rows
43-
* @returns an object containing the data and metainfo **if isPlain is set to true**
44-
* @returns an array in the format of [timestamp, API countryname, formal countryname, rows[]] **if isPlain is false**
39+
* @param country Country string
40+
* @returns Object containing the time when the data was updated, API countryname, formal countryname, and the raw data from the API
4541
*/
4642
export const getCountryInfo: (
4743
country: string
@@ -89,6 +85,9 @@ export const getCountryInfo: (
8985

9086
type getHistoricalMode = "cases" | "deaths" | "recovered" | "all";
9187

88+
// This is a way of setting conditional types depending on what was passed to the mode parameter
89+
// If the mode parameter receives "all" then the type will be different because instead of only receiving one
90+
// set of data, it will be receiving everything
9291
// prettier-ignore
9392
export async function getHistorical<T extends getHistoricalMode>(
9493
mode: T,
@@ -107,6 +106,12 @@ export async function getHistorical<T extends getHistoricalMode>(
107106
}
108107
>;
109108

109+
/**
110+
*
111+
* @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
112+
* @param country Country string
113+
* @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
114+
*/
110115
export async function getHistorical(
111116
mode: getHistoricalMode,
112117
country = "all"
@@ -138,15 +143,11 @@ export async function getHistorical(
138143

139144
const date = `${informationType} from ${dates.shift()} to ${dates.pop()}`;
140145

141-
if (mode === "all") {
142-
return {
143-
date,
144-
chartData,
145-
};
146-
} else {
147-
return {
148-
date,
149-
chartData: Object.values(chartData) as number[],
150-
};
151-
}
146+
// If mode is not all then set the chartData to the values of the dates from the API
147+
if (mode !== "all") chartData = Object.values(chartData) as number[];
148+
149+
return {
150+
date,
151+
chartData,
152+
};
152153
}

src/utils/libs/columnizeData.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
*
33
* @param data An object containing your keys and values
4+
* @param padding Option parameter that controls the amount of padding on the left side
45
* @returns A 2 row column containing your keys and values
56
*/
67
export const columnizeData: (

src/utils/libs/convertCountry.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ let countryCodes: { [key: string]: string } = {};
44
countryCodes = (await axios.get("http://country.io/names.json")).data;
55
})();
66

7+
/**
8+
*
9+
* @param country A string that is either an 2 digit ISO country code or a full length countryname
10+
* @returns Full version of the country name
11+
*/
712
export const convertCountryCode: (country: string) => Promise<string> = async (
813
country
914
) => {

src/utils/libs/getResponses.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const lines = {
99
defaultHeader: `COVID-19 Tracker & CLI v${version}`,
1010
helpMessage: `Help: Try to append the URL with /help to learn more...`,
1111
sponsorMessage: `Love this project? Help us to help others by means of coffee!\n`,
12-
BMCLink: `(Buy Us a Coffee) wareneutron.com/donate`,
12+
BMCLink: `Buy Us a Coffee - wareneutron.com/donate`,
1313
twitterPlug: `Follow the lead dev on twitter for more updates!\n`,
1414
handleHashtag: ["@warengonzaga", "#covid19trackercli"],
1515
docsLink: "Docs: docs.wareneutron.com/covid19-tracker-cli",

src/utils/routes/dashboard/dashboardHandlers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const convertHistoricalDataToChart: (historical: {
5555
*
5656
* @param country Country that the user requested
5757
* @param size Size that the user requested
58+
* @param isWeb Boolean that states if the output will be run through the html template
5859
*/
5960
export const countryDashboard = async (
6061
country: string,
@@ -155,6 +156,7 @@ export const countryDashboard = async (
155156
/**
156157
*
157158
* @param size Size that the user requested
159+
* @param isWeb Boolean that states if the output will be run through the html template
158160
*/
159161
export const globalDashboard = async (size: DashboardSize, isWeb: boolean) => {
160162
let { data, updated } = await getAllInfo();

src/utils/routes/dashboard/generateDashboardOutput.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import blessed from "blessed";
22
import contrib from "blessed-contrib";
33
import { removeANSI } from "../../libs/generateTable";
44
import { lines, welcomeMessage } from "../../libs/getResponses";
5-
import { getSaying } from "../../libs/getSaying";
65
import { blessedConfig } from "./blessedConfig";
76
import { DashboardSize } from "./dashboardHandlers";
87

@@ -206,12 +205,8 @@ export const generateDashboardOutput: (
206205
response = removeUnneededLines(response);
207206
response += "\n";
208207

209-
response += getSaying() + "\n";
210-
response += lines.WNrepoLink + "\n\n";
211208
response += lines.BMCLink + "\n";
212209
response += lines.sponsorMessage + "\n";
213-
response += lines.twitterPlug;
214-
response += lines.handleHashtag.join(" ") + "\n";
215210

216211
return response;
217212
};

src/utils/routes/dashboard/generateWebDashboard.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
/**
2+
*
3+
* @param data Data meant to be rendered to terminals
4+
* @returns Web safe version of the data using Xterm.js
5+
*/
16
export const generateWebDashboard: (data: string) => string = (data) => {
27
data = data.replace(/\n/g, "\\r\\n");
38
let response = `<!doctype html>
49
<html>
510
<head>
11+
<meta charset="UTF-8">
12+
<meta name="title" content="Covid-19 Tracker CLI" />
13+
<meta name="description" content="Find information about Covid-19 in the browser and in your terminal" />
14+
<meta name="abstract" content="Find information about Covid-19 in the browser and in your terminal" />
15+
<meta name="keywords" content="covid19, covid, pandemic, virus, bat-soup" />
16+
<meta name="revisit-after" content="5 days">
17+
<meta name="language" content="EN-US" />
18+
<meta name="robots" content="index, follow">
19+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
20+
<meta name="google" content="notranslate" />
21+
<meta name="google" content="nositelinkssearchbox" />
22+
<title></title>
623
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/xterm.min.js"></script>
724
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/xterm-addon-fit.min.js"></script>
825
</head>

src/utils/routes/plain/plainParser.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import {
44
normalizeNumbers,
55
} from "../../libs/numberNormalizers";
66

7+
/**
8+
*
9+
* @returns An object containing the data and the unix epoch timestamp of when the data was updated
10+
*/
711
export const globalInfoPlain: () => Promise<{
812
timeUpdated: number;
913
data: {
@@ -41,6 +45,11 @@ export const globalInfoPlain: () => Promise<{
4145
};
4246
};
4347

48+
/**
49+
*
50+
* @param country A country string
51+
* @returns An object containing the data, API countryname, the formal countryname, and the unix epoch timestamp of when the data was updated
52+
*/
4453
export const countryInfoPlain: (
4554
country: string
4655
) => Promise<{

src/utils/routes/regular/regularParser.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import {
44
normalizeNumbers,
55
} from "../../libs/numberNormalizers";
66

7+
/**
8+
*
9+
* @returns Object containing epoch timestamp of when the data was updated, and an array containing the rows for the table
10+
*/
711
export const globalInfo: () => Promise<{
812
timeUpdated: number;
913
rowsOfData: (string | string[])[];
@@ -35,6 +39,12 @@ export const globalInfo: () => Promise<{
3539
return { timeUpdated: updated, rowsOfData };
3640
};
3741

42+
/**
43+
*
44+
* @param country Country string
45+
* @returns An object containing the rows for the table,
46+
* API countryname, the formal countryname, and the unix epoch timestamp of when the data was updated
47+
*/
3848
export const countryInfo: (
3949
country: string
4050
) => Promise<{

0 commit comments

Comments
 (0)