按照 consola 的风格改进色彩
This commit is contained in:
119
coco.js
Normal file
119
coco.js
Normal file
@@ -0,0 +1,119 @@
|
||||
// colors only works in nodejs cli
|
||||
// consola works in nodejs and browser
|
||||
|
||||
const colors = require('colors')
|
||||
|
||||
function routeNow () {
|
||||
const pageNow = globalThis.getCurrentPages()[globalThis.getCurrentPages().length - 1]
|
||||
return pageNow()?.route || 'VueApp'
|
||||
}
|
||||
|
||||
module.exports =
|
||||
globalThis.uni && globalThis.UniApp // && globalThis.getApp?.()?.constructor?.name === 'Vue'
|
||||
? {
|
||||
// 客户端 uniapp
|
||||
cclog (...args) {
|
||||
console.log(
|
||||
`%c ${new Date().toJSON()} ${routeNow()}`,
|
||||
'background: #808080; border-radius: 0.5em;color: white; font-weight: bold; padding: 2px 0.5em;',
|
||||
...args
|
||||
)
|
||||
},
|
||||
ccinfo (...args) {
|
||||
console.info(
|
||||
`%cℹ️ ${new Date().toJSON()} ${routeNow()}`,
|
||||
'background: #0000ff; border-radius: 0.5em;color: white; font-weight: bold; padding: 2px 0.5em;',
|
||||
...args
|
||||
)
|
||||
},
|
||||
ccgood (...args) {
|
||||
console.info(
|
||||
`%c✅ ${new Date().toJSON()} ${routeNow()}`,
|
||||
'background: #2ecc71; border-radius: 0.5em;color: white; font-weight: bold; padding: 2px 0.5em;',
|
||||
...args
|
||||
)
|
||||
},
|
||||
ccwarn (...args) {
|
||||
console.warn(
|
||||
`%c❗️ ${new Date().toJSON()} ${routeNow()}`,
|
||||
'background: #f39c12; border-radius: 0.5em;color: white; font-weight: bold; padding: 2px 0.5em;',
|
||||
...args
|
||||
)
|
||||
},
|
||||
ccerror (...args) {
|
||||
console.error(
|
||||
`%c❌ ${new Date().toJSON()} ${routeNow()}`,
|
||||
'background: #c0392b; border-radius: 0.5em;color: white; font-weight: bold; padding: 2px 0.5em;',
|
||||
...args
|
||||
)
|
||||
},
|
||||
ccdebug (...args) {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
console.debug(
|
||||
`%c ${new Date().toJSON()} ${routeNow()}`,
|
||||
'background: #ff0000; border-radius: 0.5em;color: white; font-weight: bold; padding: 2px 0.5em;',
|
||||
...args
|
||||
)
|
||||
}
|
||||
},
|
||||
cctitle (...args) {
|
||||
console.debug(
|
||||
`%c ${JSON.stringify(args)}`,
|
||||
'border-color: black, border-radius: 0.5em;color: black; font-weight: bold; padding: 2px 0.5em; font-size:18px'
|
||||
)
|
||||
},
|
||||
}
|
||||
: typeof uniCloud === 'undefined'
|
||||
? {
|
||||
// 后台服务器命令行
|
||||
cclog (...args) {
|
||||
console.log(colors.blue(new Date().toJSON()), ...args)
|
||||
},
|
||||
ccinfo (...args) {
|
||||
console.info('ℹ️', colors.green(new Date().toJSON()), ...args)
|
||||
},
|
||||
ccgood (...args) {
|
||||
console.info('✅', colors.green(new Date().toJSON()), ...args)
|
||||
},
|
||||
ccwarn (...args) {
|
||||
// console.warn will appear in pm2's error log
|
||||
console.warn('❗️', colors.yellow(new Date().toJSON()), ...args)
|
||||
},
|
||||
ccerror (...args) {
|
||||
// console.error will appear in pm2's error log
|
||||
console.error('❌', colors.red(new Date().toJSON()), ...args)
|
||||
},
|
||||
ccdebug (...args) {
|
||||
if ('development' === process.env.NODE_ENV) {
|
||||
// 在server的测试环境下. 注意在 uniCloud 环境下,`process.env.NODE_ENV` 不存在. 如要应用本方法,需要手动设置 `process.env.NODE_ENV`
|
||||
console.log(colors.rainbow(new Date().toJSON()), ...args)
|
||||
}
|
||||
},
|
||||
}
|
||||
: {
|
||||
// HBuilder 内置环境的 console 不支持颜色。为了检查是否支持颜色,可测试 uniCloud 是否存在(不存在说明在自己的server环境里),或 require('supports-color'),相应的返回不同的 ccXXX 函数。
|
||||
// uniCloud 云空间
|
||||
cclog (...args) {
|
||||
console.log(new Date().toJSON(), ...args)
|
||||
},
|
||||
ccinfo (...args) {
|
||||
console.info('ℹ️', new Date().toJSON(), ...args)
|
||||
},
|
||||
ccgood (...args) {
|
||||
console.info('✅ ', new Date().toJSON(), ...args)
|
||||
},
|
||||
ccwarn (...args) {
|
||||
// console.warn will appear in pm2's error log
|
||||
console.warn('❗️', new Date().toJSON(), ...args)
|
||||
},
|
||||
ccerror (...args) {
|
||||
// console.error will appear in pm2's error log
|
||||
console.error('❌', new Date().toJSON(), ...args)
|
||||
},
|
||||
ccdebug (...args) {
|
||||
if ('development' === process.env.NODE_ENV) {
|
||||
// 在server的测试环境下. 注意在 uniCloud 环境下,`process.env.NODE_ENV` 不存在. 如要应用本方法,需要手动设置 `process.env.NODE_ENV`
|
||||
console.log(new Date().toJSON(), ...args)
|
||||
}
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user