Files
dsh-markmag/scripts/switch-to-registry.mjs
T
发条 f585ec764c 新增 scripts/switch-to-registry.mjs:profile 依赖从 file: 切到 registry 版本
带自动回滚:任何一步失败恢复 package.json,避免重演 bundles 被重置的事故。
2026-11-08 16:17:47 +08:00

69 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* 把 profile 的插件依赖从本地路径(file:)切到 registry 版本(^0.1.0)。
*
* 为什么需要它:file: 依赖只在开发机成立,别人 clone 不到;切到 registry 之后,
* 版本可追溯、可指定旧版本回滚。
*
* 为什么带自动回滚:上次改包名触发过 DSH 重置 bundles 的事故,这一步同样有风险。
* 任何一步失败都会把 package.json 和 node_modules 恢复到动手前的状态。
*/
import fs from "node:fs"
import { execSync } from "node:child_process"
import { homedir } from "node:os"
import { join } from "node:path"
const P = join(homedir(), ".dsh/profiles/desktop")
const PKG = join(P, "package.json")
const PLUGIN = "@sidaocai/dsh-markmag"
const REGISTRY = "https://brain.sidaocai.cn/git/api/packages/sidaocai/npm/"
const log = (k, v) => console.log(k.padEnd(16), v)
if (!fs.existsSync(PKG)) { console.error("✗ 没找到 profile:", PKG); process.exit(1) }
const before = fs.readFileSync(PKG, "utf8")
const bak = PKG + ".bak-switch-" + Date.now()
fs.writeFileSync(bak, before)
log("backup:", bak.split("/").pop())
function rollback(reason) {
console.error("\n✗ " + reason)
fs.writeFileSync(PKG, before)
console.log("已恢复 package.json")
console.log("插件文件仍在 node_modules 里,DSH 应能照常启动。如不能,跑 scripts/repair-profile.mjs")
process.exit(1)
}
try {
log("1/3", "改依赖为 registry 版本")
const pkg = JSON.parse(before)
pkg.dependencies ??= {}
pkg.dependencies[PLUGIN] = "^0.1.0"
fs.writeFileSync(PKG, JSON.stringify(pkg, null, 2) + "\n")
log("2/3", "用 pnpm 装(只动这一个包)")
const out = execSync(`pnpm add "${PLUGIN}@^0.1.0" --registry=${REGISTRY}`, {
cwd: P, encoding: "utf8", stdio: "pipe", timeout: 180000,
})
const tail = out.trim().split("\n").filter(Boolean).slice(-3).join(" · ")
console.log(" " + tail)
log("3/3", "自检")
const installed = join(P, "node_modules", PLUGIN, "package.json")
if (!fs.existsSync(installed)) rollback("装完找不到包目录")
const ip = JSON.parse(fs.readFileSync(installed, "utf8"))
const clientOk = fs.existsSync(join(P, "node_modules", PLUGIN, "dist/client.js"))
const patchOk = fs.existsSync(join(P, "node_modules", PLUGIN, "cordis.patch.yml"))
const checks = { version: ip.version, client: clientOk, cordisPatch: patchOk }
log("SELF_CHECK", JSON.stringify(checks))
if (!clientOk || !patchOk) rollback("装出来的包不完整")
const final = JSON.parse(fs.readFileSync(PKG, "utf8"))
log("bundles 里还在:", final.dsh.profile.bundles.includes(PLUGIN) ? "是" : "否")
if (!final.dsh.profile.bundles.includes(PLUGIN)) rollback("bundles 被清掉了")
console.log("\n切换完成:" + PLUGIN + "@" + ip.version + "(来自 registry)")
console.log("重启 DSH 验证。出问题就把 " + bak.split("/").pop() + " 覆盖回 package.json 并重启。")
} catch (e) {
rollback("执行失败:" + String(e.stderr || e.stdout || e.message).slice(-300))
}