mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 14:00:22 +08:00
fix: 发布 1.1.18,重启打开新地址并收掉重复的 settings 补丁
旧页面 token 刷新后无效,重启改为按新进程地址打开浏览器。已叠坏的 legacyApplyPath 在应用时收成一份。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
ec6ef45286
commit
4bed650680
8 files changed
+149
-28
No files matched your search
+72
-6
@@ -3406,8 +3406,65 @@ export function sanitizeYamlPersonaScalars(text) {
|
||||
return crlf ? out.replace(/\n/g, "\r\n") : out;
|
||||
}
|
||||
|
||||
function legacyApplyPathStarts(text) {
|
||||
const hay = asLf(text);
|
||||
const key = "function legacyApplyPath(section, op) {";
|
||||
let count = 0;
|
||||
let from = 0;
|
||||
while (from < hay.length) {
|
||||
const at = hay.indexOf(key, from);
|
||||
if (at < 0) break;
|
||||
count += 1;
|
||||
from = at + key.length;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
function endOfBalancedBlock(text, openAt) {
|
||||
let depth = 0;
|
||||
for (let i = openAt; i < text.length; i += 1) {
|
||||
const ch = text[i];
|
||||
if (ch === "{") depth += 1;
|
||||
else if (ch === "}") {
|
||||
depth -= 1;
|
||||
if (depth === 0) return i + 1;
|
||||
}
|
||||
}
|
||||
return text.length;
|
||||
}
|
||||
|
||||
/** 1.1.17 之前 #42 会叠多份 legacyApplyPath,settings 整文件语法错误、提供商目录起不来。 */
|
||||
export function collapseDuplicateLegacyApplyPath(text) {
|
||||
const crlf = typeof text === "string" && text.includes("\r\n");
|
||||
const src = asLf(text);
|
||||
const key = "function legacyApplyPath(section, op) {";
|
||||
const starts = [];
|
||||
let from = 0;
|
||||
while (from < src.length) {
|
||||
const at = src.indexOf(key, from);
|
||||
if (at < 0) break;
|
||||
starts.push(at);
|
||||
from = at + key.length;
|
||||
}
|
||||
if (starts.length < 2) return text;
|
||||
const firstOpen = src.indexOf("{", starts[0]);
|
||||
const firstEnd = endOfBalancedBlock(src, firstOpen);
|
||||
let out = src.slice(0, firstEnd);
|
||||
let cursor = firstEnd;
|
||||
for (let n = 1; n < starts.length; n += 1) {
|
||||
const open = src.indexOf("{", starts[n]);
|
||||
const end = endOfBalancedBlock(src, open);
|
||||
out += src.slice(cursor, starts[n]);
|
||||
cursor = end;
|
||||
}
|
||||
out += src.slice(cursor);
|
||||
out = out.replace(/\n{3,}/g, "\n\n");
|
||||
return crlf ? out.replace(/\n/g, "\r\n") : out;
|
||||
}
|
||||
|
||||
function patchApplied(text, patch) {
|
||||
const hay = asLf(text);
|
||||
if (patch?.id === 42 && legacyApplyPathStarts(hay) > 1) return false;
|
||||
if (Array.isArray(patch.forbid) && patch.forbid.some((marker) => hay.includes(asLf(marker)))) return false;
|
||||
if (Array.isArray(patch.markers) && patch.markers.length > 0) {
|
||||
// markersAny:任一标记即可;默认要全部命中。
|
||||
@@ -3522,13 +3579,22 @@ export function yamlJsCommentsToHash(text) {
|
||||
}
|
||||
|
||||
export function applyReplacementsToText(text, patch, fp = "") {
|
||||
// 插入型补丁锚点仍在,已有标记则不再 replaceAll。
|
||||
if (patch.skipIfMarked && patchApplied(text, patch)) {
|
||||
return { text, changed: false };
|
||||
let source = text;
|
||||
let preChanged = false;
|
||||
if (patch?.id === 42) {
|
||||
const collapsed = collapseDuplicateLegacyApplyPath(source);
|
||||
if (collapsed !== source) {
|
||||
source = collapsed;
|
||||
preChanged = true;
|
||||
}
|
||||
}
|
||||
const crlf = typeof text === "string" && text.includes("\r\n");
|
||||
// 插入型补丁锚点仍在,已有标记则不再 replaceAll。
|
||||
if (patch.skipIfMarked && patchApplied(source, patch)) {
|
||||
return { text: source, changed: preChanged };
|
||||
}
|
||||
const crlf = typeof source === "string" && source.includes("\r\n");
|
||||
const yaml = isYamlPath(fp);
|
||||
let out = asLf(text);
|
||||
let out = asLf(source);
|
||||
let changed = false;
|
||||
for (const { pattern, replace } of patchReplacements(patch)) {
|
||||
if (!pattern || typeof replace !== "string") continue;
|
||||
@@ -3554,7 +3620,7 @@ export function applyReplacementsToText(text, patch, fp = "") {
|
||||
changed = true;
|
||||
}
|
||||
if (crlf) out = out.replace(/\n/g, "\r\n");
|
||||
return { text: out, changed };
|
||||
return { text: out, changed: changed || preChanged };
|
||||
}
|
||||
|
||||
function rollupPatchStatus({ applied, already, missing, notFound, lastError, keyCount, layoutSoft }) {
|
||||
|
||||
+50
-6
@@ -1,5 +1,5 @@
|
||||
import net from "node:net";
|
||||
import { spawn } from "node:child_process";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { envForDetachedSpawn } from "./core.js";
|
||||
|
||||
const encoded = process.env.DSH_PURGE_RESTART;
|
||||
@@ -25,8 +25,38 @@ function portFree() {
|
||||
});
|
||||
}
|
||||
|
||||
function killOldServer() {
|
||||
const pid = Number(info.pid);
|
||||
if (!pid || pid === process.pid) return;
|
||||
if (process.platform === "win32") {
|
||||
spawnSync("taskkill", ["/PID", String(pid), "/F"], { windowsHide: true, stdio: "ignore" });
|
||||
return;
|
||||
}
|
||||
try { process.kill(pid, "SIGTERM"); } catch { /* already gone */ }
|
||||
}
|
||||
|
||||
function openBrowser(url) {
|
||||
if (!url) return;
|
||||
if (process.platform === "win32") {
|
||||
spawn("cmd.exe", ["/d", "/s", "/c", "start", "", url], {
|
||||
detached: true,
|
||||
stdio: "ignore",
|
||||
windowsHide: true,
|
||||
}).unref();
|
||||
return;
|
||||
}
|
||||
const opener = process.platform === "darwin" ? "open" : "xdg-open";
|
||||
spawn(opener, [url], { detached: true, stdio: "ignore" }).unref();
|
||||
}
|
||||
|
||||
async function waitFree() {
|
||||
for (let i = 0; i < 80; i++) {
|
||||
for (let i = 0; i < 40; i++) {
|
||||
if (await portFree()) return true;
|
||||
if (i === 8) killOldServer();
|
||||
await new Promise((r) => setTimeout(r, 120));
|
||||
}
|
||||
killOldServer();
|
||||
for (let i = 0; i < 40; i++) {
|
||||
if (await portFree()) return true;
|
||||
await new Promise((r) => setTimeout(r, 120));
|
||||
}
|
||||
@@ -36,17 +66,31 @@ async function waitFree() {
|
||||
const ok = await waitFree();
|
||||
if (!ok) process.exit(1);
|
||||
|
||||
// detached + windowsHide;shell:false 避免再走 dsh.cmd / cmd.exe。
|
||||
// 接着 stdout 里的新地址打开浏览器。旧页面的 token 在新进程里无效,只刷新会打不开。
|
||||
let opened = false;
|
||||
const child = spawn(info.execPath, info.argv, {
|
||||
detached: true,
|
||||
stdio: "ignore",
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
cwd: info.cwd || process.cwd(),
|
||||
env: envForDetachedSpawn(),
|
||||
windowsHide: true,
|
||||
shell: false,
|
||||
});
|
||||
child.on("error", () => process.exit(1));
|
||||
const takeUrl = (chunk) => {
|
||||
const text = String(chunk || "");
|
||||
if (opened) return;
|
||||
const match = text.match(/dsh web:\s+(https?:\/\/\S+)/);
|
||||
if (!match) return;
|
||||
opened = true;
|
||||
const url = match[1].replace(/[),.;]+$/, "");
|
||||
openBrowser(url);
|
||||
};
|
||||
child.stdout?.on("data", takeUrl);
|
||||
child.stderr?.on("data", takeUrl);
|
||||
child.unref();
|
||||
// 等子进程脱离 job,避免 helper 退出时带走新进程。
|
||||
await new Promise((r) => setTimeout(r, 400));
|
||||
const started = Date.now();
|
||||
while (!opened && Date.now() - started < 20000) {
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
}
|
||||
process.exit(0);
|
||||
@@ -77,6 +77,7 @@ export function scheduleRestart(request) {
|
||||
cwd: process.cwd(),
|
||||
host: hostFromRequest(request),
|
||||
port: portFromRequest(request),
|
||||
pid: process.pid,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user