|
@@ -0,0 +1,276 @@
|
|
|
|
|
+#!/usr/bin/env node
|
|
|
|
|
+/**
|
|
|
|
|
+ * APK + 下载官网发布(ai.fmode.cn/download/)
|
|
|
|
|
+ *
|
|
|
|
|
+ * 用法:
|
|
|
|
|
+ * node scripts/publish-apk.mjs # 完整:上传 APK → 更新官网 → 刷 CDN → 探活
|
|
|
|
|
+ * node scripts/publish-apk.mjs --dry-run # 只打印将要执行的动作,不执行
|
|
|
|
|
+ * node scripts/publish-apk.mjs --skip-apk # 不上传 APK,只更新官网与清单
|
|
|
|
|
+ * node scripts/publish-apk.mjs --force # 允许覆盖已存在的同名 APK 对象
|
|
|
|
|
+ * node scripts/publish-apk.mjs --channel=正式版 # 渠道标注(默认「测试版」,写进官网与清单)
|
|
|
|
|
+ *
|
|
|
|
|
+ * 设计(2026-11-23,用户裁决):
|
|
|
|
|
+ * · APK 走 S3(OBS)上传,版本号进文件名 `fmode-app-v{versionName}.apk`——
|
|
|
|
|
+ * 永不覆盖同名对象,天然免刷 CDN 缓存
|
|
|
|
|
+ * · 官网放 ai.fmode.cn/download/(Web App 在根路径,两边不互踩)
|
|
|
|
|
+ * · 官网源是 prototypes/fmode-download-20261120(暗色产品官网),
|
|
|
|
|
+ * 本脚本把 APK 信息写进 site.js 的 FMODE_SITE 后跑它的 build.mjs 重新产出
|
|
|
|
|
+ *
|
|
|
|
|
+ * 与 deploy.mjs 的分工:deploy.mjs 管 Web App(根路径),本脚本管 /download/。
|
|
|
|
|
+ * 🔴 桶里还有 2025-08 遗留构建与 assets/,全程只用 obsutil cp 逐对象上传,
|
|
|
|
|
+ * 禁止 sync -delete / rm(与 deploy.mjs、deploy-kit 同一铁律)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 凭据:obsutil 用 `~/.obsutilconfig`(43 位 AK);CDN 刷新用下方 KooCLI 专用
|
|
|
|
|
+ * 20 位 AK/SK(与 deploy.mjs 同一对,黄金规则 9 用户裁决可明文、集中存放)。
|
|
|
|
|
+ */
|
|
|
|
|
+import { createHash } from 'node:crypto';
|
|
|
|
|
+import { execFileSync, execSync } from 'node:child_process';
|
|
|
|
|
+import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
|
|
|
|
|
+import { dirname, join, resolve } from 'node:path';
|
|
|
|
|
+import { fileURLToPath } from 'node:url';
|
|
|
|
|
+
|
|
|
|
|
+const here = dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
+const projectRoot = resolve(here, '..');
|
|
|
|
|
+
|
|
|
|
|
+const BUCKET = 'obs://www-ai.fmode.cn';
|
|
|
|
|
+const CDN_BASE = 'https://ai.fmode.cn';
|
|
|
|
|
+const OBS_EP = 'obs.cn-south-1.myhuaweicloud.com';
|
|
|
|
|
+const CDN_REGION = 'cn-north-1';
|
|
|
|
|
+const APK_DIR = join(projectRoot, 'android/app/build/outputs/apk/debug');
|
|
|
|
|
+const SITE_DIR = join(projectRoot, 'prototypes/fmode-download-20261120');
|
|
|
|
|
+const TMP_DIR = join(projectRoot, 'scripts/.tmp-publish');
|
|
|
|
|
+
|
|
|
|
|
+// 与 deploy.mjs 同一对 KooCLI 凭证(见 deploy.mjs 顶部注释:不是 obsutil 那套)
|
|
|
|
|
+const HW_AK = process.env.HW_AK || '2BFF7JWXAIJ0UGNJ0OSB';
|
|
|
|
|
+const HW_SK = process.env.HW_SK || 'NaPCiJCGmD3nklCzX65s8mSK1Py13ueyhgepa0s1';
|
|
|
|
|
+
|
|
|
|
|
+const args = new Set(process.argv.slice(2));
|
|
|
|
|
+const dryRun = args.has('--dry-run');
|
|
|
|
|
+const skipApk = args.has('--skip-apk');
|
|
|
|
|
+const force = args.has('--force');
|
|
|
|
|
+const channelArg = process.argv.find((a) => a.startsWith('--channel='));
|
|
|
|
|
+const channel = channelArg ? channelArg.split('=')[1] : '测试版';
|
|
|
|
|
+
|
|
|
|
|
+/** Windows 上 npm/npx 是 .cmd,必须走 shell(同 deploy.mjs 2026-11-18 修复) */
|
|
|
|
|
+function needsShell(cmd) {
|
|
|
|
|
+ if (process.platform !== 'win32') return false;
|
|
|
|
|
+ return /\.(cmd|bat)$/i.test(cmd) || cmd === 'npm' || cmd === 'npx';
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function run(cmd, argv, opts = {}) {
|
|
|
|
|
+ const printable = `${cmd} ${argv.join(' ')}`;
|
|
|
|
|
+ console.log(`\n$ ${printable}`);
|
|
|
|
|
+ if (dryRun) return '';
|
|
|
|
|
+ try {
|
|
|
|
|
+ return execFileSync(cmd, argv, {
|
|
|
|
|
+ cwd: projectRoot,
|
|
|
|
|
+ stdio: 'inherit',
|
|
|
|
|
+ ...(needsShell(cmd) ? { shell: true } : {}),
|
|
|
|
|
+ ...opts,
|
|
|
|
|
+ });
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ console.error(`\n❌ 命令失败:${printable}`);
|
|
|
|
|
+ process.exit(1);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/** 探测型命令:失败返回 null 而不是退出(用于 stat 检查对象是否存在) */
|
|
|
|
|
+function tryRun(cmd, argv) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return execSync(`${cmd} ${argv.join(' ')}`, { cwd: projectRoot, stdio: 'pipe' }).toString();
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+async function probe(url, timeoutMs = 30_000) {
|
|
|
|
|
+ const ac = new AbortController();
|
|
|
|
|
+ const timer = setTimeout(() => ac.abort(), timeoutMs);
|
|
|
|
|
+ try {
|
|
|
|
|
+ const res = await fetch(url, { signal: ac.signal, redirect: 'follow' });
|
|
|
|
|
+ return { status: res.status, headers: res.headers, body: await res.text() };
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ return { status: 0, headers: new Headers(), body: '' };
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ clearTimeout(timer);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/** 华为云 KooCLI 探测(同 deploy.mjs:PATH 里的 hcloud 可能是 Hetzner 同名 CLI) */
|
|
|
|
|
+function resolveHcloud() {
|
|
|
|
|
+ const candidates = [
|
|
|
|
|
+ process.env.HCLOUD_BIN,
|
|
|
|
|
+ 'E:\\coding-environment\\huaweicloud-koocli\\hcloud.exe',
|
|
|
|
|
+ `${process.env.LOCALAPPDATA}\\huaweicloud\\hcloud.exe`,
|
|
|
|
|
+ `${process.env.USERPROFILE}\\tools\\hcloud\\hcloud.exe`,
|
|
|
|
|
+ '/usr/local/bin/hcloud',
|
|
|
|
|
+ 'hcloud',
|
|
|
|
|
+ ].filter(Boolean);
|
|
|
|
|
+ for (const bin of candidates) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const out = execFileSync(bin, ['version'], { encoding: 'utf8', timeout: 15_000 });
|
|
|
|
|
+ if (/KooCLI/i.test(out)) return bin;
|
|
|
|
|
+ } catch { /* 下一个 */ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// ── 1. APK 元信息 ──────────────────────────────────────────────────────────
|
|
|
|
|
+console.log('\n=== [1/6] 读取 APK 元信息 ===');
|
|
|
|
|
+const apkPath = join(APK_DIR, 'app-debug.apk');
|
|
|
|
|
+const metaPath = join(APK_DIR, 'output-metadata.json');
|
|
|
|
|
+if (!existsSync(apkPath) || !existsSync(metaPath)) {
|
|
|
|
|
+ console.error('❌ 找不到 APK 产物,请先构建:cd android && gradle assembleDebug');
|
|
|
|
|
+ process.exit(1);
|
|
|
|
|
+}
|
|
|
|
|
+const meta = JSON.parse(readFileSync(metaPath, 'utf8'));
|
|
|
|
|
+const versionName = meta.elements?.[0]?.versionName;
|
|
|
|
|
+if (!versionName) {
|
|
|
|
|
+ console.error('❌ output-metadata.json 中没有 versionName');
|
|
|
|
|
+ process.exit(1);
|
|
|
|
|
+}
|
|
|
|
|
+const { size, mtimeMs } = statSync(apkPath);
|
|
|
|
|
+const md5 = createHash('md5').update(readFileSync(apkPath)).digest('hex');
|
|
|
|
|
+const fileName = `fmode-app-v${versionName}.apk`;
|
|
|
|
|
+const fileSizeText = `${(size / 1024 / 1024).toFixed(1)} MB`;
|
|
|
|
|
+const today = new Date().toLocaleDateString('sv-SE'); // YYYY-MM-DD
|
|
|
|
|
+const apkUrl = `${CDN_BASE}/download/${fileName}`;
|
|
|
|
|
+console.log(` 版本 ${versionName} · ${fileSizeText} · md5 ${md5.slice(0, 8)}… · 渠道「${channel}」`);
|
|
|
|
|
+console.log(` 构建时间 ${new Date(mtimeMs).toLocaleString('zh-CN')}`);
|
|
|
|
|
+
|
|
|
|
|
+// ── 2. 防覆盖检查 + 上传 APK ────────────────────────────────────────────────
|
|
|
|
|
+console.log('\n=== [2/6] 上传 APK 到 OBS ===');
|
|
|
|
|
+const remoteApk = `${BUCKET}/download/${fileName}`;
|
|
|
|
|
+if (!skipApk) {
|
|
|
|
|
+ const stat = tryRun('obsutil', ['stat', remoteApk, `-e=${OBS_EP}`]);
|
|
|
|
|
+ if (stat && !force) {
|
|
|
|
|
+ console.error(`❌ ${fileName} 已存在于桶中。同名对象不覆盖(防误盖历史版本)。`);
|
|
|
|
|
+ console.error(' 确认要覆盖请加 --force;发新版本请先递增 versionName。');
|
|
|
|
|
+ process.exit(1);
|
|
|
|
|
+ }
|
|
|
|
|
+ // -meta 偶发不生效,chattri REPLACE_NEW 双保险(deploy-kit apk-publish.sh 实测写法)
|
|
|
|
|
+ run('obsutil', ['cp', apkPath, remoteApk, '-acl=public-read',
|
|
|
|
|
+ '-meta=Content-Type:application/vnd.android.package-archive', `-e=${OBS_EP}`]);
|
|
|
|
|
+ run('obsutil', ['chattri', remoteApk,
|
|
|
|
|
+ '-meta=Content-Type:application/vnd.android.package-archive', '-direct=REPLACE_NEW', `-e=${OBS_EP}`]);
|
|
|
|
|
+ run('obsutil', ['chattri', remoteApk, '-acl=public-read', `-e=${OBS_EP}`]);
|
|
|
|
|
+} else {
|
|
|
|
|
+ console.log(' 跳过 APK 上传(--skip-apk)');
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// ── 3. 更新官网配置并重新打包 ───────────────────────────────────────────────
|
|
|
|
|
+console.log('\n=== [3/6] 写入 FMODE_SITE 并重新构建官网 ===');
|
|
|
|
|
+const siteJsPath = join(SITE_DIR, 'site.js');
|
|
|
|
|
+const siteJs = readFileSync(siteJsPath, 'utf8');
|
|
|
|
|
+const fmodeSite = `const FMODE_SITE = { apkUrl: '${apkUrl}', version: '${versionName}', fileSize: '${fileSizeText}', updatedAt: '${today}', channel: '${channel}' };`;
|
|
|
|
|
+if (!/const FMODE_SITE = \{[^}]*\};/.test(siteJs)) {
|
|
|
|
|
+ console.error('❌ site.js 里找不到 FMODE_SITE 配置行');
|
|
|
|
|
+ process.exit(1);
|
|
|
|
|
+}
|
|
|
|
|
+if (!dryRun) writeFileSync(siteJsPath, siteJs.replace(/const FMODE_SITE = \{[^}]*\};/, fmodeSite));
|
|
|
|
|
+console.log(` ${fmodeSite}`);
|
|
|
|
|
+run('node', [join(SITE_DIR, 'build.mjs')]);
|
|
|
|
|
+
|
|
|
|
|
+// ── 4. 上传官网 + 清单 ──────────────────────────────────────────────────────
|
|
|
|
|
+console.log('\n=== [4/6] 上传 download/index.html 与 manifest.json ===');
|
|
|
|
|
+const indexPath = join(SITE_DIR, 'index.html');
|
|
|
|
|
+if (!existsSync(indexPath) && !dryRun) {
|
|
|
|
|
+ console.error('❌ build.mjs 未产出 index.html');
|
|
|
|
|
+ process.exit(1);
|
|
|
|
|
+}
|
|
|
|
|
+mkdirSync(TMP_DIR, { recursive: true });
|
|
|
|
|
+
|
|
|
|
|
+run('obsutil', ['cp', indexPath, `${BUCKET}/download/index.html`, '-acl=public-read',
|
|
|
|
|
+ '-meta=Content-Type:text/html;charset=utf-8', `-e=${OBS_EP}`]);
|
|
|
|
|
+run('obsutil', ['chattri', `${BUCKET}/download/index.html`,
|
|
|
|
|
+ '-meta=Content-Type:text/html;charset=utf-8', '-direct=REPLACE_NEW', `-e=${OBS_EP}`]);
|
|
|
|
|
+
|
|
|
|
|
+// 清单沿用 deploy-kit apk-publish.sh 的格式:按 file 去重、新条目插最前
|
|
|
|
|
+let manifest = { apps: [] };
|
|
|
|
|
+const current = await probe(`${CDN_BASE}/download/manifest.json?cb=${Date.now()}`);
|
|
|
|
|
+if (current.status === 200) {
|
|
|
|
|
+ try { manifest = JSON.parse(current.body); } catch { /* 用空清单 */ }
|
|
|
|
|
+ if (!Array.isArray(manifest.apps)) manifest.apps = [];
|
|
|
|
|
+} else {
|
|
|
|
|
+ console.log(' (线上暂无 manifest.json,新建)');
|
|
|
|
|
+}
|
|
|
|
|
+manifest.apps = manifest.apps.filter((a) => a.file !== fileName);
|
|
|
|
|
+manifest.apps.unshift({
|
|
|
|
|
+ file: fileName, name: 'Fmode 客户端', version: versionName, note: channel,
|
|
|
|
|
+ size, md5, url: `download/${fileName}`, time: today,
|
|
|
|
|
+});
|
|
|
|
|
+manifest.updated = new Date().toLocaleString('sv-SE').replace('T', ' ').slice(0, 16);
|
|
|
|
|
+const manifestPath = join(TMP_DIR, 'manifest.json');
|
|
|
|
|
+if (!dryRun) writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
|
|
|
|
|
+console.log(` 清单条目:${manifest.apps.map((a) => a.file).join(', ')}`);
|
|
|
|
|
+run('obsutil', ['cp', manifestPath, `${BUCKET}/download/manifest.json`, '-acl=public-read',
|
|
|
|
|
+ '-meta=Content-Type:application/json;charset=utf-8', `-e=${OBS_EP}`]);
|
|
|
|
|
+
|
|
|
|
|
+// ── 5. 刷 CDN(固定名文件才需要;APK 是新对象不刷) ─────────────────────────
|
|
|
|
|
+console.log('\n=== [5/6] 刷新 CDN ===');
|
|
|
|
|
+const refreshUrls = [`${CDN_BASE}/download/`, `${CDN_BASE}/download/index.html`, `${CDN_BASE}/download/manifest.json`];
|
|
|
|
|
+for (const u of refreshUrls) console.log(` ${u}`);
|
|
|
|
|
+const HCLOUD_BIN = resolveHcloud();
|
|
|
|
|
+if (!HCLOUD_BIN) {
|
|
|
|
|
+ // 同 deploy.mjs:找不到不静默跳过(OBS 已上传,CDN 没刷用户拿旧版)
|
|
|
|
|
+ console.error('\n❌ 未找到华为云 KooCLI(hcloud)。文件已上传,但 CDN 未刷新。');
|
|
|
|
|
+ console.error(' 安装后重跑,或用 HCLOUD_BIN=<完整路径> 指定;也可在 CDN 控制台手动刷新以上 URL。');
|
|
|
|
|
+ process.exit(1);
|
|
|
|
|
+}
|
|
|
|
|
+// KooCLI 首跑会交互询问隐私声明,stdin 喂 y(同 deploy.mjs)
|
|
|
|
|
+run(HCLOUD_BIN, [
|
|
|
|
|
+ 'CDN', 'CreateRefreshTasks/v2',
|
|
|
|
|
+ `--cli-region=${CDN_REGION}`,
|
|
|
|
|
+ '--refresh_task.type=file',
|
|
|
|
|
+ ...refreshUrls.map((u, i) => `--refresh_task.urls.${i + 1}=${u}`),
|
|
|
|
|
+ `--cli-access-key=${HW_AK}`,
|
|
|
|
|
+ `--cli-secret-key=${HW_SK}`,
|
|
|
|
|
+], { input: 'y\ny\ny\n' });
|
|
|
|
|
+
|
|
|
|
|
+// ── 6. 探活 ────────────────────────────────────────────────────────────────
|
|
|
|
|
+console.log('\n=== [6/6] 线上探活 ===');
|
|
|
|
|
+if (dryRun) {
|
|
|
|
|
+ console.log(' (dry-run,跳过)');
|
|
|
|
|
+} else {
|
|
|
|
|
+ let allOk = true;
|
|
|
|
|
+
|
|
|
|
|
+ if (!skipApk) {
|
|
|
|
|
+ // APK:状态 200 + 大小一致(Content-Type 也可能经 CDN 归一化,以大小为准)
|
|
|
|
|
+ let apkOk = false;
|
|
|
|
|
+ for (let i = 0; i < 12 && !apkOk; i++) {
|
|
|
|
|
+ const r = await probe(apkUrl, 15_000);
|
|
|
|
|
+ const len = Number(r.headers.get('content-length') || 0);
|
|
|
|
|
+ apkOk = r.status === 200 && len === size;
|
|
|
|
|
+ if (!apkOk) await new Promise((r2) => setTimeout(r2, 5_000));
|
|
|
|
|
+ }
|
|
|
|
|
+ console.log(apkOk ? ` ✓ APK 直链 200 · ${fileSizeText}` : ` ❌ APK 直链未就绪:${apkUrl}`);
|
|
|
|
|
+ allOk &&= apkOk;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 官网:CDN 刷新有延迟,以「响应含本次版本号」为准(不看 200)
|
|
|
|
|
+ let siteOk = false;
|
|
|
|
|
+ for (let i = 0; i < 18 && !siteOk; i++) {
|
|
|
|
|
+ const r = await probe(`${CDN_BASE}/download/?cb=${Date.now()}`, 15_000);
|
|
|
|
|
+ siteOk = r.body.includes(versionName) && r.body.includes(fileName);
|
|
|
|
|
+ if (!siteOk) await new Promise((r2) => setTimeout(r2, 10_000));
|
|
|
|
|
+ }
|
|
|
|
|
+ console.log(siteOk ? ' ✓ 下载页含本次版本与 APK 直链' : ' ❌ 下载页 3 分钟后仍非本次内容,请手动确认 CDN');
|
|
|
|
|
+ allOk &&= siteOk;
|
|
|
|
|
+
|
|
|
|
|
+ // Web App 不受影响(回归哨兵)
|
|
|
|
|
+ const app = await probe(`${CDN_BASE}/`, 15_000);
|
|
|
|
|
+ const appOk = app.body.includes('app-root');
|
|
|
|
|
+ console.log(appOk ? ' ✓ 根路径 Web App 正常(未受影响)' : ' ❌ 根路径 Web App 异常,立即检查!');
|
|
|
|
|
+ allOk &&= appOk;
|
|
|
|
|
+
|
|
|
|
|
+ if (!allOk) {
|
|
|
|
|
+ console.error('\n❌ 发布完成但线上校验未全部通过,见上方 ❌ 项');
|
|
|
|
|
+ process.exit(1);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+console.log('\n' + '─'.repeat(56));
|
|
|
|
|
+console.log(` 安装包直链 : ${apkUrl}`);
|
|
|
|
|
+console.log(` 下载页 : ${CDN_BASE}/download/`);
|
|
|
|
|
+console.log('─'.repeat(56));
|
|
|
|
|
+console.log('\n✅ 发布完成\n');
|