index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var spawn = require('child_process').spawn;
  2. module.exports = function(repo, targetPath, opts, cb) {
  3. if (typeof opts === 'function') {
  4. cb = opts;
  5. opts = null;
  6. }
  7. opts = opts || {};
  8. var git = opts.git || 'git';
  9. var args = ['clone'];
  10. if (opts.shallow) {
  11. args.push('--depth');
  12. args.push('1');
  13. }
  14. args.push('--');
  15. args.push(repo);
  16. args.push(targetPath);
  17. var process = spawn(git, args);
  18. process.on('close', function(status) {
  19. if (status == 0) {
  20. if (opts.checkout) {
  21. _checkout();
  22. } else {
  23. cb && cb();
  24. }
  25. } else {
  26. cb && cb(new Error("'git clone' failed with status " + status));
  27. }
  28. });
  29. function _checkout() {
  30. var args = ['checkout', opts.checkout];
  31. var process = spawn(git, args, { cwd: targetPath });
  32. process.on('close', function(status) {
  33. if (status == 0) {
  34. cb && cb();
  35. } else {
  36. cb && cb(new Error("'git checkout' failed with status " + status));
  37. }
  38. });
  39. }
  40. }