import fs from "node:fs"; import path from "node:path"; const configPath = path.join(import.meta.dirname, "../config/mask.config.json"); export const maskConfig = JSON.parse(fs.readFileSync(configPath, "utf8")); const entries = Object.entries(maskConfig.replacements).sort(([a], [b]) => b.length - a.length); const reverseEntries = entries .map(([from, to]) => [to, from]) .sort(([a], [b]) => b.length - a.length); /** 按遮蔽配置推导名称,测试与构建共用同一张表,避免两边写死而不同步。 */ export function masked(name) { if (!maskConfig.enabled) return name; return entries.reduce((result, [from, to]) => result.split(from).join(to), name); } /** 把制品中的名称还原成源码名称,让产物契约断言不依赖产物是否已经遮蔽。 */ export function unmask(name) { if (!maskConfig.enabled) return name; return reverseEntries.reduce((result, [from, to]) => result.split(from).join(to), name); }