How can I get gulp to silence some tasks (unit tests, vet, etc.)?

I have several unit test and vet gulp tasks that run when the file is changed.

I want gulp to act like --silent is passed to it when I run these tasks, since the default output of gulps clutters the output and I don't want to supply an argument every time. Is it possible?

+3


source to share


1 answer


After some digging in the gulp source: Gulp inherits Orchestrator, which inherits EventEmitter. Global gulp requires local gulp and attaches some event listeners (task_start and task_stop).

I missed handlers for them. It's an AWFULL hack, but it did the trick.



Solution (place 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;
    });
}

      

+2


source







All Articles