Search code examples
gulp

Gulp callbacks for zipping


I'm trying to use gulp to zip my build artifacts for later transfer but right now I'm having an issue where the gulp task seems to fail due to callbacks.

gulp.task("zip-build-folder", function() {
    git.revParse({args:'--abbrev-ref HEAD'}, function (err, branch) {
        console.log('current git branch: ' + branch);
        git.revParse({args:'--short HEAD'}, function (err, hash) {
            console.log('current git hash: '+hash);

            var dateString = new Date().toISOString().replace(/[-:]+/g, '_').replace(/[TZ]+/g,' ').replace(/\..+/, '');
            console.log(dateString);

            return gulp.src('build/**')
                .pipe(zip('artifact_'+branch+'_'+hash+'_'+dateString+'.zip'))
                .pipe(gulp.dest('artifacts'))
          });
    });
});

Is there a way to fix this issue with regards to gulp and callbacks?

Edit: I should say that the zip gets generated just fine, when I run npm run create-build-package it fails with "The following tasks did not complete: zip-build-folder."


Solution

  • I found out that there is a "done" function argument that changes the way gulp handles completion to wait until the function itself is called. Then new code looks like:

    gulp.task("zip-build-folder", function(done) { //changed
    
        var dateString = new Date().toISOString()
            .replace(/[-:]+/g, '_')
            .replace(/[TZ]+/g,' ')
            .replace(/\..+/, '');
        console.log(dateString);
    
        git.revParse({args:'--abbrev-ref HEAD'}, function (err, branch) {
            console.log('current git branch: ' + branch);
            git.revParse({args:'--short HEAD'}, function (err, hash) {
                //if (err) ...
                console.log('current git hash: '+hash);
    
    
                gulp.src('build/**')
                    .pipe(zip('artifact_'+branch+'_'+hash+'_'+dateString+'.zip'))
                    .pipe(gulp.dest('artifacts'));
    
                done(); //changed
              });
        });
    });