mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-30 09:00:22 +08:00
feat: 发布 1.1.11-beta.2,正式/测试分栏并能切回去
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
24a3ff1b99
commit
2287ac4be7
5 files changed
+301
-144
No files matched your search
+155
-58
@@ -70,6 +70,27 @@ function isBetaName(name) {
|
||||
return /(?:^|[-._])(beta|rc|pre|preview|test)(?:\d|$|[-._])/i.test(String(name || ""));
|
||||
}
|
||||
|
||||
function splitVersion(raw) {
|
||||
const text = String(raw || "").trim().replace(/^v/i, "");
|
||||
const cut = text.split("-");
|
||||
const core = String(cut[0] || "").split(".").map((n) => parseInt(n, 10) || 0);
|
||||
return { core: [core[0] || 0, core[1] || 0, core[2] || 0], pre: cut.slice(1).join("-") };
|
||||
}
|
||||
|
||||
export function versionIsNewer(remote, local) {
|
||||
if (!remote) return false;
|
||||
if (!local) return true;
|
||||
if (String(remote) === String(local)) return false;
|
||||
const a = splitVersion(remote);
|
||||
const b = splitVersion(local);
|
||||
for (let i = 0; i < 3; i += 1) {
|
||||
if (a.core[i] !== b.core[i]) return a.core[i] > b.core[i];
|
||||
}
|
||||
if (a.pre && !b.pre) return false;
|
||||
if (!a.pre && b.pre) return true;
|
||||
return a.pre > b.pre;
|
||||
}
|
||||
|
||||
export function readChannelState() {
|
||||
const raw = readJson(channelStatePath()) || {};
|
||||
return {
|
||||
@@ -448,6 +469,19 @@ function rememberVersion(item, seen) {
|
||||
return item;
|
||||
}
|
||||
|
||||
function markCurrent(versions, state, localVer, localSha) {
|
||||
return (versions || []).map((item) => ({
|
||||
...item,
|
||||
shaShort: item.shaShort || (item.sha ? String(item.sha).slice(0, 7) : ""),
|
||||
current: Boolean(
|
||||
(localSha && item.sha && sameRev(localSha, item.sha))
|
||||
|| (state.pin && item.ref === state.pin)
|
||||
|| (!state.pin && item.latest && item.channel === state.channel && item.version && item.version === localVer)
|
||||
|| (!state.pin && item.version && item.version === localVer && item.channel === (isBetaName(localVer) ? "beta" : "stable")),
|
||||
),
|
||||
}));
|
||||
}
|
||||
|
||||
async function listGithubTags() {
|
||||
try {
|
||||
const rows = await fetchJson(`https://api.github.com/repos/${REPO}/tags?per_page=50`);
|
||||
@@ -526,70 +560,106 @@ async function listRemoteVersions() {
|
||||
const localVer = localVersion();
|
||||
const localSha = (gitOk() ? gitHead(PLUGIN_ROOT) : "") || readStamp();
|
||||
return {
|
||||
versions: out.map((item) => ({
|
||||
...item,
|
||||
shaShort: item.sha ? String(item.sha).slice(0, 7) : "",
|
||||
current: Boolean(
|
||||
(localSha && item.sha && sameRev(localSha, item.sha))
|
||||
|| (state.pin && item.ref === state.pin)
|
||||
|| (!state.pin && item.latest && item.channel === state.channel && item.version && item.version === localVer),
|
||||
),
|
||||
})),
|
||||
versions: markCurrent(out, state, localVer, localSha),
|
||||
hasBeta: Boolean(betaSha || out.some((item) => item.channel === "beta" && item.kind === "tag")),
|
||||
stableSha,
|
||||
betaSha,
|
||||
};
|
||||
}
|
||||
|
||||
function alignChannelState(state, listed, localRev, localVer) {
|
||||
if (state.pin) return state;
|
||||
if (listed.betaSha && localRev && sameRev(localRev, listed.betaSha) && state.channel !== "beta") {
|
||||
return writeChannelState({ channel: "beta", pin: "" });
|
||||
}
|
||||
if (listed.stableSha && localRev && sameRev(localRev, listed.stableSha) && state.channel !== "stable") {
|
||||
return writeChannelState({ channel: "stable", pin: "" });
|
||||
}
|
||||
if (!localRev && localVer) {
|
||||
const inferred = isBetaName(localVer) ? "beta" : "stable";
|
||||
if (inferred !== state.channel) return writeChannelState({ channel: inferred, pin: "" });
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
function describeLane(id, listed, state, localVer, localRev) {
|
||||
const sha = id === "beta" ? listed.betaSha : listed.stableSha;
|
||||
const versions = listed.versions.filter((item) => item.channel === id);
|
||||
const latest = versions.find((item) => item.latest);
|
||||
const version = latest?.version || "";
|
||||
const onLane = state.channel === id;
|
||||
const matchesHead = Boolean(localRev && sha && sameRev(localRev, sha));
|
||||
const current = versions.some((item) => item.current) || matchesHead
|
||||
|| Boolean(onLane && version && localVer && version === localVer && !state.pin);
|
||||
let hasUpdate = false;
|
||||
if (onLane && state.pin) {
|
||||
hasUpdate = Boolean(sha && localRev && !sameRev(localRev, sha));
|
||||
} else if (onLane && sha && localRev) {
|
||||
hasUpdate = !sameRev(localRev, sha) || versionIsNewer(version, localVer);
|
||||
} else if (onLane && versionIsNewer(version, localVer)) {
|
||||
hasUpdate = true;
|
||||
}
|
||||
return {
|
||||
id,
|
||||
ref: CHANNELS[id].ref,
|
||||
version,
|
||||
sha: sha ? String(sha).slice(0, 7) : "",
|
||||
current,
|
||||
hasUpdate,
|
||||
hasRemote: Boolean(sha || versions.length),
|
||||
};
|
||||
}
|
||||
|
||||
function lanesOf(listed, state, localVer, localRev) {
|
||||
return {
|
||||
stable: describeLane("stable", listed, state, localVer, localRev),
|
||||
beta: describeLane("beta", listed, state, localVer, localRev),
|
||||
};
|
||||
}
|
||||
|
||||
export async function checkUpdate() {
|
||||
const state = readChannelState();
|
||||
let state = readChannelState();
|
||||
const localVer = localVersion();
|
||||
const listed = await listRemoteVersions();
|
||||
const channelRef = currentChannelRef(state);
|
||||
const remoteSha = state.channel === "beta" ? listed.betaSha : listed.stableSha;
|
||||
const remoteVer = remoteSha ? await remotePackageVersion(remoteSha, channelRef) : "";
|
||||
if (!remoteSha && !remoteVer) {
|
||||
if (state.channel === "beta") {
|
||||
return rememberUpdate({
|
||||
ok: true,
|
||||
hasUpdate: false,
|
||||
hasBeta: false,
|
||||
error: "还没有测试版",
|
||||
localVersion: localVer,
|
||||
remoteVersion: "",
|
||||
localSha: ((gitOk() ? gitHead(PLUGIN_ROOT) : "") || readStamp()).slice(0, 7),
|
||||
remoteSha: "",
|
||||
via: detectVia(),
|
||||
versions: listed.versions,
|
||||
...channelPublic(state),
|
||||
});
|
||||
}
|
||||
throw new Error("无法读取 GitHub master");
|
||||
}
|
||||
const gitSha = gitOk() ? gitHead(PLUGIN_ROOT) : "";
|
||||
const stamp = readStamp();
|
||||
const localRev = gitSha || stamp;
|
||||
let hasUpdate = false;
|
||||
if (state.pin) {
|
||||
hasUpdate = Boolean(remoteSha && localRev && !sameRev(localRev, remoteSha));
|
||||
} else if (remoteSha && localRev) {
|
||||
hasUpdate = !sameRev(localRev, remoteSha);
|
||||
} else if (remoteVer && localVer && remoteVer !== localVer) {
|
||||
hasUpdate = true;
|
||||
} else if (remoteSha && !localRev) {
|
||||
hasUpdate = true;
|
||||
state = alignChannelState(state, listed, localRev, localVer);
|
||||
listed.versions = markCurrent(listed.versions, state, localVer, localRev);
|
||||
const channelRef = currentChannelRef(state);
|
||||
const lanes = lanesOf(listed, state, localVer, localRev);
|
||||
const lane = lanes[state.channel] || lanes.stable;
|
||||
if (!lane.hasRemote && state.channel === "beta") {
|
||||
return rememberUpdate({
|
||||
ok: true,
|
||||
hasUpdate: false,
|
||||
hasBeta: false,
|
||||
error: "还没有测试版",
|
||||
localVersion: localVer,
|
||||
remoteVersion: "",
|
||||
localSha: localRev ? localRev.slice(0, 7) : "",
|
||||
remoteSha: "",
|
||||
updateTarget: channelRef,
|
||||
via: detectVia(),
|
||||
versions: listed.versions,
|
||||
lanes,
|
||||
...channelPublic(state),
|
||||
});
|
||||
}
|
||||
if (!lanes.stable.hasRemote) throw new Error("无法读取 GitHub master");
|
||||
const result = {
|
||||
ok: true,
|
||||
hasUpdate,
|
||||
hasUpdate: Boolean(lane.hasUpdate),
|
||||
hasBeta: listed.hasBeta,
|
||||
updateTarget: channelRef,
|
||||
pinned: Boolean(state.pin),
|
||||
localVersion: localVer,
|
||||
remoteVersion: remoteVer || (remoteSha ? remoteSha.slice(0, 7) : ""),
|
||||
remoteVersion: lane.version || lane.sha,
|
||||
localSha: localRev ? localRev.slice(0, 7) : "",
|
||||
remoteSha: remoteSha ? remoteSha.slice(0, 7) : "",
|
||||
remoteSha: lane.sha,
|
||||
via: detectVia(),
|
||||
versions: listed.versions,
|
||||
lanes,
|
||||
...channelPublic(state),
|
||||
};
|
||||
rememberUpdate(result);
|
||||
@@ -640,20 +710,25 @@ function gitResetTo(dir, ref, { force = false } = {}) {
|
||||
}
|
||||
const target = String(ref || STABLE_REF);
|
||||
try {
|
||||
if (target === STABLE_REF || target === BETA_REF) {
|
||||
runGit(dir, ["pull", "--ff-only", "origin", target], 60000);
|
||||
runGit(dir, ["fetch", "origin", "--tags"], 60000);
|
||||
} catch {
|
||||
runGit(dir, ["fetch", "origin", "--tags", target], 60000);
|
||||
}
|
||||
if (target === STABLE_REF || target === BETA_REF) {
|
||||
try {
|
||||
runGit(dir, ["checkout", "-B", target, `origin/${target}`]);
|
||||
return;
|
||||
} catch {
|
||||
runGit(dir, ["reset", "--hard", `origin/${target}`]);
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
// 再走 fetch + reset。
|
||||
}
|
||||
try {
|
||||
runGit(dir, ["fetch", "origin", "--tags", target], 60000);
|
||||
} catch {
|
||||
runGit(dir, ["fetch", "origin", "--tags"], 60000);
|
||||
// 上面已经 fetch 过全部分支。
|
||||
}
|
||||
const remote = (target === STABLE_REF || target === BETA_REF) ? `origin/${target}` : target;
|
||||
runGit(dir, ["reset", "--hard", remote], 60000);
|
||||
runGit(dir, ["reset", "--hard", target]);
|
||||
}
|
||||
|
||||
function extractZip(zip, dest) {
|
||||
@@ -690,8 +765,16 @@ async function overlayFromZip(dests, ref = STABLE_REF) {
|
||||
for (const dest of unique) {
|
||||
for (const name of COPY_NAMES) {
|
||||
const from = path.join(unpacked, name);
|
||||
const to = path.join(dest, name);
|
||||
if (!fs.existsSync(from)) continue;
|
||||
await fsp.cp(from, path.join(dest, name), { recursive: true, force: true });
|
||||
if (name === "lib" || name === "docs" || name === "bin") {
|
||||
try {
|
||||
await fsp.rm(to, { recursive: true, force: true });
|
||||
} catch {
|
||||
// 目录被占用时改为覆盖写入。
|
||||
}
|
||||
}
|
||||
await fsp.cp(from, to, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
try {
|
||||
@@ -768,25 +851,25 @@ export async function applyUpdate(jobs = planUpdates(), options = {}) {
|
||||
needRestart: true,
|
||||
via,
|
||||
versions: listed.versions,
|
||||
lanes: lanesOf(listed, saved, localVer, sha),
|
||||
...channelPublic(saved),
|
||||
});
|
||||
}
|
||||
|
||||
export async function switchChannel(channel, { apply = true } = {}) {
|
||||
const next = normalizeChannel(channel);
|
||||
writeChannelState({ channel: next, pin: "" });
|
||||
if (next === "beta") {
|
||||
const betaSha = await remoteHeadSha(BETA_REF);
|
||||
const tags = await listGithubTags();
|
||||
const hasBeta = Boolean(betaSha || tags.some((item) => item.channel === "beta"));
|
||||
if (!hasBeta) {
|
||||
const stable = writeChannelState({ channel: "stable", pin: "" });
|
||||
return rememberUpdate({
|
||||
ok: false,
|
||||
error: "还没有测试版",
|
||||
hasBeta: false,
|
||||
...channelPublic(stable),
|
||||
...channelPublic(),
|
||||
versions: [],
|
||||
lanes: { stable: { id: "stable", hasRemote: true }, beta: { id: "beta", hasRemote: false } },
|
||||
});
|
||||
}
|
||||
if (!betaSha && apply) {
|
||||
@@ -794,8 +877,11 @@ export async function switchChannel(channel, { apply = true } = {}) {
|
||||
if (tag) return applyUpdate(planUpdates(), { ref: tag.ref, channel: "beta", pin: false, force: true });
|
||||
}
|
||||
}
|
||||
if (!apply) return checkUpdate();
|
||||
return applyUpdate(planUpdates(), { ref: currentChannelRef(), channel: next, pin: false, force: true });
|
||||
if (!apply) {
|
||||
writeChannelState({ channel: next, pin: "" });
|
||||
return checkUpdate();
|
||||
}
|
||||
return applyUpdate(planUpdates(), { ref: CHANNELS[next].ref, channel: next, pin: false, force: true });
|
||||
}
|
||||
|
||||
export async function switchVersion(ref) {
|
||||
@@ -815,7 +901,10 @@ export async function handleUpdateOp(body = {}) {
|
||||
if (op === "channel") return switchChannel(body.channel, { apply: body.apply !== false });
|
||||
if (op === "switch" || op === "rollback") return switchVersion(body.ref || body.version || body.id);
|
||||
if (op === "auto") return autoUpdateIfNeeded();
|
||||
return applyUpdate(planUpdates(), { ref: body.ref || currentChannelRef(), pin: false, force: true });
|
||||
const state = readChannelState();
|
||||
const ref = body.ref || currentChannelRef(state);
|
||||
const channel = body.channel || (ref === BETA_REF ? "beta" : ref === STABLE_REF ? "stable" : state.channel);
|
||||
return applyUpdate(planUpdates(), { ref, channel, pin: false, force: true });
|
||||
}
|
||||
|
||||
export async function autoUpdateIfNeeded() {
|
||||
@@ -823,6 +912,9 @@ export async function autoUpdateIfNeeded() {
|
||||
const check = await checkUpdate();
|
||||
if (check.pinned) return rememberUpdate({ ...check, auto: true, skipped: "pinned" });
|
||||
if (!check.hasUpdate) return rememberUpdate({ ...check, auto: true, skipped: "latest" });
|
||||
if (check.updateTarget === BETA_REF && readChannelState().channel !== "beta") {
|
||||
return rememberUpdate({ ...check, auto: true, skipped: "beta" });
|
||||
}
|
||||
const jobs = planUpdates();
|
||||
const dirtyGit = jobs.filter((j) => j.kind === "git" && gitTrackedDirty(j.dir));
|
||||
const runnable = jobs.filter((j) => !(j.kind === "git" && gitTrackedDirty(j.dir)));
|
||||
@@ -834,7 +926,12 @@ export async function autoUpdateIfNeeded() {
|
||||
error: "本地插件文件有未提交改动,未自动覆盖",
|
||||
});
|
||||
}
|
||||
const applied = await applyUpdate(runnable, { ref: currentChannelRef(), pin: false, force: false });
|
||||
const applied = await applyUpdate(runnable, {
|
||||
ref: currentChannelRef(),
|
||||
channel: readChannelState().channel,
|
||||
pin: false,
|
||||
force: false,
|
||||
});
|
||||
return rememberUpdate({ ...applied, auto: true });
|
||||
} catch (e) {
|
||||
return rememberUpdate({
|
||||
|
||||
Reference in new issue
Block a user