Files
vue-cli/packages/@vue/cli-shared-utils/lib/logger.js
Guillaume Chau 0f2ee80a10 feat: generatorAPI.exitLog (#935)
* feat(logger): add tag argument

* feat(generator): add `exitLog` api

* fix(generator): extract toShortId into a util function
2018-03-04 13:22:55 -05:00

52 lines
1.5 KiB
JavaScript

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) => tag ? console.log(format(chalkTag(tag), msg)) : console.log(msg)
exports.info = (msg, tag = null) => {
console.log(format(chalk.bgBlue.black(' INFO ') + (tag ? chalkTag(tag) : ''), msg))
}
exports.done = (msg, tag = null) => {
console.log(format(chalk.bgGreen.black(' DONE ') + (tag ? chalkTag(tag) : ''), msg))
}
exports.warn = (msg, tag = null) => {
console.warn(format(chalk.bgYellow.black(' WARN ') + (tag ? chalkTag(tag) : ''), chalk.yellow(msg)))
}
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.clearConsole = title => {
if (process.stdout.isTTY) {
const blank = '\n'.repeat(process.stdout.rows)
console.log(blank)
readline.cursorTo(process.stdout, 0, 0)
readline.clearScreenDown(process.stdout)
if (title) {
console.log(title)
}
}
}
// silent all logs except errors during tests and keep record
if (process.env.VUE_CLI_TEST) {
require('./_silence')('logs', exports)
}