import type { TemplatePackageVersion } from "@prisma/client"; export function record(value: unknown): Record { return value && typeof value === "object" && !Array.isArray(value) ? (value as Record) : {}; } export function manifestRecords(value: unknown): Record[] { return Array.isArray(value) ? value.filter((item): item is Record => Boolean(item && typeof item === "object" && !Array.isArray(item)), ) : []; } export function stringList(value: unknown): string[] { return Array.isArray(value) ? value.filter((item): item is string => typeof item === "string") : []; } export function hasBindingValue(config: Record, binding: unknown): boolean { if (typeof binding !== "string" || !binding.startsWith("brand.config.")) return false; const value = binding .slice("brand.config.".length) .split(".") .reduce((current, key) => record(current)[key], config); return value !== undefined && value !== null && value !== ""; } export function compareRecords(current: Record, target: Record) { const currentKeys = Object.keys(current); const targetKeys = Object.keys(target); return { added: targetKeys .filter((key) => !(key in current)) .map((key) => ({ key, target: target[key] })), removed: currentKeys .filter((key) => !(key in target)) .map((key) => ({ key, current: current[key] })), changed: targetKeys .filter( (key) => key in current && JSON.stringify(current[key]) !== JSON.stringify(target[key]), ) .map((key) => ({ key, current: current[key], target: target[key] })), }; } export function compareKeyedRecords( current: Record[], target: Record[], ) { const currentByKey = new Map( current .filter((item) => typeof item.key === "string") .map((item) => [item.key as string, item]), ); const targetByKey = new Map( target.filter((item) => typeof item.key === "string").map((item) => [item.key as string, item]), ); return { added: [...targetByKey].filter(([key]) => !currentByKey.has(key)).map(([, item]) => item), removed: [...currentByKey].filter(([key]) => !targetByKey.has(key)).map(([, item]) => item), changed: [...targetByKey].flatMap(([key, item]) => { const previous = currentByKey.get(key); return previous && JSON.stringify(previous) !== JSON.stringify(item) ? [{ key, current: previous, target: item }] : []; }), }; } export function compareFiles( current: Record[], target: Record[], ) { const currentByPath = new Map( current .filter((item) => typeof item.path === "string") .map((item) => [item.path as string, item]), ); const targetByPath = new Map( target .filter((item) => typeof item.path === "string") .map((item) => [item.path as string, item]), ); const available = currentByPath.size > 0 && targetByPath.size > 0; return { available, ...(available ? { added: [...targetByPath] .filter(([path]) => !currentByPath.has(path)) .map(([, item]) => item), removed: [...currentByPath] .filter(([path]) => !targetByPath.has(path)) .map(([, item]) => item), changed: [...targetByPath].flatMap(([path, item]) => { const previous = currentByPath.get(path); return previous && previous.sha256 !== item.sha256 ? [{ path, current: previous, target: item }] : []; }), } : { reason: "版本验证报告未包含完整文件清单" }), }; } export function readPackageFiles( version: Pick, ): Record[] { return manifestRecords(record(version.validationReport).files); }