Files
cindy/apps/mobile/scripts/lib/bundler-cache.mjs
Lizi bb3f71d084 chore: Cindy 首次开源提交 / initial open-source release
Cindy 是心动网络 (X.D. Network Inc.) 的跨端 AI Agent 客户端。
本次提交包含 Desktop(Electron)、Mobile(Expo)及全部共享 packages
的完整源码,以 Apache-2.0 协议开源。

This is the first public release of Cindy, X.D. Network's cross-platform
AI agent client: the Electron desktop app, the Expo mobile app, and all
shared workspace packages, licensed under Apache-2.0.

- 许可与合规:根 LICENSE(Apache-2.0)+ NOTICE;第三方组件归属声明与
  SPDX SBOM 收口在 docs/legal/notices/,由 pnpm licenses:generate 生成。
- 社区文件:README / CONTRIBUTING / CODE_OF_CONDUCT / SECURITY / SUPPORT
  中英双语;issue / PR 模板与 CI workflow 在 .github/。
- 协议层以公开 submodule 引入:github.com/makecindy/cindy-protocol。
- 提交前已通过仓库门禁:全量 pnpm test:unit 与涉及 package 的 typecheck。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 08:30:44 +08:00

50 lines
2.3 KiB
JavaScript

// 清 Metro / Babel bundler 缓存。
//
// 为什么需要:EXPO_PUBLIC_* 由 babel-preset-expo 在打包时内联成字符串字面量;Metro/Babel 会缓存
// 转换结果,而**只改环境变量、不改源码时缓存 key 不变 → 缓存不失效**,会把旧值(如 placeholder)
// 继续烤进 bundle(尤其持久 CI runner 跨 pipeline 复用 $TMPDIR 缓存)。发布前清一次,确保
// EXPO_PUBLIC_(TAPTAP / OTA_URL / API 等)变更被重新内联。
//
// 说明:OTA 脚本用 `expo export --clear` 即可清;本函数供 local 脚本(gradle / xcodebuild 内部
// 触发 expo export:embed、无法直接透传 --clear)在原生构建前调用。tmp/mobileDir 可注入便于单测。
import { rmSync, readdirSync, existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
/**
* 删除 Metro/Babel/Expo 缓存目录(幂等、best-effort,删不掉不抛错)。
* @param {{ mobileDir: string, tmp?: string, log?: (m: string) => void }} opts
* @returns {string[]} 实际删除的路径
*/
export function clearBundlerCache({ mobileDir, tmp = tmpdir(), log = () => {} }) {
if (!mobileDir) throw new Error('clearBundlerCache requires mobileDir');
const targets = [];
// 1) $TMPDIR 下的 metro / haste 缓存。只匹配 Metro 自身缓存目录的已知命名
// (metro-cache / metro-cache-* / metro-bundler-* / (metro-)haste-map-*),
// 不用宽泛的 metro-* 前缀——否则会误删 $TMPDIR 下其它以 metro- 开头的无关目录。
const isMetroCacheDir = (name) =>
name === 'metro-cache' ||
name.startsWith('metro-cache-') ||
name.startsWith('metro-bundler-') ||
name.startsWith('metro-haste-map-') ||
name.startsWith('haste-map-');
try {
for (const name of readdirSync(tmp)) {
if (isMetroCacheDir(name)) targets.push(join(tmp, name));
}
} catch { /* tmp 不可读则跳过 */ }
// 2) 项目内 babel-loader / expo 缓存。
targets.push(join(mobileDir, 'node_modules', '.cache'));
targets.push(join(mobileDir, '.expo'));
const removed = [];
for (const t of targets) {
try {
if (existsSync(t)) { rmSync(t, { recursive: true, force: true }); removed.push(t); }
} catch { /* best-effort,删不掉不阻断构建 */ }
}
log(` ✓ 已清 bundler 缓存(${removed.length} 处),确保 EXPO_PUBLIC_ 变更被重新内联`);
return removed;
}