简体   繁体   中英

How can I get gulp to be silent for some tasks (unit tests,vet etc)?

I have some unit test and vet gulp tasks that are triggered on file change.

I want gulp to act like --silent was passed to it when I start those tasks, as gulps default output is cluttering the output, and I do not want to have to specify the argument every time. Is this possible?

After some digging in the source code of gulp: Gulp inherits Orchestrator that inherits EventEmitter. The global gulp requires the local gulp and attaches some event listeners (task_start and task_stop).

I stubbed out the handlers for these. This is an AWFULL hack, but it did the trick.

Solution (put this at the top of your gulpfile.js):

var gulp = require('gulp');
var cmd = String(process.argv[2]);

if (/^((watch|vet|unit-test|integration-test)(:.*)?)$/.test(cmd)) {
    console.warn('Logging silenced');

    var isWatching = /^(watch:.*)$/.test(cmd);
    var firstCall = false; // Do not clear on first run

    var on = gulp.on;
    gulp.on = function (name, handler) {
        if (/^(task_start|task_stop)$/.test(name)) {

            // Do some inspection on the handler
            // This is a ugly hack, and might break in the future
            if (/gutil\.log\(\s*'(Starting|Finished)/.test(handler.toString())) {
                return; //No operation
            }
        }
        return on.apply(gulp, arguments);
    };
    gulp.on('start', function () {
        // start fires multiple times
        // make sure we only call this once
        if (firstCall) {

            if (isWatching) {
            // Clear console
            // Ref: https://stackoverflow.com/questions/5367068/clear-the-ubuntu-bash-screen-for-real
            process.stdout.write('\033c');
            }

            console.log('Started task');
            firstCall = false;
        }
    });
    gulp.on('stop', function () {
        console.log('Task finished');
        firstCall = true;
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM