使用Gulp 4和Promises来清理/复制文件

时间:2015-10-17 00:00:57

标签: promise gulp

我是Promises的新手,但是想使用它们来顺序启动一个干净的(删除文件),然后一个make(将文件从源代码复制到构建版本)。

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 glob是:[ './source/app/**/*' ](args.noclean && !args.deploy)评估为false,它启动del模块(即v2,因此返回Promise)。由于我对Promises的理解有限,我们.then()将它make()转换为.then(done)函数,我很乐意返回一个Promise而只是链接一个stream,但是那个'{1}}我迷失的地方。目前,我刚刚返回return del(getPatterns('dest-app')) .then(function() { return gulp.src(getPatterns('src-app')) .pipe(gulp.dest(dest + app)); }); ,这导致并非所有文件都被复制。

更新

我已经采取了大约70次随机刺伤(获得经验的绝对,最悲惨的方式!)并提出了这个:

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]);

构建过程完成后,文件总是先被删除,但Gulp只复制流中的前16个文件/文件夹。

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

std::partial_sort

2 个答案:

答案 0 :(得分:3)

不,您的更新2看起来不应该有效。 Sub Duplicate_And_Move_Slide() Dim oPPT As Object Dim oPresentation As Object Dim oSlide As Object Set oPPT = CreatePPT Set oPresentation = oPPT.presentations.Open( _ "<full path to your PP Presentation") With oPresentation.slides(1) .Duplicate .MoveTo 3 End With End Sub '---------------------------------------------------------------------------------- ' Procedure : CreatePPT ' Purpose : Creates an instance of Powerpoint and passes the reference back. '----------------------------------------------------------------------------------- Public Function CreatePPT(Optional bVisible As Boolean = True) As Object Dim oTmpPPT As Object '''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Defer error trapping in case PowerPoint is not running. ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''' On Error Resume Next Set oTmpPPT = GetObject(, "PowerPoint.Application") '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'If an error occurs then create an instance of PowerPoint. ' 'Reinstate error handling. ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' If Err.Number <> 0 Then Err.Clear On Error GoTo ERROR_HANDLER Set oTmpPPT = CreateObject("PowerPoint.Application") End If oTmpPPT.Visible = bVisible Set CreatePPT = oTmpPPT On Error GoTo 0 Exit Function ERROR_HANDLER: Select Case Err.Number Case Else MsgBox "Error " & Err.Number & vbCr & _ " (" & Err.Description & ") in procedure CreatePPT." Err.Clear End Select End Function 是承诺,并且没有您可以调用的make方法,而是您需要调用为您的构造函数回调提供的resolve函数。在您的版本中,resolve(以及返回的make)永远不会解决。

这应该是这样的:

Promise.all

答案 1 :(得分:0)

如何使用Promise:

var newPromise = new Promise (function (resolve, reject) {
    gulp.src(getPatterns('src-app')) // path on which you want to work 
    .pipe(gulp.dest('./build/app/')) // pipe the task that you want to run here i am copying 
    .on('end', resolve)
    .on('error', reject);
});

newPromise
    .then(function () {
        // Do something after promise is completed
        // This will be run after promise is resolved or success
    })
    .catch(function () {
        console.log("Error");
    });