mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 14:10:22 +08:00
Merge pull request #198 from iigzx/fix/desktop-asar-swap
fix(desktop): 换包先杀 Electron main,挪不动时把原版拉起来 (#197)
This commit is contained in:
2 files changed
+45
-1
No files matched your search
+20
-1
@@ -1010,7 +1010,7 @@ function desktopRuntimeOf(ctx) {
|
|||||||
return runtime && typeof runtime.requestRestart === "function" ? runtime : null;
|
return runtime && typeof runtime.requestRestart === "function" ? runtime : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const ASAR_SWAP_PS1 = [
|
export const ASAR_SWAP_PS1 = [
|
||||||
"param(",
|
"param(",
|
||||||
" [Parameter(Mandatory = $true)][int]$WaitPid,",
|
" [Parameter(Mandatory = $true)][int]$WaitPid,",
|
||||||
" [Parameter(Mandatory = $true)][string]$AsarPath,",
|
" [Parameter(Mandatory = $true)][string]$AsarPath,",
|
||||||
@@ -1037,6 +1037,16 @@ const ASAR_SWAP_PS1 = [
|
|||||||
"# 来得及处理 host 退出事件就被 kill,UI 根本不存在,无法弹 crash dialog。",
|
"# 来得及处理 host 退出事件就被 kill,UI 根本不存在,无法弹 crash dialog。",
|
||||||
"# PowerShell 本身是 detached 独立进程(spawn 时 detached:true, stdio:ignore),",
|
"# PowerShell 本身是 detached 独立进程(spawn 时 detached:true, stdio:ignore),",
|
||||||
"# 杀 Electron 不会连带杀 PowerShell。",
|
"# 杀 Electron 不会连带杀 PowerShell。",
|
||||||
|
"# #197b: 先杀 Electron main。main 是唯一会弹「host stopped」对话框的进程:",
|
||||||
|
"# 若先杀 host,main 还活着就会弹框,然后脚本若在挪 asar 前失败,用户就卡在",
|
||||||
|
"# 「host 死了、main 半死、asar 没动」的状态(2026-10-31 实测)。先杀 main 就没框。",
|
||||||
|
"$mains = @(Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | Where-Object {",
|
||||||
|
" $ep = $_.ExecutablePath",
|
||||||
|
" if (-not $ep) { return $false }",
|
||||||
|
" ($ep.ToLower() -eq $exeNorm) -and ($_.CommandLine -notmatch '--type=')",
|
||||||
|
"})",
|
||||||
|
"foreach ($p in $mains) { Stop-Process -Id $p.ProcessId -Force -ErrorAction SilentlyContinue }",
|
||||||
|
"Start-Sleep -Milliseconds 400",
|
||||||
"Stop-HarnessInstall",
|
"Stop-HarnessInstall",
|
||||||
"Start-Sleep -Milliseconds 500",
|
"Start-Sleep -Milliseconds 500",
|
||||||
"Stop-HarnessInstall",
|
"Stop-HarnessInstall",
|
||||||
@@ -1080,7 +1090,15 @@ const ASAR_SWAP_PS1 = [
|
|||||||
" Remove-Item Env:ELECTRON_RUN_AS_NODE -ErrorAction SilentlyContinue",
|
" Remove-Item Env:ELECTRON_RUN_AS_NODE -ErrorAction SilentlyContinue",
|
||||||
" Start-Process -FilePath $ExePath",
|
" Start-Process -FilePath $ExePath",
|
||||||
"} else {",
|
"} else {",
|
||||||
|
" # #197b: 挪不动(asar 仍被占用)时不要把用户扔在空桌面上——把原版拉起来,",
|
||||||
|
" # 留下 rename-pending 让下一次空闲重试;失败原因写进 dsh-purge 日志。",
|
||||||
" Set-Content -LiteralPath $lock -Value ((Get-Date).ToString('o')) -Encoding ascii",
|
" Set-Content -LiteralPath $lock -Value ((Get-Date).ToString('o')) -Encoding ascii",
|
||||||
|
" try {",
|
||||||
|
" $log = Join-Path $env:TEMP 'dsh-purge-desktop.log'",
|
||||||
|
" Add-Content -LiteralPath $log -Value ((Get-Date).ToUniversalTime().ToString('o') + ' [asar-swap] move failed; relaunching original app (asar still sealed)')",
|
||||||
|
" } catch {}",
|
||||||
|
" Remove-Item Env:ELECTRON_RUN_AS_NODE -ErrorAction SilentlyContinue",
|
||||||
|
" Start-Process -FilePath $ExePath",
|
||||||
"}",
|
"}",
|
||||||
"",
|
"",
|
||||||
].join("\r\n");
|
].join("\r\n");
|
||||||
@@ -1940,3 +1958,4 @@ export function repairOfficialCliAfterUnpack(exe = findDesktopAppExecutable()) {
|
|||||||
}
|
}
|
||||||
return report;
|
return report;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import test from "node:test";
|
||||||
|
import assert from "node:assert/strict";
|
||||||
|
import { ASAR_SWAP_PS1 } from "../lib/desktop.js";
|
||||||
|
|
||||||
|
// #197b:桌面端点「应用」时 host 被杀、main 还活着 → 弹「host stopped」,
|
||||||
|
// 且 asar 没挪动 → 应用变成半死状态。这里守住两条顺序性约束。
|
||||||
|
|
||||||
|
test("先杀 Electron main,再杀安装目录其余进程", () => {
|
||||||
|
const mainKill = ASAR_SWAP_PS1.indexOf("Stop-Process -Id $p.ProcessId");
|
||||||
|
// 只看「作为独立一行的调用」,不要命中函数定义 `function Stop-HarnessInstall` 与缩进的内部调用
|
||||||
|
const sweep = ASAR_SWAP_PS1.indexOf("\r\nStop-HarnessInstall\r\n");
|
||||||
|
assert.ok(mainKill > 0, "缺少 main 进程 kill");
|
||||||
|
assert.ok(sweep > 0, "缺少 Stop-HarnessInstall 调用");
|
||||||
|
assert.ok(sweep > mainKill, "Stop-HarnessInstall 必须在 main kill 之后");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("main 进程的识别排除渲染/GPU 子进程(--type=)", () => {
|
||||||
|
assert.match(ASAR_SWAP_PS1, /CommandLine -notmatch '--type='/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("挪包失败时也要把原版拉起来,不留空桌面", () => {
|
||||||
|
const relaunch = ASAR_SWAP_PS1.split("Start-Process -FilePath $ExePath").length - 1;
|
||||||
|
assert.equal(relaunch, 2, "成功/失败两个分支都要重启 exe");
|
||||||
|
assert.match(ASAR_SWAP_PS1, /move failed; relaunching original app/);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user