Skip to content

Commit 1271e4a

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular/build): use listr2 for spinner and color support
The `ansi-color` and `ora` dependencies have been replaced with `listr2`. This package provides both color and spinner capabilities and further reduces the dependency count. This also aligns the dependencies with the `@angular/cli` package with already uses `listr2`. The spinner also now will not overwrite console output that happens to be written while the spinner is active. Instead, the output will be written after the spinner task is complete.
1 parent 0f5af7a commit 1271e4a

File tree

8 files changed

+35
-104
lines changed

8 files changed

+35
-104
lines changed

packages/angular/build/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,15 @@ ts_library(
7676
"@npm//@types/semver",
7777
"@npm//@types/watchpack",
7878
"@npm//@vitejs/plugin-basic-ssl",
79-
"@npm//ansi-colors",
8079
"@npm//browserslist",
8180
"@npm//critters",
8281
"@npm//esbuild",
8382
"@npm//fast-glob",
8483
"@npm//https-proxy-agent",
84+
"@npm//listr2",
8585
"@npm//lmdb",
8686
"@npm//magic-string",
8787
"@npm//mrmime",
88-
"@npm//ora",
8988
"@npm//parse5-html-rewriting-stream",
9089
"@npm//picomatch",
9190
"@npm//piscina",

packages/angular/build/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@
2626
"@babel/plugin-syntax-import-attributes": "7.24.7",
2727
"@inquirer/confirm": "3.1.14",
2828
"@vitejs/plugin-basic-ssl": "1.1.0",
29-
"ansi-colors": "4.1.3",
3029
"browserslist": "^4.23.0",
3130
"critters": "0.0.24",
3231
"esbuild": "0.23.0",
3332
"fast-glob": "3.3.2",
3433
"https-proxy-agent": "7.0.5",
34+
"listr2": "8.2.3",
3535
"lmdb": "3.0.12",
3636
"magic-string": "0.30.10",
3737
"mrmime": "2.0.0",
38-
"ora": "5.4.1",
3938
"parse5-html-rewriting-stream": "7.0.0",
4039
"picomatch": "4.0.2",
4140
"piscina": "4.6.1",

packages/angular/build/src/builders/application/options.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { access, constants } from 'node:fs/promises';
1313
import { createRequire } from 'node:module';
1414
import path from 'node:path';
1515
import { normalizeAssetPatterns, normalizeOptimization, normalizeSourceMaps } from '../../utils';
16-
import { colors } from '../../utils/color';
16+
import { supportColor } from '../../utils/color';
1717
import { useJSONBuildLogs } from '../../utils/environment-options';
1818
import { I18nOptions, createI18nOptions } from '../../utils/i18n-options';
1919
import { IndexHtmlTransform } from '../../utils/index-file/index-html-generator';
@@ -366,7 +366,7 @@ export async function normalizeOptions(
366366
plugins: extensions?.codePlugins?.length ? extensions?.codePlugins : undefined,
367367
loaderExtensions,
368368
jsonLogs: useJSONBuildLogs,
369-
colors: colors.enabled,
369+
colors: supportColor(),
370370
clearScreen,
371371
define,
372372
};

packages/angular/build/src/tools/esbuild/utils.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import { BuilderContext } from '@angular-devkit/architect';
1010
import { BuildOptions, Metafile, OutputFile, formatMessages } from 'esbuild';
11+
import { Listr } from 'listr2';
1112
import { createHash } from 'node:crypto';
1213
import { constants as fsConstants } from 'node:fs';
1314
import fs from 'node:fs/promises';
@@ -20,7 +21,6 @@ import {
2021
NormalizedOutputOptions,
2122
} from '../../builders/application/options';
2223
import { BudgetCalculatorResult } from '../../utils/bundle-calculator';
23-
import { Spinner } from '../../utils/spinner';
2424
import { BundleStats, generateEsbuildBuildStatsTable } from '../../utils/stats-table';
2525
import { BuildOutputFile, BuildOutputFileType, InitialFileRecord } from './bundler-context';
2626
import { BuildOutputAsset, ExecutionResult } from './bundler-execution-result';
@@ -146,14 +146,22 @@ export async function calculateEstimatedTransferSizes(
146146
}
147147

148148
export async function withSpinner<T>(text: string, action: () => T | Promise<T>): Promise<T> {
149-
const spinner = new Spinner(text);
150-
spinner.start();
149+
let result;
150+
const taskList = new Listr(
151+
[
152+
{
153+
title: text,
154+
async task() {
155+
result = await action();
156+
},
157+
},
158+
],
159+
{ rendererOptions: { clearOutput: true } },
160+
);
151161

152-
try {
153-
return await action();
154-
} finally {
155-
spinner.stop();
156-
}
162+
await taskList.run();
163+
164+
return result as T;
157165
}
158166

159167
export async function withNoProgress<T>(text: string, action: () => T | Promise<T>): Promise<T> {

packages/angular/build/src/utils/color.ts

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

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

12-
function supportColor(): boolean {
13-
if (process.env.FORCE_COLOR !== undefined) {
14-
// 2 colors: FORCE_COLOR = 0 (Disables colors), depth 1
15-
// 16 colors: FORCE_COLOR = 1, depth 4
16-
// 256 colors: FORCE_COLOR = 2, depth 8
17-
// 16,777,216 colors: FORCE_COLOR = 3, depth 16
18-
// See: https://nodejs.org/dist/latest-v12.x/docs/api/tty.html#tty_writestream_getcolordepth_env
19-
// and https://github.com/nodejs/node/blob/b9f36062d7b5c5039498e98d2f2c180dca2a7065/lib/internal/tty.js#L106;
20-
switch (process.env.FORCE_COLOR) {
21-
case '':
22-
case 'true':
23-
case '1':
24-
case '2':
25-
case '3':
26-
return true;
27-
default:
28-
return false;
29-
}
30-
}
11+
export { color as colors, figures } from 'listr2';
3112

32-
if (process.stdout instanceof WriteStream) {
33-
return process.stdout.hasColors();
13+
export function supportColor(stream: NodeJS.WritableStream = process.stdout): boolean {
14+
if (stream instanceof WriteStream) {
15+
return stream.hasColors();
3416
}
3517

36-
return false;
18+
try {
19+
// The hasColors function does not rely on any instance state and should ideally be static
20+
return WriteStream.prototype.hasColors();
21+
} catch {
22+
return process.env['FORCE_COLOR'] !== undefined && process.env['FORCE_COLOR'] !== '0';
23+
}
3724
}
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 };

packages/angular/build/src/utils/spinner.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

tests/legacy-cli/e2e/tests/build/progress-and-stats.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from 'node:assert/strict';
12
import { getGlobalVariable } from '../../utils/env';
23
import { ng } from '../../utils/process';
34

@@ -15,7 +16,9 @@ export default async function () {
1516

1617
let logs;
1718
if (getGlobalVariable('argv')['esbuild']) {
18-
logs = ['Building...'];
19+
assert.match(stdout, /Building\.\.\./);
20+
21+
return;
1922
} else {
2023
logs = [
2124
'Browser application bundle generation complete',

yarn.lock

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,16 +402,15 @@ __metadata:
402402
"@babel/plugin-syntax-import-attributes": "npm:7.24.7"
403403
"@inquirer/confirm": "npm:3.1.14"
404404
"@vitejs/plugin-basic-ssl": "npm:1.1.0"
405-
ansi-colors: "npm:4.1.3"
406405
browserslist: "npm:^4.23.0"
407406
critters: "npm:0.0.24"
408407
esbuild: "npm:0.23.0"
409408
fast-glob: "npm:3.3.2"
410409
https-proxy-agent: "npm:7.0.5"
410+
listr2: "npm:8.2.3"
411411
lmdb: "npm:3.0.12"
412412
magic-string: "npm:0.30.10"
413413
mrmime: "npm:2.0.0"
414-
ora: "npm:5.4.1"
415414
parse5-html-rewriting-stream: "npm:7.0.0"
416415
picomatch: "npm:4.0.2"
417416
piscina: "npm:4.6.1"

0 commit comments

Comments
 (0)