按顺序运行Gulp任务似乎不起作用 [英] Running Gulp tasks in sequence does not seem to work

查看:109
本文介绍了按顺序运行Gulp任务似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Gulp很新,我很喜欢它,但也有点不太了解,所以我可能会完全错误地解决这个问题。



我有这样的文件结构:

  main 
| --build
| - big_file1.txt
| -big_file2.txt
| -small_file3.txt
| -med_file4.txt

我想将 build 的内容复制到一个新的目录中,然后删除一些文件,这样我就设置了两个任务: p>

  var gulp = require(gulp); 
var foreach = require(gulp-foreach);
var del = require(del);
$ b $ gulp.task(taskA,function(cb){
var err = undefined;
gulp.src(./ build / * file *)
.pipe(foreach(function(stream,file){
stream.pipe(gulp.dest(./test));
return stream;
}));

cb(err);
});
$ b $ gulp.task(taskB,[taskA],function(cb){
var err = undefined;
del([./ test / * big *]);
cb(err);
});

当我手动运行 gulp taskA 文件出现在 ./测试中,如果我然后运行 gulp taskB 前缀为被删除。所有groovy和预期的。

然后,我尝试像这样顺序运行任务:

  gulp.task(both,[taskA,taskB]); 
$ b $ gulp.task(taskA,function(cb){
var err = undefined;
gulp.src(./ build / * file *)
.pipe(foreach(function(stream,file){
stream.pipe(gulp.dest(./test));
return stream;
}));

cb(err);
});
$ b $ gulp.task(taskB,[taskA],function(cb){
var err = undefined;
del([./ test / * big *]);
cb(err);
});

它的工作方式不一样。日志说明 taskA 开始然后完成THEN taskB THEN 并且重复文件在 ./ test 中创建,但删除从不发生。任何人都可以解释这里发生了什么,并提出一种解决办法吗?

预先感谢您:)

解决方案

顺序完成任务



好吧,根据你给出的描述,你应该返回你的流,完成任务:

  gulp.task(taskA,function(){
return gulp.src(。 / build / * file *)
.pipe(gulp.dest(./ test));
});
$ b gulp.task(taskB,[taskA],function(){
return del([./ test / * big *]);
});

使用回调是可能的,但您需要在正确的位置。在你问题中的代码中,你可以在之前调用它们 gulp.src()。pipe ... 执行的操作有机会跑步。它们是异步的。



简化Gulpfile



我已经解释了如何修复代码,可以工作,但可以将两个任务合并为一个;

  gulp.task(single,function(){
return gulp.src([./ build / * file *,!./ build / * big *])
.pipe(gulp.dest(./ test));
});

考虑到您输入的内容,这会给您提供与运行任务相同的结果两个。它复制匹配 ./ build / * file * ./ build / * big * ,因为在第二种模式开始时感叹号。这样做的好处是不需要进行不必要的复制操作,只需要删除多余的文件即可。


I am pretty new to Gulp and I kind of love it but also kind of don't really understand it at all so I might be going about this the wrong way entirely.

I have a file structure like this:

main
|-build
  |-big_file1.txt
  |-big_file2.txt
  |-small_file3.txt
  |-med_file4.txt

I want to copy the contents of build to a new dir and then delete some files so I set up two tasks like this:

var gulp = require("gulp");
var foreach = require("gulp-foreach");
var del =require("del");

gulp.task("taskA", function(cb){
    var err = undefined;
    gulp.src("./build/*file*")
        .pipe(foreach(function(stream, file){
            stream.pipe(gulp.dest("./test"));
            return stream;
        }));

    cb(err);
});

gulp.task("taskB", ["taskA"], function(cb){
    var err = undefined;
    del(["./test/*big*"]);
    cb(err);
});

When I manually run gulp taskA the duplicate files appear in ./test as expected and if I then run gulp taskB the ones prefixed with big are deleted. All groovy and as expected.

I then try to run the tasks in sequence like this:

gulp.task("both", ["taskA", "taskB"]);

gulp.task("taskA", function(cb){
    var err = undefined;
    gulp.src("./build/*file*")
        .pipe(foreach(function(stream, file){
            stream.pipe(gulp.dest("./test"));
            return stream;
        }));

    cb(err);
});

gulp.task("taskB", ["taskA"], function(cb){
    var err = undefined;
    del(["./test/*big*"]);
    cb(err);
});

And it doesn't work the same way. The log says that taskA is started then finished THEN taskB THEN both and the duplicate files are created in ./test but the deletion never occurs. Can anyone explain what is happening here and suggest a way to fix it?

Thank you in advance :)

解决方案

Making the tasks sequential

Well, based on the description you give, you should just return your streams to tell gulp when your tasks finished:

gulp.task("taskA", function (){
    return gulp.src("./build/*file*")
        .pipe(gulp.dest("./test"));
});

gulp.task("taskB", ["taskA"], function (){
    return del(["./test/*big*"]);
});

It is possible to use callbacks but you need to call them in the right place. In the code in your question you call them before the operations to be performed by gulp.src().pipe... have had a chance to run. They are asynchronous.

Simplifying the Gulpfile

I've explained how to fix your code so that the sequencing would work but you could combine the two tasks into one with;

gulp.task("single", function (){
    return gulp.src(["./build/*file*", "!./build/*big*"])
        .pipe(gulp.dest("./test"));
});

Given the input you've shown, this will give you the same result as running the task both. It copies the files that match ./build/*file* but not ./build/*big* because of the exclamation point at the beginning of the 2nd pattern. This has the advantage of not doing needless copying only to delete extraneous files afterwards.

这篇关于按顺序运行Gulp任务似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆