Skip to content

Commit

Permalink
packager: verify validity of TTY before using it
Browse files Browse the repository at this point in the history
Reviewed By: mkonicek

Differential Revision: D4689779

fbshipit-source-id: 9bc2c1447bd64ec392adef772b1189a782f83545
  • Loading branch information
Jean Lauliac authored and facebook-github-bot committed Mar 10, 2017
1 parent 434ca24 commit 014eef3
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions packager/src/lib/terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ function chunkString(str: string, size: number): Array<string> {
return str.match(new RegExp(`.{1,${size}}`, 'g')) || [];
}

/**
* Get the stream as a TTY if it effectively looks like a valid TTY.
*/
function getTTYStream(stream: net$Socket): ?tty.WriteStream {
if (
stream instanceof tty.WriteStream &&
stream.isTTY &&
stream.columns >= 1
) {
return stream;
}
return null;
}

/**
* We don't just print things to the console, sometimes we also want to show
* and update progress. This utility just ensures the output stays neat: no
Expand Down Expand Up @@ -93,19 +107,20 @@ class Terminal {
*/
_update(): void {
const {_statusStr, _stream} = this;
const ttyStream = getTTYStream(_stream);
if (_statusStr === this._nextStatusStr && this._logLines.length === 0) {
return;
}
if (_stream instanceof tty.WriteStream) {
clearStringBackwards(_stream, _statusStr);
if (ttyStream != null) {
clearStringBackwards(ttyStream, _statusStr);
}
this._logLines.forEach(line => {
_stream.write(line);
_stream.write('\n');
});
this._logLines = [];
if (_stream instanceof tty.WriteStream) {
this._nextStatusStr = chunkString(this._nextStatusStr, _stream.columns).join('\n');
if (ttyStream != null) {
this._nextStatusStr = chunkString(this._nextStatusStr, ttyStream.columns).join('\n');
_stream.write(this._nextStatusStr);
}
this._statusStr = this._nextStatusStr;
Expand Down

0 comments on commit 014eef3

Please sign in to comment.