{"version":3,"file":"writeUnmergedDictionary.cjs","names":["filterInvalidDictionaries","parallelize","writeJsonIfChanged","x"],"sources":["../../../src/buildIntlayerDictionary/writeUnmergedDictionary.ts"],"sourcesContent":["import { mkdir } from 'node:fs/promises';\nimport { resolve } from 'node:path';\nimport { colorizePath, x } from '@intlayer/config/logger';\nimport { assertPathWithin } from '@intlayer/config/utils';\nimport { orderDictionaries } from '@intlayer/core/dictionaryManipulator';\nimport type { IntlayerConfig } from '@intlayer/types/config';\nimport type { Dictionary, DictionaryKey } from '@intlayer/types/dictionary';\nimport { filterInvalidDictionaries } from '../filterInvalidDictionaries';\nimport { parallelize } from '../utils/parallelize';\nimport { writeJsonIfChanged } from '../writeJsonIfChanged';\n\nexport const groupDictionariesByKey = (\n  dictionaries: Dictionary[]\n): Record<string, Dictionary[]> =>\n  dictionaries.reduce(\n    (acc, dictionary) => {\n      const key = dictionary.key;\n      if (!acc[key]) {\n        acc[key] = [];\n      }\n      acc[key].push(dictionary);\n      return acc;\n    },\n    {} as Record<string, Dictionary[]>\n  );\n\nexport type UnmergedDictionaryResult = {\n  dictionaryPath: string;\n  dictionaries: Dictionary[];\n};\n\nexport type UnmergedDictionaryOutput = Record<\n  DictionaryKey,\n  UnmergedDictionaryResult\n>;\n\n/**\n * Write the unmerged dictionaries to the unmergedDictionariesDir\n * @param dictionaries - The dictionaries to write\n * @param configuration - The configuration\n * @returns The grouped dictionaries\n *\n * @example\n * ```ts\n * const unmergedDictionaries = await writeUnmergedDictionaries(dictionaries);\n * console.log(unmergedDictionaries);\n *\n * // .intlayer/unmerged_dictionaries/home.json\n * // {\n * //   [\n * //     { key: 'home', content: { ... } },\n * //     { key: 'home', content: { ... } },\n * //   ],\n * // }\n * ```\n */\nexport const writeUnmergedDictionaries = async (\n  dictionaries: Dictionary[],\n  configuration: IntlayerConfig,\n  env: 'prod' | 'dev'\n): Promise<UnmergedDictionaryOutput> => {\n  const { unmergedDictionariesDir } = configuration.system;\n\n  // Create the dictionaries folder if it doesn't exist\n  await mkdir(resolve(unmergedDictionariesDir), { recursive: true });\n\n  const filteredDictionaries = filterInvalidDictionaries(\n    dictionaries,\n    configuration,\n    { checkSchema: true }\n  );\n\n  //  Group dictionaries by key and write to unmergedDictionariesDir\n  const groupedDictionaries = groupDictionariesByKey(filteredDictionaries);\n\n  const results = await parallelize(\n    Object.entries(groupedDictionaries),\n    async ([key, dictionaries]) => {\n      if (key === 'undefined') {\n        return undefined as unknown as readonly [\n          string,\n          UnmergedDictionaryResult,\n        ];\n      }\n\n      const orderedDictionaries = orderDictionaries(dictionaries);\n\n      const outputFileName = `${key}.json`;\n      const unmergedFilePath = resolve(unmergedDictionariesDir, outputFileName);\n\n      assertPathWithin(unmergedFilePath, unmergedDictionariesDir);\n\n      // Write the grouped dictionaries in disk if the editor is enabled\n      // To make work the visual editor on the server\n      // No need them if the editor is disabled\n      // But in local env (env: 'dev') we write them for the vscode extension\n      if (configuration.editor.enabled || env === 'dev') {\n        await writeJsonIfChanged(unmergedFilePath, orderedDictionaries).catch(\n          (err) => {\n            console.error(\n              `${x} Error creating unmerged ${colorizePath(unmergedFilePath)}:`,\n              err\n            );\n          }\n        );\n      }\n\n      return [\n        key,\n        {\n          dictionaryPath: unmergedFilePath,\n          dictionaries: dictionaries,\n        } as UnmergedDictionaryResult,\n      ] as const;\n    }\n  );\n\n  return Object.fromEntries(\n    results.filter(Boolean) as Array<\n      readonly [string, UnmergedDictionaryResult]\n    >\n  );\n};\n"],"mappings":";;;;;;;;;;;;AAWA,MAAa,0BACX,iBAEA,aAAa,QACV,KAAK,eAAe;CACnB,MAAM,MAAM,WAAW;CACvB,IAAI,CAAC,IAAI,MACP,IAAI,OAAO,CAAC;CAEd,IAAI,KAAK,KAAK,UAAU;CACxB,OAAO;AACT,GACA,CAAC,CACH;;;;;;;;;;;;;;;;;;;;;AAgCF,MAAa,4BAA4B,OACvC,cACA,eACA,QACsC;CACtC,MAAM,EAAE,4BAA4B,cAAc;CAGlD,yDAAoB,uBAAuB,GAAG,EAAE,WAAW,KAAK,CAAC;CASjE,MAAM,sBAAsB,uBAPCA,4DAC3B,cACA,eACA,EAAE,aAAa,KAAK,CAIgD,CAAC;CAEvE,MAAM,UAAU,MAAMC,sCACpB,OAAO,QAAQ,mBAAmB,GAClC,OAAO,CAAC,KAAK,kBAAkB;EAC7B,IAAI,QAAQ,aACV;EAMF,MAAM,kFAAwC,YAAY;EAG1D,MAAM,0CAA2B,yBAAyB,GADhC,IAAI,MAC0C;EAExE,6CAAiB,kBAAkB,uBAAuB;EAM1D,IAAI,cAAc,OAAO,WAAW,QAAQ,OAC1C,MAAMC,8CAAmB,kBAAkB,mBAAmB,EAAE,OAC7D,QAAQ;GACP,QAAQ,MACN,GAAGC,0BAAE,qEAAwC,gBAAgB,EAAE,IAC/D,GACF;EACF,CACF;EAGF,OAAO,CACL,KACA;GACE,gBAAgB;GACF;EAChB,CACF;CACF,CACF;CAEA,OAAO,OAAO,YACZ,QAAQ,OAAO,OAAO,CAGxB;AACF"}