saveGit.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  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. execSync('git pull');
  12. exec('git status', (err, stream) => {
  13. if (err) return console.err(err);
  14. // 获取当前已修改文件列表
  15. const list = stream.split('\n') || [];
  16. let haveAdd = false;
  17. for (let i = 0; i < list.length; i++) {
  18. const v = list[i];
  19. haveAdd = /Changes not staged for commit:/.test(v);
  20. if (haveAdd) break;
  21. }
  22. readline.question('请输入提交内容:', Input => {
  23. haveAdd && execSync('git add .');
  24. exec(`git commit -m ${Input || '提交'}`, err => {
  25. if (err)
  26. return console.log(`git commit -m ${Input || '提交'} 报错:`, err);
  27. exec(`git push`, err => {
  28. if (err) return console.log(err);
  29. readline.close(); //关闭对话
  30. });
  31. });
  32. });
  33. });
  34. }
  35. execFun();