diff --git a/coco.js b/coco.js new file mode 100644 index 0000000..c5db5ea --- /dev/null +++ b/coco.js @@ -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) + } + }, + } diff --git a/cocon.js b/cocon.js deleted file mode 100644 index 7a0c6b2..0000000 --- a/cocon.js +++ /dev/null @@ -1,83 +0,0 @@ -const colors = require('colors') - -// HBuilder 内置环境的 console 不支持颜色。为了检查是否支持颜色,可测试 uniCloud 是否存在(不存在说明在自己的server环境里),或 require('supports-color'),相应的返回不同的 ccXXX 函数。 - -function pageNow () { - return globalThis.getCurrentPages()[globalThis.getCurrentPages().length - 1] -} - -module.exports = - globalThis.uni && globalThis.UniApp // && globalThis.getApp?.()?.constructor?.name === 'Vue' // 是前端 uniapp。 - ? { - cclog (...args) { - const pageName = pageNow()?.route || 'VueApp' - console.log('%c ' + JSON.stringify({ time: new Date().toJSON(), page: pageName }), 'color:blue;background:lightgrey', ...args) - }, - ccinfo (...args) { - const pageName = pageNow()?.route || 'VueApp' - console.info('%c ' + JSON.stringify({ time: new Date().toJSON(), page: pageName }), 'color:green;background:lightgrey', ...args) - }, - ccwarn (...args) { - const pageName = pageNow()?.route || 'VueApp' - console.warn('%c ' + JSON.stringify({ time: new Date().toJSON(), page: pageName }), 'color:orange;background:lightgrey', ...args) - }, - ccerror (...args) { - const pageName = pageNow()?.route || 'VueApp' - console.error('%c ' + JSON.stringify({ time: new Date().toJSON(), page: pageName }), 'color:red;background:lightgrey', ...args) - }, - ccdebug (...args) { - if (process.env.NODE_ENV !== 'production') { - const pageName = pageNow()?.route || 'VueApp' - console.debug('%c ' + JSON.stringify({ time: new Date().toJSON(), page: pageName }), 'color:cyan;background:lightgrey', ...args) - } - }, - cctitle (...args) { - const pageName = pageNow().route || 'VueApp' - console.debug('%c ' + JSON.stringify({ time: new Date().toJSON(), page: pageName }), 'color:cyan;background:lightgrey', ...args) - }, - } - : typeof uniCloud === 'undefined' - ? { - cclog (...args) { - console.log(colors.blue({ timeiso: new Date().toJSON() }), ...args) - }, - ccinfo (...args) { - console.info(colors.green({ timeiso: new Date().toJSON() }), ...args) - }, - ccerror (...args) { - // console.error will appear in pm2's error log - console.error(colors.red({ timeiso: new Date().toJSON() }), ...args) - }, - ccwarn (...args) { - // console.warn will appear in pm2's error log - console.warn(colors.yellow({ timeiso: 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({ timeiso: new Date().toJSON() }), ...args) - } - }, - } - : { - cclog (...args) { - console.log({ timeiso: new Date().toJSON() }, ...args) - }, - ccinfo (...args) { - console.info({ timeiso: new Date().toJSON() }, ...args) - }, - ccerror (...args) { - // console.error will appear in pm2's error log - console.error({ timeiso: new Date().toJSON() }, ...args) - }, - ccwarn (...args) { - // console.warn will appear in pm2's error log - console.warn({ timeiso: new Date().toJSON() }, ...args) - }, - ccdebug (...args) { - if ('development' === process.env.NODE_ENV) { - // 在server的测试环境下. 注意在 uniCloud 环境下,`process.env.NODE_ENV` 不存在. 如要应用本方法,需要手动设置 `process.env.NODE_ENV` - console.log({ timeiso: new Date().toJSON() }, ...args) - } - }, - } diff --git a/package.json b/package.json index d6df8e9..f597677 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,13 @@ { - "name": "wo-base-cocon", + "name": "wo-core-coco", "version": "1.0.0", "description": "", - "main": "cocon.js", + "main": "coco.js", "scripts": {}, "author": "", "license": "ISC", "dependencies": { - "colors": "^1.4.0" + "colors": "^1.4.0", + "consola": "^2.15.3" } }