packager: verify validity of TTY before using it

Reviewed By: mkonicek

Differential Revision: D4689779

fbshipit-source-id: 9bc2c1447bd64ec392adef772b1189a782f83545
This commit is contained in:
Jean Lauliac 2017-03-10 10:43:09 -08:00 committed by Facebook Github Bot
parent da94f5e5af
commit 4e106d45c3
1 changed files with 19 additions and 4 deletions

View File

@ -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
@ -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;