feat(electron-shell): 启动链路修复与执行/配置收口插件侧

- 启动链路 P1-P7 修复:预算对齐、完成事件推送、端口占用提示、前端阶段文案接管
- 静态缓存按 IS_FROZEN 区分开发/打包态,bootPhaseOwned 竞态修复
- 一键执行与参数配置统一收口到插件界面,定时任务引用 config_id
- 环境包迁移到 %LOCALAPPDATA%\37duAutomation\python-env,重装零解压
- shell-updater:缓存包 sha256 校验、静默升级后自动拉起新版、自动清理旧安装包
- after-pack 钩子生成 shell.version 修复打包态版本识别
- 插件 xq0031/xq0045 迁移至 plugins/XQ0001-0100/ 号段分桶中文目录
This commit is contained in:
2026-09-28 14:20:35 +08:00
parent 3214305dea
commit 87f995ca47
55 changed files with 1426 additions and 4280 deletions
@@ -19,6 +19,7 @@ let _checking = false;
let _downloading = false;
let _pendingVersion = "";
let _pendingNotes = "";
let _pendingPkg = ""; // 已通过 sha256 校验的缓存安装包绝对路径
const _STATE = {
checking: false,
@@ -68,20 +69,54 @@ async function fetchCloudVersion() {
}
}
function cachedPkg(version) {
// 缓存包校验:sha256 与云端登记不一致(同版本号重新发版/下载中断残留)→ 删除旧包返回 null,
// 触发重新下载,避免"缓存命中秒显示下载完成、实际是旧坏包"
function validateCache(version, expectSha) {
const p = path.join(SHELL_CACHE, `setup-37du-host-${version}.exe`);
return fs.existsSync(p) ? p : null;
if (!fs.existsSync(p)) return null;
if (expectSha) {
try {
if (sha256Hex(fs.readFileSync(p)) !== String(expectSha).toLowerCase()) {
logger.warning(`缓存安装包与云端 sha256 不符(旧坏包),删除后重新下载:${p}`);
try { fs.unlinkSync(p); } catch (_) { /* 文件占用时下次再清 */ }
return null;
}
} catch (e) {
return null;
}
}
return p;
}
function restorePending(cloud, notes) {
function restorePending(cloud, notes, pkgPath) {
_pendingVersion = cloud;
_pendingNotes = notes;
_pendingPkg = pkgPath || "";
_STATE.dl_version = cloud;
_STATE.dl_status = "downloaded";
_STATE.dl_pct = 100;
logger.info(`壳子新版本 ${cloud} 已在缓存中,等待用户重启更新`);
}
// 启动清扫:删除缓存中"已安装/过期"的安装包(~80MB/个)与升级批处理。
// 版本 <= 本机版本 → 已装或更旧,删除;版本 > 本机 → 可能是已下载待更新的包,保留。
// 仅打包态执行(dev 不动缓存),应用启动时调用一次。
function cleanupStalePackages() {
if (!config.IS_FROZEN) return;
const cur = localShellVersion();
let files = [];
try { files = fs.readdirSync(SHELL_CACHE); } catch (_) { return; }
for (const f of files) {
const pkg = f.match(/^setup-37du-host-(.+)\.exe$/);
const stale = (pkg && (!cur || !versionLt(cur, pkg[1]))) || /^apply-update-.+\.cmd$/.test(f);
if (!stale) continue;
try {
fs.unlinkSync(path.join(SHELL_CACHE, f));
logger.info(`清理升级遗留文件:${f}`);
} catch (_) { /* 占用中下次启动再清 */ }
}
}
async function checkUpdate() {
if (_checking) return;
_checking = true;
@@ -110,7 +145,20 @@ async function checkUpdate() {
return;
}
_STATE.available = true;
if (cachedPkg(cloud)) restorePending(cloud, notes);
// 多版本跳更:已下载待更新的版本落后于云端 → 作废旧下载(删安装包),重走下载流程。
// 否则 pendingStatus 持续报旧版本"已下载",且 startDownload 被 downloaded 态卡死。
if (_pendingVersion && versionLt(_pendingVersion, cloud)) {
logger.info(`已下载的 ${_pendingVersion} 落后于云端 ${cloud},作废旧安装包`);
try { if (_pendingPkg && fs.existsSync(_pendingPkg)) fs.unlinkSync(_pendingPkg); } catch (_) {}
_pendingVersion = "";
_pendingNotes = "";
_pendingPkg = "";
_STATE.dl_version = "";
_STATE.dl_status = "idle";
_STATE.dl_pct = 0;
}
const cached = validateCache(cloud, info.sha256);
if (cached) restorePending(cloud, notes, cached);
} finally {
_STATE.checking = false;
_checking = false;
@@ -136,22 +184,35 @@ async function startDownload() {
if (!cur) return;
if (_STATE.dl_status === "downloading") return;
if (_STATE.dl_status === "downloaded" && _pendingVersion) return;
const info = await fetchCloudVersion();
if (!info) return;
const cloud = String(info.version || "");
if (!cloud || !versionLt(cur, cloud)) return;
if (cachedPkg(cloud)) {
restorePending(cloud, String(info.notes || ""));
return;
}
if (_downloading) return;
_downloading = true;
_STATE.dl_version = cloud;
// 先置"下载中"态(版本号暂用最近一次检查结果):拉版本信息有网络往返,
// 若等它结束才改状态,前端点击后的抢拉会读到 idle,误判成"已下载完成"
_STATE.dl_version = _STATE.latest || "";
_STATE.dl_status = "downloading";
_STATE.dl_received = 0;
_STATE.dl_total = 0;
_STATE.dl_pct = 0;
_STATE.dl_error = "";
const info = await fetchCloudVersion();
if (!info) {
_STATE.dl_status = "failed";
_STATE.dl_error = "获取云端版本信息失败";
return;
}
const cloud = String(info.version || "");
if (!cloud || !versionLt(cur, cloud)) {
_STATE.dl_status = "idle";
_STATE.dl_version = "";
return;
}
_STATE.dl_version = cloud;
const cached = validateCache(cloud, info.sha256);
if (cached) {
restorePending(cloud, String(info.notes || ""), cached);
return;
}
if (_downloading) return;
_downloading = true;
logger.info(`开始下载壳子新版本 ${cloud}`);
try {
await download(cloud, info);
} catch (e) {
@@ -162,7 +223,6 @@ async function startDownload() {
} finally {
_downloading = false;
}
logger.info(`开始下载壳子新版本 ${cloud}`);
}
async function download(cloud, info) {
@@ -224,7 +284,7 @@ async function download(cloud, info) {
_STATE.dl_pct = 100;
_STATE.dl_received = received;
_STATE.dl_total = total;
restorePending(cloud, String(info.notes || ""));
restorePending(cloud, String(info.notes || ""), pkg);
}
async function downloadFromUrl(url, headers, out, sha, onProgress) {
@@ -256,10 +316,12 @@ function cleanupPart(cloud) {
function pendingStatus() {
let cur = localShellVersion();
let downloaded = !!_pendingVersion && cachedPkg(_pendingVersion) !== null;
// 用已校验的 _pendingPkg 判断(不做全量 sha 重算——状态接口被前端高频轮询)
const downloaded = !!_pendingVersion && !!_pendingPkg && fs.existsSync(_pendingPkg);
if (!downloaded) {
_pendingVersion = "";
_pendingNotes = "";
_pendingPkg = "";
}
return {
current: cur,
@@ -285,17 +347,28 @@ function pendingStatus() {
function applyUpdate() {
const version = _pendingVersion;
const pkg = cachedPkg(version);
if (!pkg) throw new Error(`未找到已下载的安装包:setup-37du-host-${version}.exe`);
const pkg = _pendingPkg || validateCache(version, "");
if (!pkg || !fs.existsSync(pkg)) {
throw new Error(`未找到已下载的安装包:${pkg || `setup-37du-host-${version}.exe`}`);
}
_STATE.applying = true;
logger.info(`开始静默升级到 ${version}:${pkg}`);
const child = spawn(pkg, ["/VERYSILENT", "/SUPPRESSMSGBOXES", "/NORESTART"], {
cwd: path.dirname(pkg),
// electron-builder NSIS 静默安装完成后不会自动拉起新版本(旧壳 Inno [Run] 的行为不可用),
// 用批处理串联"等待安装器完成 → 启动新版本";本进程 1s 后退出,让安装器接管收尾。
const newExe = path.join(path.dirname(process.execPath), path.basename(process.execPath));
const bat = path.join(SHELL_CACHE, `apply-update-${version}.cmd`);
fs.writeFileSync(bat, [
"@echo off",
`start "" /wait "${pkg}" /VERYSILENT /SUPPRESSMSGBOXES /NORESTART`,
`start "" "${newExe}"`,
"",
].join("\r\n"), "utf-8");
spawn("cmd.exe", ["/c", bat], {
cwd: SHELL_CACHE,
windowsHide: true,
stdio: "ignore",
detached: true,
});
child.unref();
}).unref();
// 给安装器接管进程,随后退出
setTimeout(() => {
if (typeof process.exit === "function") process.exit(0);
@@ -308,6 +381,6 @@ module.exports = {
startDownload,
pendingStatus,
applyUpdate,
cachedPkg,
cleanupStalePackages,
localShellVersion,
};