12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import fs from "fs";
- import AdmZip from "adm-zip";
- import crypto from "crypto";
- // 读取 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);
- // 判断有没有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
- files.forEach((file) => {
- const filePath = `dist/${file}`;
- if (fs.statSync(filePath).isFile()) {
- // 确保是文件(非子目录)
- zip.addLocalFile(filePath);
- }
- });
- // 保存 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}`);
- // 4. 修改 update.json 的 checksum
- updata.checksum = hash; // 假设 update.json 有 checksum 字段
- fs.writeFileSync(updateJsonPath, JSON.stringify(updata, null, 2)); // 2 空格缩进
- console.log(`已更新 ${updateJsonPath} 的 checksum: ${hash}`);
- } else {
- console.log("dist 文件夹不存在,跳过打包");
- }
|