Files
cindy/apps/mobile/scripts/lib/sim-environment.mjs
Dash 93933624fe feat(dev): 桌面与手机共用的免登录 UI 模拟数据(--mock)
- @cindy/maker-shared/ui-mock:两端共用的演示项目与任务(桌面数据库行格式)
- Desktop:restart --mock 使用独立 ui-mock 沙箱,首次免账号进入并补齐模拟任务
- Mobile:sim:start/whoami/rebuild 支持 --mock,visual mock 改用共享数据
- 文档:docs/dev-rules/ui-mock-data.md,并在启动说明与 AGENTS 索引中链接

Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>
2026-10-24 14:01:03 +13:00

38 lines
1.7 KiB
JavaScript

import { readFileSync, existsSync } from 'node:fs';
import { join } from 'node:path';
import { parseProjectEnv } from '@expo/env';
import { metroEnvironmentFingerprint } from '../sim-metro.mjs';
export const UI_MOCK_ENV = 'EXPO_PUBLIC_CINDY_MOBILE_VISUAL_MOCK';
/**
* `--mock` selects the login-free UI mock data (docs/dev-rules/ui-mock-data.md).
* It only sets the bundle env, so start, whoami and rebuild must all pass it.
*/
export function applySimMockArg(args, env = process.env) {
const passthrough = args.filter((arg) => arg !== '--mock');
if (passthrough.length !== args.length) env[UI_MOCK_ENV] = '1';
return passthrough;
}
/** Read the same inputs for start, diagnostics and rebuild, without repairing files. */
export function readSimEnvironment(mobileDir, buildEnv, systemEnv = process.env) {
const projectEnv = parseProjectEnv(mobileDir, {
mode: systemEnv.NODE_ENV ?? 'development', silent: true, systemEnv: { ...systemEnv },
});
const loginScenario = systemEnv.EXPO_PUBLIC_LOGIN_SCENARIO?.trim()
?? projectEnv.env.EXPO_PUBLIC_LOGIN_SCENARIO?.trim() ?? '';
const uiMock = ['1', 'true', 'yes'].includes(
(systemEnv[UI_MOCK_ENV] ?? projectEnv.env[UI_MOCK_ENV] ?? '').trim().toLowerCase());
const files = {};
for (const name of ['.env', 'scripts/self-host-regions.json']) {
const file = join(mobileDir, name);
files[name] = existsSync(file) ? readFileSync(file, 'utf8') : null;
}
return { loginScenario, uiMock, envFingerprint: metroEnvironmentFingerprint({
// Only a mock Metro adds the key, so existing real-data fingerprints stay valid.
env: { ...buildEnv, EXPO_PUBLIC_LOGIN_SCENARIO: loginScenario, ...(uiMock ? { [UI_MOCK_ENV]: '1' } : {}) },
files,
}) };
}