Skip to content

Commit 6cc799b

Browse files
committed
feat(@angular-devkit/build-angular): provide option to allow automatically cleaning the terminal screen during rebuilds
When setting `"clearScreen": true` to the appliction builder during rebuilds the terminimal screen will be cleaned. ```json { "projects": { "my-app": { "projectType": "application", "root": "", "sourceRoot": "src", "architect": { "build": { "builder": "@angular-devkit/build-angular:application", "options": { "clearScreen": true } } } } } } ``` Closes #25699
1 parent 492b36d commit 6cc799b

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

packages/angular_devkit/build_angular/src/builders/application/build-action.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { BuilderOutput } from '@angular-devkit/architect';
1010
import type { logging } from '@angular-devkit/core';
1111
import { existsSync } from 'node:fs';
1212
import path from 'node:path';
13+
import { clearScreenDown, cursorTo } from 'node:readline';
1314
import { BuildOutputFile } from '../../tools/esbuild/bundler-context';
1415
import { ExecutionResult, RebuildState } from '../../tools/esbuild/bundler-execution-result';
1516
import { shutdownSassWorkerPool } from '../../tools/esbuild/stylesheets/sass-language';
@@ -19,6 +20,20 @@ import { shouldWatchRoot } from '../../utils/environment-options';
1920
import { NormalizedCachedOptions } from '../../utils/normalize-cache';
2021
import { NormalizedOutputOptions } from './options';
2122

23+
// Watch workspace for package manager changes
24+
const packageWatchFiles = [
25+
// manifest can affect module resolution
26+
'package.json',
27+
// npm lock file
28+
'package-lock.json',
29+
// pnpm lock file
30+
'pnpm-lock.yaml',
31+
// yarn lock file including Yarn PnP manifest files (https://yarnpkg.com/advanced/pnp-spec/)
32+
'yarn.lock',
33+
'.pnp.cjs',
34+
'.pnp.data.json',
35+
];
36+
2237
export async function* runEsBuildBuildAction(
2338
action: (rebuildState?: RebuildState) => ExecutionResult | Promise<ExecutionResult>,
2439
options: {
@@ -36,13 +51,15 @@ export async function* runEsBuildBuildAction(
3651
poll?: number;
3752
signal?: AbortSignal;
3853
preserveSymlinks?: boolean;
54+
clearScreen?: boolean;
3955
},
4056
): AsyncIterable<(ExecutionResult['outputWithFiles'] | ExecutionResult['output']) & BuilderOutput> {
4157
const {
4258
writeToFileSystemFilter,
4359
writeToFileSystem,
4460
watch,
4561
poll,
62+
clearScreen,
4663
logger,
4764
deleteOutputPath,
4865
cacheOptions,
@@ -113,20 +130,6 @@ export async function* runEsBuildBuildAction(
113130
watcher.add(projectRoot);
114131
}
115132

116-
// Watch workspace for package manager changes
117-
const packageWatchFiles = [
118-
// manifest can affect module resolution
119-
'package.json',
120-
// npm lock file
121-
'package-lock.json',
122-
// pnpm lock file
123-
'pnpm-lock.yaml',
124-
// yarn lock file including Yarn PnP manifest files (https://yarnpkg.com/advanced/pnp-spec/)
125-
'yarn.lock',
126-
'.pnp.cjs',
127-
'.pnp.data.json',
128-
];
129-
130133
watcher.add(
131134
packageWatchFiles
132135
.map((file) => path.join(workspaceRoot, file))
@@ -164,6 +167,10 @@ export async function* runEsBuildBuildAction(
164167
break;
165168
}
166169

170+
if (clearScreen) {
171+
clearTerminal();
172+
}
173+
167174
if (verbose) {
168175
logger.info(changes.toDebugString());
169176
}
@@ -212,3 +219,12 @@ export async function* runEsBuildBuildAction(
212219
shutdownSassWorkerPool();
213220
}
214221
}
222+
223+
function clearTerminal(): void {
224+
const rowCount = process.stdout.rows - 2;
225+
const clean = rowCount > 0 ? '\n'.repeat(rowCount) : '';
226+
// eslint-disable-next-line no-console
227+
console.log(clean);
228+
cursorTo(process.stdout, 0, 0);
229+
clearScreenDown(process.stdout);
230+
}

packages/angular_devkit/build_angular/src/builders/application/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export async function* buildApplicationInternal(
126126
projectRoot: normalizedOptions.projectRoot,
127127
workspaceRoot: normalizedOptions.workspaceRoot,
128128
progress: normalizedOptions.progress,
129+
clearScreen: normalizedOptions.clearScreen,
129130
writeToFileSystem,
130131
// For app-shell and SSG server files are not required by users.
131132
// Omit these when SSR is not enabled.

packages/angular_devkit/build_angular/src/builders/application/options.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ export async function normalizeOptions(
294294
namedChunks,
295295
budgets,
296296
deployUrl,
297+
clearScreen,
297298
} = options;
298299

299300
// Return all the normalized options
@@ -348,6 +349,7 @@ export async function normalizeOptions(
348349
loaderExtensions,
349350
jsonLogs: useJSONBuildLogs,
350351
colors: colors.enabled,
352+
clearScreen,
351353
};
352354
}
353355

packages/angular_devkit/build_angular/src/builders/application/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@
133133
},
134134
"default": []
135135
},
136+
"clearScreen": {
137+
"type": "boolean",
138+
"default": false,
139+
"description": "Automatically clearing the terminal screen during rebuilds."
140+
},
136141
"optimization": {
137142
"description": "Enables optimization of the build output. Including minification of scripts and styles, tree-shaking, dead-code elimination, inlining of critical CSS and fonts inlining. For more information, see https://angular.io/guide/workspace-config#optimization-configuration.",
138143
"default": true,

0 commit comments

Comments
 (0)