保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import stringWidth from 'string-width'
import figures from 'figures'
import chalk from 'chalk'
import BasicReporter from './basic'
import { parseStack } from '../utils/error'
import { chalkColor, chalkBgColor } from '../utils/chalk'
import { TYPE_COLOR_MAP, LEVEL_COLOR_MAP } from '../utils/fancy'
 
const DEFAULTS = {
  secondaryColor: 'grey',
  formatOptions: {
    date: true,
    colors: true,
    compact: false
  }
}
 
const TYPE_ICONS = {
  info: figures('ℹ'),
  success: figures('✔'),
  debug: figures('›'),
  trace: figures('›'),
  log: ''
}
 
export default class FancyReporter extends BasicReporter {
  constructor (options) {
    super(Object.assign({}, DEFAULTS, options))
  }
 
  formatStack (stack) {
    const grey = chalkColor('grey')
    const cyan = chalkColor('cyan')
 
    return '\n' + parseStack(stack)
      .map(line => '  ' + line
        .replace(/^at +/, m => grey(m))
        .replace(/\((.+)\)/, (_, m) => `(${cyan(m)})`)
      )
      .join('\n')
  }
 
  formatType (logObj, isBadge) {
    const typeColor = TYPE_COLOR_MAP[logObj.type] ||
      LEVEL_COLOR_MAP[logObj.level] ||
      this.options.secondaryColor
 
    if (isBadge) {
      return chalkBgColor(typeColor).black(` ${logObj.type.toUpperCase()} `)
    }
 
    const _type = typeof TYPE_ICONS[logObj.type] === 'string' ? TYPE_ICONS[logObj.type] : (logObj.icon || logObj.type)
    return _type ? chalkColor(typeColor)(_type) : ''
  }
 
  formatLogObj (logObj, { width }) {
    const [message, ...additional] = this.formatArgs(logObj.args).split('\n')
 
    const isBadge = typeof logObj.badge !== 'undefined' ? Boolean(logObj.badge) : logObj.level < 2
 
    const secondaryColor = chalkColor(this.options.secondaryColor)
 
    const date = this.formatDate(logObj.date)
    const coloredDate = date && secondaryColor(date)
 
    const type = this.formatType(logObj, isBadge)
 
    const tag = logObj.tag ? secondaryColor(logObj.tag) : ''
 
    const formattedMessage = message.replace(/`([^`]+)`/g, (_, m) => chalk.cyan(m))
 
    let line
    const left = this.filterAndJoin([type, formattedMessage])
    const right = this.filterAndJoin([tag, coloredDate])
    const space = width - stringWidth(left) - stringWidth(right) - 2
 
    if (space > 0 && width >= 80) {
      line = left + ' '.repeat(space) + right
    } else {
      line = left
    }
 
    line += additional.length ? '\n' + additional.join('\n') : ''
 
    return isBadge ? '\n' + line + '\n' : line
  }
}