updataSetVersion.js 1.7 KB

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