Skip to content

Commit 41597e5

Browse files
author
scinorandex
committed
Add quiet mode
1 parent 80545a7 commit 41597e5

File tree

6 files changed

+89
-53
lines changed

6 files changed

+89
-53
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ debug.log
44
.DS_Store
55
.now
66
.env
7+
dist/

src/api.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ const app = express();
1111
app.use(morgan("common"));
1212
app.use(userAgentMiddleware);
1313

14-
app.use("/", router);
15-
app.use("/", errorHandler);
14+
app.use(["/quiet", "/"], router);
15+
app.use(["/quiet", "/"], errorHandler);
1616

1717
app.use("*", (_req, res) =>
18-
res.send(
19-
`Welcome to COVID-19 Tracker CLI v${version} by Waren Gonzaga with Wareneutron Developers\n
18+
res.send(
19+
`Welcome to COVID-19 Tracker CLI v${version} by Waren Gonzaga with Wareneutron Developers\n
2020
Please visit: https://warengonza.ga/covid19-tracker-cli\n`
21-
)
21+
)
2222
);
2323

2424
app.listen(port, () => {
25-
console.log(`Express listening on port ${port}`);
25+
console.log(`Express listening on port ${port}`);
2626
});

src/api/router.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ router.get(
2525

2626
// if the mode is not in the api then return to next handler
2727
if (!["cases", "deaths", "recovered"].includes(mode)) return next();
28-
res.send(await globalHistory(mode));
28+
res.send(await globalHistory(mode, req.baseUrl.startsWith("/quiet")));
2929
})
3030
);
3131

@@ -42,21 +42,33 @@ router.get(
4242

4343
// if the mode is not in the api then return to next handler
4444
if (!["cases", "deaths", "recovered"].includes(mode)) return next();
45-
res.send(await historyPerCountry(country, mode));
45+
res.send(
46+
await historyPerCountry(
47+
country,
48+
mode,
49+
req.baseUrl.startsWith("/quiet")
50+
)
51+
);
4652
})
4753
);
4854

4955
router.get(
5056
"/:country",
5157
handleAsync(async (req, res, _next) => {
58+
console.log(req.path);
5259
const country = req.params.country;
53-
res.send(await informationPerCountry(country));
60+
res.send(
61+
await informationPerCountry(
62+
country,
63+
req.baseUrl.startsWith("/quiet")
64+
)
65+
);
5466
})
5567
);
5668

5769
router.get(
5870
"/",
59-
handleAsync(async (_req, res, _next) => {
60-
res.send(await globalInformation());
71+
handleAsync(async (req, res, _next) => {
72+
res.send(await globalInformation(req.baseUrl.startsWith("/quiet")));
6173
})
6274
);

src/cli.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from "./utils/handlers";
99

1010
const args = argv(process.argv.slice(2));
11-
let { history, mode, help } = args;
11+
let { history, mode, help, quiet } = args;
1212
const country = args._[0];
1313

1414
const { version } = require("../package.json");
@@ -22,23 +22,25 @@ Country: Can be a country name or ISO 3166-1 alpha-2 country code
2222
2323
Options:
2424
--history Show a chart of country's cases of world's cases
25-
--mode Use with --history to make show a chart of cases, deaths, or recovered`;
25+
--mode Use with --history to make show a chart of cases, deaths, or recovered
26+
--quiet Only show necessary information`;
2627

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

3133
if (history === undefined || typeof history === "undefined") {
32-
if (country === undefined) output = await globalInformation();
33-
else output = await informationPerCountry(country);
34+
if (country === undefined) output = await globalInformation(quiet);
35+
else output = await informationPerCountry(country, quiet);
3436
}
3537

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

3941
if (history) {
40-
if (country === undefined) output = await globalHistory(mode);
41-
else output = await historyPerCountry(country, mode);
42+
if (country === undefined) output = await globalHistory(mode, quiet);
43+
else output = await historyPerCountry(country, mode, quiet);
4244
}
4345

4446
console.log(output);

src/utils/addFooterAndGenerateChart.ts renamed to src/utils/generateOutput.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,35 @@ const { version } = require("../../package.json");
99
* @param chartType The type of chart that will be placed on the header
1010
* @param updateTime The unix timestamp from the API of when the data was last updated
1111
* @param data The data formatted into tables
12+
* @param quiet Optional, set to true if the user does not want unnecessary information
1213
* @returns A string containing a formatted table
1314
*/
14-
export const addFooterAndGenerateChart: (
15+
export const generateOutput: (
1516
chartType: string,
1617
updateTime: number,
17-
data: (string | string[])[]
18-
) => string = (chartType, updateTime, data) => {
18+
data: (string | string[])[],
19+
quiet?: boolean
20+
) => string = (chartType, updateTime, data, quiet) => {
21+
quiet = quiet === undefined ? true : quiet;
1922
let header = `COVID-19 Tracker CLI v${version} - ${chartType}`;
2023
let timestamp = getTimestamp(updateTime).yellow;
2124

22-
data.unshift(header, timestamp);
23-
data = data.concat([
24-
"Help: Try to append the URL with /help to learn more...",
25-
"Source: https://disease.sh/v3/covid-19/",
26-
"Code: https://github.com/wareneutron/covid19-tracker-cli",
27-
]);
25+
data.unshift(timestamp);
26+
if (!quiet) data.unshift(header);
27+
28+
if (!quiet)
29+
data = data.concat([
30+
"Help: Try to append the URL with /help to learn more...",
31+
"Source: https://disease.sh/v3/covid-19/",
32+
"Code: https://github.com/wareneutron/covid19-tracker-cli",
33+
]);
2834

2935
let response = generateColorTable(data, "cyan");
30-
response += `\n${getSaying().green}\n`; //saying
31-
response += `\n${"═".repeat(60)}\n`;
36+
if (!quiet) {
37+
response += `\n${getSaying().green}\n`; //saying
38+
response += `\n${"═".repeat(60)}\n`;
39+
}
40+
3241
response += `Love this project? Help us to help others by means of coffee!\n`; // support msg
3342

3443
// Include GCash message if the query is to the PH
@@ -38,12 +47,15 @@ export const addFooterAndGenerateChart: (
3847

3948
// @ts-expect-error: Missing type definitions causes TS to highlight brightRed
4049
response += `(Buy Me A Coffee) warengonza.ga/coffee4dev\n`.brightRed; //BMC link
41-
response += `${"═".repeat(60)}\n`;
42-
response += `Follow me on twitter for more updates!\n`;
43-
response +=
44-
["@warengonzaga", "#covid19trackercli"]
45-
.map((text) => text.black.bgCyan)
46-
.join(" ") + "\n";
50+
51+
if (!quiet) {
52+
response += `${"═".repeat(60)}\n`;
53+
response += `Follow me on twitter for more updates!\n`;
54+
response +=
55+
["@warengonzaga", "#covid19trackercli"]
56+
.map((text) => text.black.bgCyan)
57+
.join(" ") + "\n";
58+
}
4759

4860
return response;
4961
};

src/utils/handlers.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from "axios";
2-
import { addFooterAndGenerateChart } from "./addFooterAndGenerateChart";
2+
import { generateOutput } from "./generateOutput";
33
import { generateAsciichart } from "./generateAsciichart";
44

55
axios.defaults.baseURL = "https://disease.sh/v3/covid-19";
@@ -85,11 +85,13 @@ const getCountryInfo: (
8585
* Today Cases, Today Deaths, Critical, Mortality %, Recovery in a chart
8686
* @param country country code or country name that the user wants to query
8787
* @param mode Mode that the user wants to query must be: "cases" | "deaths" | "recoveries"
88+
* @param quiet tells the response to be in quiet mode or not
8889
*/
8990
export const historyPerCountry: (
9091
country: string,
91-
mode: "cases" | "deaths" | "recovered"
92-
) => Promise<string> = async (country, mode) => {
92+
mode: "cases" | "deaths" | "recovered",
93+
quiet: boolean
94+
) => Promise<string> = async (country, mode, quiet) => {
9395
// Get summary info about a country
9496
let [updated, apiCountryname, countryName, rows] = await getCountryInfo(
9597
country
@@ -114,10 +116,11 @@ export const historyPerCountry: (
114116
rows = rows.concat(chart);
115117

116118
// generate table
117-
let response = addFooterAndGenerateChart(
119+
let response = generateOutput(
118120
`${countryName} Historical Chart`,
119121
updated,
120-
rows
122+
rows,
123+
quiet
121124
);
122125

123126
return response;
@@ -127,10 +130,13 @@ export const historyPerCountry: (
127130
* globalHistory shows a tablechart of the cases of all the countries
128131
* Shows Cases, Deaths, Recovered, Active, Cases/Million
129132
* and a graph of a country's cases
133+
* @param mode Mode that the user wants to query must be: "cases" | "deaths" | "recoveries"
134+
* @param quiet tells the response to be in quiet mode or not
130135
*/
131136
export const globalHistory: (
132-
mode: "cases" | "deaths" | "recovered"
133-
) => Promise<string> = async (mode) => {
137+
mode: "cases" | "deaths" | "recovered",
138+
quiet: boolean
139+
) => Promise<string> = async (mode, quiet) => {
134140
// Get summary info
135141
let [updated, rows] = await getAllInfo();
136142

@@ -150,10 +156,11 @@ export const globalHistory: (
150156
rows.push(`${ mode.charAt(0).toUpperCase() + mode.slice(1) } from ${firstDate} to ${lastDate}`.magenta)
151157
rows = rows.concat(chart);
152158

153-
let response = addFooterAndGenerateChart(
159+
let response = generateOutput(
154160
"Global Historical Chart",
155161
updated,
156-
rows
162+
rows,
163+
quiet
157164
);
158165

159166
return response;
@@ -164,16 +171,19 @@ export const globalHistory: (
164171
* Shows Cases, Deaths, Recovered, Active, Cases/Million
165172
* Today Cases, Today Deaths, Critical, Mortality %, Recovery in a chart
166173
* @param country country code or country name that the user wants to query
174+
* @param quiet tells the response to be in quiet mode or not
167175
*/
168176
export const informationPerCountry: (
169-
country: string
170-
) => Promise<string> = async (country) => {
177+
country: string,
178+
quiet: boolean
179+
) => Promise<string> = async (country, quiet) => {
171180
let [updated, _, countryName, rows] = await getCountryInfo(country);
172181

173-
let response = addFooterAndGenerateChart(
182+
let response = generateOutput(
174183
`${countryName} Update`,
175184
updated,
176-
rows
185+
rows,
186+
quiet
177187
);
178188

179189
// return response;
@@ -183,15 +193,14 @@ export const informationPerCountry: (
183193
/**
184194
* globalInformation tracks the info of all countries
185195
* Shows Cases, Deaths, Recovered, Mortality %, Recovered% in a chart
196+
* @param quiet tells the response to be in quiet mode or not
186197
*/
187-
export const globalInformation: () => Promise<string> = async () => {
198+
export const globalInformation: (quiet: boolean) => Promise<string> = async (
199+
quiet
200+
) => {
188201
const [updated, rowsOfData] = await getAllInfo();
189202

190-
let response = addFooterAndGenerateChart(
191-
"Global Update",
192-
updated,
193-
rowsOfData
194-
);
203+
let response = generateOutput("Global Update", updated, rowsOfData, quiet);
195204

196205
return response;
197206
};

0 commit comments

Comments
 (0)