Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing Gulp dependencies when spawning child processes

I have a gulp task that spawns a jekyll child process. It compiles my markdown into an html file in _site.

I have another task that has this task as a dependency, as it performs some post-processing of the generated html. However, it triggers itself too early - as it appears that child processes do not factor into the dependency management

How can I ensure that html always runs after jekyll - preferably without using:

jekyll.on('exit', function (code, signal) {
    gulp.run('html');
});

Tasks:

gulp.task('jekyll', ['scripts', 'styles'], function () {
    var spawn = require('child_process').spawn;
    var jekyll = spawn('jekyll', ['build', '--config', 'app/markdown/_config.yml', '--trace'], {stdio: 'inherit'});
});



gulp.task('html', ['jekyll'] function () {
    return gulp.src('_site/*.html')
    .pipe($.useref.assets())
});
like image 772
Joseph Avatar asked Apr 20 '14 17:04

Joseph


1 Answers

Change your jekyll task to include an async callback, like so:

gulp.task('jekyll', ['scripts', 'styles'], function (gulpCallBack) {
    var spawn = require('child_process').spawn;
    var jekyll = spawn('jekyll', ['build', '--config', 'app/markdown/_config.yml', '--trace'], {stdio: 'inherit'});

    jekyll.on('exit', function(code) {
        gulpCallBack(code === 0 ? null :'ERROR: Jekyll process exited with code: '+code');
    });
});

All that is changed is adding the callback function argument to the task function's signature, and listening to the exit event on your spawned process to handle the callback.

Now gulp can be notified when the Jekyll process exits. Using the exit code will allow you to capture errors and stop processing.

Note: You might be able to simplify this to, depending on the exit code:

jekyll.on('exit', gulpCallBack);
like image 87
OverZealous Avatar answered Nov 10 '22 02:11

OverZealous