UNPKG

better-consoley

Version:

Better-consoley It is a drop-in replacement for Node's default console that provides additional colors in console

78 lines (77 loc) 2.91 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.console = void 0; const chalk_1 = __importDefault(require("chalk")); const isNodeEnvironment = typeof process !== "undefined" && process.versions != null && process.versions.node != null; const infoColor = "#10d3ff"; const successColor = "#23ff23"; const warningColor = "#ffff23"; const errorColor = "#ff2323"; const defaultColors = { info: infoColor, success: successColor, warning: warningColor, error: errorColor, }; function logMessage(message, color, fontWeight = "") { if (!isNodeEnvironment) { exports.console.log(`%c${message}`, `color: ${color}; ${fontWeight}`); } else { exports.console.log(chalk_1.default.hex(color)(message)); } } class CustomConsole { constructor(customColors) { this.colors = Object.assign(Object.assign({}, defaultColors), customColors); } success(message) { logMessage(message, this.colors.success); } warning(message) { logMessage(message, this.colors.warning); } info(message) { logMessage(message, this.colors.info); } error(message) { logMessage(message, this.colors.error, "font-weight: bold"); } table(data) { if (!Array.isArray(data) || data.length === 0) { exports.console.error("%c Invalid data format for table.", `color: ${this.colors.error}`); return; } if (isNodeEnvironment) { try { const Table = require("cli-table3"); const table = new Table({ head: Object.keys(data[0]), colWidths: Array(Object.keys(data[0]).length).fill(20), }); data.forEach((row) => { table.push(Object.values(row)); }); exports.console.log(table.toString()); } catch (err) { exports.console.error("Error loading cli-table3:", err); } } else { exports.console.table(data); } } log(level, message) { this[level](message); } } const customConsoleInstance = new CustomConsole(); if (typeof globalThis !== "undefined") { const originalConsole = globalThis.console; globalThis.console = Object.assign(Object.assign({}, originalConsole), { success: customConsoleInstance.success.bind(customConsoleInstance), warning: customConsoleInstance.warning.bind(customConsoleInstance), info: customConsoleInstance.info.bind(customConsoleInstance), error: customConsoleInstance.error.bind(customConsoleInstance), table: customConsoleInstance.table.bind(customConsoleInstance) }); } exports.console = globalThis.console;