Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
280f84f
📦 NEW: add open collective URL
warengonzaga Mar 20, 2021
40e3133
👌 IMPROVE: Update funding URL with proper links
warengonzaga Mar 20, 2021
05d8250
👌 IMPROVE: Update .gitignore and dependencies
warengonzaga Mar 20, 2021
9d81d64
use relative image urls #62
jcads Mar 27, 2021
d61e74a
Merge pull request #68 from jcads/fix-img-links
warengonzaga Mar 27, 2021
efadd84
📦 NEW: Complete rewrite
warengonzaga Mar 29, 2021
80545a7
Add editorconfig
Mar 29, 2021
41597e5
Add quiet mode
Mar 29, 2021
0fd572d
Remove styling from error handler for better compatibility
Mar 29, 2021
d1373bf
Add missing newline
Mar 29, 2021
11708da
Add plain mode
Mar 30, 2021
f5c2abe
Add documentation and quiet mode to plain routes
Mar 30, 2021
a82a1ae
Add and improve documentation
Mar 30, 2021
f17145f
Make not found handler send a 404 response code
Mar 30, 2021
818e046
Add function dedicated to handling historical data
Mar 31, 2021
f5bf1e8
Add plain mode to CLI
Mar 31, 2021
dff091f
Point vercel to src/index.ts
Mar 31, 2021
5c9e92d
Point vercel to src/api.ts
Mar 31, 2021
9e36076
Make vercel config simple
Mar 31, 2021
eaa6e2e
Revert broken vercel configuration
Mar 31, 2021
c4293a9
Merge pull request #70 from scinscinscin/v4
warengonzaga Mar 31, 2021
60ba2f1
👌 IMPROVE: Update ignore files
warengonzaga Mar 31, 2021
c0ceb5e
👌 IMPROVE: Update funding information
warengonzaga Mar 31, 2021
72764dd
👌 IMPROVE: Update package file info
warengonzaga Mar 31, 2021
2706199
👌 IMPROVE: Update info
warengonzaga Mar 31, 2021
c9f09a9
👌 IMPROVE: Update help and info
warengonzaga Apr 1, 2021
b55ccd8
Merge pull request #73 from wareneutron/new/info
warengonzaga Apr 1, 2021
991e044
Add central location for links
Apr 1, 2021
3affb03
Replace source link with docs link
Apr 1, 2021
6ba6b5d
Remove GCash message
Apr 1, 2021
f9755e9
Merge pull request #74 from scinscinscin/v4
warengonzaga Apr 1, 2021
650335e
👌 IMPROVE: Update info
warengonzaga Apr 2, 2021
bb94e78
Add linking feature
Apr 3, 2021
170eddc
Remove vercel rewrite
Apr 3, 2021
b30f159
Point vercel to /src/api.ts
Apr 3, 2021
1c74ab6
Merge pull request #76 from scinscinscin/v4
warengonzaga Apr 3, 2021
827feda
Change color code replacer to tilde
Apr 3, 2021
709c9c5
Fix broken connector lines
Apr 3, 2021
1b291f2
Add ENV variable to override colors
Apr 3, 2021
728a5a4
Merge pull request #77 from scinscinscin/v4
warengonzaga Apr 3, 2021
15ea081
👌 IMPROVE: Update info
warengonzaga Apr 3, 2021
16523b1
📦 NEW: Revised code of conduct
warengonzaga Apr 3, 2021
2cdc1f9
📦 NEW: Improved readme file with new banner
warengonzaga Apr 3, 2021
3ac0225
📖 DOC: Typo
warengonzaga Apr 3, 2021
1e5f896
📖 DOC: Add Digital Ocean logo
warengonzaga Apr 4, 2021
4ed2bd6
🤖 TEST: CircleCI config
warengonzaga Apr 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};