wo-core-coco/coco.js
2023-01-31 10:42:49 +08:00

120 lines
4.5 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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.blue(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 ('production' !== 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 ('production' !== process.env.NODE_ENV) {
// 在server的测试环境下. 注意在 uniCloud 环境下,`process.env.NODE_ENV` 不存在. 如要应用本方法,需要手动设置 `process.env.NODE_ENV`
console.log(new Date().toJSON(), ...args)
}
},
}