Skip to content
Prev Previous commit
Next Next commit
Add quiet mode
  • Loading branch information
scinorandex committed Mar 29, 2021
commit 41597e5ede026d80e3db1f629d786ed0d14a0902
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ debug.log
.DS_Store
.now
.env
dist/
12 changes: 6 additions & 6 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ const app = express();
app.use(morgan("common"));
app.use(userAgentMiddleware);

app.use("/", router);
app.use("/", errorHandler);
app.use(["/quiet", "/"], router);
app.use(["/quiet", "/"], errorHandler);

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

app.listen(port, () => {
console.log(`Express listening on port ${port}`);
console.log(`Express listening on port ${port}`);
});
22 changes: 17 additions & 5 deletions src/api/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ router.get(

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

Expand All @@ -42,21 +42,33 @@ router.get(

// if the mode is not in the api then return to next handler
if (!["cases", "deaths", "recovered"].includes(mode)) return next();
res.send(await historyPerCountry(country, mode));
res.send(
await historyPerCountry(
country,
mode,
req.baseUrl.startsWith("/quiet")
)
);
})
);

router.get(
"/:country",
handleAsync(async (req, res, _next) => {
console.log(req.path);
const country = req.params.country;
res.send(await informationPerCountry(country));
res.send(
await informationPerCountry(
country,
req.baseUrl.startsWith("/quiet")
)
);
})
);

router.get(
"/",
handleAsync(async (_req, res, _next) => {
res.send(await globalInformation());
handleAsync(async (req, res, _next) => {
res.send(await globalInformation(req.baseUrl.startsWith("/quiet")));
})
);
14 changes: 8 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "./utils/handlers";

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

const { version } = require("../package.json");
Expand All @@ -22,23 +22,25 @@ 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`;
--mode Use with --history to make show a chart of cases, deaths, or recovered
--quiet Only show necessary information`;

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

if (history === undefined || typeof history === "undefined") {
if (country === undefined) output = await globalInformation();
else output = await informationPerCountry(country);
if (country === undefined) output = await globalInformation(quiet);
else output = await informationPerCountry(country, quiet);
}

mode = mode === undefined || typeof mode === "undefined" ? "cases" : mode; // defauilt 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);
else output = await historyPerCountry(country, mode);
if (country === undefined) output = await globalHistory(mode, quiet);
else output = await historyPerCountry(country, mode, quiet);
}

console.log(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,35 @@ const { version } = require("../../package.json");
* @param chartType The type of chart that will be placed on the header
* @param updateTime The unix timestamp from the API of when the data was last updated
* @param data The data formatted into tables
* @param quiet Optional, set to true if the user does not want unnecessary information
* @returns A string containing a formatted table
*/
export const addFooterAndGenerateChart: (
export const generateOutput: (
chartType: string,
updateTime: number,
data: (string | string[])[]
) => string = (chartType, updateTime, data) => {
data: (string | string[])[],
quiet?: boolean
) => string = (chartType, updateTime, data, quiet) => {
quiet = quiet === undefined ? true : quiet;
let header = `COVID-19 Tracker CLI v${version} - ${chartType}`;
let timestamp = getTimestamp(updateTime).yellow;

data.unshift(header, timestamp);
data = data.concat([
"Help: Try to append the URL with /help to learn more...",
"Source: https://disease.sh/v3/covid-19/",
"Code: https://github.com/wareneutron/covid19-tracker-cli",
]);
data.unshift(timestamp);
if (!quiet) data.unshift(header);

if (!quiet)
data = data.concat([
"Help: Try to append the URL with /help to learn more...",
"Source: https://disease.sh/v3/covid-19/",
"Code: https://github.com/wareneutron/covid19-tracker-cli",
]);

let response = generateColorTable(data, "cyan");
response += `\n${getSaying().green}\n`; //saying
response += `\n${"═".repeat(60)}\n`;
if (!quiet) {
response += `\n${getSaying().green}\n`; //saying
response += `\n${"═".repeat(60)}\n`;
}

response += `Love this project? Help us to help others by means of coffee!\n`; // support msg

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

// @ts-expect-error: Missing type definitions causes TS to highlight brightRed
response += `(Buy Me A Coffee) warengonza.ga/coffee4dev\n`.brightRed; //BMC link
response += `${"═".repeat(60)}\n`;
response += `Follow me on twitter for more updates!\n`;
response +=
["@warengonzaga", "#covid19trackercli"]
.map((text) => text.black.bgCyan)
.join(" ") + "\n";

if (!quiet) {
response += `${"═".repeat(60)}\n`;
response += `Follow me on twitter for more updates!\n`;
response +=
["@warengonzaga", "#covid19trackercli"]
.map((text) => text.black.bgCyan)
.join(" ") + "\n";
}

return response;
};
47 changes: 28 additions & 19 deletions src/utils/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios";
import { addFooterAndGenerateChart } from "./addFooterAndGenerateChart";
import { generateOutput } from "./generateOutput";
import { generateAsciichart } from "./generateAsciichart";

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

// generate table
let response = addFooterAndGenerateChart(
let response = generateOutput(
`${countryName} Historical Chart`,
updated,
rows
rows,
quiet
);

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

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

let response = addFooterAndGenerateChart(
let response = generateOutput(
"Global Historical Chart",
updated,
rows
rows,
quiet
);

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

let response = addFooterAndGenerateChart(
let response = generateOutput(
`${countryName} Update`,
updated,
rows
rows,
quiet
);

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

let response = addFooterAndGenerateChart(
"Global Update",
updated,
rowsOfData
);
let response = generateOutput("Global Update", updated, rowsOfData, quiet);

return response;
};