-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
get_color_fns.ts
72 lines (68 loc) · 2.12 KB
/
get_color_fns.ts
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
import { Writable } from 'node:stream'
import chalk from 'chalk'
import { ColorInfo, supportsColor } from 'supports-color'
import { TestStepResultStatus } from '@cucumber/messages'
import { doesNotHaveValue } from '../value_checker'
export type IColorFn = (text: string) => string
export interface IColorFns {
forStatus: (status: TestStepResultStatus) => IColorFn
location: IColorFn
tag: IColorFn
diffAdded: IColorFn
diffRemoved: IColorFn
errorMessage: IColorFn
errorStack: IColorFn
}
export default function getColorFns(
stream: Writable,
env: NodeJS.ProcessEnv,
enabled?: boolean
): IColorFns {
const support: ColorInfo = detectSupport(stream, env, enabled)
if (support) {
const chalkInstance = new chalk.Instance(support)
return {
forStatus(status: TestStepResultStatus) {
return {
AMBIGUOUS: chalkInstance.red.bind(chalk),
FAILED: chalkInstance.red.bind(chalk),
PASSED: chalkInstance.green.bind(chalk),
PENDING: chalkInstance.yellow.bind(chalk),
SKIPPED: chalkInstance.cyan.bind(chalk),
UNDEFINED: chalkInstance.yellow.bind(chalk),
UNKNOWN: chalkInstance.yellow.bind(chalk),
}[status]
},
location: chalkInstance.gray.bind(chalk),
tag: chalkInstance.cyan.bind(chalk),
diffAdded: chalkInstance.green.bind(chalk),
diffRemoved: chalkInstance.red.bind(chalk),
errorMessage: chalkInstance.red.bind(chalk),
errorStack: chalkInstance.grey.bind(chalk),
}
} else {
return {
forStatus(_status: TestStepResultStatus) {
return (x) => x
},
location: (x) => x,
tag: (x) => x,
diffAdded: (x) => x,
diffRemoved: (x) => x,
errorMessage: (x) => x,
errorStack: (x) => x,
}
}
}
function detectSupport(
stream: Writable,
env: NodeJS.ProcessEnv,
enabled?: boolean
): ColorInfo {
const support: ColorInfo = supportsColor(stream)
// if we find FORCE_COLOR, we can let the supports-color library handle that
if ('FORCE_COLOR' in env || doesNotHaveValue(enabled)) {
return support
}
return enabled ? support || { level: 1 } : false
}