import { styleDictionaryStructure } from './sortTokensToStyleDictionary.js'; import { isEmpty, merge } from 'lodash-es'; import { FilesOutput } from '../../../parsersEngine/definitions/parserOutput.js'; export function sortedTokensToFiles( tokens: styleDictionaryStructure, directoryPath: string, ): FilesOutput['files'] { const formattedDirectoryPath = directoryPath.endsWith('/') || directoryPath === '' ? directoryPath : `${directoryPath}/`; return Object.entries(tokens).map(([path, value]) => { return { path: `${formattedDirectoryPath}${path}.json`, content: { type: 'text', text: JSON.stringify( path .split('/') .reverse() .reduce( (acc, key) => isEmpty(acc) ? { [key]: Object.entries(value).reduce( (acc, [path, value]) => merge( acc, path .split('.') .reverse() .reduce( (acc, key) => (isEmpty(acc) ? { [key]: value } : { [key]: acc }), {}, ), ), {}, ), } : { [key]: acc }, {}, ), ), }, }; }); }