-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(log-update): support concurrent writes to stdout/stderr on render
- Loading branch information
Showing
2 changed files
with
63 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,36 +6,93 @@ import wrapAnsi from 'wrap-ansi'; | |
export default class LogUpdate { | ||
constructor() { | ||
this.prevLineCount = 0; | ||
this.listening = false; | ||
this.extraLines = ''; | ||
this._onData = this._onData.bind(this); | ||
this._streams = [process.stdout, process.stderr]; | ||
} | ||
|
||
render(lines) { | ||
this.listen(); | ||
|
||
const wrappedLines = wrapAnsi(lines, this.columns, { | ||
trim: false, | ||
hard: true, | ||
wordWrap: false, | ||
}); | ||
|
||
const earaseChars = ansiEscapes.eraseLines(this.prevLineCount); | ||
const data = | ||
ansiEscapes.eraseLines(this.prevLineCount) + | ||
wrappedLines + | ||
'\n' + | ||
this.extraLines; | ||
|
||
this.write(earaseChars + wrappedLines + '\n'); | ||
this.write(data); | ||
|
||
this.prevLineCount = wrappedLines.split('\n').length + 1; | ||
this.prevLineCount = data.split('\n').length; | ||
} | ||
|
||
get columns() { | ||
return (process.stderr.columns || 80) - 2; | ||
} | ||
|
||
write(data) { | ||
process.stderr.write(data, 'utf-8'); | ||
if (process.stderr.__write) { | ||
process.stderr.__write(data, 'utf-8'); | ||
} else { | ||
process.stderr.write(data, 'utf-8'); | ||
} | ||
} | ||
|
||
clear() { | ||
this.done(); | ||
this.write(ansiEscapes.eraseLines(this.prevLineCount)); | ||
this.prevLineCount = 0; | ||
} | ||
|
||
done() { | ||
this.stopListen(); | ||
|
||
this.prevLineCount = 0; | ||
this.extraLines = ''; | ||
} | ||
|
||
_onData(data) { | ||
const str = String(data); | ||
const lines = str.split('\n').length - 1; | ||
if (lines > 0) { | ||
this.prevLineCount += lines; | ||
this.extraLines += data; | ||
} | ||
} | ||
|
||
listen() { | ||
if (this.listening) { | ||
return; | ||
} | ||
|
||
const t = this; | ||
|
||
for (const stream of this._streams) { | ||
if (!stream.__write) { | ||
stream.__write = stream.write; | ||
stream.write = function write(data, ...args) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
phorsuedzie
Contributor
|
||
t._onData(data); | ||
this.__write(data, ...args); | ||
}; | ||
} | ||
} | ||
|
||
this.listening = true; | ||
} | ||
|
||
stopListen() { | ||
for (const stream of this._streams) { | ||
if (stream.__write) { | ||
stream.write = stream.__write; | ||
delete stream.__write; | ||
} | ||
} | ||
|
||
this.listening = false; | ||
} | ||
} |
Note: karma captures this intermediate
process.stdout.write
here: https://github.com/karma-runner/karma/blob/376142e282d09ae827038969414498f586ab00cd/lib/reporters/base.js#L9Unfortunately, karma uses the replaced
write
afterstopListen
directly, which then misses__write
. See: https://cmty.app/nuxt/webpackbar/issues/c31.