diff --git a/lib/logger.js b/lib/logger.js index 5ac8cb70..e640d54e 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -1,36 +1,62 @@ +const colors = { + grey: "\x1b[90m", + red: "\x1b[31m", + yellow: "\x1b[33m", + green: "\x1b[32m", + cyan: "\x1b[36m", + white: "\x1b[37m", + reset: "\x1b[0m", +}; + +const types = { + error: { console: "error", label: "Error", color: "red" }, + warn: { console: "log", label: "Warning", color: "yellow" }, + success: { console: "log", label: "Success", color: "green" }, + info: { console: "info", label: "Info", color: "cyan" }, +}; + /** * Module for pretty cli logs * * @module logger - * @param {string} [type] - Can be any of "error", "warn" or "success" + * @param {string} [type] * @param {string} message + * @returns {string} */ - -const colors = require("picocolors"); - module.exports = function log(type, message) { if (process.env.MIYAGI_JS_API) return; const date = new Date(); - const dateStr = `${date.getFullYear()}/${date - .getMonth() - .toString() - .padStart(2, "0")}/${date - .getDate() - .toString() - .padStart(2, "0")} ${date.getHours()}:${date.getMinutes()}`; + const year = date.getFullYear(); + const month = pad(date.getMonth()); + const day = pad(date.getDate()); + const hours = pad(date.getHours()); + const minutes = pad(date.getMinutes()); + const seconds = pad(date.getSeconds()); + + const dateStr = `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`; - if (type === "error") { - console.error(`${colors.gray(dateStr)} ${colors.red("Error:")} ${message}`); - } else if (type === "warn") { - console.info( - `${colors.gray(dateStr)} ${colors.yellow("Warning:")} ${message}` - ); - } else if (type === "success") { - console.info( - `${colors.gray(dateStr)} ${colors.green("Success:")} ${message}` - ); - } else { - console.info(`${colors.gray(dateStr)} ${colors.cyan("Info:")} ${message}`); - } + return console[types[type].console]( + `${colorize("grey", dateStr)} ${colorize( + types[type].color, + `${types[type].label}:` + )} ${colors.reset}${message}` + ); }; + +/** + * @param {string} color + * @param {string} str + * @returns {string} + */ +function colorize(color, str) { + return `${colors[color]}${str}`; +} + +/** + * @param {Date} value + * @returns {string} + */ +function pad(value) { + return value.toString().padStart(2, "0"); +} diff --git a/package-lock.json b/package-lock.json index def87b47..26a6c066 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,6 @@ "jsdom": "^20.0.0", "marked": "^4.0.17", "node-watch": "^0.7.3", - "picocolors": "^1.0.0", "socket.io": "^4.5.1", "yargs": "^17.5.1" }, @@ -12410,7 +12409,8 @@ "node_modules/picocolors": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz", - "integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==" + "integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==", + "dev": true }, "node_modules/picomatch": { "version": "2.3.1", @@ -26478,7 +26478,8 @@ "picocolors": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz", - "integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==" + "integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==", + "dev": true }, "picomatch": { "version": "2.3.1", diff --git a/package.json b/package.json index 64ed1309..30030687 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,6 @@ "jsdom": "^20.0.0", "marked": "^4.0.17", "node-watch": "^0.7.3", - "picocolors": "^1.0.0", "socket.io": "^4.5.1", "yargs": "^17.5.1" },