UNPKG

quick-js-cli

Version:
39 lines (32 loc) 1.04 kB
const chalk = require('chalk') const readline = require('readline') const padStart = require('string.prototype.padstart') const format = (label, msg) => { return msg.split('\n').map((line, i) => { return i === 0 ? `${label} ${line}` : padStart(line, chalk.reset(label).length) }).join('\n') } const chalkTag = msg => chalk.bgBlackBright.white.dim(` ${msg} `) exports.log = (msg = '', tag = null) => { process.stdout.write(`${msg}\n`) } exports.error = (msg, tag = null) => { console.error(format(chalk.bgRed(' ERROR ') + (tag ? chalkTag(tag) : ''), chalk.red(msg))) if (msg instanceof Error) { console.error(msg.stack) } } exports.warn = (msg, tag = null) => { console.warn(format(chalk.bgYellow.black(' WARN ') + (tag ? chalkTag(tag) : ''), chalk.yellow(msg))) } exports.clearConsole = title => { if (process.stdout.isTTY) { // 在用户bash里面 readline.cursorTo(process.stdout, 0, 0) readline.clearScreenDown(process.stdout) if (title) { process.stdout.write(title) } } }