Skip to content

Commit e9d1ef5

Browse files
committed
Fix opts.colors: true
1 parent dc5fc78 commit e9d1ef5

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/colors.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
'use strict'
22

33
const chalk = require('chalk')
4+
const { stdout: { level } = {} } = require('supports-color')
45

5-
const getColors = function({ opts: { colors } }) {
6-
// Can disable or force colors with `opts.colors`.
7-
// chalk will automatically disable colors if output does not support it.
8-
const chalkOpts = { enabled: Boolean(colors) }
9-
6+
const getColors = function({ opts }) {
7+
const chalkOpts = getChalkOpts({ opts })
108
return chalk.constructor(chalkOpts)
119
}
1210

11+
// Can disable or force colors with `opts.colors`.
12+
// chalk will automatically disable colors if output does not support it.
13+
const getChalkOpts = function({ opts: { colors } }) {
14+
if (colors === undefined) {
15+
return {}
16+
}
17+
18+
if (colors === false) {
19+
return { enabled: false }
20+
}
21+
22+
// Chalk `level` must be set otherwise `enabled` will be noop if terminal
23+
// does not support color (e.g. during a shell file redirection)
24+
// However it should only be set if it is `undefined` or `0`
25+
return { enabled: true, level: level || 2 }
26+
}
27+
1328
module.exports = {
1429
getColors,
1530
}

0 commit comments

Comments
 (0)