diff --git a/lib/reporters/default.js b/lib/reporters/default.js index 7dbfcaeec..6aa86bb79 100644 --- a/lib/reporters/default.js +++ b/lib/reporters/default.js @@ -18,7 +18,6 @@ const colors = require('./colors'); const formatSerializedError = require('./format-serialized-error'); const improperUsageMessages = require('./improper-usage-messages'); const prefixTitle = require('./prefix-title'); -const whileCorked = require('./while-corked'); const nodeInternals = require('stack-utils').nodeInternals(); @@ -97,6 +96,48 @@ class LineWriterWithSpinner extends LineWriter { } } +function manageCorking(stream) { + let corked = false; + const cork = () => { + corked = true; + stream.cork(); + }; + + const uncork = () => { + corked = false; + stream.uncork(); + }; + + return { + decorateFlushingWriter(fn) { + return function (...args) { + if (corked) { + stream.uncork(); + } + + try { + return fn.apply(this, args); + } finally { + if (corked) { + stream.cork(); + } + } + }; + }, + + decorateWriter(fn) { + return function (...args) { + cork(); + try { + return fn.apply(this, args); + } finally { + uncork(); + } + }; + } + }; +} + class Reporter { constructor({ verbose, @@ -112,13 +153,16 @@ class Reporter { this.stdStream = stdStream; this.watching = watching; this.relativeFile = file => path.relative(projectDir, file); - this.consumeStateChange = whileCorked(this.reportStream, this.consumeStateChange); + + const {decorateWriter, decorateFlushingWriter} = manageCorking(this.reportStream); + this.consumeStateChange = decorateWriter(this.consumeStateChange); + this.endRun = decorateWriter(this.endRun); if (this.verbose) { this.durationThreshold = durationThreshold || 100; this.spinner = null; + this.clearSpinner = () => {}; this.lineWriter = new LineWriter(this.reportStream); - this.endRun = whileCorked(this.reportStream, this.endRun); } else { this.spinner = ora({ isEnabled: true, @@ -128,8 +172,8 @@ class Reporter { spinner: spinner || (process.platform === 'win32' ? 'line' : 'dots'), stream: reportStream }); + this.clearSpinner = decorateFlushingWriter(this.spinner.clear.bind(this.spinner)); this.lineWriter = new LineWriterWithSpinner(this.reportStream, this.spinner); - this.endRun = whileCorked(this.reportStream, whileCorked(this.lineWriter, this.endRun)); } this.reset(); @@ -362,9 +406,7 @@ class Reporter { case 'worker-stderr': { // Forcibly clear the spinner, writing the chunk corrupts the TTY. - if (this.spinner !== null) { - this.spinner.clear(); - } + this.clearSpinner(); this.stdStream.write(event.chunk); // If the chunk does not end with a linebreak, *forcibly* write one to @@ -386,9 +428,7 @@ class Reporter { case 'worker-stdout': { // Forcibly clear the spinner, writing the chunk corrupts the TTY. - if (this.spinner !== null) { - this.spinner.clear(); - } + this.clearSpinner(); this.stdStream.write(event.chunk); // If the chunk does not end with a linebreak, *forcibly* write one to diff --git a/lib/reporters/while-corked.js b/lib/reporters/while-corked.js deleted file mode 100644 index dd151a34f..000000000 --- a/lib/reporters/while-corked.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict'; -function whileCorked(stream, fn) { - return function (...args) { - stream.cork(); - try { - fn.apply(this, args); - } finally { - stream.uncork(); - } - }; -} - -module.exports = whileCorked;