add.js 927 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use strict';
  2. var through = require('through2');
  3. var log = require('fancy-log');
  4. var exec = require('child_process').exec;
  5. var escape = require('any-shell-escape');
  6. module.exports = function (opt) {
  7. if (!opt) opt = {};
  8. if (!opt.args) opt.args = ' ';
  9. var paths = [];
  10. var files = [];
  11. var fileCwd = process.cwd();
  12. var write = function(file, enc, cb) {
  13. paths.push(file.path);
  14. files.push(file);
  15. fileCwd = file.cwd;
  16. cb();
  17. };
  18. var flush = function(cb) {
  19. var cwd = opt.cwd || fileCwd;
  20. var cmd = 'git add ' + escape(paths) + ' ' + opt.args;
  21. var that = this;
  22. var maxBuffer = opt.maxBuffer || 200 * 1024;
  23. exec(cmd, {cwd: cwd, maxBuffer: maxBuffer}, function(err, stdout, stderr) {
  24. if (err) cb(err);
  25. if (!opt.quiet) log(stdout, stderr);
  26. files.forEach(that.push.bind(that));
  27. that.emit('end');
  28. cb();
  29. });
  30. };
  31. return through.obj(write, flush);
  32. };