问题描述
我有多个Gulp任务向shell发送一系列命令。第二个任务是依赖于第一个。如何确保在执行第二个任务之前,第一个任务中发送给async.series的所有命令都已完成。
gulp.task('task1',function(cb){
process.chdir(__ dirname);
process.chdir('path');
var cmdArray = getCmdsForTask1();
runSeries(cmdArray,'Task 1');
return cb();
});
gulp.task('task2',['task1'],function(){
process.chdir(__ dirname);
process.chdir('task2_path');
var cmd2 = getCmdForTask2();
runSeries([cmd2],'Task 2');
});
$ b $ var runSeries = function(cmdArray,taskName){
async.series(cmdArray,function(err,results){
results.forEach(function ){
gutil.log(gutil.colors.cyan(taskName +'complete:')+ gutil.colors.white(result));
});
});
};
非常感谢!
您使用async.series进入正确的轨道。一个缺失的部分是你完成时没有告诉原始任务。
$ $ $ $ $ $ $ $ $ gulp.task('task1',function (cb){
process.chdir(__ dirname);
process.chdir('path');
var cmdArray = getCmdsForTask1();
runSeries(cmdArray ,'Task 1',cb);
});
gulp.task('task2',['task1'],function(cb){
process.chdir(__ dirname);
process.chdir('task2_path') ;
var cmd2 = getCmdForTask2();
runSeries([cmd2],'Task 2',cb);
});
$ b $ var runSeries = function(cmdArray,taskName,cb){
async.series(cmdArray,function(err,results){
results.forEach(function (result){
gutil.log(gutil.colors.cyan(taskName +'complete:')+ gutil.colors.white(result));
});
cb(err );
});
};
请注意我是如何处理每个任务的回调,并在async.series完成时调用它。
I have multiple Gulp tasks that send a series of commands to the shell. The second task is dependent on the first. How do make ensure all commands sent to async.series in the first task are complete before the second task is executed.
gulp.task('task1', function(cb) {
process.chdir(__dirname);
process.chdir('path');
var cmdArray = getCmdsForTask1();
runSeries(cmdArray, 'Task 1');
return cb();
});
gulp.task('task2',['task1'], function() {
process.chdir(__dirname);
process.chdir('task2_path');
var cmd2 = getCmdForTask2();
runSeries([cmd2], 'Task 2');
});
var runSeries = function(cmdArray, taskName) {
async.series(cmdArray, function(err, results) {
results.forEach( function(result) {
gutil.log(gutil.colors.cyan(taskName + ' complete:') + gutil.colors.white(result) );
});
});
};
Thanks Much!
You're on the right track using async.series. The one missing piece is you haven't told the original task when it's done.
gulp.task('task1', function(cb) {
process.chdir(__dirname);
process.chdir('path');
var cmdArray = getCmdsForTask1();
runSeries(cmdArray, 'Task 1', cb);
});
gulp.task('task2',['task1'], function(cb) {
process.chdir(__dirname);
process.chdir('task2_path');
var cmd2 = getCmdForTask2();
runSeries([cmd2], 'Task 2', cb);
});
var runSeries = function(cmdArray, taskName, cb) {
async.series(cmdArray, function(err, results) {
results.forEach( function(result) {
gutil.log(gutil.colors.cyan(taskName + ' complete:') + gutil.colors.white(result) );
});
cb(err);
});
};
Note how I took in a callback to each task, and called it when the async.series was complete.
这篇关于在async.series帮助函数中触发shell命令时,如何依次触发Gulp任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!