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

我正在项目上工作,我想提交和推git使用git,但我正在面临一些问题,当我运行git任务,所以推,然后任务不等待提交….

任何人都可以帮助我! 我想使任务像第一次运行提交,然后自动推,不要运行推任务,直到完成提交任务….

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插件并行运行任务。 解决scheme非常简单:

 // 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之后会运行。

此外,我build议你在gulp中返回stream:push并使用gulp中的donecallback参数: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(); }); 

编辑:你必须返回stream吞吐:提交任务,我改变了我的答案,告诉你如何。

gulp-git提交并不实际返回一个stream(按devise)。 参见Commit不会end #49 。

为了解决这个问题,你可以使用蓝鸟这样的承诺库。

以下是github用户bfricka的build议:

 gulp.task('commit-changes', function (cb) { var q = require('bluebird'); //commit needs aa 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. ; }); });