gulpjs / gulp-cli

Command Line Interface for gulp.
MIT License
401 stars 106 forks source link

StdOut messed with after gulpfile.js runs #165

Closed betaorbust closed 6 years ago

betaorbust commented 6 years ago

Description

If you make any modifications to process.stdout.write within a gulpfile they will be immediately undone before the tasks are executed.

Technical Description

The root cause seems to be that mute-stdout's unmute is called every time, no matter what, https://github.com/gulpjs/gulp-cli/blob/1b801881aa8c12fc1c74fb76ff6f297561e6f283/lib/versioned/%5E4.0.0/index.js#L40-L41 https://github.com/gulpjs/gulp-cli/blob/1b801881aa8c12fc1c74fb76ff6f297561e6f283/lib/versioned/%5E3.7.0/index.js#L33-L34 and mute-stdout replaces process.stdout.write even when mute-stdout's mute hasn't been called. https://github.com/js-cli/mute-stdout/blob/acf7a79299c01bd5a43c6e1ce25fead75c956838/index.js#L3-L18

This means that anything you do inside a gulpfile that modifies process.stdout.write is blown away once the gulpfile has run.

Repo Case

//Gulpfile.js
'use strict';
const gulp = require('gulp');
const callsToMyStdout = [];
console.log('starting');
process.stdout.write = (function(write) {
    return function(string, encoding, fd) {
        const args = Array.from(arguments);
        args[0] = /Hide/.test(string) ? '' : string;
        callsToMyStdout.push(string);
        write.apply(process.stdout, args);
    };
})(process.stdout.write);
console.log('Hide me 1');
console.log('Show me 1');
gulp.task('default', function() {
  console.log('Hide me 2');
  console.log('Show me 2');
  console.log(callsToMyStdout);
});

Which currently results in: image So, Hide me 1 and Hide me 2 are processed correctly, but then between the gulpfile run and the task run, stdout.write is overwritten, and Hide me 2/Show me 2 are not captured.

sttk commented 6 years ago

@betaorbust Thank you for the report. This cause is in mute-stdout as you pointed, and I sended a pr to it to fix this issue.

phated commented 6 years ago

I've just published 1.0.1 of mute-stdout with @sttk's fix. Please update your dependencies (and make sure it's not locked to 1.0.0 in your lockfile) and try again.