Gulp Git运行第一个提交任务然后推送任务

时间:2016-05-07 22:26:18

标签: javascript node.js gulp gulp-git

我正在研究项目,我想通过gulp提交和推送git但是当我运行git任务时我遇到了一些问题所以推送然后任务不等待提交....

任何人都可以帮助我!我想让任务像第一次运行提交然后自动推送,并且在完成提交任务之前不要运行推送任务....

Gulp Git承诺和推送的任务!

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

2 个答案:

答案 0 :(得分:1)

问题是您是否正在告诉 runSequence 插件并行运行任务。解决方案非常简单:

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

通过这样做,您可以确保只有在 gulp:commit 完成后才会运行 gulp:push ,并且在完成推送后 gulp:done 会跑。

另外,我建议你在 gulp:push 中返回流,并使用 gulp:done 中的完成回调参数,如果你不做那个gulp我不知道这个任务何时结束:

// 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();
});

编辑:你必须在 gulp:commit 任务中返回流,我改变了我的答案,告诉你如何。

答案 1 :(得分:0)

gulp-git commit实际上并不返回流(按设计)。请参阅Commit doesn't fire end #49

要解决此问题,您可以使用bluebird等承诺库。

根据github用户bfricka的建议,这对我有用:

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.


        ;


    });

});