import { type Parser } from 'style-dictionary/types'; /** * Custom parser that fixes cross-brand token references as they're being loaded * This ensures references are corrected before Style Dictionary resolves them */ export const crossBrandParser: Parser = { name: 'crossBrandParser', pattern: /tokens\/cross-brand-light-color\/.+\.json$/, parser: ({ contents, filePath }) => { const data = JSON.parse(contents); // Process the parsed JSON to fix token references function processTokenReferences(obj: any, path: string[] = []): any { if (typeof obj === 'object' && obj !== null) { if (obj.$value && typeof obj.$value === 'string' && obj.$value.includes('{color.')) { const oldValue = obj.$value; obj.$value = obj.$value.replace(/{color\.([^}]+)}/g, '{cross-brand-light-color.$1}'); console.log(`[PARSER] ${filePath} at ${path.join('.')}: ${oldValue} → ${obj.$value}`); } // Recursively process all object properties for (const key in obj) { if (key !== '$value') { obj[key] = processTokenReferences(obj[key], [...path, key]); } } } return obj; } return processTokenReferences(data); }, };