@@ -10,7 +10,33 @@ import * as ansiColors from 'ansi-colors';
1010import { WriteStream } from 'tty' ;
1111
1212type AnsiColors = typeof ansiColors ;
13- const supportsColor = process . stdout instanceof WriteStream && process . stdout . getColorDepth ( ) > 1 ;
13+
14+ function supportColor ( ) : boolean {
15+ if ( process . env . FORCE_COLOR !== undefined ) {
16+ // 2 colors: FORCE_COLOR = 0 (Disables colors), depth 1
17+ // 16 colors: FORCE_COLOR = 1, depth 4
18+ // 256 colors: FORCE_COLOR = 2, depth 8
19+ // 16,777,216 colors: FORCE_COLOR = 3, depth 16
20+ // See: https://nodejs.org/dist/latest-v12.x/docs/api/tty.html#tty_writestream_getcolordepth_env
21+ // and https://github.com/nodejs/node/blob/b9f36062d7b5c5039498e98d2f2c180dca2a7065/lib/internal/tty.js#L106;
22+ switch ( process . env . FORCE_COLOR ) {
23+ case '' :
24+ case 'true' :
25+ case '1' :
26+ case '2' :
27+ case '3' :
28+ return true ;
29+ default :
30+ return false ;
31+ }
32+ }
33+
34+ if ( process . stdout instanceof WriteStream ) {
35+ return process . stdout . getColorDepth ( ) > 1 ;
36+ }
37+
38+ return false ;
39+ }
1440
1541export function removeColor ( text : string ) : string {
1642 // This has been created because when colors.enabled is false unstyle doesn't work
@@ -21,6 +47,6 @@ export function removeColor(text: string): string {
2147// Create a separate instance to prevent unintended global changes to the color configuration
2248// Create function is not defined in the typings. See: https://github.com/doowb/ansi-colors/pull/44
2349const colors = ( ansiColors as AnsiColors & { create : ( ) => AnsiColors } ) . create ( ) ;
24- colors . enabled = supportsColor ;
50+ colors . enabled = supportColor ( ) ;
2551
2652export { colors } ;
0 commit comments