UNPKG

1.87 kBJavaScriptView Raw
1const chalk = require('chalk')
2
3/**
4 * 生成 log 消息内容
5 * @variation 只提供内容
6 * @param {String} content 内容
7 *//**
8* 生成 log 消息内容
9* @variation 提供标记和内容
10* @param {String|Boolean} [mark=""] 标记
11* @param {String} content 内容
12*//**
13 * 生成 log 消息内容
14* @variation 提供标记、类型和内容
15* @param {String|Boolean} [mark=""] 标记
16* @param {String} [type=""] 操作类型
17* @param {String} content 内容
18*/
19const generateLogMsg = (mark, type, content) => {
20 if (typeof mark !== 'undefined' && typeof type === 'undefined' && typeof content === 'undefined')
21 return generateLogMsg('', '', mark)
22 if (typeof type !== 'undefined' && typeof content === 'undefined')
23 return generateLogMsg(mark, '', type)
24
25 let markColor = 'cyan'
26 if (mark === false) mark = ''
27 switch (mark.toLowerCase()) {
28 case '×':
29 case 'x':
30 case 'x ':
31 case 'error': {
32 mark = '× '
33 markColor = 'redBright'
34 break
35 }
36 case 'success': {
37 mark = '√ '
38 markColor = 'green'
39 break
40 }
41 case 'cb':
42 case 'callback': {
43 mark = '⚑ '
44 markColor = 'cyan'
45 break
46 }
47 case 'warning': {
48 mark = '! '
49 markColor = 'yellowBright'
50 break
51 }
52 case '': {
53 break
54 }
55 default: {
56 mark = ' '
57 }
58 }
59
60 const typeColor = (() => {
61 switch (type) {
62 default: return 'yellowBright'
63 }
64 })()
65
66 return (mark === '' ? '' : chalk[markColor](mark))
67 + chalk[typeColor](`[koot${type ? `/${type}` : ''}] `)
68 + content
69}
70
71module.exports = generateLogMsg