Skip to content

Commit 68b1c69

Browse files
author
Waren Gonzaga
committed
Add Windows CMD support
1 parent 1a2e8d9 commit 68b1c69

File tree

2 files changed

+121
-16
lines changed

2 files changed

+121
-16
lines changed

app.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ app.get('/', async (req, res, next) => {
3030
return next();
3131
});
3232

33+
// for cmd and powershell
34+
app.get(['/plain','/cmd','/basic'], async (req, res, next) => {
35+
const userAgent = req.headers['user-agent'],
36+
api = await axios.get(`${apiBaseURL}/all`),
37+
data = api.data;
38+
if (util.isCommandline(userAgent)) {
39+
await res.send(covid19.plainglobaltracker(
40+
data.cases, data.deaths,
41+
data.recovered, data.updated
42+
));
43+
return null;
44+
}
45+
return next();
46+
});
47+
3348
// by country route for covid19 tracker
3449
app.get('/:country', async (req, res, next) => {
3550
const userAgent = req.headers['user-agent'],
@@ -50,6 +65,26 @@ app.get('/:country', async (req, res, next) => {
5065
return next();
5166
});
5267

68+
// by country route for covid19 tracker
69+
app.get(['/plain/:country','/cmd/:country','/basic/:country'], async (req, res, next) => {
70+
const userAgent = req.headers['user-agent'],
71+
countryData = req.params.country,
72+
api = await axios.get(`${apiBaseURL}/countries/${countryData}`),
73+
all = await axios.get(`${apiBaseURL}/all`),
74+
u = all.data,
75+
d = api.data;
76+
if (util.isCommandline(userAgent)) {
77+
await res.send(covid19.plaincountrytracker(
78+
d.country, d.cases, d.todayCases,
79+
d.deaths, d.todayDeaths, d.recovered,
80+
d.active, d.critical, d.casesPerOneMillion,
81+
u.updated
82+
));
83+
return null;
84+
}
85+
return next();
86+
});
87+
5388
app.get('*', (req, res) => res.send(`
5489
Sorry, CLI version is only available at the moment...
5590
\n

lib/cli/index.js

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
/* eslint-disable no-await-in-loop */
22

3-
const style = require('ansi-styles'),
3+
const color = require('ansi-styles'),
44
fs = require('fs'),
55
table3 = require('cli-table3');
66

7-
// ansi style and colors
7+
// ansi colors
88

99
const
1010
// table color
1111
tblclr = (border) => {return cyan(border);},
1212
// normal ansi colors
13-
white = (txt) => {return style.white.open+txt+style.white.close;},
14-
black = (txt) => {return style.black.open+txt+style.black.close;},
15-
green = (txt) => {return style.green.open+txt+style.green.close;},
16-
cyan = (txt) => {return style.cyan.open+txt+style.cyan.close;},
17-
magenta = (txt) => {return style.magenta.open+txt+style.magenta.close;},
18-
yellow = (txt) => {return style.yellow.open+txt+style.yellow.close;},
19-
red = (txt) => {return style.red.open+txt+style.red.close;},
13+
white = (txt) => {return color.white.open+txt+color.white.close;},
14+
black = (txt) => {return color.black.open+txt+color.black.close;},
15+
green = (txt) => {return color.green.open+txt+color.green.close;},
16+
cyan = (txt) => {return color.cyan.open+txt+color.cyan.close;},
17+
magenta = (txt) => {return color.magenta.open+txt+color.magenta.close;},
18+
yellow = (txt) => {return color.yellow.open+txt+color.yellow.close;},
19+
red = (txt) => {return color.red.open+txt+color.red.close;},
2020
// bright ansi colors
21-
cyanBright = (txt) => {return style.cyanBright.open+txt+style.cyanBright.close;},
22-
magentaBright = (txt) => {return style.magentaBright.open+txt+style.magentaBright.close;},
23-
redBright = (txt) => {return style.redBright.open+txt+style.redBright.close;},
21+
cyanBright = (txt) => {return color.cyanBright.open+txt+color.cyanBright.close;},
22+
magentaBright = (txt) => {return color.magentaBright.open+txt+color.magentaBright.close;},
23+
redBright = (txt) => {return color.redBright.open+txt+color.redBright.close;},
2424
// background ansi color
25-
cyanBG = (txt) => {return style.bgCyan.open+txt+style.bgCyan.close;};
25+
cyanBG = (txt) => {return color.bgCyan.open+txt+color.bgCyan.close;},
26+
// horizontal line
27+
line = '-'.repeat(60);
2628

2729

2830
// package.json information
2931
const pkg = JSON.parse(fs.readFileSync('package.json'));
3032

3133
// format data
32-
const formatNumber = (num) => {
33-
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
34+
const formatNumber = (value) => {
35+
return value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
3436
};
3537

3638
// time data
@@ -96,7 +98,7 @@ exports.covid19countrytracker = (n, c, tC, d, tD, r, a, cl, cPOM, u) => {
9698
});
9799
table.push(
98100
[{colSpan:5,content:yellow('As of '+asof.toLocaleString()+' [Date:'+currentdate+']')}],
99-
[magenta('Cases'), red('Deaths'), green('Recovered'), cyan('Active'), cyanBright('Cases Per Million')],
101+
[magenta('Cases'), red('Deaths'), green('Recovered'), cyan('Active'), cyanBright('Cases/Million')],
100102
[formatNumber(cases), formatNumber(deaths), formatNumber(recovered), formatNumber(active), formatNumber(casesPerOneMillion)],
101103
[magentaBright('Today Cases'), redBright('Today Deaths'), redBright('Critical'), red('Mortality %'), green('Recovered %')],
102104
[formatNumber(todayCases), formatNumber(todayDeaths), formatNumber(critical), mortalityPercentage.toFixed(2), recoveredPercentage.toFixed(2)],
@@ -105,6 +107,62 @@ exports.covid19countrytracker = (n, c, tC, d, tD, r, a, cl, cPOM, u) => {
105107
return table.toString()+'\n'+footer;
106108
};
107109

110+
exports.plainglobaltracker = (c, d, r, u) => {
111+
const cases = c, deaths = d, recovered = r, asof = new Date(u),
112+
mortalityPercentage = (d/c)*100, recoveredPercentage = (r/c)*100;
113+
114+
const visual = `
115+
${line}
116+
COVID-19 Tracker CLI v${pkg.version} - Global Update
117+
${line}
118+
As of ${asof.toLocaleString()} [Date: ${currentdate}]
119+
${line}
120+
Cases | ${formatNumber(cases)}
121+
Deaths | ${formatNumber(deaths)}
122+
Mortality % | ${mortalityPercentage.toFixed(2)}
123+
Recovered | ${formatNumber(recovered)}
124+
Recovery % | ${recoveredPercentage.toFixed(2)}
125+
${line}
126+
${source}
127+
${line}
128+
${repo}
129+
${line}
130+
`;
131+
return visual+plainfooter;
132+
};
133+
134+
exports.plaincountrytracker = (n, c, tC, d, tD, r, a, cl, cPOM, u) => {
135+
const name = n, cases = c, todayCases = tC,
136+
deaths = d, todayDeaths = tD, recovered = r,
137+
active = a, critical = cl, casesPerOneMillion = cPOM,
138+
mortalityPercentage = (d/c)*100, recoveredPercentage = (r/c)*100,
139+
asof = new Date(u);
140+
141+
const visual = `
142+
${line}
143+
COVID-19 Tracker CLI v${pkg.version} - ${name} Update
144+
${line}
145+
As of ${asof.toLocaleString()} [Date: ${currentdate}]
146+
${line}
147+
Cases | ${formatNumber(cases)}
148+
Today Cases | ${formatNumber(todayCases)}
149+
Deaths | ${formatNumber(deaths)}
150+
Today Deaths | ${formatNumber(todayDeaths)}
151+
Critical | ${formatNumber(critical)}
152+
Mortality % | ${mortalityPercentage.toFixed(2)}
153+
Recovered | ${formatNumber(recovered)}
154+
Recovery % | ${recoveredPercentage.toFixed(2)}
155+
Active | ${formatNumber(active)}
156+
Cases/Million | ${formatNumber(casesPerOneMillion)}
157+
${line}
158+
${source}
159+
${line}
160+
${repo}
161+
${line}
162+
`;
163+
return visual+plainfooter;
164+
};
165+
108166
// cli footer
109167
const footer = `
110168
Always wash your hands, stay safe...
@@ -116,3 +174,15 @@ const footer = `
116174
---
117175
Follow ${cyanBG(black('@warengonzaga'))} for more updates!
118176
\n`;
177+
178+
// cli plain footer
179+
const plainfooter = `
180+
Always wash your hands, stay safe...
181+
182+
---
183+
Love this project? Please consider buying me a cup of coffee!
184+
[ warengonzaga.com/buymeacoffee ]
185+
186+
---
187+
Follow [ @warengonzaga ] for more updates!
188+
\n`;

0 commit comments

Comments
 (0)