Windows 上的安卓应用运行器:双击 APK 即装,应用以独立窗口与桌面图标运行,自持 QEMU 运行时
'use strict';
/**
* 更新链路自检(不需要真的发布新版本、也不会动到当前安装)。
*
* 1) 能不能读到 GitHub 最新 release,并对当前形态挑到正确的资产;
* 2) 版本比较逻辑;
* 3) 「解压 + 覆盖 + 重启」脚本是真的能跑:造一个假安装目录,让脚本去覆盖它。
*
* 用法: node tools/check-update.js
*/
const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawn, execFileSync } = require('child_process');
const { Updater, cmpVer } = require('../src/core/updater');
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const ok = (c, m) => console.log(`${c ? '✓' : '✗'} ${m}`);
(async () => {
let pass = true;
const check = (c, m) => { ok(c, m); if (!c) pass = false; };
// ---- 1. 版本比较 ----
check(cmpVer('v0.3.2', '0.3.1') === 1, 'v0.3.2 > 0.3.1');
check(cmpVer('0.3.1', 'v0.3.1') === 0, 'v 前缀不影响相等');
check(cmpVer('v0.10.0', '0.9.9') === 1, '0.10.0 > 0.9.9(按数字比,不是字符串)');
check(cmpVer('1.0.0', '1.0.1') === -1, '1.0.0 < 1.0.1');
// ---- 2. GitHub release ----
const sandbox = fs.mkdtempSync(path.join(os.tmpdir(), 'apkstart-upd-'));
const install = path.join(sandbox, 'install');
fs.mkdirSync(install, { recursive: true });
const u = new Updater({
currentVersion: '0.0.0',
exePath: path.join(install, 'ApkStart.exe'),
isPackaged: true,
dataDir: sandbox,
});
let info = null;
try {
info = await u.check();
check(!!info && !!info.version, `读取到最新版本 ${info && info.tag}`);
check(info.hasUpdate === true, '当前版本 0.0.0 → 判定有更新');
check(!!info.asset && /\.(zip|exe)$/i.test(info.asset.name), `便携形态挑到资产 ${info.asset && info.asset.name}`);
} catch (e) {
check(false, `GitHub 检查失败:${e.message}`);
}
// 安装形态(有卸载器)应当挑 Setup.exe
fs.writeFileSync(path.join(install, 'Uninstall ApkStart.exe'), 'stub');
const u2 = new Updater({
currentVersion: '0.0.0',
exePath: path.join(install, 'ApkStart.exe'),
isPackaged: true,
dataDir: sandbox,
});
check(u2.flavor === 'setup', '识别为安装版(存在 Uninstall ApkStart.exe)');
// ---- 3. 覆盖脚本真的能跑 ----
const payload = path.join(sandbox, 'payload');
fs.mkdirSync(path.join(payload, 'resources', 'app'), { recursive: true });
fs.writeFileSync(path.join(payload, 'ApkStart.exe'), 'new-exe');
fs.writeFileSync(path.join(payload, 'resources', 'app', 'main.js'), 'new-main');
fs.writeFileSync(path.join(install, 'ApkStart.exe'), 'old-exe');
// 用一个立刻会退出的进程当「被等待的旧进程」
const decoy = spawn(process.execPath, ['-e', 'setTimeout(()=>{},1500)'], { stdio: 'ignore' });
const script = u.apply(
{ mode: 'zip', payload, version: '9.9.9' },
{ waitPid: decoy.pid, spawn: false },
);
check(/Copy-Item/.test(script) && /Wait-Process/.test(script), '脚本包含等待进程与覆盖动作');
const ps = path.join(process.env.SystemRoot || 'C:\\Windows', 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe');
// 不真去 Start-Process 拉起 ApkStart.exe(那是个假 exe),只验证复制
const body = script.replace(/if \(Test-Path[^\r\n]*relaunched' \}/, "'skipped relaunch'");
const b64 = Buffer.from(body, 'utf16le').toString('base64');
try {
execFileSync(ps, ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand', b64], {
stdio: 'ignore', timeout: 60000, windowsHide: true,
});
} catch (e) {
check(false, `覆盖脚本执行失败:${e.message}`);
}
await sleep(600);
const newExe = fs.readFileSync(path.join(install, 'ApkStart.exe'), 'utf8');
const newMain = fs.readFileSync(path.join(install, 'resources', 'app', 'main.js'), 'utf8');
check(newExe === 'new-exe', '安装目录里的 ApkStart.exe 已被替换');
check(newMain === 'new-main', '子目录里的文件也替换成功');
// ---- 4. apply() 真的起一个进程去替换(异步链路,不走网络)----
const install2 = path.join(sandbox, 'install2');
const payload2 = path.join(sandbox, 'payload2');
fs.mkdirSync(install2, { recursive: true });
fs.mkdirSync(payload2, { recursive: true });
fs.writeFileSync(path.join(install2, 'ApkStart.exe'), 'old-exe');
fs.writeFileSync(path.join(install2, 'marker.txt'), 'old');
fs.writeFileSync(path.join(payload2, 'ApkStart.exe'), 'new-exe');
fs.writeFileSync(path.join(payload2, 'marker.txt'), 'new');
const u3 = new Updater({
currentVersion: '0.0.0',
exePath: path.join(install2, 'ApkStart.exe'),
isPackaged: true,
dataDir: sandbox,
});
const decoy2 = spawn(process.execPath, ['-e', 'setTimeout(()=>{},600)'], { stdio: 'ignore' });
u3.apply({ mode: 'zip', payload: payload2, version: '9.9.8' }, { waitPid: decoy2.pid });
let asyncOk = false;
for (let i = 0; i < 40; i += 1) {
await sleep(500);
try {
if (fs.readFileSync(path.join(install2, 'marker.txt'), 'utf8') === 'new') { asyncOk = true; break; }
} catch (_) { /* 还在替换 */ }
}
check(asyncOk, 'apply() 在后台进程里完成了替换(异步链路可用)');
try { decoy.kill(); } catch (_) { /* ignore */ }
try { decoy2.kill(); } catch (_) { /* ignore */ }
fs.rmSync(sandbox, { recursive: true, force: true });
console.log('');
console.log(pass ? '结果:通过' : '结果:有失败项');
process.exit(pass ? 0 : 1);
})().catch((e) => { console.error('FATAL', e); process.exit(1); });