将Gulp 4与Promises一起使用以清理/复制文件 [英] Using Gulp 4 with Promises to Clean/Copy Files

查看:93
本文介绍了将Gulp 4与Promises一起使用以清理/复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Promises还是很陌生,但是我想用它们顺序启动一个干净的文件(删除文件),然后启动一个make的文件(从源代码复制文件到构建文件).

I am very new to Promises, but would like to use them to sequentially fire off a clean (deleting files) and then a make (copying files from source to build).

gulp.task('make-app', function makeApp(done) {
    function make() {
        return gulp.src(getPatterns('src-app'))
            .pipe(gulp.dest('./build/app/'));
    }

    return (args.noclean && !args.deploy)
        ? make()
        : del(getPatterns('dest-app')
            .then(make);
            // .then(done);
});

一些注意事项:

getPatterns()是一个仅从我的配置返回glob模式的函数. src-app球体是:[ './source/app/**/*' ]. (args.noclean && !args.deploy)评估为false,这将启动del模块(它是v2,因此它返回Promise).就我对Promises的有限理解而言,我们将其.then()绑定到make()函数,我很想返回一个Promise并只链接一个.then(done),但这就是我迷路的地方.目前,我只是返回stream,这导致并非所有文件都被复制.

getPatterns() is a function which simply returns a glob pattern from my config. The src-app glob is: [ './source/app/**/*' ]. The (args.noclean && !args.deploy) evaluate to false, which starts the del module (which is v2, so it returns a Promise). To my limited understanding of Promises, we .then() it to the make() function, which I'd love to return a Promise and just chain a .then(done), but that's where I'm lost. Currently, I'm just returning a stream, which is resulting in not all the files copying over.

更新

我已经采取了大约70次随机刺伤(这是获得经验的绝对,最痛苦的方法!),并且提出了以下建议:

I've taken about 70 more random stabs (the absolute, most miserable way to gain experience!) and have come up with this:

return del(getPatterns('dest-app'))
    .then(function() {
        return gulp.src(getPatterns('src-app'))
            .pipe(gulp.dest(dest + app));
    });

构建过程完成后,始终总是首先删除文件,但是Gulp只会从流中复制前16个文件/文件夹.

And the build process completes, the files always get deleted first, but Gulp only copies over the first 16 files/folders from the stream.

更新2 -下面的代码看起来像是可以正常工作,但是Gulp报告它没有完成(因此没有返回Stream,Promise或Callback).

UPDATE 2 - The below code LOOKS as if it'd work, but Gulp reports that it is not completing (so a Stream, Promise or Callback is not being returned).

var make = new Promise(function(resolve, reject) {});        
var clean = del(getPatterns('dest-app'))
    .then(function() {
        var makeStream = gulp.src(getPatterns('src-app'))
            .pipe(gulp.dest(dest + app));
        makeStream.on('end', function() {
            make.resolve();
        });
    });

return Promise.all([clean, make]);

推荐答案

否,您的更新2似乎不起作用. make是承诺,没有可以调用的resolve方法,相反,您需要调用构造函数回调函数中提供的resolve函数.在您的版本中,make(和返回的Promise.all)永远无法解析.

No, your update 2 doesn't look like it should be working. make is promise and has no resolve method that you could call, instead you'd need to call the resolve function that is given to your constructor callback. In your version, make (and the returned Promise.all) never resolve.

它应该是这样的:

function make() {
    return new Promise(function(resolve, reject) {
        gulp.src(getPatterns('src-app'))
        .pipe(gulp.dest('./build/app/'))
        .on("end", resolve)
        .on("error", reject);
    });
}

return (args.noclean && !args.deploy)
    ? make()
    : del(getPatterns('dest-app')).then(make);

这篇关于将Gulp 4与Promises一起使用以清理/复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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