Skip to content

Commit e221c9a

Browse files
clydindgp1130
authored andcommitted
fix(@angular/cli): directly remove ansi color codes when no color support
Third party libraries can attempt to write color codes to the output even though the CLI has already determined that color should not be used. The previously implemented color removal code is no longer functional since the update of ansi-colors to 4.1.0. While this appears to be a defect in the aforementioned package, the new CLI removal method not only bypasses the defect but also unneeded execution logic that the CLI does not need in this case. Fixes: #17053
1 parent ecce067 commit e221c9a

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

packages/angular/cli/lib/cli/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { createConsoleLogger } from '@angular-devkit/core/node';
99
import { format } from 'util';
1010
import { runCommand } from '../../models/command-runner';
11-
import { colors, supportsColor } from '../../utilities/color';
11+
import { colors, removeColor, supportsColor } from '../../utilities/color';
1212
import { getWorkspaceRaw } from '../../utilities/config';
1313
import { writeErrorToLogFile } from '../../utilities/log-file';
1414
import { getWorkspaceDetails } from '../../utilities/project';
@@ -34,11 +34,11 @@ export default async function(options: { testing?: boolean; cliArgs: string[] })
3434
}
3535

3636
const logger = createConsoleLogger(isDebug, process.stdout, process.stderr, {
37-
info: s => (supportsColor ? s : colors.unstyle(s)),
38-
debug: s => (supportsColor ? s : colors.unstyle(s)),
39-
warn: s => (supportsColor ? colors.bold.yellow(s) : colors.unstyle(s)),
40-
error: s => (supportsColor ? colors.bold.red(s) : colors.unstyle(s)),
41-
fatal: s => (supportsColor ? colors.bold.red(s) : colors.unstyle(s)),
37+
info: s => (supportsColor ? s : removeColor(s)),
38+
debug: s => (supportsColor ? s : removeColor(s)),
39+
warn: s => (supportsColor ? colors.bold.yellow(s) : removeColor(s)),
40+
error: s => (supportsColor ? colors.bold.red(s) : removeColor(s)),
41+
fatal: s => (supportsColor ? colors.bold.red(s) : removeColor(s)),
4242
});
4343

4444
// Redirect console to logger

packages/angular/cli/utilities/color.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import * as colors from 'ansi-colors';
8+
import * as ansiColors from 'ansi-colors';
99
import { WriteStream } from 'tty';
1010

1111
// Typings do not contain the function call (added in Node.js v9.9.0)
1212
export const supportsColor =
1313
process.stdout instanceof WriteStream &&
1414
((process.stdout as unknown) as { getColorDepth(): number }).getColorDepth() > 1;
1515

16-
(colors as { enabled: boolean }).enabled = supportsColor;
16+
export function removeColor(text: string): string {
17+
return text.replace(new RegExp(ansiColors.ansiRegex), '');
18+
}
19+
20+
// tslint:disable-next-line: no-any
21+
const colors = (ansiColors as any).create() as typeof ansiColors;
22+
colors.enabled = supportsColor;
1723

1824
export { colors };

0 commit comments

Comments
 (0)