diff --git a/app.js b/app.js index a804a4b..41953e9 100644 --- a/app.js +++ b/app.js @@ -16,38 +16,40 @@ function errorHandler(error, res) { } app.use(morgan(':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms')); +app.use((req, res, next) => { + res.setHeader('Cache-Control', 'no-cache'); + next(); +}); app.get('/', (req, res) => { + const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null; const format = req.query.format ? req.query.format : ''; if (format.toLowerCase() === 'json') { return getJSONData().then(result => { - res.setHeader('Cache-Control', 's-maxage=900'); return res.json(result); }).catch(error => errorHandler(error, res)); } - return getCompleteTable().then(result => { - res.setHeader('Cache-Control', 's-maxage=900'); + return getCompleteTable({ isCurl }).then(result => { return res.send(result); }).catch(error => errorHandler(error, res)); }); app.get('/:country', (req, res) => { const { country } = req.params; + const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null; const format = req.query.format ? req.query.format : ''; if (!country || 'ALL' === country.toUpperCase()) { if (format.toLowerCase() === 'json') { return getJSONData().then(result => { - res.setHeader('Cache-Control', 's-maxage=900'); return res.json(result); }).catch(error => errorHandler(error, res)); } - return getCompleteTable().then(result => { - res.setHeader('Cache-Control', 's-maxage=900'); + return getCompleteTable({ isCurl }).then(result => { return res.send(result); }).catch(error => errorHandler(error, res)); } @@ -69,13 +71,11 @@ app.get('/:country', (req, res) => { if (format.toLowerCase() === 'json') { return getJSONDataForCountry(iso2).then(result => { - res.setHeader('Cache-Control', 's-maxage=900'); return res.json(result); }).catch(error => errorHandler(error, res)); } - return getCountryTable(iso2).then(result => { - res.setHeader('Cache-Control', 's-maxage=900'); + return getCountryTable({ countryCode: iso2, isCurl }).then(result => { return res.send(result); }).catch(error => errorHandler(error, res)); }); diff --git a/bin/index.js b/bin/index.js index 110de9e..610987c 100755 --- a/bin/index.js +++ b/bin/index.js @@ -1,6 +1,5 @@ #!/usr/bin/env node -const yargonaut = require('yargonaut') - .style('green'); +const yargonaut = require('yargonaut').style('green'); const yargs = require('yargs'); const chalk = require('chalk'); const { getCompleteTable } = require('../lib/corona'); @@ -8,7 +7,7 @@ const { getCountryTable } = require('../lib/byCountry'); const { lookupCountry } = require('../lib/helpers'); const { argv } = yargs - .command('$0 [country]','Tool to COVID-19 statistics for the world or the given country', yargs => + .command('$0 [country]','Tool to track COVID-19 statistics for the world or the given country', yargs => yargs.positional('country', { coerce(arg) { if ('ALL' === arg.toUpperCase()) { @@ -50,8 +49,8 @@ const { argv } = yargs const { emojis, country } = argv; ( country === 'ALL' - ? getCompleteTable(emojis) - : getCountryTable(country, emojis) + ? getCompleteTable({emojis}) + : getCountryTable({ countryCode: country, emojis }) ) .then(console.log) .catch(console.error); \ No newline at end of file diff --git a/lib/api.js b/lib/api.js index 1d79d2e..6f811b0 100644 --- a/lib/api.js +++ b/lib/api.js @@ -1,4 +1,4 @@ -const NodeCache = require( "node-cache" ); +const NodeCache = require('node-cache'); const axios = require('axios'); const myCache = new NodeCache( { stdTTL: 100, checkperiod: 600 } ); const CORONA_ALL_KEY = 'coronaAll'; diff --git a/lib/byCountry.js b/lib/byCountry.js index 197dd0c..79e01c3 100644 --- a/lib/byCountry.js +++ b/lib/byCountry.js @@ -4,6 +4,7 @@ const _ = require('lodash'); const chalk = require('chalk'); const helpers = require('./helpers'); const api = require('./api'); +const stripAnsi = require('strip-ansi'); const { getCountry, getConfirmed, @@ -79,7 +80,7 @@ exports.getJSONDataForCountry = async (countryCode) => { return countryData; } -exports.getCountryTable = async (countryCode, emojis = false) => { +exports.getCountryTable = async ({countryCode, emojis = false, isCurl = true}) => { const head = [ '', 'State', @@ -147,5 +148,30 @@ exports.getCountryTable = async (countryCode, emojis = false) => { }); } const lastUpdated = countryData[0].lastUpdated; + if (!isCurl) { + const template = ` + +
+ + + +${table.toString() + footer(lastUpdated)}
+
+ `;
+ return stripAnsi(template);
+ }
return table.toString() + footer(lastUpdated);
}
\ No newline at end of file
diff --git a/lib/corona.js b/lib/corona.js
index 917215f..44aa5be 100644
--- a/lib/corona.js
+++ b/lib/corona.js
@@ -4,6 +4,7 @@ const _ = require('lodash');
const chalk = require('chalk');
const helpers = require('./helpers');
const api = require('./api');
+const stripAnsi = require('strip-ansi');
const {
getCountry,
getConfirmed,
@@ -79,7 +80,7 @@ function extraStats(dataArr) {
);
}
-exports.getCompleteTable = async (emojis = false) => {
+exports.getCompleteTable = async ({isCurl = true, emojis = false}) => {
const head = [
'',
'Country',
@@ -139,5 +140,30 @@ exports.getCompleteTable = async (emojis = false) => {
table.push({ [rank++]: values })
});
const lastUpdated = countryData[0].lastUpdated;
+ if (!isCurl) {
+ const template = `
+
+
+
+
+
+ ${table.toString() + footer(lastUpdated)}
+
+ `
+ return stripAnsi(template);
+ }
return table.toString() + footer(lastUpdated);
-}
\ No newline at end of file
+}
diff --git a/package-lock.json b/package-lock.json
index 933035a..775b5a7 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "coronavirus-tracker-cli",
- "version": "0.3.0",
+ "version": "0.4.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@@ -34,10 +34,9 @@
}
},
"ansi-regex": {
- "version": "3.0.0",
- "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz",
- "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=",
- "dev": true
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.0.tgz",
+ "integrity": "sha512-bY6fj56OUQ0hU1KjFNDQuJFezqKdrAyFdIevADiqrWHwSlbmBNMHp5ak2f40Pm8JTFyM2mqxkG6ngkHO11f/lg=="
},
"ansi-styles": {
"version": "4.2.1",
@@ -1567,15 +1566,31 @@
"requires": {
"is-fullwidth-code-point": "^2.0.0",
"strip-ansi": "^4.0.0"
+ },
+ "dependencies": {
+ "ansi-regex": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz",
+ "integrity": "sha1-7QMXwyIGT3lGbAKWa922Bas32Zg=",
+ "dev": true
+ },
+ "strip-ansi": {
+ "version": "4.0.0",
+ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz",
+ "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=",
+ "dev": true,
+ "requires": {
+ "ansi-regex": "^3.0.0"
+ }
+ }
}
},
"strip-ansi": {
- "version": "4.0.0",
- "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-4.0.0.tgz",
- "integrity": "sha1-qEeQIusaw2iocTibY1JixQXuNo8=",
- "dev": true,
+ "version": "6.0.0",
+ "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.0.tgz",
+ "integrity": "sha512-AuvKTrTfQNYNIctbR1K/YGTR1756GycPsg7b9bdV9Duqur4gv6aKqHXah67Z8ImS7WEz5QVcOtlfW2rZEugt6w==",
"requires": {
- "ansi-regex": "^3.0.0"
+ "ansi-regex": "^5.0.0"
}
},
"strip-eof": {
diff --git a/package.json b/package.json
index 6926344..11d7c87 100644
--- a/package.json
+++ b/package.json
@@ -7,7 +7,7 @@
"corona": "./bin/index.js"
},
"scripts": {
- "dev": "nodemon app.js",
+ "dev": "nodemon --inspect app.js",
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
@@ -29,6 +29,7 @@
"moment": "^2.24.0",
"morgan": "^1.9.1",
"node-cache": "^5.1.0",
+ "strip-ansi": "^6.0.0",
"yargonaut": "^1.1.4",
"yargs": "15.3.1"
},