Skip to content

Commit 303807a

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular/cli): remove direct use of ansi-colors package
The newly introduced `listr2` dependency used for the updated `ng add` console UI provides color support. This removes the need to retain a direct dependency on a color package within the `@angular/cli`.
1 parent 6486604 commit 303807a

File tree

6 files changed

+16
-23
lines changed

6 files changed

+16
-23
lines changed

packages/angular/cli/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ ts_library(
6565
"@npm//@types/yargs",
6666
"@npm//@types/yarnpkg__lockfile",
6767
"@npm//@yarnpkg/lockfile",
68-
"@npm//ansi-colors",
6968
"@npm//ini",
7069
"@npm//jsonc-parser",
7170
"@npm//listr2",

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { logging } from '@angular-devkit/core';
1010
import { format, stripVTControlCharacters } from 'node:util';
1111
import { CommandModuleError } from '../../src/command-builder/command-module';
1212
import { runCommand } from '../../src/command-builder/command-runner';
13-
import { colors } from '../../src/utilities/color';
13+
import { colors, supportColor } from '../../src/utilities/color';
1414
import { ngDebug } from '../../src/utilities/environment-options';
1515
import { writeErrorToLogFile } from '../../src/utilities/log-file';
1616

@@ -38,20 +38,21 @@ export default async function (options: { cliArgs: string[] }) {
3838
const colorLevels: Record<string, (message: string) => string> = {
3939
info: (s) => s,
4040
debug: (s) => s,
41-
warn: (s) => colors.bold.yellow(s),
42-
error: (s) => colors.bold.red(s),
43-
fatal: (s) => colors.bold.red(s),
41+
warn: (s) => colors.bold(colors.yellow(s)),
42+
error: (s) => colors.bold(colors.red(s)),
43+
fatal: (s) => colors.bold(colors.red(s)),
4444
};
4545
const logger = new logging.IndentLogger('cli-main-logger');
4646
const logInfo = console.log;
4747
const logError = console.error;
48+
const useColor = supportColor();
4849

4950
const loggerFinished = logger.forEach((entry) => {
5051
if (!ngDebug && entry.level === 'debug') {
5152
return;
5253
}
5354

54-
const color = colors.enabled ? colorLevels[entry.level] : stripVTControlCharacters;
55+
const color = useColor ? colorLevels[entry.level] : stripVTControlCharacters;
5556
const message = color(entry.message);
5657

5758
switch (entry.level) {

packages/angular/cli/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"@listr2/prompt-adapter-inquirer": "2.0.8",
3030
"@schematics/angular": "0.0.0-PLACEHOLDER",
3131
"@yarnpkg/lockfile": "1.1.0",
32-
"ansi-colors": "4.1.3",
3332
"ini": "4.1.3",
3433
"jsonc-parser": "3.2.1",
3534
"listr2": "8.2.1",

packages/angular/cli/src/commands/update/cli.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
} from '../../command-builder/command-module';
3131
import { SchematicEngineHost } from '../../command-builder/utilities/schematic-engine-host';
3232
import { subscribeToWorkflow } from '../../command-builder/utilities/schematic-workflow';
33-
import { colors } from '../../utilities/color';
33+
import { colors, figures } from '../../utilities/color';
3434
import { disableVersionCheck } from '../../utilities/environment-options';
3535
import { assertIsError } from '../../utilities/error';
3636
import { writeErrorToLogFile } from '../../utilities/log-file';
@@ -241,7 +241,7 @@ export default class UpdateCommandModule extends CommandModule<UpdateCommandArgs
241241
}
242242
}
243243

244-
logger.info(`Using package manager: ${colors.grey(packageManager.name)}`);
244+
logger.info(`Using package manager: ${colors.gray(packageManager.name)}`);
245245
logger.info('Collecting installed dependencies...');
246246

247247
const rootDependencies = await getProjectDependencies(this.context.root);
@@ -303,12 +303,12 @@ export default class UpdateCommandModule extends CommandModule<UpdateCommandArgs
303303
return { success: !workflowSubscription.error, files: workflowSubscription.files };
304304
} catch (e) {
305305
if (e instanceof UnsuccessfulWorkflowExecution) {
306-
logger.error(`${colors.symbols.cross} Migration failed. See above for further details.\n`);
306+
logger.error(`${figures.cross} Migration failed. See above for further details.\n`);
307307
} else {
308308
assertIsError(e);
309309
const logPath = writeErrorToLogFile(e);
310310
logger.fatal(
311-
`${colors.symbols.cross} Migration failed: ${e.message}\n` +
311+
`${figures.cross} Migration failed: ${e.message}\n` +
312312
` See "${logPath}" for further details.\n`,
313313
);
314314
}
@@ -438,7 +438,7 @@ export default class UpdateCommandModule extends CommandModule<UpdateCommandArgs
438438
for (const migration of migrations) {
439439
const { title, description } = getMigrationTitleAndDescription(migration);
440440

441-
logger.info(colors.cyan(colors.symbols.pointer) + ' ' + colors.bold(title));
441+
logger.info(colors.cyan(figures.pointer) + ' ' + colors.bold(title));
442442

443443
if (description) {
444444
logger.info(' ' + description);
@@ -1088,7 +1088,7 @@ export default class UpdateCommandModule extends CommandModule<UpdateCommandArgs
10881088
if (!isTTY()) {
10891089
for (const migration of optionalMigrations) {
10901090
const { title } = getMigrationTitleAndDescription(migration);
1091-
logger.info(colors.cyan(colors.symbols.pointer) + ' ' + colors.bold(title));
1091+
logger.info(colors.cyan(figures.pointer) + ' ' + colors.bold(title));
10921092
logger.info(colors.gray(` ng update ${packageName} --name ${migration.name}`));
10931093
logger.info(''); // Extra trailing newline.
10941094
}

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import * as ansiColors from 'ansi-colors';
10-
import { WriteStream } from 'tty';
9+
import { WriteStream } from 'node:tty';
1110

12-
function supportColor(): boolean {
11+
export { color as colors, figures } from 'listr2';
12+
13+
export function supportColor(): boolean {
1314
if (process.env.FORCE_COLOR !== undefined) {
1415
// 2 colors: FORCE_COLOR = 0 (Disables colors), depth 1
1516
// 16 colors: FORCE_COLOR = 1, depth 4
@@ -35,9 +36,3 @@ function supportColor(): boolean {
3536

3637
return false;
3738
}
38-
39-
// Create a separate instance to prevent unintended global changes to the color configuration
40-
const colors = ansiColors.create();
41-
colors.enabled = supportColor();
42-
43-
export { colors };

yarn.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,6 @@ __metadata:
516516
"@listr2/prompt-adapter-inquirer": "npm:2.0.8"
517517
"@schematics/angular": "npm:0.0.0-PLACEHOLDER"
518518
"@yarnpkg/lockfile": "npm:1.1.0"
519-
ansi-colors: "npm:4.1.3"
520519
ini: "npm:4.1.3"
521520
jsonc-parser: "npm:3.2.1"
522521
listr2: "npm:8.2.1"

0 commit comments

Comments
 (0)