Skip to content

Commit a70932b

Browse files
clydinmgechev
authored andcommitted
refactor(@angular/cli): standardize color handling and support checking
Node.js 10+ provides built-in functionality to test for color support based on chalk's `supports-color` package as well as several others. This alleviates the need for custom code or third-party packages to determine color support. In addition for this PR, the `ansi-colors` package is added to the CLI which provides color, cross-platform symbol, and style/color removal support. The package is light-weight, contains typings, and has no dependencies. The removal support is leveraged to remove all styling from logger messages when color is not supported. This removes a current defect in which color/styling is still displayed if generated manually or via methods that do not perform supportability checks. Finally, the typically used console functions are overriden to leverage the logger to ensure that the color processing is applied to third-party code (e.g., webpack internally as well as some of its loaders and plugins) which output to the console directly.
1 parent 58599e1 commit a70932b

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
"@types/webpack-sources": "^0.1.5",
108108
"@yarnpkg/lockfile": "1.1.0",
109109
"ajv": "6.10.0",
110+
"ansi-colors": "3.2.4",
110111
"common-tags": "^1.8.0",
111112
"conventional-changelog": "^1.1.0",
112113
"conventional-commits-parser": "^3.0.0",

packages/angular/cli/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ ts_library(
4141
"@npm//@types/semver",
4242
"@npm//@types/universal-analytics",
4343
"@npm//@types/uuid",
44+
"@npm//ansi-colors",
4445
"@npm//rxjs",
4546
],
4647
)

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,38 @@
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 { terminal } from '@angular-devkit/core';
98
import { createConsoleLogger } from '@angular-devkit/core/node';
9+
import * as colors from 'ansi-colors';
10+
import { WriteStream } from 'tty';
11+
import { format } from 'util';
1012
import { runCommand } from '../../models/command-runner';
1113
import { getWorkspaceRaw } from '../../utilities/config';
1214
import { getWorkspaceDetails } from '../../utilities/project';
1315

14-
1516
export default async function(options: { testing?: boolean, cliArgs: string[] }) {
17+
// Typings do not contain the function call (added in Node.js v9.9.0)
18+
const supportsColor = process.stdout instanceof WriteStream &&
19+
(process.stdout as unknown as { getColorDepth(): number }).getColorDepth() > 1;
20+
1621
const logger = createConsoleLogger(
1722
false,
1823
process.stdout,
1924
process.stderr,
2025
{
21-
warn: s => terminal.bold(terminal.yellow(s)),
22-
error: s => terminal.bold(terminal.red(s)),
23-
fatal: s => terminal.bold(terminal.red(s)),
26+
info: s => supportsColor ? s : colors.unstyle(s),
27+
debug: s => supportsColor ? s : colors.unstyle(s),
28+
warn: s => supportsColor ? colors.bold.yellow(s) : colors.unstyle(s),
29+
error: s => supportsColor ? colors.bold.red(s) : colors.unstyle(s),
30+
fatal: s => supportsColor ? colors.bold.red(s) : colors.unstyle(s),
2431
},
2532
);
2633

34+
// Redirect console to logger
35+
console.log = function() { logger.info(format.apply(null, arguments)); };
36+
console.info = function() { logger.info(format.apply(null, arguments)); };
37+
console.warn = function() { logger.warn(format.apply(null, arguments)); };
38+
console.error = function() { logger.error(format.apply(null, arguments)); };
39+
2740
let projectDetails = getWorkspaceDetails();
2841
if (projectDetails === null) {
2942
const [, localPath] = getWorkspaceRaw('local');

packages/angular/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@schematics/angular": "0.0.0",
3232
"@schematics/update": "0.0.0",
3333
"@yarnpkg/lockfile": "1.1.0",
34+
"ansi-colors": "3.2.4",
3435
"debug": "^4.1.1",
3536
"ini": "1.3.5",
3637
"inquirer": "6.3.1",

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,11 @@ ansi-align@^2.0.0:
988988
dependencies:
989989
string-width "^2.0.0"
990990

991+
ansi-colors@3.2.4:
992+
version "3.2.4"
993+
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-3.2.4.tgz#e3a3da4bfbae6c86a9c285625de124a234026fbf"
994+
integrity sha512-hHUXGagefjN2iRrID63xckIvotOXOojhQKWIPUZ4mNUZ9nLZW+7FMNoE1lOkEhNWYsx/7ysGIuJYCiMAA9FnrA==
995+
991996
ansi-colors@^3.0.0:
992997
version "3.2.1"
993998
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-3.2.1.tgz#9638047e4213f3428a11944a7d4b31cba0a3ff95"

0 commit comments

Comments
 (0)