【问题标题】:Gulp Git run first commit task then push taskGulp Git 运行第一个提交任务然后推送任务
【发布时间】:2016-09-02 19:46:33
【问题描述】:

我正在做项目,我想通过 gulp 提交和推送 git,但是当我运行 git 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'])
});

【问题讨论】:

    标签: javascript node.js gulp gulp-git


    【解决方案1】:

    问题是你告诉 runSequence 插件并行运行任务。解决方法很简单:

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

    通过这样做,您可以确保 gulp:push 仅在 gulp:commit 完成后运行,并且在推送完成后 gulp:done > 将运行。

    另外,我建议你在 gulp:push 中返回流,并在 gulp:done 中使用 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 任务中返回流,我更改了答案以向您展示如何操作。

    【讨论】:

    • 不工作......并且推送任务没有等待完成提交任务!
    • 您也不会在 gulp:commit 中返回流。我编辑了我的答案,向您展示如何,尝试一下并告诉我它是否有效。
    • 谢谢,这段代码正在运行,但我注意到一件事,它不是推送我当前的提交,而是推送最近在 github 上的提交!
    • 我想我不能帮你解决这个问题,我自己不使用 gulp-git 插件。很高兴看到我的回答对您有所帮助,如果对您有用,请给我点赞:D
    【解决方案2】:

    gulp-git commit 实际上并不返回流(按设计)。见Commit doesn't 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.
    
    
            ;
    
    
        });
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多