mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 06:30:22 +08:00
官方客户端可以应用并自己重启;修掉重启弹窗、残留 PowerShell,以及更新时要手动还原再应用的问题。 Co-authored-by: Cursor <cursoragent@cursor.com>
133 lines
3.0 KiB
JavaScript
133 lines
3.0 KiB
JavaScript
import fs from "node:fs";
|
|
import { execFileSync, spawn } from "node:child_process";
|
|
|
|
const encoded = process.env.DSH_PURGE_RESTART;
|
|
if (!encoded) process.exit(1);
|
|
|
|
let info;
|
|
try {
|
|
info = JSON.parse(encoded);
|
|
} catch {
|
|
process.exit(1);
|
|
}
|
|
|
|
const cleanup = Array.isArray(info.cleanup) ? info.cleanup : [];
|
|
|
|
function spawnEnv() {
|
|
const env = { ...process.env };
|
|
delete env.DSH_PERMISSION_MODE;
|
|
delete env.DSH_PURGE_RESTART;
|
|
for (const key of Object.keys(env)) {
|
|
if (key.toUpperCase() === "ELECTRON_RUN_AS_NODE") delete env[key];
|
|
}
|
|
return env;
|
|
}
|
|
|
|
function pidAlive(pid) {
|
|
const n = Number(pid);
|
|
if (!n) return false;
|
|
try {
|
|
process.kill(n, 0);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function waitPidGone(pid) {
|
|
if (!pid) {
|
|
await new Promise((r) => setTimeout(r, 400));
|
|
return true;
|
|
}
|
|
for (let i = 0; i < 80; i++) {
|
|
if (!pidAlive(pid)) return true;
|
|
await new Promise((r) => setTimeout(r, 120));
|
|
}
|
|
return !pidAlive(pid);
|
|
}
|
|
|
|
function rmTree(dir) {
|
|
if (!dir) return;
|
|
try {
|
|
fs.rmSync(dir, { recursive: true, force: true, maxRetries: 8, retryDelay: 120 });
|
|
} catch {
|
|
// 锁着就留下
|
|
}
|
|
}
|
|
|
|
function pidsForImage(image) {
|
|
const out = execFileSync("tasklist", ["/FI", `IMAGENAME eq ${image}`, "/FO", "CSV", "/NH"], {
|
|
encoding: "utf8",
|
|
windowsHide: true,
|
|
timeout: 8000,
|
|
});
|
|
const pids = [];
|
|
for (const line of String(out || "").split(/\r?\n/)) {
|
|
const cols = line.split(",");
|
|
const pid = Number(String(cols[1] || "").replace(/"/g, "").trim());
|
|
if (pid) pids.push(pid);
|
|
}
|
|
return pids;
|
|
}
|
|
|
|
function listDesktopPids() {
|
|
if (process.platform !== "win32") return [];
|
|
const images = ["DeepSeek Harness.exe", "DSH Desktop.exe"];
|
|
const pids = [];
|
|
for (const image of images) {
|
|
try {
|
|
pids.push(...pidsForImage(image));
|
|
} catch {
|
|
// 该进程名不存在
|
|
}
|
|
}
|
|
return pids;
|
|
}
|
|
|
|
function killOtherDesktopProcesses(keepPid) {
|
|
for (const pid of listDesktopPids()) {
|
|
if (pid === keepPid || pid === process.pid) continue;
|
|
try {
|
|
process.kill(pid);
|
|
} catch {
|
|
// 已经退了
|
|
}
|
|
}
|
|
}
|
|
|
|
if (info.killApp) {
|
|
await new Promise((r) => setTimeout(r, 400));
|
|
for (const dir of cleanup) rmTree(dir);
|
|
if (info.helper) {
|
|
try { fs.unlinkSync(info.helper); } catch { /* ignore */ }
|
|
}
|
|
killOtherDesktopProcesses(process.pid);
|
|
await new Promise((r) => setTimeout(r, 500));
|
|
} else {
|
|
const gone = await waitPidGone(info.pid);
|
|
if (!gone) process.exit(1);
|
|
for (const dir of cleanup) rmTree(dir);
|
|
if (info.helper) {
|
|
try { fs.unlinkSync(info.helper); } catch { /* ignore */ }
|
|
}
|
|
}
|
|
|
|
if (info.respawn === false) {
|
|
process.exit(0);
|
|
}
|
|
|
|
if (!info.execPath) process.exit(1);
|
|
|
|
const child = spawn(info.execPath, Array.isArray(info.argv) ? info.argv : [], {
|
|
detached: true,
|
|
stdio: "ignore",
|
|
cwd: info.cwd || process.cwd(),
|
|
env: spawnEnv(),
|
|
windowsHide: true,
|
|
shell: false,
|
|
});
|
|
child.on("error", () => process.exit(1));
|
|
child.unref();
|
|
await new Promise((r) => setTimeout(r, 400));
|
|
process.exit(0);
|