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

Jos*_*eph 5 jekyll node.js gulp

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

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

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

jekyll.on('exit', function (code, signal) {
    gulp.run('html');
});
Run Code Online (Sandbox Code Playgroud)

任务:

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())
});
Run Code Online (Sandbox Code Playgroud)

Ove*_*ous 13

更改您的jekyll任务以包含异步回调,如下所示:

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');
    });
});
Run Code Online (Sandbox Code Playgroud)

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

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

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

jekyll.on('exit', gulpCallBack);
Run Code Online (Sandbox Code Playgroud)