Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 743
Rimian Avatar asked Jan 15 '15 23:01

Rimian


People also ask

What is Gulpfile?

A gulpfile is a file in your project directory titled gulpfile. js (or capitalized as Gulpfile. js , like Makefile), that automatically loads when you run the gulp command.

What is gulp watch command?

The watch() API connects globs to tasks using a file system watcher. It watches for changes to files that match the globs and executes the task when a change occurs. If the task doesn't signal Async Completion, it will never be run a second time.

How do you define a task in gulp?

gulp. task('task-name', function() { // Stuff here }); task-name refers to the name of the task, which would be used whenever you want to run a task in Gulp. You can also run the same task in the command line by writing gulp task-name .


1 Answers

(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".

like image 99
Jacob Budin Avatar answered Sep 20 '22 13:09

Jacob Budin