2016-05-07 28 views
6

Tôi đang làm việc trên dự án và tôi muốn cam kết và đẩy git bằng cách sử dụng bởi gulp nhưng tôi đang đối mặt với một số vấn đề khi tôi đang chạy nhiệm vụ git để đẩy sau đó nhiệm vụ không chờ đợi cam kết ....Gulp Git chạy nhiệm vụ cam kết đầu tiên sau đó đẩy nhiệm vụ

Mọi người đều có thể giúp tôi! Tôi muốn thực hiện nhiệm vụ như lần đầu tiên chạy cam kết và sau đó đẩy tự động và không chạy nhiệm vụ đẩy cho đến khi hoàn thành nhiệm vụ cam kết ....

Giết công việc cho Gulp Git Cam kết và đẩy!

var gulp    = require('gulp'), 
 
    runSequence  = require('run-sequence'), 
 
    gutil    = require('gulp-util'), 
 
    git    = require('gulp-git'), 
 
    prompt   = require('gulp-prompt'); 
 

 

 
/* task to commit and push all file on git 
 
******************************************************************/ 
 
// git commit task with gulp prompt 
 
gulp.task('gulp:commit', function(){ 
 
    // just source anything here - we just wan't to call the prompt for now 
 
    gulp.src('./*') 
 
    .pipe(prompt.prompt({ 
 
    type: 'input', 
 
    name: 'commit', 
 
    message: 'Please enter commit message...' 
 
    }, function(res){ 
 
    // now add all files that should be committed 
 
    // but make sure to exclude the .gitignored ones, since gulp-git tries to commit them, too 
 
    return gulp.src([ '!node_modules/', './*' ], {buffer:false}) 
 
    .pipe(git.commit(res.commit)); 
 
    })); 
 
}); 
 

 
// Run git push, remote is the remote repo, branch is the remote branch to push to 
 
gulp.task('gulp:push', ['gulp:commit'], function(cb){ 
 
    git.push('origin', 'master', cb, function(err){ 
 
     if (err) throw err; 
 
    }); 
 
}); 
 

 
// # task completed notification with green color! 
 
gulp.task('gulp:done', ['gulp:push'], function(){ 
 
    console.log(''); 
 
    gutil.log(gutil.colors.green('************** Git push is done! **************')); 
 
    console.log(''); 
 
}); 
 

 
// gulp task to commit and push data on git 
 
gulp.task('git', function(){ 
 
    runSequence(['gulp:commit', 'gulp:push', 'gulp:done']) 
 
});

Trả lời

1

Vấn đề là bạn đang nói runSequence plugin để chạy các tác vụ song song. Giải pháp là thực sự đơn giản:

// gulp task to commit and push data on git 
gulp.task('git', function(){ 
    return runSequence('gulp:commit', 'gulp:push', 'gulp:done'); 
}); 

Bằng cách này bạn đảm bảo rằng ngụm: đẩy sẽ chỉ chạy sau ngụm: cam xong, và sau khi đẩy được thực hiện ngụm: thực hiện sẽ chạy.

Ngoài ra, tôi khuyên bạn nên trả lại dòng suối trong ngụm: đẩy và sử dụng tham số callback thực hiện trong ngụm: thực hiện, nếu bạn không làm ngụm mà không biết khi nào nhiệm vụ này kết thúc:

// git commit task with gulp prompt 
gulp.task('gulp:commit', function(){ 
    // just source anything here - we just wan't to call the prompt for now 
    return gulp.src('./*') 
    .pipe(prompt.prompt({ 
    type: 'input', 
    name: 'commit', 
    message: 'Please enter commit message...' 
    }, function(res){ 
    // now add all files that should be committed 
    // but make sure to exclude the .gitignored ones, since gulp-git tries to commit them, too 
    return gulp.src([ '!node_modules/', './*' ], {buffer:false}) 
    .pipe(git.commit(res.commit)); 
    })); 
}); 

// Run git push, remote is the remote repo, branch is the remote branch to push to 
gulp.task('gulp:push', ['gulp:commit'], function(cb){ 
    return git.push('origin', 'master', cb, function(err){ 
    if (err) throw err; 
    }); 
}); 

// # task completed notification with green color! 
gulp.task('gulp:done', ['gulp:push'], function(done){ 
    console.log(''); 
    gutil.log(gutil.colors.green('************** Git push is done! **************')); 
    console.log(''); 
    done(); 
}); 

EDIT: Bạn phải trả lại luồng trong gulp: cam kết tác vụ quá, tôi đã thay đổi câu trả lời của mình để cho bạn biết cách thực hiện.

+0

không làm việc ... và đẩy nhiệm vụ không được chờ đợi hoàn thành cam kết nhiệm vụ! – Faizy

+0

Bạn cũng không trả lại luồng trong ** gulp: commit **. Tôi đã chỉnh sửa câu trả lời của mình để chỉ cho bạn cách thực hiện, hãy thử và cho tôi biết nếu nó hoạt động. – franmartosr

+0

cảm ơn, mã này đang làm việc nhưng tôi nhận thấy một điều rằng nó không phải là đẩy cam kết hiện tại của tôi nhưng đẩy cam kết gần đây trên gitihub! – Faizy

0

cam kết gulp-git không thực sự trả về luồng (theo thiết kế). Xem Commit doesn't fire end #49.

Để giải quyết vấn đề này, bạn có thể sử dụng thư viện lời hứa như bluebird.

Đây là những gì làm việc cho tôi, theo đề nghị của github bfricka sử dụng:

gulp.task('commit-changes', function (cb) { 


    var q = require('bluebird'); //commit needs a a promise 
    return new q(function (resolve, reject) { 



     gulp.src('../') 
       .pipe(git.add()) 
       .pipe(stream = git.commit("my commit message")).on('error', function (e) { 
      console.log('No changes to commit'); 
     }) 

       .on('error', resolve) //resolve to allow the commit to resume even when there are not changes. 
       .on('end', resolve); 



     stream.resume(); //needed since commmit doesnt return stream by design. 


     ; 


    }); 

}); 
+0

"Cảm ơn" bạn rất nhiều – Faizy

+0

@Faizy - vui vì nó đã giúp! bạn sẽ tử tế đủ để chấp nhận câu trả lời, cảm ơn! – AndrewD

Các vấn đề liên quan