diff --git a/scripts/update.mjs b/scripts/update.mjs new file mode 100644 index 0000000..b7d6933 --- /dev/null +++ b/scripts/update.mjs @@ -0,0 +1,69 @@ +#!/usr/bin/env node +/** + * 更新已安装的插件到最新版本。 + * + * 默认(源码模式):git pull → 重建 client → 装进 profile + * 今后(registry 模式):从 Gitea npm registry 拉包 → 装进 profile(等 token 配好) + * + * 为什么现在用源码模式:registry 发布需要带 package 权限的 token,还没配。 + * 源码模式对单人使用完全够用,也和现在的 file: 依赖一致。 + */ +import { execSync, execFileSync } from "node:child_process" +import { existsSync } from "node:fs" +import { join, dirname } from "node:path" +import { fileURLToPath } from "node:url" + +const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..") +const step = (n, t) => console.log("\n[" + n + "] " + t) +const run = (cmd, label) => { + try { + return execSync(cmd, { cwd: ROOT, encoding: "utf8", stdio: "pipe" }).trim() + } catch (e) { + console.error("✗ " + label + ":" + String(e.stderr || e.stdout || e.message).slice(-300)) + process.exit(1) + } +} + +if (!existsSync(join(ROOT, ".git"))) { + console.error("✗ 这个目录不是 git 仓库,无法更新") + process.exit(1) +} + +step("1/3", "检查远程有没有更新") +const before = run("git rev-parse --short HEAD", "读当前版本") +run("git fetch gitea main --quiet", "拉取远程") +const behind = run("git rev-list --count HEAD..gitea/main", "比对版本") +if (behind === "0") { + console.log(" 已是最新(" + before + ")") + if (!process.argv.includes("--force")) { + console.log("\n没有更新。要强制重装当前版本,加 --force") + process.exit(0) + } + console.log(" --force:继续重装") +} else { + console.log(" 落后 " + behind + " 个提交,开始更新") + run("git pull gitea main --quiet --ff-only", "拉取代码") + const after = run("git rev-parse --short HEAD", "读新版本") + console.log(" " + before + " → " + after) +} + +step("2/3", "重建客户端产物") +try { + const out = execFileSync("node", [join(ROOT, "scripts/build-client.mjs")], { encoding: "utf8" }) + console.log(" " + out.trim()) +} catch (e) { + console.error("✗ 构建失败:" + String(e.stdout || e.message).slice(-200)) + process.exit(1) +} + +step("3/3", "装进桌面版 profile") +try { + const out = execFileSync("node", [join(ROOT, "scripts/install.mjs")], { encoding: "utf8" }) + const m = out.match(/SELF_CHECK\s+(\{.*\})/) + console.log(" " + (m ? m[1] : out.trim().split("\n").slice(-1)[0])) +} catch (e) { + console.error("✗ 安装失败:" + String(e.stdout || e.message).slice(-300)) + process.exit(1) +} + +console.log("\n完成。重启 DSH 生效。")