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?

  • Gulp: Generate sourcemaps for both minified and non-minified scripts
  • Coffeescript + Browserify + Gulp + Uglify
  • How to run gulp tasks sequentially?
  • Gulp.js with foundation / compass
  • Karma: Running a single test file from command line
  • Gulp tasks from within other Gulp tasks?
  • Using gulp to watch multiple files within a folder
  • Exclude sub-directory in gulp.watch
  • Gulp watch - execute tasks in order (synchronous)
  • Using gulp to compile bootstrap.less files into main bootstrap.css?
  • Gulp BrowserSync does not refresh
  • Reference JSPM packages within HTML without the version in path
  • One Solution collect form web for “How can I get gulp to be silent for some tasks (unit tests,vet etc)?”

    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;
        });
    }