Skip to content

Commit 622d084

Browse files
committedSep 10, 2020
refactor: use ansi-colors instead of removed terminal utils
1 parent 6d43e32 commit 622d084

File tree

21 files changed

+149
-76
lines changed

21 files changed

+149
-76
lines changed
 

‎bin/devkit-admin

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ process.chdir(path.join(__dirname, '..'));
3333
let logger = null;
3434
try {
3535
logger = new (require('@angular-devkit/core').logging.IndentLogger)('root');
36-
const { bold, gray, red, yellow, white } = require('@angular-devkit/core').terminal;
36+
const colors = require('ansi-colors').create();
3737
const filter = require('rxjs/operators').filter;
3838

3939
logger
4040
.pipe(filter(entry => (entry.level !== 'debug' || args.verbose)))
4141
.subscribe(entry => {
42-
let color = gray;
42+
let color = colors.gray;
4343
let output = process.stdout;
4444
switch (entry.level) {
45-
case 'info': color = white; break;
46-
case 'warn': color = yellow; break;
47-
case 'error': color = red; output = process.stderr; break;
48-
case 'fatal': color = x => bold(red(x)); output = process.stderr; break;
45+
case 'info': color = colors.white; break;
46+
case 'warn': color = colors.yellow; break;
47+
case 'error': color = colors.red; output = process.stderr; break;
48+
case 'fatal': color = x => colors.bold.red(x); output = process.stderr; break;
4949
}
5050

5151
output.write(color(entry.message) + '\n');

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

+6-6
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, removeColor, supportsColor } from '../../utilities/color';
11+
import { colors, removeColor } from '../../utilities/color';
1212
import { getWorkspaceRaw } from '../../utilities/config';
1313
import { writeErrorToLogFile } from '../../utilities/log-file';
1414
import { getWorkspaceDetails } from '../../utilities/project';
@@ -49,11 +49,11 @@ export default async function(options: { testing?: boolean; cliArgs: string[] })
4949
}
5050

5151
const logger = createConsoleLogger(isDebug, process.stdout, process.stderr, {
52-
info: s => (supportsColor ? s : removeColor(s)),
53-
debug: s => (supportsColor ? s : removeColor(s)),
54-
warn: s => (supportsColor ? colors.bold.yellow(s) : removeColor(s)),
55-
error: s => (supportsColor ? colors.bold.red(s) : removeColor(s)),
56-
fatal: s => (supportsColor ? colors.bold.red(s) : removeColor(s)),
52+
info: s => (colors.enabled ? s : removeColor(s)),
53+
debug: s => (colors.enabled ? s : removeColor(s)),
54+
warn: s => (colors.enabled ? colors.bold.yellow(s) : removeColor(s)),
55+
error: s => (colors.enabled ? colors.bold.red(s) : removeColor(s)),
56+
fatal: s => (colors.enabled ? colors.bold.red(s) : removeColor(s)),
5757
});
5858

5959
// Redirect console to logger

‎packages/angular/cli/utilities/color.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@
88
import * as ansiColors from 'ansi-colors';
99
import { WriteStream } from 'tty';
1010

11+
type AnsiColors = typeof ansiColors;
12+
1113
// Typings do not contain the function call (added in Node.js v9.9.0)
12-
export const supportsColor =
14+
const supportsColor =
1315
process.stdout instanceof WriteStream &&
1416
((process.stdout as unknown) as { getColorDepth(): number }).getColorDepth() > 1;
1517

1618
export function removeColor(text: string): string {
17-
return text.replace(new RegExp(ansiColors.ansiRegex), '');
19+
// This has been created because when colors.enabled is false unstyle doesn't work
20+
// see: https://github.com/doowb/ansi-colors/blob/a4794363369d7b4d1872d248fc43a12761640d8e/index.js#L38
21+
return text.replace(ansiColors.ansiRegex, '');
1822
}
1923

20-
// create a separate instance to prevent unintended global changes to the color configuration
21-
// create function is not defined in the typings
22-
const colors = (ansiColors as typeof ansiColors & { create: () => typeof ansiColors }).create();
24+
// Create a separate instance to prevent unintended global changes to the color configuration
25+
// Create function is not defined in the typings. See: https://github.com/doowb/ansi-colors/pull/44
26+
const colors = (ansiColors as AnsiColors & { create: () => AnsiColors }).create();
2327
colors.enabled = supportsColor;
2428

2529
export { colors };

‎packages/angular_devkit/architect_cli/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ts_library(
2222
"@npm//@types/minimist",
2323
"@npm//@types/node",
2424
"@npm//@types/progress",
25+
"@npm//ansi-colors",
2526
"@npm//rxjs",
2627
],
2728
)

‎packages/angular_devkit/architect_cli/bin/architect.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
*/
99
import { Architect, BuilderInfo, BuilderProgressState, Target } from '@angular-devkit/architect';
1010
import { WorkspaceNodeModulesArchitectHost } from '@angular-devkit/architect/node';
11-
import { logging, schema, tags, terminal, workspaces } from '@angular-devkit/core';
11+
import { logging, schema, tags, workspaces } from '@angular-devkit/core';
1212
import { NodeJsSyncHost, createConsoleLogger } from '@angular-devkit/core/node';
13+
import * as ansiColors from 'ansi-colors';
1314
import { existsSync } from 'fs';
1415
import * as minimist from 'minimist';
1516
import * as path from 'path';
@@ -68,6 +69,10 @@ interface BarInfo {
6869
target?: Target;
6970
}
7071
72+
// Create a separate instance to prevent unintended global changes to the color configuration
73+
// Create function is not defined in the typings. See: https://github.com/doowb/ansi-colors/pull/44
74+
const colors = (ansiColors as typeof ansiColors & { create: () => typeof ansiColors }).create();
75+
7176
async function _executeTarget(
7277
parentLogger: logging.Logger,
7378
workspace: workspaces.WorkspaceDefinition,
@@ -139,9 +144,9 @@ async function _executeTarget(
139144
.pipe(
140145
tap(result => {
141146
if (result.success) {
142-
parentLogger.info(terminal.green('SUCCESS'));
147+
parentLogger.info(colors.green('SUCCESS'));
143148
} else {
144-
parentLogger.info(terminal.yellow('FAILURE'));
149+
parentLogger.info(colors.red('FAILURE'));
145150
}
146151
parentLogger.info('Result: ' + JSON.stringify({ ...result, info: undefined }, null, 4));
147152
@@ -157,7 +162,7 @@ async function _executeTarget(
157162
158163
return success ? 0 : 1;
159164
} catch (err) {
160-
parentLogger.info(terminal.red('ERROR'));
165+
parentLogger.info(colors.red('ERROR'));
161166
parentLogger.info('\nLogs:');
162167
logs.forEach(l => parentLogger.next(l));
163168
@@ -173,7 +178,13 @@ async function main(args: string[]): Promise<number> {
173178
const argv = minimist(args, { boolean: ['help'] });
174179
175180
/** Create the DevKit Logger used through the CLI. */
176-
const logger = createConsoleLogger(argv['verbose']);
181+
const logger = createConsoleLogger(argv['verbose'], process.stdout, process.stderr, {
182+
info: s => s,
183+
debug: s => s,
184+
warn: s => colors.bold.yellow(s),
185+
error: s => colors.bold.red(s),
186+
fatal: s => colors.bold.red(s),
187+
});
177188
178189
// Check the target.
179190
const targetStr = argv._[0] || '';

‎packages/angular_devkit/architect_cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"dependencies": {
1616
"@angular-devkit/architect": "0.0.0",
1717
"@angular-devkit/core": "0.0.0",
18+
"ansi-colors": "4.1.1",
1819
"minimist": "1.2.5",
1920
"progress": "2.0.3",
2021
"rxjs": "6.6.3",

‎packages/angular_devkit/benchmark/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ ts_library(
3232
"@npm//@types/minimist",
3333
"@npm//@types/node",
3434
"@npm//@types/pidusage",
35+
"@npm//ansi-colors",
3536
"@npm//rxjs",
3637
],
3738
)

‎packages/angular_devkit/benchmark/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"dependencies": {
1717
"@angular-devkit/core": "0.0.0",
18+
"ansi-colors": "4.1.1",
1819
"minimist": "1.2.5",
1920
"pidusage": "2.0.21",
2021
"pidtree": "0.5.0",

‎packages/angular_devkit/benchmark/src/main.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
* found in the LICENSE file at https://angular.io/license
88
*/
99

10-
import { logging, tags, terminal } from '@angular-devkit/core';
10+
import { logging, tags } from '@angular-devkit/core';
1111
import { ProcessOutput } from '@angular-devkit/core/node';
12+
import * as ansiColors from 'ansi-colors';
1213
import { appendFileSync, writeFileSync } from 'fs';
1314
import * as minimist from 'minimist';
1415
import { filter, map, toArray } from 'rxjs/operators';
@@ -102,26 +103,30 @@ export async function main({
102103
})),
103104
);
104105

106+
// Create a separate instance to prevent unintended global changes to the color configuration
107+
// Create function is not defined in the typings. See: https://github.com/doowb/ansi-colors/pull/44
108+
const colors = (ansiColors as typeof ansiColors & { create: () => typeof ansiColors }).create();
109+
105110
// Log to console.
106111
logger
107112
.pipe(filter(entry => (entry.level != 'debug' || argv['verbose'])))
108113
.subscribe(entry => {
109-
let color: (s: string) => string = x => terminal.dim(terminal.white(x));
114+
let color: (s: string) => string = x => colors.dim.white(x);
110115
let output = stdout;
111116
switch (entry.level) {
112117
case 'info':
113118
color = s => s;
114119
break;
115120
case 'warn':
116-
color = terminal.yellow;
121+
color = colors.yellow;
117122
output = stderr;
118123
break;
119124
case 'error':
120-
color = terminal.red;
125+
color = colors.red;
121126
output = stderr;
122127
break;
123128
case 'fatal':
124-
color = (x: string) => terminal.bold(terminal.red(x));
129+
color = (x: string) => colors.bold.red(x);
125130
output = stderr;
126131
break;
127132
}

‎packages/angular_devkit/build_angular/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ ts_library(
131131
"@npm//@types/webpack-dev-server",
132132
"@npm//@types/webpack-sources",
133133
"@npm//ajv",
134+
"@npm//ansi-colors",
134135
"@npm//autoprefixer",
135136
"@npm//babel-loader",
136137
"@npm//browserslist",

‎packages/angular_devkit/build_angular/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@babel/template": "7.10.4",
2020
"@jsdevtools/coverage-istanbul-loader": "3.0.5",
2121
"@ngtools/webpack": "0.0.0",
22+
"ansi-colors": "4.1.1",
2223
"autoprefixer": "9.8.6",
2324
"babel-loader": "8.1.0",
2425
"browserslist": "^4.9.1",

‎packages/angular_devkit/build_angular/src/angular-cli-files/utilities/stats.ts

+11-13
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
*/
88
// tslint:disable
99
// TODO: cleanup this file, it's copied as is from Angular CLI.
10-
import { logging, tags, terminal } from '@angular-devkit/core';
10+
import { logging, tags } from '@angular-devkit/core';
1111
import { WebpackLoggingCallback } from '@angular-devkit/build-webpack';
1212
import * as path from 'path';
13-
14-
15-
const { bold, green, red, reset, white, yellow } = terminal;
13+
import { colors as ansiColors } from '../../utils/color';
1614

1715
export function formatSize(size: number): string {
1816
if (size <= 0) {
@@ -37,8 +35,8 @@ export function generateBundleStats(
3735
},
3836
colors: boolean,
3937
): string {
40-
const g = (x: string) => (colors ? bold(green(x)) : x);
41-
const y = (x: string) => (colors ? bold(yellow(x)) : x);
38+
const g = (x: string) => (colors ? ansiColors.bold.green(x) : x);
39+
const y = (x: string) => (colors ? ansiColors.bold.yellow(x) : x);
4240

4341
const id = info.id ? y(info.id.toString()) : '';
4442
const size = typeof info.size === 'number' ? ` ${formatSize(info.size)}` : '';
@@ -53,14 +51,14 @@ export function generateBundleStats(
5351
}
5452

5553
export function generateBuildStats(hash: string, time: number, colors: boolean): string {
56-
const w = (x: string) => colors ? bold(white(x)) : x;
54+
const w = (x: string) => colors ? ansiColors.bold.white(x) : x;
5755
return `Date: ${w(new Date().toISOString())} - Hash: ${w(hash)} - Time: ${w('' + time)}ms`
5856
}
5957

6058
export function statsToString(json: any, statsConfig: any) {
6159
const colors = statsConfig.colors;
62-
const rs = (x: string) => colors ? reset(x) : x;
63-
const w = (x: string) => colors ? bold(white(x)) : x;
60+
const rs = (x: string) => colors ? ansiColors.reset(x) : x;
61+
const w = (x: string) => colors ? ansiColors.bold.white(x) : x;
6462

6563
const changedChunksStats = json.chunks
6664
.filter((chunk: any) => chunk.rendered)
@@ -97,8 +95,8 @@ const ERRONEOUS_WARNINGS_FILTER = (warning: string) => ![
9795

9896
export function statsWarningsToString(json: any, statsConfig: any): string {
9997
const colors = statsConfig.colors;
100-
const rs = (x: string) => colors ? reset(x) : x;
101-
const y = (x: string) => colors ? bold(yellow(x)) : x;
98+
const rs = (x: string) => colors ? ansiColors.reset(x) : x;
99+
const y = (x: string) => colors ? ansiColors.bold.yellow(x) : x;
102100
const warnings = [...json.warnings];
103101
if (json.children) {
104102
warnings.push(...json.children
@@ -116,8 +114,8 @@ export function statsWarningsToString(json: any, statsConfig: any): string {
116114

117115
export function statsErrorsToString(json: any, statsConfig: any): string {
118116
const colors = statsConfig.colors;
119-
const rs = (x: string) => colors ? reset(x) : x;
120-
const r = (x: string) => colors ? bold(red(x)) : x;
117+
const rs = (x: string) => colors ? ansiColors.reset(x) : x;
118+
const r = (x: string) => colors ? ansiColors.bold.red(x) : x;
121119
const errors = [...json.errors];
122120
if (json.children) {
123121
errors.push(...json.children
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import * as ansiColors from 'ansi-colors';
9+
import { WriteStream } from 'tty';
10+
11+
type AnsiColors = typeof ansiColors;
12+
13+
// Typings do not contain the function call (added in Node.js v9.9.0)
14+
const supportsColor =
15+
process.stdout instanceof WriteStream &&
16+
((process.stdout as unknown) as { getColorDepth(): number }).getColorDepth() > 1;
17+
18+
export function removeColor(text: string): string {
19+
// This has been created because when colors.enabled is false unstyle doesn't work
20+
// see: https://github.com/doowb/ansi-colors/blob/a4794363369d7b4d1872d248fc43a12761640d8e/index.js#L38
21+
return text.replace(ansiColors.ansiRegex, '');
22+
}
23+
24+
// Create a separate instance to prevent unintended global changes to the color configuration
25+
// Create function is not defined in the typings. See: https://github.com/doowb/ansi-colors/pull/44
26+
const colors = (ansiColors as AnsiColors & { create: () => AnsiColors }).create();
27+
colors.enabled = supportsColor;
28+
29+
export { colors };

‎packages/angular_devkit/core/node/cli-logger.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { filter } from 'rxjs/operators';
9-
import { logging, terminal } from '../src';
9+
import { logging } from '../src';
1010

1111
export interface ProcessOutput {
1212
write(buffer: string | Buffer): boolean;
@@ -24,20 +24,15 @@ export function createConsoleLogger(
2424
const logger = new logging.IndentLogger('cling');
2525

2626
logger
27-
.pipe(filter(entry => (entry.level != 'debug' || verbose)))
27+
.pipe(filter(entry => entry.level !== 'debug' || verbose))
2828
.subscribe(entry => {
29-
let color = colors && colors[entry.level];
29+
const color = colors && colors[entry.level];
3030
let output = stdout;
31+
3132
switch (entry.level) {
32-
case 'info':
33-
break;
3433
case 'warn':
35-
color = color || (s => terminal.bold(terminal.yellow(s)));
36-
output = stderr;
37-
break;
3834
case 'fatal':
3935
case 'error':
40-
color = color || (s => terminal.bold(terminal.red(s)));
4136
output = stderr;
4237
break;
4338
}

‎packages/angular_devkit/core/node/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
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+
89
import * as experimental from './experimental/jobs/job-registry';
910
import * as fs from './fs';
1011
export * from './cli-logger';

‎packages/angular_devkit/schematics_cli/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ ts_library(
4141
"@npm//@types/inquirer",
4242
"@npm//@types/minimist",
4343
"@npm//@types/node",
44+
"@npm//ansi-colors",
4445
"@npm//inquirer", # @external
4546
"@npm//minimist", # @external
4647
"@npm//rxjs",

0 commit comments

Comments
 (0)