Files
cindy/apps/mobile/scripts/lib/sim-native-identity.mjs
DavidShen f9b47da8a8 fix(mobile): 保留行内公式语义并支持 Android 验收
修复样式命令参数与符号边界,补齐 Android 验收入口及指纹读取失败回归。

Assisted-by: Cindy (https://cindy.app)
Signed-off-by: DavidShen <david@xd.com>
2026-11-09 15:59:07 +08:00

101 lines
3.7 KiB
JavaScript

import { execFileSync } from "node:child_process";
import { readFileSync } from "node:fs";
import { createRequire } from "node:module";
import { dirname, join, resolve } from "node:path";
function fingerprint(value) {
return typeof value === "string" && /^[a-f0-9]{40}$/.test(value.trim())
? value.trim()
: null;
}
// Use the same Expo Updates workflow as the native build. The CI/cache hash
// uses a different entrypoint and must not be compared with the embedded hash.
export function computeSimulatorNativeFingerprint(
projectDir,
env = process.env,
platform = 'ios',
) {
const projectRequire = createRequire(
join(resolve(projectDir), "package.json"),
);
const packagePath = projectRequire.resolve("expo-updates/package.json");
const pkg = JSON.parse(readFileSync(packagePath, "utf8"));
const bin = join(dirname(packagePath), pkg.bin["expo-updates"]);
try {
const output = execFileSync(
process.execPath,
[bin, "fingerprint:generate", "--platform", platform],
{
cwd: projectDir,
env,
encoding: "utf8",
maxBuffer: 64 * 1024 * 1024,
timeout: 120_000,
killSignal: "SIGKILL",
stdio: ["ignore", "pipe", "pipe"],
},
);
const hash = fingerprint(JSON.parse(output).hash);
if (!hash) throw new Error("Invalid fingerprint");
return hash;
} catch (error) {
throw Object.assign(new Error("Expo Updates native fingerprint calculation failed"), {
code: error.code === "ETIMEDOUT" ? "NATIVE_FINGERPRINT_TIMEOUT" : "NATIVE_FINGERPRINT_FAILED",
});
}
}
/**
* @param {{ appPath?: string, projectDir?: string, env?: Record<string, string | undefined>,
* expectedFingerprint?: string, compute?: typeof computeSimulatorNativeFingerprint,
* read?: (path: string) => string, platform?: 'ios' | 'android',
* readInstalledFingerprint?: () => string }} options
*/
export function inspectSimulatorNativeIdentity({
appPath,
projectDir,
env,
expectedFingerprint,
compute = computeSimulatorNativeFingerprint,
read = (path) => readFileSync(path, "utf8"),
platform = 'ios',
readInstalledFingerprint = () => read(join(appPath, "EXUpdates.bundle", "fingerprint")),
}) {
const result = {
healthy: false,
code: "native-unknown",
installedFingerprint: null,
expectedFingerprint: null,
};
if (!appPath) return { ...result, code: "native-missing" };
try {
result.installedFingerprint = fingerprint(
readInstalledFingerprint(),
);
} catch (error) {
// A failed read is not evidence of an incompatible build. Keep a bounded
// reason, never the child output; a genuinely missing file can be rebuilt.
const reason = error?.code === 'ENOENT' ? 'installed-fingerprint-missing'
: error?.code === 'ANDROID_PROBE_TIMEOUT' || error?.code === 'ETIMEDOUT'
? 'installed-fingerprint-timeout' : 'installed-fingerprint-unreadable';
return { ...result, reason };
}
if (!result.installedFingerprint) return { ...result, reason: "installed-fingerprint-missing" };
try {
result.expectedFingerprint = fingerprint(
expectedFingerprint ?? compute(projectDir, env, platform),
);
if (!result.expectedFingerprint)
return { ...result, reason: "native-fingerprint-unavailable" };
result.healthy = result.installedFingerprint === result.expectedFingerprint;
result.code = result.healthy ? "native-matched" : "native-mismatch";
return result;
} catch (error) {
// Unknown is never compatibility evidence. Keep CLI output free of config
// contents or child-process diagnostics that may contain local secrets.
return { ...result, reason: error.code === "NATIVE_FINGERPRINT_TIMEOUT"
? "native-fingerprint-timeout" : "native-fingerprint-unavailable" };
}
}