updataSetVersion.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import fs from "fs";
  2. import AdmZip from "adm-zip";
  3. import crypto from "crypto";
  4. import semver from "semver";
  5. // 读取 update.json
  6. const updateJsonPath = "bin/update.json"
  7. const upDataJson = fs.readFileSync("src/updater/update.json", "utf8");
  8. let updata = JSON.parse(upDataJson);
  9. console.log(updata.version);
  10. // updata.version 加1
  11. updata.version = semver.inc(updata.version, "patch");
  12. // 判断有没有bin 文件夹没有,就创建一个,有的话就删除bin中的所有内容
  13. if (!fs.existsSync("bin")) {
  14. fs.mkdirSync("bin");
  15. } else {
  16. fs.readdirSync("bin").forEach((file) => {
  17. fs.unlinkSync(`bin/${file}`);
  18. });
  19. }
  20. // 判断有没有dist 文件夹,有的话,进去dist中,吧里面所有内容打成一个zip包,名称为123.zip, 同时打印下123.zip 的shasum -a 256 内容
  21. // 检查 dist 文件夹并打包
  22. if (fs.existsSync("dist")) {
  23. const files = fs.readdirSync("dist");
  24. const zip = new AdmZip();
  25. const zipName = `v${updata.version}.zip`;
  26. zip.addLocalFolder('dist', ''); // 第二个参数 '' 表示把 dist 本身作为根目录
  27. // 保存 ZIP 文件
  28. const zipPath = `bin/${zipName}`;
  29. zip.writeZip(zipPath);
  30. console.log(`ZIP 包已生成: ${zipPath}`);
  31. // 计算 SHA-256
  32. const zipData = fs.readFileSync(zipPath);
  33. const hash = crypto.createHash("sha256").update(zipData).digest("hex");
  34. console.log(`SHA-256: ${hash}`);
  35. // 当前时间
  36. const now = new Date();
  37. const fmt = now.toISOString()
  38. .replace('T', ' ')
  39. .slice(0, 19);
  40. // 4. 修改 update.json 的 checksum
  41. updata.releaseDate = fmt
  42. updata.checksum = hash; // 假设 update.json 有 checksum 字段
  43. fs.writeFileSync(updateJsonPath, JSON.stringify(updata, null, 2)); // 2 空格缩进
  44. fs.writeFileSync("src/updater/update.json", JSON.stringify(updata, null, 2)); // 2 空格缩进
  45. console.log(`已更新 ${updateJsonPath} 的 checksum: ${hash}`);
  46. } else {
  47. console.log("dist 文件夹不存在,跳过打包");
  48. }