Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const tmpdir = require('os').tmpdir();
const crypto = require('crypto');
const fs = require('fs');
const through = require('through2');
const wrapConsoleMethod = require('./wrap-console-method');

const DEFAULT_WRITE_LEVEL = 'INFO';

Expand Down Expand Up @@ -37,6 +38,9 @@ class UI {
// Output stream
this.progress = false;
this.actualOutputStream = options.outputStream || process.stdout;

this._tapConsole();

this.outputStream = through(function (data, enc, callback) {
if (this.process) {
spinner.stop();
Expand All @@ -57,6 +61,13 @@ class UI {
this.errorReport = null;
this.ci = !!options.ci;
}

_tapConsole() {
Object.keys(console).forEach(member => {
if (typeof original !== 'function') { return; }
wrapConsoleMethod(this, console, member);
});
}
/**
Unified mechanism to write a string to the console.
Optionally include a writeLevel, this is used to decide if the specific
Expand Down
14 changes: 14 additions & 0 deletions lib/wrap-console-method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

module.exports = function wrapConsoleMethod(ui, console, name) {
const original = console[name];

console[name] = function(...args) {
if (ui.progress) { ui.spinner.stop(); }
try {
original.apply(console, args);
} finally {
if (ui.progress) { ui.spinner.start(); }
}
}
};
28 changes: 28 additions & 0 deletions tests/unit/wrap-console-method-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const wrapConsoleMethod = require('../../lib/wrap-console-method');
const { expect } = require('chai');

describe('wrapConsoleMethod', function() {
it('works', function() {
let called = [];
function foo(...args) {
expect(this).to.eql(console);
called.push(args)
}
const ui = { };
const console = { foo };

expect(console.foo).to.eql(foo);
wrapConsoleMethod(ui, console, 'foo');

expect(console.foo).to.not.eql(foo);
console.foo(1,2,3,4);
expect(called).to.eql([[1,2,3,4]])
console.foo(2,3,4,5);
expect(called).to.eql([
[1,2,3,4],
[2,3,4,5]
])
})
});