Skip to content

Commit 8d80511

Browse files
committed
refactor(@angular/cli): improve output format for ng cache info command
This commit refactors the output to be more user-friendly and consistent with other commands like `ng version`. It introduces a clean, aligned, two-column layout with colors to improve readability. - Labels are now bolded for emphasis. - Values are colored for clarity (cyan for general info, green/red for statuses). - The layout is padded to ensure vertical alignment, making the information easier to scan.
1 parent 50453fd commit 8d80511

File tree

2 files changed

+48
-47
lines changed
  • packages/angular/cli/src/commands/cache/info
  • tests/legacy-cli/e2e/tests/commands/cache

2 files changed

+48
-47
lines changed

packages/angular/cli/src/commands/cache/info/cli.ts

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

9-
import { tags } from '@angular-devkit/core';
109
import * as fs from 'node:fs/promises';
1110
import { join } from 'node:path';
1211
import { Argv } from 'yargs';
@@ -15,6 +14,7 @@ import {
1514
CommandModuleImplementation,
1615
CommandScope,
1716
} from '../../../command-builder/command-module';
17+
import { colors } from '../../../utilities/color';
1818
import { isCI } from '../../../utilities/environment-options';
1919
import { getCacheConfig } from '../utilities';
2020

@@ -29,15 +29,44 @@ export class CacheInfoCommandModule extends CommandModule implements CommandModu
2929
}
3030

3131
async run(): Promise<void> {
32-
const { path, environment, enabled } = getCacheConfig(this.context.workspace);
33-
34-
this.context.logger.info(tags.stripIndents`
35-
Enabled: ${enabled ? 'yes' : 'no'}
36-
Environment: ${environment}
37-
Path: ${path}
38-
Size on disk: ${await this.getSizeOfDirectory(path)}
39-
Effective status on current machine: ${this.effectiveEnabledStatus() ? 'enabled' : 'disabled'}
40-
`);
32+
const cacheConfig = getCacheConfig(this.context.workspace);
33+
const { path, environment, enabled } = cacheConfig;
34+
35+
const effectiveStatus = this.effectiveEnabledStatus(cacheConfig);
36+
const sizeOnDisk = await this.getSizeOfDirectory(path);
37+
38+
const info: { label: string; value: string }[] = [
39+
{
40+
label: 'Enabled',
41+
value: enabled ? colors.green('Yes') : colors.red('No'),
42+
},
43+
{
44+
label: 'Environment',
45+
value: colors.cyan(environment),
46+
},
47+
{
48+
label: 'Path',
49+
value: colors.cyan(path),
50+
},
51+
{
52+
label: 'Size on disk',
53+
value: colors.cyan(sizeOnDisk),
54+
},
55+
{
56+
label: 'Effective Status',
57+
value:
58+
(effectiveStatus ? colors.green('Enabled') : colors.red('Disabled')) +
59+
' (current machine)',
60+
},
61+
];
62+
63+
const maxLabelLength = Math.max(...info.map((l) => l.label.length));
64+
65+
const output = info
66+
.map(({ label, value }) => colors.bold(label.padEnd(maxLabelLength + 2)) + `: ${value}`)
67+
.join('\n');
68+
69+
this.context.logger.info(`\n${colors.bold('Cache Information')}\n\n${output}\n`);
4170
}
4271

4372
private async getSizeOfDirectory(path: string): Promise<string> {
@@ -82,8 +111,8 @@ export class CacheInfoCommandModule extends CommandModule implements CommandModu
82111
return `${roundedSize.toFixed(fractionDigits)} ${abbreviations[index]}`;
83112
}
84113

85-
private effectiveEnabledStatus(): boolean {
86-
const { enabled, environment } = getCacheConfig(this.context.workspace);
114+
private effectiveEnabledStatus(cacheConfig: { enabled: boolean; environment: string }): boolean {
115+
const { enabled, environment } = cacheConfig;
87116

88117
if (enabled) {
89118
switch (environment) {

tests/legacy-cli/e2e/tests/commands/cache/cache-info.ts

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,31 @@ export default async function () {
77
try {
88
// Should be enabled by default for local builds.
99
await configureTest('0' /** envCI */);
10-
await execAndWaitForOutputToMatch(
11-
'ng',
12-
['cache', 'info'],
13-
/Effective status on current machine: enabled/,
14-
);
10+
await execAndWaitForOutputToMatch('ng', ['cache', 'info'], /Effective Status\s*: Enabled/);
1511

1612
// Should be disabled by default for CI builds.
1713
await configureTest('1' /** envCI */, { enabled: true });
18-
await execAndWaitForOutputToMatch(
19-
'ng',
20-
['cache', 'info'],
21-
/Effective status on current machine: disabled/,
22-
);
14+
await execAndWaitForOutputToMatch('ng', ['cache', 'info'], /Effective Status\s*: Disabled/);
2315

2416
// Should be enabled by when environment is local and env is not CI.
2517
await configureTest('0' /** envCI */, { environment: 'local' });
26-
await execAndWaitForOutputToMatch(
27-
'ng',
28-
['cache', 'info'],
29-
/Effective status on current machine: enabled/,
30-
);
18+
await execAndWaitForOutputToMatch('ng', ['cache', 'info'], /Effective Status\s*: Enabled/);
3119

3220
// Should be disabled by when environment is local and env is CI.
3321
await configureTest('1' /** envCI */, { environment: 'local' });
34-
await execAndWaitForOutputToMatch(
35-
'ng',
36-
['cache', 'info'],
37-
/Effective status on current machine: disabled/,
38-
);
22+
await execAndWaitForOutputToMatch('ng', ['cache', 'info'], /Effective Status\s*: Disabled/);
3923

4024
// Effective status should be enabled when 'environment' is set to 'all' or 'ci'.
4125
await configureTest('1' /** envCI */, { environment: 'all' });
42-
await execAndWaitForOutputToMatch(
43-
'ng',
44-
['cache', 'info'],
45-
/Effective status on current machine: enabled/,
46-
);
26+
await execAndWaitForOutputToMatch('ng', ['cache', 'info'], /Effective Status\s*: Enabled/);
4727

4828
// Effective status should be enabled when 'environment' is set to 'ci' and run is in ci
4929
await configureTest('1' /** envCI */, { environment: 'ci' });
50-
await execAndWaitForOutputToMatch(
51-
'ng',
52-
['cache', 'info'],
53-
/Effective status on current machine: enabled/,
54-
);
30+
await execAndWaitForOutputToMatch('ng', ['cache', 'info'], /Effective Status\s*: Enabled/);
5531

5632
// Effective status should be disabled when 'enabled' is set to false
5733
await configureTest('1' /** envCI */, { environment: 'all', enabled: false });
58-
await execAndWaitForOutputToMatch(
59-
'ng',
60-
['cache', 'info'],
61-
/Effective status on current machine: disabled/,
62-
);
34+
await execAndWaitForOutputToMatch('ng', ['cache', 'info'], /Effective Status\s*: Disabled/);
6335
} finally {
6436
process.env['CI'] = originalCIValue;
6537
}

0 commit comments

Comments
 (0)