Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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 Windows CMD support
  • Loading branch information
Waren Gonzaga committed Mar 23, 2020
commit 68b1c69bc6f27e5731dd026ad01642b9ef042b54
35 changes: 35 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ app.get('/', async (req, res, next) => {
return next();
});

// for cmd and powershell
app.get(['/plain','/cmd','/basic'], async (req, res, next) => {
const userAgent = req.headers['user-agent'],
api = await axios.get(`${apiBaseURL}/all`),
data = api.data;
if (util.isCommandline(userAgent)) {
await res.send(covid19.plainglobaltracker(
data.cases, data.deaths,
data.recovered, data.updated
));
return null;
}
return next();
});

// by country route for covid19 tracker
app.get('/:country', async (req, res, next) => {
const userAgent = req.headers['user-agent'],
Expand All @@ -50,6 +65,26 @@ app.get('/:country', async (req, res, next) => {
return next();
});

// by country route for covid19 tracker
app.get(['/plain/:country','/cmd/:country','/basic/:country'], async (req, res, next) => {
const userAgent = req.headers['user-agent'],
countryData = req.params.country,
api = await axios.get(`${apiBaseURL}/countries/${countryData}`),
all = await axios.get(`${apiBaseURL}/all`),
u = all.data,
d = api.data;
if (util.isCommandline(userAgent)) {
await res.send(covid19.plaincountrytracker(
d.country, d.cases, d.todayCases,
d.deaths, d.todayDeaths, d.recovered,
d.active, d.critical, d.casesPerOneMillion,
u.updated
));
return null;
}
return next();
});

app.get('*', (req, res) => res.send(`
Sorry, CLI version is only available at the moment...
\n
Expand Down
102 changes: 86 additions & 16 deletions lib/cli/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
/* eslint-disable no-await-in-loop */

const style = require('ansi-styles'),
const color = require('ansi-styles'),
fs = require('fs'),
table3 = require('cli-table3');

// ansi style and colors
// ansi colors

const
// table color
tblclr = (border) => {return cyan(border);},
// normal ansi colors
white = (txt) => {return style.white.open+txt+style.white.close;},
black = (txt) => {return style.black.open+txt+style.black.close;},
green = (txt) => {return style.green.open+txt+style.green.close;},
cyan = (txt) => {return style.cyan.open+txt+style.cyan.close;},
magenta = (txt) => {return style.magenta.open+txt+style.magenta.close;},
yellow = (txt) => {return style.yellow.open+txt+style.yellow.close;},
red = (txt) => {return style.red.open+txt+style.red.close;},
white = (txt) => {return color.white.open+txt+color.white.close;},
black = (txt) => {return color.black.open+txt+color.black.close;},
green = (txt) => {return color.green.open+txt+color.green.close;},
cyan = (txt) => {return color.cyan.open+txt+color.cyan.close;},
magenta = (txt) => {return color.magenta.open+txt+color.magenta.close;},
yellow = (txt) => {return color.yellow.open+txt+color.yellow.close;},
red = (txt) => {return color.red.open+txt+color.red.close;},
// bright ansi colors
cyanBright = (txt) => {return style.cyanBright.open+txt+style.cyanBright.close;},
magentaBright = (txt) => {return style.magentaBright.open+txt+style.magentaBright.close;},
redBright = (txt) => {return style.redBright.open+txt+style.redBright.close;},
cyanBright = (txt) => {return color.cyanBright.open+txt+color.cyanBright.close;},
magentaBright = (txt) => {return color.magentaBright.open+txt+color.magentaBright.close;},
redBright = (txt) => {return color.redBright.open+txt+color.redBright.close;},
// background ansi color
cyanBG = (txt) => {return style.bgCyan.open+txt+style.bgCyan.close;};
cyanBG = (txt) => {return color.bgCyan.open+txt+color.bgCyan.close;},
// horizontal line
line = '-'.repeat(60);


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

// format data
const formatNumber = (num) => {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
const formatNumber = (value) => {
return value.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
};

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

exports.plainglobaltracker = (c, d, r, u) => {
const cases = c, deaths = d, recovered = r, asof = new Date(u),
mortalityPercentage = (d/c)*100, recoveredPercentage = (r/c)*100;

const visual = `
${line}
COVID-19 Tracker CLI v${pkg.version} - Global Update
${line}
As of ${asof.toLocaleString()} [Date: ${currentdate}]
${line}
Cases | ${formatNumber(cases)}
Deaths | ${formatNumber(deaths)}
Mortality % | ${mortalityPercentage.toFixed(2)}
Recovered | ${formatNumber(recovered)}
Recovery % | ${recoveredPercentage.toFixed(2)}
${line}
${source}
${line}
${repo}
${line}
`;
return visual+plainfooter;
};

exports.plaincountrytracker = (n, c, tC, d, tD, r, a, cl, cPOM, u) => {
const name = n, cases = c, todayCases = tC,
deaths = d, todayDeaths = tD, recovered = r,
active = a, critical = cl, casesPerOneMillion = cPOM,
mortalityPercentage = (d/c)*100, recoveredPercentage = (r/c)*100,
asof = new Date(u);

const visual = `
${line}
COVID-19 Tracker CLI v${pkg.version} - ${name} Update
${line}
As of ${asof.toLocaleString()} [Date: ${currentdate}]
${line}
Cases | ${formatNumber(cases)}
Today Cases | ${formatNumber(todayCases)}
Deaths | ${formatNumber(deaths)}
Today Deaths | ${formatNumber(todayDeaths)}
Critical | ${formatNumber(critical)}
Mortality % | ${mortalityPercentage.toFixed(2)}
Recovered | ${formatNumber(recovered)}
Recovery % | ${recoveredPercentage.toFixed(2)}
Active | ${formatNumber(active)}
Cases/Million | ${formatNumber(casesPerOneMillion)}
${line}
${source}
${line}
${repo}
${line}
`;
return visual+plainfooter;
};

// cli footer
const footer = `
Always wash your hands, stay safe...
Expand All @@ -116,3 +174,15 @@ const footer = `
---
Follow ${cyanBG(black('@warengonzaga'))} for more updates!
\n`;

// cli plain footer
const plainfooter = `
Always wash your hands, stay safe...

---
Love this project? Please consider buying me a cup of coffee!
[ warengonzaga.com/buymeacoffee ]

---
Follow [ @warengonzaga ] for more updates!
\n`;