Skip to content

Commit 5ac22e8

Browse files
committed
Add opts.colors
1 parent 8091a36 commit 5ac22e8

File tree

6 files changed

+35
-12
lines changed

6 files changed

+35
-12
lines changed

src/colors.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
const chalk = require('chalk')
4+
5+
const getColors = function({ opts: { colors } }) {
6+
// Can disable colors with `opts.colors: false`.
7+
// chalk will automatically disable colors if output does not support it.
8+
const enabled = Boolean(colors)
9+
10+
return chalk.constructor({ enabled })
11+
}
12+
13+
module.exports = {
14+
getColors,
15+
}

src/default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const DEFAULT_OPTS = {
1010
getMessage: defaultGetMessage,
1111
log: defaultLog,
1212
exitOnExceptions: true,
13+
colors: true,
1314
}
1415

1516
module.exports = {

src/handle.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const { exit } = require('process')
44

55
const { getInfo } = require('./info')
6+
const { getColors } = require('./colors')
67
const { getLevel } = require('./level')
78
const { getMessage } = require('./message')
89

@@ -29,8 +30,9 @@ const handleEvent = async function({
2930
return
3031
}
3132

33+
const colors = getColors({ opts })
3234
const level = getLevel({ opts, info })
33-
const message = getMessage({ opts, info, level })
35+
const message = getMessage({ opts, info, level, colors })
3436

3537
opts.log(message, level, info)
3638

src/level.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
const { platform } = require('process')
44

5-
const { red, yellow } = require('chalk')
6-
75
// Retrieve error's `level`
86
const getLevel = function({ opts, info }) {
97
const level = opts.getLevel(info)
@@ -21,11 +19,11 @@ const isWindows = platform === 'win32'
2119
// Each level is printed in a different way
2220
const LEVELS = {
2321
warn: {
24-
COLOR: yellow,
22+
COLOR: 'yellow',
2523
SIGN: isWindows ? '\u203C' : '\u26A0',
2624
},
2725
error: {
28-
COLOR: red,
26+
COLOR: 'red',
2927
SIGN: isWindows ? '\u00D7' : '\u2718',
3028
},
3129
}

src/message.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const { printMultiline, print, prettify } = require('./serialize')
66

77
// Retrieve `message` which sums up all information that can be gathered about
88
// the event.
9-
const getMessage = function({ opts, info, level }) {
10-
const message = opts.getMessage({ ...info, level })
9+
const getMessage = function({ opts, info, level, colors }) {
10+
const message = opts.getMessage({ ...info, level, colors })
1111
// Ensure this is a string
1212
const messageA = typeof message === 'string' ? message : inspect(message)
1313
return messageA
@@ -22,6 +22,7 @@ const defaultGetMessage = function({
2222
secondPromiseValue,
2323
error,
2424
level,
25+
colors,
2526
}) {
2627
const message = MESSAGES[eventName]({
2728
promiseState,
@@ -31,7 +32,7 @@ const defaultGetMessage = function({
3132
error,
3233
})
3334

34-
const messageA = prettify({ message, eventName, level })
35+
const messageA = prettify({ message, eventName, level, colors })
3536
return messageA
3637
}
3738

src/serialize.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
const { inspect } = require('util')
44

5-
const { bold, dim, inverse } = require('chalk')
6-
75
const { LEVELS } = require('./level')
86

97
const printMultiline = function(value) {
@@ -21,12 +19,20 @@ const print = function(value) {
2119
return inspect(value)
2220
}
2321

24-
const prettify = function({ message, eventName, level }) {
22+
const prettify = function({
23+
message,
24+
eventName,
25+
level,
26+
colors,
27+
colors: { bold, dim, inverse },
28+
}) {
2529
const [header, ...lines] = message.split('\n')
2630

2731
// Add color, icon and `eventName` to first message line.
2832
const { COLOR, SIGN } = LEVELS[level]
29-
const headerA = COLOR(`${bold(inverse(` ${SIGN} ${eventName} `))} ${header}`)
33+
const headerA = colors[COLOR](
34+
`${bold(inverse(` ${SIGN} ${eventName} `))} ${header}`,
35+
)
3036
// Add gray color and indentation to other lines.
3137
const linesA = lines.map(line => dim(`\t${VERTICAL_BAR} ${line}`))
3238

0 commit comments

Comments
 (0)