import { mkdir } from 'node:fs/promises'; import { resolve } from 'node:path'; import { colorizePath, x } from '@intlayer/config/logger'; import { assertPathWithin, parallelize } from '@intlayer/config/utils'; import { orderDictionaries } from '@intlayer/core/dictionaryManipulator'; import type { IntlayerConfig } from '@intlayer/types/config'; import type { Dictionary, DictionaryKey } from '@intlayer/types/dictionary'; import { filterInvalidDictionaries } from '../filterInvalidDictionaries'; import { writeJsonIfChanged } from '../writeJsonIfChanged'; export const groupDictionariesByKey = ( dictionaries: Dictionary[] ): Record => dictionaries.reduce( (acc, dictionary) => { const key = dictionary.key; if (!acc[key]) { acc[key] = []; } acc[key].push(dictionary); return acc; }, {} as Record ); export type UnmergedDictionaryResult = { dictionaryPath: string; dictionaries: Dictionary[]; }; export type UnmergedDictionaryOutput = Record< DictionaryKey, UnmergedDictionaryResult >; /** * Write the unmerged dictionaries to the unmergedDictionariesDir * @param dictionaries - The dictionaries to write * @param configuration - The configuration * @returns The grouped dictionaries * * @example * ```ts * const unmergedDictionaries = await writeUnmergedDictionaries(dictionaries); * console.log(unmergedDictionaries); * * // .intlayer/unmerged_dictionaries/home.json * // { * // [ * // { key: 'home', content: { ... } }, * // { key: 'home', content: { ... } }, * // ], * // } * ``` */ export const writeUnmergedDictionaries = async ( dictionaries: Dictionary[], configuration: IntlayerConfig, env: 'prod' | 'dev', failOnSchemaError = env === 'prod' ): Promise => { const { unmergedDictionariesDir } = configuration.system; // Create the dictionaries folder if it doesn't exist await mkdir(resolve(unmergedDictionariesDir), { recursive: true }); const filteredDictionaries = filterInvalidDictionaries( dictionaries, configuration, { checkSchema: true, throwOnSchemaError: failOnSchemaError } ); // Group dictionaries by key and write to unmergedDictionariesDir const groupedDictionaries = groupDictionariesByKey(filteredDictionaries); const results = await parallelize( Object.entries(groupedDictionaries), async ([key, dictionaries]) => { if (key === 'undefined') { return undefined as unknown as readonly [ string, UnmergedDictionaryResult, ]; } const orderedDictionaries = orderDictionaries(dictionaries); const outputFileName = `${key}.json`; const unmergedFilePath = resolve(unmergedDictionariesDir, outputFileName); assertPathWithin(unmergedFilePath, unmergedDictionariesDir); // Write the grouped dictionaries in disk if the editor is enabled // To make work the visual editor on the server // No need them if the editor is disabled // But in local env (env: 'dev') we write them for the vscode extension if (configuration.editor.enabled || env === 'dev') { await writeJsonIfChanged(unmergedFilePath, orderedDictionaries).catch( (err) => { console.error( `${x} Error creating unmerged ${colorizePath(unmergedFilePath)}:`, err ); } ); } return [ key, { dictionaryPath: unmergedFilePath, dictionaries: dictionaries, } as UnmergedDictionaryResult, ] as const; } ); return Object.fromEntries( results.filter(Boolean) as Array< readonly [string, UnmergedDictionaryResult] > ); };