123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import fs from "fs";
- import AdmZip from "adm-zip";
- import crypto from "crypto";
- import semver from "semver";
- // 读取 update.json
- const updateJsonPath = "bin/update.json"
- const upDataJson = fs.readFileSync("src/updater/update.json", "utf8");
- let updata = JSON.parse(upDataJson);
- console.log(updata.version);
- // updata.version 加1
- updata.version = semver.inc(updata.version, "patch");
-
- // 判断有没有bin 文件夹没有,就创建一个,有的话就删除bin中的所有内容
- if (!fs.existsSync("bin")) {
- fs.mkdirSync("bin");
- } else {
- fs.readdirSync("bin").forEach((file) => {
- fs.unlinkSync(`bin/${file}`);
- });
- }
- // 判断有没有dist 文件夹,有的话,进去dist中,吧里面所有内容打成一个zip包,名称为123.zip, 同时打印下123.zip 的shasum -a 256 内容
- // 检查 dist 文件夹并打包
- if (fs.existsSync("dist")) {
- const files = fs.readdirSync("dist");
- const zip = new AdmZip();
- const zipName = `v${updata.version}.zip`;
- zip.addLocalFolder('dist', ''); // 第二个参数 '' 表示把 dist 本身作为根目录
-
- // 保存 ZIP 文件
- const zipPath = `bin/${zipName}`;
- zip.writeZip(zipPath);
- console.log(`ZIP 包已生成: ${zipPath}`);
- // 计算 SHA-256
- const zipData = fs.readFileSync(zipPath);
- const hash = crypto.createHash("sha256").update(zipData).digest("hex");
- console.log(`SHA-256: ${hash}`);
- // 当前时间
- const now = new Date();
- const fmt = now.toISOString()
- .replace('T', ' ')
- .slice(0, 19);
- // 4. 修改 update.json 的 checksum
- updata.releaseDate = fmt
- updata.checksum = hash; // 假设 update.json 有 checksum 字段
- fs.writeFileSync(updateJsonPath, JSON.stringify(updata, null, 2)); // 2 空格缩进
- fs.writeFileSync("src/updater/update.json", JSON.stringify(updata, null, 2)); // 2 空格缩进
- console.log(`已更新 ${updateJsonPath} 的 checksum: ${hash}`);
-
-
- } else {
- console.log("dist 文件夹不存在,跳过打包");
- }
|