liyongli 2 rokov pred
rodič
commit
950866bbcb
1 zmenil súbory, kde vykonal 45 pridanie a 25 odobranie
  1. 45 25
      saveGit.js

+ 45 - 25
saveGit.js

@@ -1,26 +1,46 @@
-const { execSync } = require("child_process");
-
-(async function Init() {
-
-  /**
-   * 上传git
-   */
-  function execFun() {
-    try {
-      execSync("git pull");
-      execSync("git add .");
-      execSync('git commit -m "提交"');
-      execSync("git push");
-      console.log("上传git---完成");
-    } catch (e) {
-      console.log("上传git---失败");
-      console.error(e);
+const { exec, execSync } = require('child_process');
+const readline = require('readline').createInterface({
+  //引入模块以便在命令行获取输入内容
+  input: process.stdin,
+  output: process.stdout,
+});
+/**
+ * 上传git
+ */
+function execFun() {
+  // try {
+  //   execSync('git pull');
+  //   execSync('git add .');
+  //   execSync('git commit -m "提交"');
+  //   execSync('git push');
+  //   console.log('上传git---完成');
+  // } catch (e) {
+  //   console.log('上传git---失败');
+  //   console.error(e);
+  // }
+  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;
     }
-  }
-
-  try {
-    execFun();
-  } catch (err) {
-    console.log("失败", err);
-  }
-})();
+    console.log(haveAdd);
+    readline.question('请输入提交内容:', Input => {
+      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();