Browse Source

feat:自动化更新

liming 2 weeks ago
parent
commit
c6692fb787
5 changed files with 79 additions and 13 deletions
  1. 2 1
      .gitignore
  2. 3 1
      package.json
  3. 19 8
      src/updater/index.js
  4. 3 3
      src/updater/update.json
  5. 52 0
      updataSetVersion.js

+ 2 - 1
.gitignore

@@ -29,4 +29,5 @@ coverage
 
 *.tsbuildinfo
 android
-ios
+ios
+bin

+ 3 - 1
package.json

@@ -5,13 +5,14 @@
   "type": "module",
   "scripts": {
     "dev": "vite --host",
+    "updata": "node ./updataSetVersion.js",
     "s": "cross-env DAPP_BUILD=1 npx cap sync",
     "a": "npx cap run android --live-reload --host=192.168.0.59 --port=5173",
     "app": "npx cap run android --live-reload --host=192.168.0.70 --port=5173",
     "ios": "npx cap run ios --live-reload --host=192.168.0.59 --port=5173",
     "android": "npx cap add android && npx cap sync",
     "android:clean": "(cd android && ./gradlew clean)",
-    "build": "vite build",
+    "build": "vite build && npm run updata",
     "build:apk": "npm run s && cross-env DAPP_BUILD=1 npx cap copy  && (cd android && ./gradlew assembleRelease)",
     "build:apk:windows": "vite build && npx cap copy && cd android && .\\gradlew.bat build",
     "icon": "npx capacitor-assets generate --android",
@@ -57,6 +58,7 @@
     "@capacitor/assets": "^3.0.5",
     "@vitejs/plugin-vue": "^5.2.3",
     "@vitejs/plugin-vue-jsx": "^5.0.1",
+    "adm-zip": "^0.5.16",
     "code-inspector-plugin": "^0.20.12",
     "cordova-plugin-console": "^1.1.0",
     "cross-env": "^7.0.3",

+ 19 - 8
src/updater/index.js

@@ -6,7 +6,7 @@ import { showDialog } from "vant";
 const HOST = "https://nim.angeltokens.io/updates/down/";
 const UPDATE_URL = HOST + "update.json";
 
-// 检查并更新
+// 检查并更新: shasum -a 256
 export async function checkAndUpdate() {
   // 判断是web 还是 ios
   if (Capacitor.getPlatform() === "web") {
@@ -19,14 +19,21 @@ export async function checkAndUpdate() {
   console.log("更新", meta);
 
   // 2. 基本校验
+  // 判断是ios 
+  if (Capacitor.getPlatform() === "ios") {
+    await CapacitorUpdater.ready(); // 等插件完全就绪
+  }else{
+    await new Promise(r => setTimeout(r, 1000));
+  }
   const current = await CapacitorUpdater.current();
   const { version } = await App.getInfo();
 
-  console.log("版本:", current.bundle.version, meta.version)
-  if (
-    current.bundle.version != "builtin" &&
-    semver.gte(current.bundle.version, meta.version)
-  ) {
+  console.log("版本:", current.bundle.version, meta.version);
+  if (!current.bundle.version ||  current.bundle.version == "builtin") {
+    console.log("无新版本!");
+    return;
+  }
+  if (semver.gte(current.bundle.version, meta.version)) {
     console.log("无新版本");
     return;
   }
@@ -41,7 +48,6 @@ export async function checkAndUpdate() {
     checksum: meta.checksum,
   });
 
-  await CapacitorUpdater.set(update); // 设置新版本
   // 通知
   if (meta.mandatory) {
     await showDialog({
@@ -49,8 +55,13 @@ export async function checkAndUpdate() {
       confirmButtonText: "立即體驗",
       message: meta.upDataDescription,
     }).then(async () => {
+      await CapacitorUpdater.set(update); // 设置新版本
       console.log("✅ 已切换到新版本,准备重启");
       await App.exitApp(); // 冷启动加载新 bundle
     });
-  } 
+  } else {
+    await CapacitorUpdater.set(update); // 设置新版本
+    console.log("✅ 已切换到新版本,准备重启");
+    await App.exitApp(); // 冷启动加载新 bundle
+  }
 }

+ 3 - 3
src/updater/update.json

@@ -1,8 +1,8 @@
 {
-  "version": "1.0.2",
+  "version": "1.0.9",
   "releaseDate": "2025-07-29 12:00:00", 
-  "checksum": "9bbb76110107c102dee68f1146c8d556a59b0cb550b3abcfc6cdb221182b0f12",
+  "checksum": "dbe314c14114dfce518bbc876ef71cf0827871250d88e5a20e54800f79097eca",
   "minBinaryVersion": "1.0.0",
   "mandatory": true,
-  "upDataDescription":"✨修正一些錯誤!"
+  "upDataDescription":"✨修正一些錯誤。。。。!!!"
 }

+ 52 - 0
updataSetVersion.js

@@ -0,0 +1,52 @@
+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 文件夹不存在,跳过打包");
+}