diff --git a/packages/nx/src/tasks-runner/forked-process-task-runner.ts b/packages/nx/src/tasks-runner/forked-process-task-runner.ts index b9f47c0ba4a1f..cd1edbc5d418f 100644 --- a/packages/nx/src/tasks-runner/forked-process-task-runner.ts +++ b/packages/nx/src/tasks-runner/forked-process-task-runner.ts @@ -20,6 +20,7 @@ import { } from './batch/batch-messages'; import { stripIndents } from '../utils/strip-indents'; import { Task } from '../config/task-graph'; +import { Transform } from 'stream'; const workerPath = join(__dirname, './batch/run-batch.js'); @@ -152,9 +153,13 @@ export class ForkedProcessTaskRunner { const prefixText = `${task.target.project}:`; p.stdout + .pipe( + logClearLineToPrefixTransformer(color.bold(prefixText) + ' ') + ) .pipe(logTransformer({ tag: color.bold(prefixText) })) .pipe(process.stdout); p.stderr + .pipe(logClearLineToPrefixTransformer(color(prefixText) + ' ')) .pipe(logTransformer({ tag: color(prefixText) })) .pipe(process.stderr); } else { @@ -493,3 +498,20 @@ function getColor(projectName: string) { return colors[colorIndex]; } + +/** + * Prevents terminal escape sequence from clearing line prefix. + */ +function logClearLineToPrefixTransformer(prefix) { + let prevChunk = null; + return new Transform({ + transform(chunk, _encoding, callback) { + if (prevChunk && prevChunk.toString() === '\x1b[2K') { + chunk = chunk.toString().replace(/\x1b\[1G/g, (m) => m + prefix); + } + this.push(chunk); + prevChunk = chunk; + callback(); + }, + }); +}