saveGit.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // const client = require("ssh2-sftp-client");
  2. // const path = require("path");
  3. // const fs = require("fs");
  4. const { execSync } = require("child_process");
  5. (async function Init() {
  6. // const localDir = "dist/";
  7. // const dirPath = path.resolve(__dirname, "./" + localDir);
  8. // const list = [];
  9. /**
  10. * 获取localDir下的所有文件路径
  11. * @param {string} pathDir
  12. */
  13. // async function getFiles(pathDir) {
  14. // const dirList = fs.readdirSync(pathDir) || [];
  15. // for (let i = 0; i < dirList.length; i++) {
  16. // const P = [pathDir, dirList[i]];
  17. // const stat = fs.lstatSync(P.join("\\"));
  18. // if (!stat.isDirectory()) {
  19. // list.push(P);
  20. // } else getFiles(P.join("\\"));
  21. // }
  22. // }
  23. // async function upServer() {
  24. // const config = {
  25. // path: {
  26. // // 远程地址
  27. // romotePath: "/data2/zyx/static/",
  28. // // 本地地址
  29. // localPath: path.resolve(__dirname, "./dist"),
  30. // },
  31. // romote: {
  32. // // 服务器 ip 地址
  33. // host: "172.16.101.11",
  34. // // 端口号,默认是 22
  35. // port: 22,
  36. // // 登录的用户名
  37. // username: "root",
  38. // // 登录密码
  39. // password: "",
  40. // },
  41. // };
  42. // const sftp = new client();
  43. // try {
  44. // await sftp.connect(config.romote);
  45. // const romotePath = config.path.romotePath;
  46. // const isCss = await sftp.exists(romotePath + "css");
  47. // const isFonts = await sftp.exists(romotePath + "fonts");
  48. // const isJs = await sftp.exists(romotePath + "js");
  49. // const isIndexHtml = await sftp.exists(romotePath + "index.html");
  50. // isCss && await sftp.rmdir(romotePath + "css", true);
  51. // isFonts && await sftp.rmdir(romotePath + "fonts", true);
  52. // isJs && await sftp.rmdir(romotePath + "js", true);
  53. // isIndexHtml && await sftp.delete(romotePath + "index.html", true);
  54. // console.log("已清空原有文件");
  55. // await sftp.uploadDir(config.path.localPath, romotePath);
  56. // console.log("上传完成");
  57. // // 断开连接
  58. // sftp.end();
  59. // console.log("断开链接");
  60. // } catch (err) {
  61. // console.log("更新失败", err);
  62. // }
  63. // }
  64. /**
  65. * 上传git
  66. */
  67. function execFun() {
  68. try {
  69. execSync("git add .");
  70. execSync('git commit -m "提交"');
  71. execSync("git push");
  72. console.log("上传git---完成");
  73. } catch (e) {
  74. console.log("上传git---失败");
  75. console.error(e);
  76. }
  77. }
  78. try {
  79. execFun();
  80. // await getFiles(dirPath);
  81. // console.log("获取文件路径---完成");
  82. // await upServer();
  83. } catch (err) {
  84. console.log("失败", err);
  85. }
  86. })();