在生成子进程时管理Gulp依赖项

时间:2014-04-20 17:24:34

标签: node.js jekyll gulp

我有一个gulp任务,它会产生一个jekyll子进程。它将我的markdown编译成_site中的html文件。

我有另一项任务将此任务作为依赖项,因为它执行生成的html的一些后处理。但是,它过早触发 - 因为看起来子进程没有考虑到依赖关系管理

如何确保html始终在jekyll之后运行 - 最好不要使用:

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

任务:

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

1 个答案:

答案 0 :(得分:13)

将您的jekyll任务更改为include an async callback,如下所示:

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

所有改变的是将回调函数参数添加到任务函数的签名中,并在生成的进程上侦听exit事件来处理回调。

现在可以在Jekyll进程退出时通知gulp。使用退出代码可以捕获错误并停止处理。

注意:您可以将此简化为,具体取决于退出代码:

jekyll.on('exit', gulpCallBack);