Skip to content

Commit

Permalink
fix(@angular/cli): directly remove ansi color codes when no color sup…
Browse files Browse the repository at this point in the history
…port

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
  • Loading branch information
clydin authored and dgp1130 committed Feb 24, 2020
1 parent ecce067 commit e221c9a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
12 changes: 6 additions & 6 deletions packages/angular/cli/lib/cli/index.ts
Expand Up @@ -8,7 +8,7 @@
import { createConsoleLogger } from '@angular-devkit/core/node';
import { format } from 'util';
import { runCommand } from '../../models/command-runner';
import { colors, supportsColor } from '../../utilities/color';
import { colors, removeColor, supportsColor } from '../../utilities/color';
import { getWorkspaceRaw } from '../../utilities/config';
import { writeErrorToLogFile } from '../../utilities/log-file';
import { getWorkspaceDetails } from '../../utilities/project';
Expand All @@ -34,11 +34,11 @@ export default async function(options: { testing?: boolean; cliArgs: string[] })
}

const logger = createConsoleLogger(isDebug, process.stdout, process.stderr, {
info: s => (supportsColor ? s : colors.unstyle(s)),
debug: s => (supportsColor ? s : colors.unstyle(s)),
warn: s => (supportsColor ? colors.bold.yellow(s) : colors.unstyle(s)),
error: s => (supportsColor ? colors.bold.red(s) : colors.unstyle(s)),
fatal: s => (supportsColor ? colors.bold.red(s) : colors.unstyle(s)),
info: s => (supportsColor ? s : removeColor(s)),
debug: s => (supportsColor ? s : removeColor(s)),
warn: s => (supportsColor ? colors.bold.yellow(s) : removeColor(s)),
error: s => (supportsColor ? colors.bold.red(s) : removeColor(s)),
fatal: s => (supportsColor ? colors.bold.red(s) : removeColor(s)),
});

// Redirect console to logger
Expand Down
10 changes: 8 additions & 2 deletions packages/angular/cli/utilities/color.ts
Expand Up @@ -5,14 +5,20 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as colors from 'ansi-colors';
import * as ansiColors from 'ansi-colors';
import { WriteStream } from 'tty';

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

(colors as { enabled: boolean }).enabled = supportsColor;
export function removeColor(text: string): string {
return text.replace(new RegExp(ansiColors.ansiRegex), '');
}

// tslint:disable-next-line: no-any
const colors = (ansiColors as any).create() as typeof ansiColors;
colors.enabled = supportsColor;

export { colors };

1 comment on commit e221c9a

@jamie-pate
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to enable color? I'm trying to pipe output through nodejs and the builder supports FORCE_COLOR= from supports-color but this change then erases all the color codes :(

Please sign in to comment.