[6a3a178] | 1 | let tty = require('tty')
|
---|
| 2 |
|
---|
| 3 | let isDisabled = 'NO_COLOR' in process.env
|
---|
| 4 | let isForced = 'FORCE_COLOR' in process.env
|
---|
| 5 | let isWindows = process.platform === 'win32'
|
---|
| 6 |
|
---|
| 7 | let isCompatibleTerminal =
|
---|
| 8 | tty && tty.isatty(1) && process.env.TERM && process.env.TERM !== 'dumb'
|
---|
| 9 |
|
---|
| 10 | let isCI =
|
---|
| 11 | 'CI' in process.env &&
|
---|
| 12 | ('GITHUB_ACTIONS' in process.env ||
|
---|
| 13 | 'GITLAB_CI' in process.env ||
|
---|
| 14 | 'CIRCLECI' in process.env)
|
---|
| 15 |
|
---|
| 16 | let isColorSupported =
|
---|
| 17 | !isDisabled && (isForced || isWindows || isCompatibleTerminal || isCI)
|
---|
| 18 |
|
---|
| 19 | let nope = s => String(s)
|
---|
| 20 |
|
---|
| 21 | function color(open, close, closeRegexp) {
|
---|
| 22 | return s => {
|
---|
| 23 | if (s === '') {
|
---|
| 24 | return s
|
---|
| 25 | } else {
|
---|
| 26 | return (
|
---|
| 27 | open +
|
---|
| 28 | (!!~('' + s).indexOf(close, 4) ? s.replace(closeRegexp, open) : s) +
|
---|
| 29 | close
|
---|
| 30 | )
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | let close39 = '\x1b[39m'
|
---|
| 36 | let close49 = '\x1b[49m'
|
---|
| 37 | let regexp39 = /\x1b\[39m/g
|
---|
| 38 | let regexp49 = /\x1b\[49m/g
|
---|
| 39 |
|
---|
| 40 | function createColors(enabled = isColorSupported) {
|
---|
| 41 | if (enabled) {
|
---|
| 42 | return {
|
---|
| 43 | isColorSupported: true,
|
---|
| 44 | reset: s => `\x1b[0m${s}\x1b[0m`,
|
---|
| 45 | bold: color('\x1b[1m', '\x1b[22m', /\x1b\[22m/g),
|
---|
| 46 | dim: color('\x1b[2m', '\x1b[22m', /\x1b\[22m/g),
|
---|
| 47 | italic: color('\x1b[3m', '\x1b[23m', /\x1b\[23m/g),
|
---|
| 48 | underline: color('\x1b[4m', '\x1b[24m', /\x1b\[24m/g),
|
---|
| 49 | inverse: color('\x1b[7m', '\x1b[27m', /\x1b\[27m/g),
|
---|
| 50 | hidden: color('\x1b[8m', '\x1b[28m', /\x1b\[28m/g),
|
---|
| 51 | strikethrough: color('\x1b[9m', '\x1b[29m', /\x1b\[29m/g),
|
---|
| 52 | black: color('\x1b[30m', close39, regexp39),
|
---|
| 53 | red: color('\x1b[31m', close39, regexp39),
|
---|
| 54 | green: color('\x1b[32m', close39, regexp39),
|
---|
| 55 | yellow: color('\x1b[33m', close39, regexp39),
|
---|
| 56 | blue: color('\x1b[34m', close39, regexp39),
|
---|
| 57 | magenta: color('\x1b[35m', close39, regexp39),
|
---|
| 58 | cyan: color('\x1b[36m', close39, regexp39),
|
---|
| 59 | white: color('\x1b[37m', close39, regexp39),
|
---|
| 60 | gray: color('\x1b[90m', close39, regexp39),
|
---|
| 61 | bgBlack: color('\x1b[40m', close49, regexp49),
|
---|
| 62 | bgRed: color('\x1b[41m', close49, regexp49),
|
---|
| 63 | bgGreen: color('\x1b[42m', close49, regexp49),
|
---|
| 64 | bgYellow: color('\x1b[43m', close49, regexp49),
|
---|
| 65 | bgBlue: color('\x1b[44m', close49, regexp49),
|
---|
| 66 | bgMagenta: color('\x1b[45m', close49, regexp49),
|
---|
| 67 | bgCyan: color('\x1b[46m', close49, regexp49),
|
---|
| 68 | bgWhite: color('\x1b[47m', close49, regexp49)
|
---|
| 69 | }
|
---|
| 70 | } else {
|
---|
| 71 | return {
|
---|
| 72 | isColorSupported: false,
|
---|
| 73 | reset: nope,
|
---|
| 74 | bold: nope,
|
---|
| 75 | dim: nope,
|
---|
| 76 | italic: nope,
|
---|
| 77 | underline: nope,
|
---|
| 78 | inverse: nope,
|
---|
| 79 | hidden: nope,
|
---|
| 80 | strikethrough: nope,
|
---|
| 81 | black: nope,
|
---|
| 82 | red: nope,
|
---|
| 83 | green: nope,
|
---|
| 84 | yellow: nope,
|
---|
| 85 | blue: nope,
|
---|
| 86 | magenta: nope,
|
---|
| 87 | cyan: nope,
|
---|
| 88 | white: nope,
|
---|
| 89 | gray: nope,
|
---|
| 90 | bgBlack: nope,
|
---|
| 91 | bgRed: nope,
|
---|
| 92 | bgGreen: nope,
|
---|
| 93 | bgYellow: nope,
|
---|
| 94 | bgBlue: nope,
|
---|
| 95 | bgMagenta: nope,
|
---|
| 96 | bgCyan: nope,
|
---|
| 97 | bgWhite: nope
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | module.exports = createColors(isColorSupported)
|
---|
| 103 | module.exports.createColors = createColors
|
---|