diff --git a/src/api.ts b/src/api.ts index 8dd1803..978a078 100644 --- a/src/api.ts +++ b/src/api.ts @@ -4,8 +4,8 @@ import { errorHandler } from "./api/errorHandler"; import { plainRouter } from "./api/plainRouter"; import { router } from "./api/router"; import { userAgentMiddleware } from "./api/userAgent"; +import { lines } from "./utils/getResponses"; -const { version } = require("../package.json"); const port = parseInt(process.env.PORT!) || 7070; const app = express(); @@ -23,12 +23,7 @@ app.use(["/quiet", "/"], router); app.use("/", errorHandler); // Not found handler -app.use("*", (_req, res) => - res.status(404).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.use("*", (_req, res) => res.status(404).send(lines.notFound)); app.listen(port, () => { console.log(`Express listening on port ${port}`); diff --git a/src/api/userAgent.ts b/src/api/userAgent.ts index b3c65ad..c1f39a1 100644 --- a/src/api/userAgent.ts +++ b/src/api/userAgent.ts @@ -1,5 +1,5 @@ import { Request, Response, NextFunction } from "express"; -const { version } = require("../../package.json"); +import { lines } from "../utils/getResponses"; // Type of middleware and handler export type Handler = ( @@ -28,9 +28,7 @@ export const userAgentMiddleware: Handler = (req, res, next) => { */ const userAgent = req.headers["user-agent"]; if (!isTerminal(userAgent)) { - res.send( - `Welcome to COVID-19 Tracker CLI v${version} by Waren Gonzaga with Wareneutron Developers\nPlease visit: https://warengonza.ga/covid19-tracker-cli\n` - ); + res.send(lines.notFound); return; } diff --git a/src/cli.ts b/src/cli.ts index b608747..df06217 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,4 +1,5 @@ import argv from "minimist"; +import { lines, welcomeMessage } from "./utils/getResponses"; import { globalHistory, globalInformation, @@ -16,9 +17,7 @@ const args = argv(process.argv.slice(2)); let { history, mode, help, quiet, plain } = args; const country = args._[0]; -const { version } = require("../package.json"); - -const helpMessage = `COVID-19 Tracker & CLI v${version} by Waren Gonzaga with Wareneutron Developers +const helpMessage = `${welcomeMessage} Usage: covid [COUNTRY] [OPTIONS...] Country: Can be a country name or ISO 3166-1 alpha-2 country code @@ -32,9 +31,9 @@ Options: --plain Enable plain mode Useful Links: - Docs: docs.wareneutron.com/covid19-tracker-cli - Repo: repo.wareneutron.com/covid19-tracker-cli - Donate: wareneutron.com/donate`; + ${lines.docsLink} + ${lines.WNrepoLink} + ${lines.WNDonateLink}`; let output: string = ""; const main = async () => { @@ -72,7 +71,11 @@ const main = async () => { } } - console.log(output); + // remove magic? newline + let response = output.split("\n"); + response.pop(); + + console.log(response.join("\n")); }; main().catch((err) => { diff --git a/src/utils/generateOutput.ts b/src/utils/generateOutput.ts index fb2ef69..238ea02 100644 --- a/src/utils/generateOutput.ts +++ b/src/utils/generateOutput.ts @@ -1,8 +1,7 @@ import { generateColorTable } from "./generateTable"; import { getTimestamp } from "./getTimestamp"; import { getSaying } from "./getSaying"; - -const { version } = require("../../package.json"); +import { lines } from "./getResponses"; /** * @@ -19,7 +18,7 @@ export const generateOutput: ( quiet?: boolean ) => string = (chartType, updateTime, data, quiet) => { quiet = quiet === undefined ? true : quiet; - let header = `COVID-19 Tracker & CLI v${version} - ${chartType}`; + let header = `${lines.defaultHeader} - ${chartType}`; let timestamp = getTimestamp(updateTime).yellow; data.unshift(timestamp); @@ -27,9 +26,9 @@ export const generateOutput: ( if (!quiet) data = data.concat([ - "Help: Try to append the URL with /help to learn more...", - "Docs: docs.wareneutron.com/covid19-tracker-cli", - "Repo: repo.wareneutron.com/covid19-tracker-cli", + lines.helpMessage, + lines.docsLink, + lines.WNrepoLink, ]); let response = generateColorTable(data, "cyan"); @@ -38,24 +37,19 @@ export const generateOutput: ( 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 - response += chartType.toLowerCase().includes("philippines") - ? "(GCash) +639176462753".blue + "\n" - : ""; + response += lines.sponsorMessage; // support msg // @ts-expect-error: Missing type definitions causes TS to highlight brightRed - response += `(Buy Us A Coffee) wareneutron.com/donate\n`.brightRed; //BMC link + response += `${lines.BMCLink}\n`.brightRed; //BMC link if (!quiet) { response += `${"═".repeat(60)}\n`; - response += `Follow me on twitter for more updates!\n`; + response += `${lines.twitterPlug}`; response += - ["@warengonzaga", "#covid19trackercli"] - .map((text) => text.black.bgCyan) - .join(" ") + "\n"; + lines.handleHashtag.map((text) => text.black.bgCyan).join(" ") + + "\n"; } + response += "\n"; return response; }; diff --git a/src/utils/generatePlainOutput.ts b/src/utils/generatePlainOutput.ts index cb4b4c2..6ee16b6 100644 --- a/src/utils/generatePlainOutput.ts +++ b/src/utils/generatePlainOutput.ts @@ -1,7 +1,7 @@ import { PlainData } from "./getInformation"; +import { lines } from "./getResponses"; import { getSaying } from "./getSaying"; import { getTimestamp } from "./getTimestamp"; -const { version } = require("../../package.json"); /** * @param info The plain data that will be shown at the top in two columns @@ -20,15 +20,10 @@ export const generatePlainOutput: ( let line = extraRows === undefined ? "-".repeat(60) : "-".repeat(68); line += "\n"; - let header = `COVID-19 Tracker & CLI v${version} - ${chartType}`; + let header = `${lines.defaultHeader} - ${chartType}`; let timestamp = getTimestamp(metainfo.updated as number); let saying = getSaying(); - // Include GCash message if the query is to the PH - let GCashMessage = chartType.toLowerCase().includes("philippines") - ? "(GCash) +639176462753\n" - : ""; - // Generate table let table = ""; @@ -65,19 +60,17 @@ export const generatePlainOutput: ( // Add the help msg and other messages if (!quiet) responseArray = responseArray.concat([ - "Help: Try to append the URL with /help to learn more...", - "Source: https://www.worldometers.info/coronavirus/", - "Code: https://github.com/warengonzaga/covid19-tracker-cli", + lines.helpMessage, + lines.docsLink, + lines.WNrepoLink, `\n${saying}\n`, ]); - responseArray.push( - `Love this project? Help us to help others by means of coffee!\n${GCashMessage}(Buy Me A Coffee) warengonza.ga/coffee4dev` - ); + responseArray.push(`${lines.sponsorMessage}${lines.BMCLink}`); if (!quiet) responseArray.push( - `Follow me on twitter for more updates!\n@warengonzaga #covid19trackercli` + `${lines.twitterPlug}${lines.handleHashtag.join(" ")}` ); // Construct the final output @@ -94,6 +87,5 @@ export const generatePlainOutput: ( .join("\n"); response += "\n"; - return response; }; diff --git a/src/utils/getResponses.ts b/src/utils/getResponses.ts new file mode 100644 index 0000000..d170029 --- /dev/null +++ b/src/utils/getResponses.ts @@ -0,0 +1,18 @@ +const { version } = require("../../package.json"); + +// This file is a centralized location to get responses to the user +// Such as the BMC link, the twitter link, github repo page etc. + +export const welcomeMessage = `Welcome to COVID-19 Tracker & CLI v${version} by Waren Gonzaga with Wareneutron Developers`; +export const lines = { + notFound: `\n${welcomeMessage}\n\nPlease visit: https://warengonza.ga/covid19-tracker-cli\n\n`, + defaultHeader: `COVID-19 Tracker & CLI v${version}`, + helpMessage: `Help: Try to append the URL with /help to learn more...`, + sponsorMessage: `Love this project? Help us to help others by means of coffee!\n`, + BMCLink: `(Buy Me A Coffee) warengonza.ga/coffee4dev`, + twitterPlug: `Follow me on twitter for more updates!\n`, + handleHashtag: ["@warengonzaga", "#covid19trackercli"], + docsLink: "Docs: docs.wareneutron.com/covid19-tracker-cli", + WNrepoLink: "Repo: repo.wareneutron.com/covid19-tracker-cli", + WNDonateLink: "Donate: wareneutron.com/donate", +};