Skip to content
Advertisement

console.log to stdout on gulp events

I want to log to stdout (the config environment) when a gulp task is running or has run.

Something like this:

gulp.task('scripts', function () {
  var enviroment = argv.env || 'development';
  var config = gulp.src('config/' + enviroment + '.json')
      .pipe(ngConstant({name: 'app.config'}));
  var scripts = gulp.src('js/*');

  return es.merge(config, scripts)
    .pipe(concat('app.js'))
    .pipe(gulp.dest('app/dist'))
    .on('success', function() { 
      console.log('Configured environment: ' + environment);
    });
});

I am not sure what event I should be responding to or where to find a list of these. Any pointers? Many thanks.

Advertisement

Answer

(In December 2017, the gulp-util module, which provided logging, was deprecated. The Gulp team recommended that developers replace this functionality with the fancy-log module. This answer has been updated to reflect that.)

fancy-log provides logging and was originally built by the Gulp team.

var log = require('fancy-log');
log('Hello world!');

To add logging, Gulp’s API documentation tell us that .src returns:

Returns a stream of Vinyl files that can be piped to plugins.

Node.js’s Stream documentation provides a list of events. Put together, here’s an example:

gulp.task('default', function() {
    return gulp.src('main.scss')
        .pipe(sass({ style: 'expanded' }))
        .on('end', function(){ log('Almost there...'); })
        .pipe(minifycss())
        .pipe(gulp.dest('.'))
        .on('end', function(){ log('Done!'); });
});

Note: The end event may be called before the plugin is complete (and has sent all of its own output), because the event is called when “all data has been flushed to the underlying system”.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement