Skip to content

Instantly share code, notes, and snippets.

@yocontra
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yocontra/9538591 to your computer and use it in GitHub Desktop.
Save yocontra/9538591 to your computer and use it in GitHub Desktop.
var through = require('through2');
var gutil = require('gulp-util');
var exec = require('child_process').exec;
var escape = require('any-shell-escape');
module.exports = function (message, opt) {
if(!opt) opt = {};
if(!message) throw new Error('gulp-git: Commit message is required git.commit("commit message")');
if(!opt.args) opt.args = ' ';
var files = [];
var defaultCwd = process.cwd();
var write = function(file, enc, cb){
files.push(file);
cb();
};
var flush = function(cb){
// if we got files pick the cwd off the first one, otherwise just use process.cwd
var fileCwd = (files.length > 0 ? files[0].cwd : defaultCwd);
// if we got cwd as an option prefer that over fileCwd
var cwd = opt.cwd || fileCwd;
var cmd = 'git commit -m "' + message + '" ' + escape(files) + ' ' + opt.args;
exec(cmd, {cwd: cwd}, function(err, stdout, stderr){
if(err) return cb(err);
gutil.log(stdout, stderr);
// emit all buffered files
files.forEach(this.push.bind(this));
cb();
});
};
var stream = through.obj(write, flush);
return stream;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment