1 | 'use strict'
|
2 |
|
3 | const { LEVELS, LEVEL_NAMES } = require('./constants')
|
4 |
|
5 | const nocolor = input => input
|
6 | const plain = {
|
7 | default: nocolor,
|
8 | 60: nocolor,
|
9 | 50: nocolor,
|
10 | 40: nocolor,
|
11 | 30: nocolor,
|
12 | 20: nocolor,
|
13 | 10: nocolor,
|
14 | message: nocolor
|
15 | }
|
16 |
|
17 | const chalk = require('chalk')
|
18 | const ctx = new chalk.constructor({ enabled: true, level: 3 })
|
19 | const colored = {
|
20 | default: ctx.white,
|
21 | 60: ctx.bgRed,
|
22 | 50: ctx.red,
|
23 | 40: ctx.yellow,
|
24 | 30: ctx.green,
|
25 | 20: ctx.blue,
|
26 | 10: ctx.grey,
|
27 | message: ctx.cyan
|
28 | }
|
29 |
|
30 | function colorizeLevel (level, colorizer) {
|
31 | if (Number.isInteger(+level)) {
|
32 | return LEVELS.hasOwnProperty(level)
|
33 | ? colorizer[level](LEVELS[level])
|
34 | : colorizer.default(LEVELS.default)
|
35 | }
|
36 | const levelNum = LEVEL_NAMES[level.toLowerCase()] || 'default'
|
37 | return colorizer[levelNum](LEVELS[levelNum])
|
38 | }
|
39 |
|
40 | function plainColorizer (level) {
|
41 | return colorizeLevel(level, plain)
|
42 | }
|
43 | plainColorizer.message = plain.message
|
44 |
|
45 | function coloredColorizer (level) {
|
46 | return colorizeLevel(level, colored)
|
47 | }
|
48 | coloredColorizer.message = colored.message
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | module.exports = function getColorizer (useColors = false) {
|
65 | return useColors ? coloredColorizer : plainColorizer
|
66 | }
|