const process = require('process') const styles = require('ansi-styles') // via https://github.com/sindresorhus/is-unicode-supported/blob/main/index.js function isUnicodeSupported() { if (process.platform !== 'win32') return process.env.TERM !== 'linux'; // Linux console (kernel) return Boolean(process.env.CI) || Boolean(process.env.WT_SESSION) // Windows Terminal || process.env.ConEmuTask === '{cmd::Cmder}' // ConEmu and cmder || process.env.TERM_PROGRAM === 'vscode' || process.env.TERM === 'xterm-256color' || process.env.TERM === 'alacritty'; } const allowed = isUnicodeSupported() const SPACES = ' ' const SUCCESS = allowed ? '✔' : '√' const INFO = allowed ? 'ℹ' : 'i' const WARNING = allowed ? '⚠' : '‼' const ERROR = allowed ? '✖' : '×' const colors = { default: ['white', ''], success: ['greenBright', `${SUCCESS}${SPACES}`], info: ['cyanBright', `${INFO}${SPACES}`], warning: ['yellowBright', `${WARNING}${SPACES}`], error: ['redBright', `${ERROR}${SPACES}`] } function log(type, msg) { const [color, prefix] = colors[type] || colors.default console.log(`${styles[color].open}${prefix}${msg}${styles[color].close}`) } const success = log.bind(null, 'success') const info = log.bind(null, 'info') const warning = log.bind(null, 'warning') const error = log.bind(null, 'error') console.log('Nice logs') success('Success! Yay it worked') info('Info: Additional details here') warning('Warning: Watch out') error('Error: Oh no!')