Skip to content

Commit

Permalink
fix(core): clear line escape code shouldn't erase prefix in output (#…
Browse files Browse the repository at this point in the history
…15674)

(cherry picked from commit 72d64b6)
  • Loading branch information
AgentEnder authored and FrozenPandaz committed Mar 15, 2023
1 parent c3d5041 commit 2b97817
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/nx/src/tasks-runner/forked-process-task-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
},
});
}

0 comments on commit 2b97817

Please sign in to comment.