const { exec, execSync } = require('child_process');
const readline = require('readline').createInterface({
  //引入模块以便在命令行获取输入内容
  input: process.stdin,
  output: process.stdout,
});
/**
 * 上传git
 */
function execFun() {
  execSync('git pull');
  exec('git status', (err, stream) => {
    if (err) return console.err(err);
    // 获取当前已修改文件列表
    const list = stream.split('\n') || [];
    let haveAdd = false;
    for (let i = 0; i < list.length; i++) {
      const v = list[i];
      haveAdd = /Changes not staged for commit:/.test(v);
      if (haveAdd) break;
    }
    readline.question('请输入提交内容：', Input => {
      haveAdd && execSync('git add .');
      exec(`git commit -m ${Input || '提交'}`, err => {
        if (err)
          return console.log(`git commit -m ${Input || '提交'} 报错:`, err);
        exec(`git push`, err => {
          if (err) return console.log(err);
          readline.close(); //关闭对话
        });
      });
    });
  });
}
execFun();
