UNPKG

@intlayer/chokidar

Version:

Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.

59 lines (57 loc) 2.43 kB
import { parallelize } from "../utils/parallelize.mjs"; import { writeJsonIfChanged } from "../writeJsonIfChanged.mjs"; import { filterInvalidDictionaries } from "../filterInvalidDictionaries.mjs"; import { mkdir } from "node:fs/promises"; import { resolve } from "node:path"; import { colorizePath, x } from "@intlayer/config/logger"; import { assertPathWithin } from "@intlayer/config/utils"; import { orderDictionaries } from "@intlayer/core/dictionaryManipulator"; //#region src/buildIntlayerDictionary/writeUnmergedDictionary.ts const groupDictionariesByKey = (dictionaries) => dictionaries.reduce((acc, dictionary) => { const key = dictionary.key; if (!acc[key]) acc[key] = []; acc[key].push(dictionary); return acc; }, {}); /** * 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: { ... } }, * // ], * // } * ``` */ const writeUnmergedDictionaries = async (dictionaries, configuration, env) => { const { unmergedDictionariesDir } = configuration.system; await mkdir(resolve(unmergedDictionariesDir), { recursive: true }); const groupedDictionaries = groupDictionariesByKey(filterInvalidDictionaries(dictionaries, configuration, { checkSchema: true })); const results = await parallelize(Object.entries(groupedDictionaries), async ([key, dictionaries]) => { if (key === "undefined") return; const orderedDictionaries = orderDictionaries(dictionaries); const unmergedFilePath = resolve(unmergedDictionariesDir, `${key}.json`); assertPathWithin(unmergedFilePath, unmergedDictionariesDir); 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 }]; }); return Object.fromEntries(results.filter(Boolean)); }; //#endregion export { groupDictionariesByKey, writeUnmergedDictionaries }; //# sourceMappingURL=writeUnmergedDictionary.mjs.map