saveGit.js 2.6 KB

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