packServer/backend/scripts/deploy/publish-desktop.cjs
liufumingren 228723e0c3 设备端扫码上报 + 桌面端/Android 正式包发布脚手架
AndroidStation(原中控改造为纯设备端)
- 只做三件事:UDP 48899 被桌面端发现、TCP 8087 推送 USB 扫码结果、离线语音播报
- 扫码不再经过 UI:去掉逐键重绘,50ms 自动提交,同一码重复扫码也一律上报
- 离线语音种子(数字/字母/“开始打包”/欢迎语)随包安装,MediaPlayer 播放,启动把音量拉满
- 启动播报「欢迎使用溯源系统,初始化已经完成,扫码即可开始打包」
- 移除 Device Owner / 设备管理员 / lock task / 网络与 IP 设置等非普通应用能力(仅保留开机自启)
- 正式签名 keystore + release 签名配置,versionName/versionCode 可由打包脚本覆盖

发布脚手架
- backend/android_client:Android 正式包构建(build.ps1 + package.config.json)
- backend/scripts/deploy/publish-android.cjs + npm run android:build / android:publish
- 桌面端安装包改为 download/desktop,Android 为 download/android_client;
  version.json 仍走 /download/version.json(老客户端写死),新增 /download/android_client/version.json
- 谷歌插件随桌面端 MSI 安装到 <安装目录>\extension,工具菜单新增「复制谷歌插件地址」
- git-safety-check 增加仓库级禁区(keystore.properties / *.jks / local.properties)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-21 05:53:27 +08:00

246 lines
8.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 桌面端正式包:构建 MSI + version.json,并单独发布到 nginx 静态目录
*
* 特点:
* - 只动桌面端产物,不重新发布后端、不重启 Egg、不跑数据库迁移;
* - 构建复用 desktop/installer/build.ps1(Production 环境);
* - 安装包上传到 deploy.config.local.cjs 里的 desktopDownloadDir(deployRoot/download/desktop);
* - version.json 上传到 desktopVersionDir(默认 deployRoot/download,nginx 对 /download/version.json 关缓存);
* - 同时刷新 front.conf 里的 /download/ 静态托管配置并 reload nginx。
*
* 用法:
* npm run desktop:publish # 自动递增补丁版本并发布
* npm run desktop:publish -- --version=1.0.90
* npm run desktop:publish -- --notes="修复 xxx" --keep=10
* npm run desktop:build # 只构建,不上传(--dry-run)
* npm run desktop:publish -- --skip-nginx # 不改 nginx
*/
const fs = require('node:fs');
const path = require('node:path');
const {
ROOT,
ensureCommandAvailable,
getNowStamp,
loadDeployConfig,
quoteForBash,
refreshNginx,
runCommand,
runScp,
runSsh,
} = require('./deploy-lib.cjs');
function parseArgs(argv) {
const options = {
envName: 'production',
version: '',
notes: '',
keep: 0,
dryRun: false,
skipNginx: false,
};
for (const item of argv) {
if (!item) continue;
if (!item.startsWith('--') && !options.envNameExplicit) {
options.envName = item;
options.envNameExplicit = true;
continue;
}
if (item === '--dry-run') {
options.dryRun = true;
} else if (item === '--skip-nginx') {
options.skipNginx = true;
} else if (item.startsWith('--version=')) {
options.version = item.slice('--version='.length).trim().replace(/^v/i, '');
} else if (item.startsWith('--notes=')) {
options.notes = item.slice('--notes='.length);
} else if (item.startsWith('--keep=')) {
options.keep = Number(item.slice('--keep='.length)) || 0;
}
}
if (options.version && !/^\d+\.\d+\.\d+$/.test(options.version)) {
throw new Error(`版本号格式应为 x.y.z:${options.version}`);
}
return options;
}
function compareVersion(a, b) {
const pa = a.split('.').map(Number);
const pb = b.split('.').map(Number);
for (let i = 0; i < 3; i++) {
if (pa[i] !== pb[i]) return pa[i] - pb[i];
}
return 0;
}
/** 取本地已有 MSI / version.json 中最高的版本,并默认 +1 补丁号 */
function nextVersion(outDir) {
const versions = [];
if (fs.existsSync(outDir)) {
for (const name of fs.readdirSync(outDir)) {
const match = name.match(/^YuncangTraceSetup-(\d+\.\d+\.\d+)\.msi$/);
if (match) versions.push(match[1]);
}
}
const versionFile = path.join(outDir, 'version.json');
if (fs.existsSync(versionFile)) {
try {
const data = JSON.parse(fs.readFileSync(versionFile, 'utf8'));
if (data && /^\d+\.\d+\.\d+$/.test(String(data.version || ''))) {
versions.push(String(data.version));
}
} catch (error) {
// 忽略损坏的本地 version.json
}
}
if (!versions.length) return '1.0.0';
versions.sort(compareVersion);
const [ major, minor, patch ] = versions[versions.length - 1].split('.').map(Number);
return `${major}.${minor}.${patch + 1}`;
}
/** 用 build.ps1 构建 Production 包 */
function buildDesktop(config, { version, notes, baseUrl }) {
const buildScript = path.resolve(ROOT, config.desktopBuildScript);
if (!fs.existsSync(buildScript)) {
throw new Error(`找不到桌面端构建脚本: ${buildScript}`);
}
const shell = process.platform === 'win32' ? 'powershell.exe' : 'pwsh';
const args = [
'-NoProfile',
'-ExecutionPolicy', 'Bypass',
'-File', buildScript,
'-Environment', 'Production',
'-Version', version,
'-ServerBaseUrl', baseUrl,
];
if (notes) {
args.push('-ReleaseNotes', notes);
}
console.log(`构建桌面端正式包: ${version}(${baseUrl})`);
runCommand(shell, args, { cwd: path.dirname(buildScript), stdio: 'inherit' });
}
/** 写 front.conf(静态 /download + /api 反代)并 reload nginx */
function main() {
const options = parseArgs(process.argv.slice(2));
const config = loadDeployConfig(options.envName);
const outDir = path.resolve(ROOT, config.desktopOutDir);
const remoteDir = config.desktopDownloadDir;
// version.json 不放安装包目录里:nginx 对 /download/version.json 单独关缓存,安装包目录走长缓存
const remoteVersionDir = config.desktopVersionDir || path.posix.dirname(String(remoteDir || ''));
const baseUrl = String(config.desktopBaseUrl || `http://${config.serverName}`).replace(/\/+$/, '');
const keep = options.keep > 0 ? options.keep : Number(config.desktopKeepVersions || 5);
const version = options.version || nextVersion(outDir);
if (!remoteDir) {
throw new Error('未配置 desktopDownloadDir(桌面安装包发布目录)。');
}
if (!remoteVersionDir || remoteVersionDir === '.' || remoteVersionDir === '/') {
throw new Error('未配置 desktopVersionDir(version.json 发布目录)。');
}
buildDesktop(config, { version, notes: options.notes, baseUrl });
const msiName = `YuncangTraceSetup-${version}.msi`;
const msiPath = path.join(outDir, msiName);
const versionPath = path.join(outDir, 'version.json');
if (!fs.existsSync(msiPath)) {
throw new Error(`构建完成但 MSI 不存在: ${msiPath}`);
}
if (!fs.existsSync(versionPath)) {
throw new Error(`构建完成但 version.json 不存在: ${versionPath}`);
}
const versionJson = JSON.parse(fs.readFileSync(versionPath, 'utf8'));
console.log(`安装包: ${msiPath}(${(fs.statSync(msiPath).size / 1024 / 1024).toFixed(1)} MB)`);
console.log(`版本文件: ${versionPath}`);
console.log(`下载地址: ${versionJson.downloadUrl}`);
console.log(`发布目录: ${config.username}@${config.host}:${remoteDir}`);
console.log(`版本目录: ${config.username}@${config.host}:${remoteVersionDir}`);
if (options.dryRun) {
console.log('DRY-RUN:只构建,不上传。');
return;
}
ensureCommandAvailable('ssh', '请先安装 OpenSSH Client 并确保 ssh 在 PATH 中。');
ensureCommandAvailable('scp', '请先安装 OpenSSH Client 并确保 scp 在 PATH 中。');
const stamp = getNowStamp();
const remoteMsiTmp = `${remoteDir}/.${msiName}.upload-${stamp}`;
const remoteVersionTmp = `${remoteVersionDir}/.version.json.upload-${stamp}`;
runSsh(config, [
'set -euo pipefail',
`DIR=${quoteForBash(remoteDir)}`,
`VDIR=${quoteForBash(remoteVersionDir)}`,
'mkdir -p "$DIR" "$VDIR"',
// 清理上次中断留下的隐藏临时文件
'find "$DIR" "$VDIR" -maxdepth 1 -type f -name ".*.upload-*" -delete',
].join('\n'), { stdio: 'pipe' });
console.log(`上传安装包(${(fs.statSync(msiPath).size / 1024 / 1024).toFixed(1)} MB)…`);
runScp(config, msiPath, remoteMsiTmp);
runSsh(config, [
'set -euo pipefail',
`DIR=${quoteForBash(remoteDir)}`,
`TMP=${quoteForBash(remoteMsiTmp)}`,
`FINAL=${quoteForBash(`${remoteDir}/${msiName}`)}`,
'mv "$TMP" "$FINAL"',
'chmod 644 "$FINAL"',
`chown ${quoteForBash(config.desktopOwner || 'www:www')} "$FINAL" || true`,
'echo "MSI_UPLOADED"',
].join('\n'), { stdio: 'pipe' });
console.log(`安装包已发布: ${remoteDir}/${msiName}`);
// MSI 就位后再更新 version.json,避免客户端拿到指向不存在文件的版本信息
runScp(config, versionPath, remoteVersionTmp);
runSsh(config, [
'set -euo pipefail',
`DIR=${quoteForBash(remoteDir)}`,
`VDIR=${quoteForBash(remoteVersionDir)}`,
`TMP=${quoteForBash(remoteVersionTmp)}`,
'mv "$TMP" "$VDIR/version.json"',
'chmod 644 "$VDIR/version.json"',
`chown ${quoteForBash(config.desktopOwner || 'www:www')} "$VDIR/version.json" || true`,
'echo "VERSION_JSON_UPLOADED"',
// 只保留最近 N 个安装包
'COUNT=0',
`for FILE in $(ls -1t "$DIR"/YuncangTraceSetup-*.msi 2>/dev/null || true); do`,
' COUNT=$((COUNT + 1))',
` if [ "$COUNT" -gt ${Number(keep)} ]; then rm -f "$FILE"; fi`,
'done',
'ls -1t "$DIR" | head -20',
].join('\n'), { stdio: 'pipe' });
if (!options.skipNginx) {
refreshNginx(config);
}
console.log('');
console.log('桌面端发布完成:');
console.log(` 版本: ${version}`);
console.log(` 安装包: ${baseUrl}/download/desktop/${msiName}`);
console.log(` 更新信息: ${baseUrl}/download/version.json`);
}
try {
main();
} catch (err) {
console.error(`[DESKTOP] ${err && err.message ? err.message : err}`);
process.exit(1);
}