saveGit.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const { exec, execSync } = require('child_process');
  2. const readline = require('readline').createInterface({
  3. //引入模块以便在命令行获取输入内容
  4. input: process.stdin,
  5. output: process.stdout,
  6. });
  7. /**
  8. * 上传git
  9. */
  10. function execFun() {
  11. // try {
  12. // execSync('git pull');
  13. // execSync('git add .');
  14. // execSync('git commit -m "提交"');
  15. // execSync('git push');
  16. // console.log('上传git---完成');
  17. // } catch (e) {
  18. // console.log('上传git---失败');
  19. // console.error(e);
  20. // }
  21. execSync('git pull');
  22. exec('git status', (err, stream) => {
  23. if (err) return console.err(err);
  24. // 获取当前已修改文件列表
  25. const list = stream.split('\n') || [];
  26. let haveAdd = false;
  27. for (let i = 0; i < list.length; i++) {
  28. const v = list[i];
  29. haveAdd = /Changes not staged for commit:/.test(v);
  30. if (haveAdd) break;
  31. }
  32. readline.question('请输入提交内容:', Input => {
  33. execSync('git add .');
  34. exec(`git commit -m ${Input || '提交'}`, err => {
  35. if (err)
  36. return console.log(`git commit -m ${Input || '提交'} 报错:`, err);
  37. exec(`git push`, err => {
  38. if (err) return console.log(err);
  39. readline.close(); //关闭对话
  40. });
  41. });
  42. });
  43. });
  44. }
  45. execFun();