54 lines
2.2 KiB
TypeScript
54 lines
2.2 KiB
TypeScript
/**
|
|
* 已添加设备注册表 —— 添加成功后把"首页实际设备名"写入,供功能/设置用例作为进入设备页的入口。
|
|
*
|
|
* **按平台分文件**:test-plan/added-devices.<platform>.json(android / ios 各一份)。
|
|
* - 同机同时跑 Android + iOS 时是两套真机、设备名不同 → 各读各的,互不干扰;
|
|
* - 两个进程写不同文件,天然无写冲突。
|
|
* 平台取 process.env.PLATFORM(与 config/app.config 一致,默认 ios)。
|
|
*
|
|
* 读取优先级见 config/device.config.ts 的 getDeviceName:env > 本注册表 > 配置默认值。
|
|
* 设备名是变量、随真机实际显示而定,不写死。
|
|
*/
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
|
|
function currentPlatform(): string {
|
|
return (process.env.PLATFORM || 'ios').toLowerCase();
|
|
}
|
|
|
|
function registryFile(platform = currentPlatform()): string {
|
|
return path.resolve(__dirname, '../../test-plan', `added-devices.${platform}.json`);
|
|
}
|
|
|
|
function readRegistry(platform = currentPlatform()): Record<string, string> {
|
|
try {
|
|
const f = registryFile(platform);
|
|
if (fs.existsSync(f)) return JSON.parse(fs.readFileSync(f, 'utf-8'));
|
|
} catch { /* 忽略损坏文件 */ }
|
|
return {};
|
|
}
|
|
|
|
/** 写入某品类添加后的实际设备名(进入功能页的入口),写到当前平台的注册表。 */
|
|
export function saveAddedDevice(category: string, deviceName: string): void {
|
|
const platform = currentPlatform();
|
|
const f = registryFile(platform);
|
|
const reg = readRegistry(platform);
|
|
reg[category] = deviceName;
|
|
fs.mkdirSync(path.dirname(f), { recursive: true });
|
|
fs.writeFileSync(f, JSON.stringify(reg, null, 2), 'utf-8');
|
|
console.log(`已记录设备入口[${platform}]: ${category} → "${deviceName}" (${f})`);
|
|
}
|
|
|
|
/** 读当前平台某品类已添加的设备名;无则返回 undefined。 */
|
|
export function getAddedDevice(category: string): string | undefined {
|
|
return readRegistry()[category];
|
|
}
|
|
|
|
/** 清空当前平台的注册表(必测重跑前清设备后调用,避免残留旧入口名)。 */
|
|
export function clearAddedDevices(): void {
|
|
const platform = currentPlatform();
|
|
const f = registryFile(platform);
|
|
if (fs.existsSync(f)) fs.writeFileSync(f, '{}\n', 'utf-8');
|
|
console.log(`已清空设备注册表[${platform}] (${f})`);
|
|
}
|