Skip to content

Commit ea60f0f

Browse files
alan-agius4filipesilva
authored andcommitted
fix(@angular-devkit/build-angular): handle FORCE_COLOR when stdout is not instance of WriteStream
In some cases, custom implementation of stdout, don't extend `WriteStream` which causes colors not to be included in the output. Closes #21732 (cherry picked from commit df8f909)
1 parent 2c9f5b1 commit ea60f0f

File tree

1 file changed

+28
-2
lines changed
  • packages/angular_devkit/build_angular/src/utils

1 file changed

+28
-2
lines changed

packages/angular_devkit/build_angular/src/utils/color.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,33 @@ import * as ansiColors from 'ansi-colors';
1010
import { WriteStream } from 'tty';
1111

1212
type 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

1541
export 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
2349
const colors = (ansiColors as AnsiColors & { create: () => AnsiColors }).create();
24-
colors.enabled = supportsColor;
50+
colors.enabled = supportColor();
2551

2652
export { colors };

0 commit comments

Comments
 (0)