Gulp Git首先运行提交任务,然后运行推送任务



我正在进行项目,我想通过gump提交并推送git,但我在运行git任务时遇到了一些问题,所以push-then-task没有等待提交。。。。

任何人都可以帮我!我想让任务像第一次运行提交,然后自动推送,直到完成提交任务才运行推送任务。。。。

Gulp Git的Gulp任务提交并推送!

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

问题是您告诉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,并且在推送完成后将运行chulp:commed

此外,我建议您在gulp:push中返回流,并在gulp:fdone使用done回调参数,如果您不这样做,则不知道此任务何时结束:

// 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任务中返回流,我更改了答案以向您展示如何返回。

gulp-git-commit实际上并没有返回流(根据设计)。请参阅Commit not fire end#49。

为了解决这个问题,你可以使用像bluebird这样的promise库。

以下是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.

        ;

    });
});

最新更新