@intlayer/chokidar 8.1.2 → 8.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (396) hide show
  1. package/dist/assets/installSkills/skills/angular.md +3 -0
  2. package/dist/assets/installSkills/skills/astro.md +3 -0
  3. package/dist/assets/installSkills/skills/cli.md +7 -4
  4. package/dist/assets/installSkills/skills/config.md +3 -0
  5. package/dist/assets/installSkills/skills/content.md +3 -0
  6. package/dist/assets/installSkills/skills/next_js.md +3 -0
  7. package/dist/assets/installSkills/skills/preact.md +3 -2
  8. package/dist/assets/installSkills/skills/react.md +3 -0
  9. package/dist/assets/installSkills/skills/solid.md +3 -0
  10. package/dist/assets/installSkills/skills/svelte.md +3 -0
  11. package/dist/assets/installSkills/skills/usage.md +2 -0
  12. package/dist/assets/installSkills/skills/vue.md +3 -0
  13. package/dist/cjs/_virtual/_rolldown/runtime.cjs +1 -29
  14. package/dist/cjs/_virtual/_utils_asset.cjs +2 -98
  15. package/dist/cjs/build.cjs +1 -51
  16. package/dist/cjs/buildIntlayerDictionary/buildIntlayerDictionary.cjs +1 -53
  17. package/dist/cjs/buildIntlayerDictionary/buildIntlayerDictionary.cjs.map +1 -1
  18. package/dist/cjs/buildIntlayerDictionary/index.cjs +1 -6
  19. package/dist/cjs/buildIntlayerDictionary/processContentDeclaration.cjs +1 -26
  20. package/dist/cjs/buildIntlayerDictionary/processContentDeclaration.cjs.map +1 -1
  21. package/dist/cjs/buildIntlayerDictionary/writeDynamicDictionary.cjs +4 -78
  22. package/dist/cjs/buildIntlayerDictionary/writeDynamicDictionary.cjs.map +1 -1
  23. package/dist/cjs/buildIntlayerDictionary/writeFetchDictionary.cjs +4 -62
  24. package/dist/cjs/buildIntlayerDictionary/writeFetchDictionary.cjs.map +1 -1
  25. package/dist/cjs/buildIntlayerDictionary/writeMergedDictionary.cjs +1 -46
  26. package/dist/cjs/buildIntlayerDictionary/writeMergedDictionary.cjs.map +1 -1
  27. package/dist/cjs/buildIntlayerDictionary/writeRemoteDictionary.cjs +1 -50
  28. package/dist/cjs/buildIntlayerDictionary/writeRemoteDictionary.cjs.map +1 -1
  29. package/dist/cjs/buildIntlayerDictionary/writeUnmergedDictionary.cjs +1 -59
  30. package/dist/cjs/buildIntlayerDictionary/writeUnmergedDictionary.cjs.map +1 -1
  31. package/dist/cjs/cleanOutputDir.cjs +1 -30
  32. package/dist/cjs/cleanOutputDir.cjs.map +1 -1
  33. package/dist/cjs/cleanRemovedContentDeclaration.cjs +1 -96
  34. package/dist/cjs/cleanRemovedContentDeclaration.cjs.map +1 -1
  35. package/dist/cjs/cli.cjs +1 -21
  36. package/dist/cjs/createDictionaryEntryPoint/createDictionaryEntryPoint.cjs +1 -70
  37. package/dist/cjs/createDictionaryEntryPoint/createDictionaryEntryPoint.cjs.map +1 -1
  38. package/dist/cjs/createDictionaryEntryPoint/generateDictionaryListContent.cjs +7 -41
  39. package/dist/cjs/createDictionaryEntryPoint/generateDictionaryListContent.cjs.map +1 -1
  40. package/dist/cjs/createDictionaryEntryPoint/getBuiltDictionariesPath.cjs +1 -23
  41. package/dist/cjs/createDictionaryEntryPoint/getBuiltDictionariesPath.cjs.map +1 -1
  42. package/dist/cjs/createDictionaryEntryPoint/getBuiltDynamicDictionariesPath.cjs +1 -24
  43. package/dist/cjs/createDictionaryEntryPoint/getBuiltDynamicDictionariesPath.cjs.map +1 -1
  44. package/dist/cjs/createDictionaryEntryPoint/getBuiltFetchDictionariesPath.cjs +1 -24
  45. package/dist/cjs/createDictionaryEntryPoint/getBuiltFetchDictionariesPath.cjs.map +1 -1
  46. package/dist/cjs/createDictionaryEntryPoint/getBuiltRemoteDictionariesPath.cjs +1 -23
  47. package/dist/cjs/createDictionaryEntryPoint/getBuiltRemoteDictionariesPath.cjs.map +1 -1
  48. package/dist/cjs/createDictionaryEntryPoint/getBuiltUnmergedDictionariesPath.cjs +1 -23
  49. package/dist/cjs/createDictionaryEntryPoint/getBuiltUnmergedDictionariesPath.cjs.map +1 -1
  50. package/dist/cjs/createDictionaryEntryPoint/index.cjs +1 -16
  51. package/dist/cjs/createType/createModuleAugmentation.cjs +9 -110
  52. package/dist/cjs/createType/createModuleAugmentation.cjs.map +1 -1
  53. package/dist/cjs/createType/createType.cjs +1 -29
  54. package/dist/cjs/createType/createType.cjs.map +1 -1
  55. package/dist/cjs/createType/index.cjs +1 -8
  56. package/dist/cjs/fetchDistantDictionaries.cjs +1 -50
  57. package/dist/cjs/fetchDistantDictionaries.cjs.map +1 -1
  58. package/dist/cjs/filterInvalidDictionaries.cjs +1 -49
  59. package/dist/cjs/filterInvalidDictionaries.cjs.map +1 -1
  60. package/dist/cjs/formatDictionary.cjs +1 -49
  61. package/dist/cjs/formatDictionary.cjs.map +1 -1
  62. package/dist/cjs/getContentDeclarationFileTemplate/getContentDeclarationFileTemplate.cjs +1 -39
  63. package/dist/cjs/getContentDeclarationFileTemplate/getContentDeclarationFileTemplate.cjs.map +1 -1
  64. package/dist/cjs/handleAdditionalContentDeclarationFile.cjs +1 -34
  65. package/dist/cjs/handleAdditionalContentDeclarationFile.cjs.map +1 -1
  66. package/dist/cjs/handleContentDeclarationFileChange.cjs +1 -49
  67. package/dist/cjs/handleContentDeclarationFileChange.cjs.map +1 -1
  68. package/dist/cjs/handleContentDeclarationFileMoved.cjs +1 -24
  69. package/dist/cjs/handleContentDeclarationFileMoved.cjs.map +1 -1
  70. package/dist/cjs/handleUnlinkedContentDeclarationFile.cjs +1 -37
  71. package/dist/cjs/handleUnlinkedContentDeclarationFile.cjs.map +1 -1
  72. package/dist/cjs/index.cjs +1 -131
  73. package/dist/cjs/init/index.cjs +1 -242
  74. package/dist/cjs/init/index.cjs.map +1 -1
  75. package/dist/cjs/init/utils/configManipulation.cjs +1 -164
  76. package/dist/cjs/init/utils/configManipulation.cjs.map +1 -1
  77. package/dist/cjs/init/utils/fileSystem.cjs +1 -30
  78. package/dist/cjs/init/utils/fileSystem.cjs.map +1 -1
  79. package/dist/cjs/init/utils/index.cjs +1 -13
  80. package/dist/cjs/init/utils/jsonParser.cjs +3 -42
  81. package/dist/cjs/init/utils/jsonParser.cjs.map +1 -1
  82. package/dist/cjs/init/utils/tsConfig.cjs +1 -18
  83. package/dist/cjs/init/utils/tsConfig.cjs.map +1 -1
  84. package/dist/cjs/initConfig/index.cjs +1 -38
  85. package/dist/cjs/initConfig/index.cjs.map +1 -1
  86. package/dist/cjs/installSkills/index.cjs +1 -172
  87. package/dist/cjs/installSkills/index.cjs.map +1 -1
  88. package/dist/cjs/listDictionariesPath.cjs +1 -41
  89. package/dist/cjs/listDictionariesPath.cjs.map +1 -1
  90. package/dist/cjs/listGitFiles.cjs +5 -95
  91. package/dist/cjs/listGitFiles.cjs.map +1 -1
  92. package/dist/cjs/listProjects.cjs +1 -47
  93. package/dist/cjs/listProjects.cjs.map +1 -1
  94. package/dist/cjs/loadDictionaries/getIntlayerBundle.cjs +1 -66
  95. package/dist/cjs/loadDictionaries/getIntlayerBundle.cjs.map +1 -1
  96. package/dist/cjs/loadDictionaries/index.cjs +1 -12
  97. package/dist/cjs/loadDictionaries/loadContentDeclaration.cjs +2 -72
  98. package/dist/cjs/loadDictionaries/loadContentDeclaration.cjs.map +1 -1
  99. package/dist/cjs/loadDictionaries/loadDictionaries.cjs +1 -125
  100. package/dist/cjs/loadDictionaries/loadDictionaries.cjs.map +1 -1
  101. package/dist/cjs/loadDictionaries/loadLocalDictionaries.cjs +1 -28
  102. package/dist/cjs/loadDictionaries/loadLocalDictionaries.cjs.map +1 -1
  103. package/dist/cjs/loadDictionaries/loadRemoteDictionaries.cjs +1 -61
  104. package/dist/cjs/loadDictionaries/loadRemoteDictionaries.cjs.map +1 -1
  105. package/dist/cjs/loadDictionaries/log.cjs +3 -135
  106. package/dist/cjs/loadDictionaries/log.cjs.map +1 -1
  107. package/dist/cjs/prepareIntlayer.cjs +1 -95
  108. package/dist/cjs/prepareIntlayer.cjs.map +1 -1
  109. package/dist/cjs/reduceDictionaryContent/applyMask.cjs +1 -30
  110. package/dist/cjs/reduceDictionaryContent/applyMask.cjs.map +1 -1
  111. package/dist/cjs/reduceDictionaryContent/reduceDictionaryContent.cjs +1 -12
  112. package/dist/cjs/reduceDictionaryContent/reduceDictionaryContent.cjs.map +1 -1
  113. package/dist/cjs/transformFiles/extractDictionaryKey.cjs +1 -16
  114. package/dist/cjs/transformFiles/extractDictionaryKey.cjs.map +1 -1
  115. package/dist/cjs/transformFiles/index.cjs +1 -10
  116. package/dist/cjs/transformFiles/transformFiles.cjs +1 -251
  117. package/dist/cjs/transformFiles/transformFiles.cjs.map +1 -1
  118. package/dist/cjs/utils/autoDecorateContent.cjs +1 -87
  119. package/dist/cjs/utils/autoDecorateContent.cjs.map +1 -1
  120. package/dist/cjs/utils/buildFilesList.cjs +1 -50
  121. package/dist/cjs/utils/buildFilesList.cjs.map +1 -1
  122. package/dist/cjs/utils/chunkJSON.cjs +1 -336
  123. package/dist/cjs/utils/chunkJSON.cjs.map +1 -1
  124. package/dist/cjs/utils/formatter.cjs +1 -16
  125. package/dist/cjs/utils/formatter.cjs.map +1 -1
  126. package/dist/cjs/utils/getChunk.cjs +1 -34
  127. package/dist/cjs/utils/getChunk.cjs.map +1 -1
  128. package/dist/cjs/utils/getComponentTransformPattern.cjs +1 -48
  129. package/dist/cjs/utils/getComponentTransformPattern.cjs.map +1 -1
  130. package/dist/cjs/utils/getFileHash.cjs +1 -12
  131. package/dist/cjs/utils/getFileHash.cjs.map +1 -1
  132. package/dist/cjs/utils/getFormatFromExtension.cjs +1 -31
  133. package/dist/cjs/utils/getFormatFromExtension.cjs.map +1 -1
  134. package/dist/cjs/utils/index.cjs +1 -46
  135. package/dist/cjs/utils/pLimit.cjs +1 -128
  136. package/dist/cjs/utils/pLimit.cjs.map +1 -1
  137. package/dist/cjs/utils/parallelize.cjs +1 -11
  138. package/dist/cjs/utils/parallelize.cjs.map +1 -1
  139. package/dist/cjs/utils/parallelizeGlobal.cjs +1 -29
  140. package/dist/cjs/utils/parallelizeGlobal.cjs.map +1 -1
  141. package/dist/cjs/utils/reduceObjectFormat.cjs +1 -31
  142. package/dist/cjs/utils/reduceObjectFormat.cjs.map +1 -1
  143. package/dist/cjs/utils/resolveObjectPromises.cjs +1 -21
  144. package/dist/cjs/utils/resolveObjectPromises.cjs.map +1 -1
  145. package/dist/cjs/utils/runOnce.cjs +1 -95
  146. package/dist/cjs/utils/runOnce.cjs.map +1 -1
  147. package/dist/cjs/utils/runParallel/bin.cjs +1 -54
  148. package/dist/cjs/utils/runParallel/bin.cjs.map +1 -1
  149. package/dist/cjs/utils/runParallel/bootstrap.cjs +1 -24
  150. package/dist/cjs/utils/runParallel/bootstrap.cjs.map +1 -1
  151. package/dist/cjs/utils/runParallel/index.cjs +1 -110
  152. package/dist/cjs/utils/runParallel/index.cjs.map +1 -1
  153. package/dist/cjs/utils/runParallel/pidTree.cjs +1 -145
  154. package/dist/cjs/utils/runParallel/pidTree.cjs.map +1 -1
  155. package/dist/cjs/utils/runParallel/ps.cjs +1 -49
  156. package/dist/cjs/utils/runParallel/ps.cjs.map +1 -1
  157. package/dist/cjs/utils/runParallel/runTask.cjs +1 -154
  158. package/dist/cjs/utils/runParallel/runTask.cjs.map +1 -1
  159. package/dist/cjs/utils/runParallel/spawnPosix.cjs +1 -59
  160. package/dist/cjs/utils/runParallel/spawnPosix.cjs.map +1 -1
  161. package/dist/cjs/utils/runParallel/spawnWin32.cjs +1 -43
  162. package/dist/cjs/utils/runParallel/spawnWin32.cjs.map +1 -1
  163. package/dist/cjs/utils/runParallel/wmic.cjs +1 -52
  164. package/dist/cjs/utils/runParallel/wmic.cjs.map +1 -1
  165. package/dist/cjs/utils/sortAlphabetically.cjs +1 -7
  166. package/dist/cjs/utils/sortAlphabetically.cjs.map +1 -1
  167. package/dist/cjs/utils/splitTextByLine.cjs +1 -11
  168. package/dist/cjs/utils/splitTextByLine.cjs.map +1 -1
  169. package/dist/cjs/utils/verifyIdenticObjectFormat.cjs +1 -60
  170. package/dist/cjs/utils/verifyIdenticObjectFormat.cjs.map +1 -1
  171. package/dist/cjs/watcher.cjs +1 -95
  172. package/dist/cjs/watcher.cjs.map +1 -1
  173. package/dist/cjs/writeConfiguration/index.cjs +1 -34
  174. package/dist/cjs/writeConfiguration/index.cjs.map +1 -1
  175. package/dist/cjs/writeContentDeclaration/detectExportedComponentName.cjs +1 -28
  176. package/dist/cjs/writeContentDeclaration/detectExportedComponentName.cjs.map +1 -1
  177. package/dist/cjs/writeContentDeclaration/detectFormatCommand.cjs +1 -31
  178. package/dist/cjs/writeContentDeclaration/detectFormatCommand.cjs.map +1 -1
  179. package/dist/cjs/writeContentDeclaration/index.cjs +1 -12
  180. package/dist/cjs/writeContentDeclaration/processContentDeclarationContent.cjs +1 -93
  181. package/dist/cjs/writeContentDeclaration/processContentDeclarationContent.cjs.map +1 -1
  182. package/dist/cjs/writeContentDeclaration/transformJSFile.cjs +2 -1775
  183. package/dist/cjs/writeContentDeclaration/transformJSFile.cjs.map +1 -1
  184. package/dist/cjs/writeContentDeclaration/transformJSONFile.cjs +1 -77
  185. package/dist/cjs/writeContentDeclaration/transformJSONFile.cjs.map +1 -1
  186. package/dist/cjs/writeContentDeclaration/writeContentDeclaration.cjs +1 -123
  187. package/dist/cjs/writeContentDeclaration/writeContentDeclaration.cjs.map +1 -1
  188. package/dist/cjs/writeContentDeclaration/writeJSFile.cjs +1 -71
  189. package/dist/cjs/writeContentDeclaration/writeJSFile.cjs.map +1 -1
  190. package/dist/cjs/writeFileIfChanged.cjs +1 -12
  191. package/dist/cjs/writeFileIfChanged.cjs.map +1 -1
  192. package/dist/cjs/writeJsonIfChanged.cjs +1 -17
  193. package/dist/cjs/writeJsonIfChanged.cjs.map +1 -1
  194. package/dist/esm/_virtual/_rolldown/runtime.mjs +1 -8
  195. package/dist/esm/_virtual/_utils_asset.mjs +2 -97
  196. package/dist/esm/build.mjs +1 -24
  197. package/dist/esm/buildIntlayerDictionary/buildIntlayerDictionary.mjs +1 -51
  198. package/dist/esm/buildIntlayerDictionary/buildIntlayerDictionary.mjs.map +1 -1
  199. package/dist/esm/buildIntlayerDictionary/index.mjs +1 -4
  200. package/dist/esm/buildIntlayerDictionary/processContentDeclaration.mjs +1 -24
  201. package/dist/esm/buildIntlayerDictionary/processContentDeclaration.mjs.map +1 -1
  202. package/dist/esm/buildIntlayerDictionary/writeDynamicDictionary.mjs +4 -75
  203. package/dist/esm/buildIntlayerDictionary/writeDynamicDictionary.mjs.map +1 -1
  204. package/dist/esm/buildIntlayerDictionary/writeFetchDictionary.mjs +4 -59
  205. package/dist/esm/buildIntlayerDictionary/writeFetchDictionary.mjs.map +1 -1
  206. package/dist/esm/buildIntlayerDictionary/writeMergedDictionary.mjs +1 -44
  207. package/dist/esm/buildIntlayerDictionary/writeMergedDictionary.mjs.map +1 -1
  208. package/dist/esm/buildIntlayerDictionary/writeRemoteDictionary.mjs +1 -48
  209. package/dist/esm/buildIntlayerDictionary/writeRemoteDictionary.mjs.map +1 -1
  210. package/dist/esm/buildIntlayerDictionary/writeUnmergedDictionary.mjs +1 -56
  211. package/dist/esm/buildIntlayerDictionary/writeUnmergedDictionary.mjs.map +1 -1
  212. package/dist/esm/cleanOutputDir.mjs +1 -28
  213. package/dist/esm/cleanOutputDir.mjs.map +1 -1
  214. package/dist/esm/cleanRemovedContentDeclaration.mjs +1 -93
  215. package/dist/esm/cleanRemovedContentDeclaration.mjs.map +1 -1
  216. package/dist/esm/cli.mjs +1 -8
  217. package/dist/esm/createDictionaryEntryPoint/createDictionaryEntryPoint.mjs +1 -68
  218. package/dist/esm/createDictionaryEntryPoint/createDictionaryEntryPoint.mjs.map +1 -1
  219. package/dist/esm/createDictionaryEntryPoint/generateDictionaryListContent.mjs +7 -39
  220. package/dist/esm/createDictionaryEntryPoint/generateDictionaryListContent.mjs.map +1 -1
  221. package/dist/esm/createDictionaryEntryPoint/getBuiltDictionariesPath.mjs +1 -20
  222. package/dist/esm/createDictionaryEntryPoint/getBuiltDictionariesPath.mjs.map +1 -1
  223. package/dist/esm/createDictionaryEntryPoint/getBuiltDynamicDictionariesPath.mjs +1 -21
  224. package/dist/esm/createDictionaryEntryPoint/getBuiltDynamicDictionariesPath.mjs.map +1 -1
  225. package/dist/esm/createDictionaryEntryPoint/getBuiltFetchDictionariesPath.mjs +1 -21
  226. package/dist/esm/createDictionaryEntryPoint/getBuiltFetchDictionariesPath.mjs.map +1 -1
  227. package/dist/esm/createDictionaryEntryPoint/getBuiltRemoteDictionariesPath.mjs +1 -20
  228. package/dist/esm/createDictionaryEntryPoint/getBuiltRemoteDictionariesPath.mjs.map +1 -1
  229. package/dist/esm/createDictionaryEntryPoint/getBuiltUnmergedDictionariesPath.mjs +1 -20
  230. package/dist/esm/createDictionaryEntryPoint/getBuiltUnmergedDictionariesPath.mjs.map +1 -1
  231. package/dist/esm/createDictionaryEntryPoint/index.mjs +1 -9
  232. package/dist/esm/createType/createModuleAugmentation.mjs +9 -106
  233. package/dist/esm/createType/createModuleAugmentation.mjs.map +1 -1
  234. package/dist/esm/createType/createType.mjs +1 -26
  235. package/dist/esm/createType/createType.mjs.map +1 -1
  236. package/dist/esm/createType/index.mjs +1 -4
  237. package/dist/esm/fetchDistantDictionaries.mjs +1 -48
  238. package/dist/esm/fetchDistantDictionaries.mjs.map +1 -1
  239. package/dist/esm/filterInvalidDictionaries.mjs +1 -46
  240. package/dist/esm/filterInvalidDictionaries.mjs.map +1 -1
  241. package/dist/esm/formatDictionary.mjs +1 -44
  242. package/dist/esm/formatDictionary.mjs.map +1 -1
  243. package/dist/esm/getContentDeclarationFileTemplate/getContentDeclarationFileTemplate.mjs +1 -37
  244. package/dist/esm/getContentDeclarationFileTemplate/getContentDeclarationFileTemplate.mjs.map +1 -1
  245. package/dist/esm/handleAdditionalContentDeclarationFile.mjs +1 -32
  246. package/dist/esm/handleAdditionalContentDeclarationFile.mjs.map +1 -1
  247. package/dist/esm/handleContentDeclarationFileChange.mjs +1 -47
  248. package/dist/esm/handleContentDeclarationFileChange.mjs.map +1 -1
  249. package/dist/esm/handleContentDeclarationFileMoved.mjs +1 -22
  250. package/dist/esm/handleContentDeclarationFileMoved.mjs.map +1 -1
  251. package/dist/esm/handleUnlinkedContentDeclarationFile.mjs +1 -35
  252. package/dist/esm/handleUnlinkedContentDeclarationFile.mjs.map +1 -1
  253. package/dist/esm/index.mjs +1 -57
  254. package/dist/esm/init/index.mjs +1 -240
  255. package/dist/esm/init/index.mjs.map +1 -1
  256. package/dist/esm/init/utils/configManipulation.mjs +1 -161
  257. package/dist/esm/init/utils/configManipulation.mjs.map +1 -1
  258. package/dist/esm/init/utils/fileSystem.mjs +1 -26
  259. package/dist/esm/init/utils/fileSystem.mjs.map +1 -1
  260. package/dist/esm/init/utils/index.mjs +1 -6
  261. package/dist/esm/init/utils/jsonParser.mjs +3 -40
  262. package/dist/esm/init/utils/jsonParser.mjs.map +1 -1
  263. package/dist/esm/init/utils/tsConfig.mjs +1 -16
  264. package/dist/esm/init/utils/tsConfig.mjs.map +1 -1
  265. package/dist/esm/initConfig/index.mjs +1 -36
  266. package/dist/esm/initConfig/index.mjs.map +1 -1
  267. package/dist/esm/installSkills/index.mjs +1 -167
  268. package/dist/esm/installSkills/index.mjs.map +1 -1
  269. package/dist/esm/listDictionariesPath.mjs +1 -37
  270. package/dist/esm/listDictionariesPath.mjs.map +1 -1
  271. package/dist/esm/listGitFiles.mjs +5 -90
  272. package/dist/esm/listGitFiles.mjs.map +1 -1
  273. package/dist/esm/listProjects.mjs +1 -43
  274. package/dist/esm/listProjects.mjs.map +1 -1
  275. package/dist/esm/loadDictionaries/getIntlayerBundle.mjs +1 -65
  276. package/dist/esm/loadDictionaries/getIntlayerBundle.mjs.map +1 -1
  277. package/dist/esm/loadDictionaries/index.mjs +1 -6
  278. package/dist/esm/loadDictionaries/loadContentDeclaration.mjs +2 -69
  279. package/dist/esm/loadDictionaries/loadContentDeclaration.mjs.map +1 -1
  280. package/dist/esm/loadDictionaries/loadDictionaries.mjs +1 -123
  281. package/dist/esm/loadDictionaries/loadDictionaries.mjs.map +1 -1
  282. package/dist/esm/loadDictionaries/loadLocalDictionaries.mjs +1 -26
  283. package/dist/esm/loadDictionaries/loadLocalDictionaries.mjs.map +1 -1
  284. package/dist/esm/loadDictionaries/loadRemoteDictionaries.mjs +1 -58
  285. package/dist/esm/loadDictionaries/loadRemoteDictionaries.mjs.map +1 -1
  286. package/dist/esm/loadDictionaries/log.mjs +3 -132
  287. package/dist/esm/loadDictionaries/log.mjs.map +1 -1
  288. package/dist/esm/prepareIntlayer.mjs +1 -92
  289. package/dist/esm/prepareIntlayer.mjs.map +1 -1
  290. package/dist/esm/reduceDictionaryContent/applyMask.mjs +1 -28
  291. package/dist/esm/reduceDictionaryContent/applyMask.mjs.map +1 -1
  292. package/dist/esm/reduceDictionaryContent/reduceDictionaryContent.mjs +1 -10
  293. package/dist/esm/reduceDictionaryContent/reduceDictionaryContent.mjs.map +1 -1
  294. package/dist/esm/transformFiles/extractDictionaryKey.mjs +1 -14
  295. package/dist/esm/transformFiles/extractDictionaryKey.mjs.map +1 -1
  296. package/dist/esm/transformFiles/index.mjs +1 -4
  297. package/dist/esm/transformFiles/transformFiles.mjs +1 -244
  298. package/dist/esm/transformFiles/transformFiles.mjs.map +1 -1
  299. package/dist/esm/utils/autoDecorateContent.mjs +1 -85
  300. package/dist/esm/utils/autoDecorateContent.mjs.map +1 -1
  301. package/dist/esm/utils/buildFilesList.mjs +1 -47
  302. package/dist/esm/utils/buildFilesList.mjs.map +1 -1
  303. package/dist/esm/utils/chunkJSON.mjs +1 -333
  304. package/dist/esm/utils/chunkJSON.mjs.map +1 -1
  305. package/dist/esm/utils/formatter.mjs +1 -12
  306. package/dist/esm/utils/formatter.mjs.map +1 -1
  307. package/dist/esm/utils/getChunk.mjs +1 -33
  308. package/dist/esm/utils/getChunk.mjs.map +1 -1
  309. package/dist/esm/utils/getComponentTransformPattern.mjs +1 -44
  310. package/dist/esm/utils/getComponentTransformPattern.mjs.map +1 -1
  311. package/dist/esm/utils/getFileHash.mjs +1 -9
  312. package/dist/esm/utils/getFileHash.mjs.map +1 -1
  313. package/dist/esm/utils/getFormatFromExtension.mjs +1 -28
  314. package/dist/esm/utils/getFormatFromExtension.mjs.map +1 -1
  315. package/dist/esm/utils/index.mjs +1 -20
  316. package/dist/esm/utils/pLimit.mjs +1 -125
  317. package/dist/esm/utils/pLimit.mjs.map +1 -1
  318. package/dist/esm/utils/parallelize.mjs +1 -10
  319. package/dist/esm/utils/parallelize.mjs.map +1 -1
  320. package/dist/esm/utils/parallelizeGlobal.mjs +1 -26
  321. package/dist/esm/utils/parallelizeGlobal.mjs.map +1 -1
  322. package/dist/esm/utils/reduceObjectFormat.mjs +1 -29
  323. package/dist/esm/utils/reduceObjectFormat.mjs.map +1 -1
  324. package/dist/esm/utils/resolveObjectPromises.mjs +1 -19
  325. package/dist/esm/utils/resolveObjectPromises.mjs.map +1 -1
  326. package/dist/esm/utils/runOnce.mjs +1 -92
  327. package/dist/esm/utils/runOnce.mjs.map +1 -1
  328. package/dist/esm/utils/runParallel/bin.mjs +1 -52
  329. package/dist/esm/utils/runParallel/bin.mjs.map +1 -1
  330. package/dist/esm/utils/runParallel/bootstrap.mjs +1 -22
  331. package/dist/esm/utils/runParallel/bootstrap.mjs.map +1 -1
  332. package/dist/esm/utils/runParallel/index.mjs +1 -108
  333. package/dist/esm/utils/runParallel/index.mjs.map +1 -1
  334. package/dist/esm/utils/runParallel/pidTree.mjs +1 -142
  335. package/dist/esm/utils/runParallel/pidTree.mjs.map +1 -1
  336. package/dist/esm/utils/runParallel/ps.mjs +1 -46
  337. package/dist/esm/utils/runParallel/ps.mjs.map +1 -1
  338. package/dist/esm/utils/runParallel/runTask.mjs +1 -152
  339. package/dist/esm/utils/runParallel/runTask.mjs.map +1 -1
  340. package/dist/esm/utils/runParallel/spawnPosix.mjs +1 -57
  341. package/dist/esm/utils/runParallel/spawnPosix.mjs.map +1 -1
  342. package/dist/esm/utils/runParallel/spawnWin32.mjs +1 -41
  343. package/dist/esm/utils/runParallel/spawnWin32.mjs.map +1 -1
  344. package/dist/esm/utils/runParallel/wmic.mjs +1 -49
  345. package/dist/esm/utils/runParallel/wmic.mjs.map +1 -1
  346. package/dist/esm/utils/sortAlphabetically.mjs +1 -5
  347. package/dist/esm/utils/sortAlphabetically.mjs.map +1 -1
  348. package/dist/esm/utils/splitTextByLine.mjs +1 -9
  349. package/dist/esm/utils/splitTextByLine.mjs.map +1 -1
  350. package/dist/esm/utils/verifyIdenticObjectFormat.mjs +1 -58
  351. package/dist/esm/utils/verifyIdenticObjectFormat.mjs.map +1 -1
  352. package/dist/esm/watcher.mjs +1 -92
  353. package/dist/esm/watcher.mjs.map +1 -1
  354. package/dist/esm/writeConfiguration/index.mjs +1 -31
  355. package/dist/esm/writeConfiguration/index.mjs.map +1 -1
  356. package/dist/esm/writeContentDeclaration/detectExportedComponentName.mjs +1 -26
  357. package/dist/esm/writeContentDeclaration/detectExportedComponentName.mjs.map +1 -1
  358. package/dist/esm/writeContentDeclaration/detectFormatCommand.mjs +1 -29
  359. package/dist/esm/writeContentDeclaration/detectFormatCommand.mjs.map +1 -1
  360. package/dist/esm/writeContentDeclaration/index.mjs +1 -7
  361. package/dist/esm/writeContentDeclaration/processContentDeclarationContent.mjs +1 -91
  362. package/dist/esm/writeContentDeclaration/processContentDeclarationContent.mjs.map +1 -1
  363. package/dist/esm/writeContentDeclaration/transformJSFile.mjs +2 -1773
  364. package/dist/esm/writeContentDeclaration/transformJSFile.mjs.map +1 -1
  365. package/dist/esm/writeContentDeclaration/transformJSONFile.mjs +1 -75
  366. package/dist/esm/writeContentDeclaration/transformJSONFile.mjs.map +1 -1
  367. package/dist/esm/writeContentDeclaration/writeContentDeclaration.mjs +1 -121
  368. package/dist/esm/writeContentDeclaration/writeContentDeclaration.mjs.map +1 -1
  369. package/dist/esm/writeContentDeclaration/writeJSFile.mjs +1 -69
  370. package/dist/esm/writeContentDeclaration/writeJSFile.mjs.map +1 -1
  371. package/dist/esm/writeFileIfChanged.mjs +1 -10
  372. package/dist/esm/writeFileIfChanged.mjs.map +1 -1
  373. package/dist/esm/writeJsonIfChanged.mjs +1 -16
  374. package/dist/esm/writeJsonIfChanged.mjs.map +1 -1
  375. package/dist/types/build.d.ts +3 -1
  376. package/dist/types/buildIntlayerDictionary/buildIntlayerDictionary.d.ts.map +1 -1
  377. package/dist/types/buildIntlayerDictionary/writeDynamicDictionary.d.ts.map +1 -1
  378. package/dist/types/buildIntlayerDictionary/writeFetchDictionary.d.ts.map +1 -1
  379. package/dist/types/buildIntlayerDictionary/writeMergedDictionary.d.ts.map +1 -1
  380. package/dist/types/buildIntlayerDictionary/writeRemoteDictionary.d.ts.map +1 -1
  381. package/dist/types/createDictionaryEntryPoint/generateDictionaryListContent.d.ts.map +1 -1
  382. package/dist/types/fetchDistantDictionaries.d.ts.map +1 -1
  383. package/dist/types/index.d.ts +16 -24
  384. package/dist/types/initConfig/index.d.ts +1 -1
  385. package/dist/types/initConfig/index.d.ts.map +1 -1
  386. package/dist/types/listGitFiles.d.ts.map +1 -1
  387. package/dist/types/loadDictionaries/getIntlayerBundle.d.ts.map +1 -1
  388. package/dist/types/loadDictionaries/loadContentDeclaration.d.ts.map +1 -1
  389. package/dist/types/prepareIntlayer.d.ts.map +1 -1
  390. package/dist/types/transformFiles/transformFiles.d.ts +1 -1
  391. package/dist/types/transformFiles/transformFiles.d.ts.map +1 -1
  392. package/dist/types/utils/formatter.d.ts +1 -1
  393. package/dist/types/watcher.d.ts +1 -1
  394. package/dist/types/writeContentDeclaration/processContentDeclarationContent.d.ts.map +1 -1
  395. package/dist/types/writeContentDeclaration/transformJSFile.d.ts.map +1 -1
  396. package/package.json +10 -15
@@ -1,1774 +1,3 @@
1
- import { getNodeType } from "@intlayer/core";
2
- import { NodeType } from "@intlayer/types";
3
- import { IndentationText, NewLineKind, Node, Project, QuoteKind, SyntaxKind, ts } from "ts-morph";
4
-
5
- //#region src/writeContentDeclaration/transformJSFile.ts
6
- /**
7
- * Builds a translation initializer string for the 't' function call.
8
- * Creates a properly formatted translation object with locale keys and values.
9
- *
10
- * @param translationMap - Map of locale codes to translation values
11
- * @param typeArgumentsText - Optional generic type arguments for the translation function
12
- * @returns Formatted string for the translation function call
13
- */
14
- const buildTranslationInitializer = (translationMap, typeArgumentsText) => {
15
- const translationEntries = Object.entries(translationMap).sort(([firstKey], [secondKey]) => firstKey.localeCompare(secondKey));
16
- const translationParts = [];
17
- for (const [localeCode, translationValue] of translationEntries) {
18
- const formattedLocaleKey = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(localeCode) ? localeCode : JSON.stringify(localeCode);
19
- if (typeof translationValue === "string") translationParts.push(`${formattedLocaleKey}: ${JSON.stringify(translationValue)}`);
20
- else if (Array.isArray(translationValue)) {
21
- const serializedArrayElements = translationValue.map((arrayElement) => JSON.stringify(arrayElement)).join(", ");
22
- translationParts.push(`${formattedLocaleKey}: [ ${serializedArrayElements} ]`);
23
- } else translationParts.push(`${formattedLocaleKey}: ${JSON.stringify(translationValue)}`);
24
- }
25
- return `t${typeArgumentsText ?? ""}({ ${translationParts.join(", ")} })`;
26
- };
27
- /**
28
- * Synchronizes numeric suffixes across locales to maintain consistency.
29
- * When updating a fallback locale's numeric suffix, this function updates
30
- * the corresponding numeric suffixes in other locales to match.
31
- *
32
- * This is useful for maintaining numbered lists across translations,
33
- * e.g., "Hello 1" / "Bonjour 1" when updating to "Hello 3".
34
- *
35
- * @param existingTranslationMap - Current translation map with locale values
36
- * @param fallbackLocaleCode - The locale being updated (fallback)
37
- * @param newFallbackValue - The new value for the fallback locale
38
- * @returns Updated translation map with synchronized numeric suffixes
39
- */
40
- const syncNumericSuffixAcrossLocales = (existingTranslationMap, fallbackLocaleCode, newFallbackValue) => {
41
- const updatedTranslationMap = {
42
- ...existingTranslationMap,
43
- [fallbackLocaleCode]: newFallbackValue
44
- };
45
- const trailingNumberMatch = newFallbackValue.match(/\d+(?!.*\d)/);
46
- if (!trailingNumberMatch) return updatedTranslationMap;
47
- const newTrailingNumber = trailingNumberMatch[0];
48
- for (const [localeCode, currentValue] of Object.entries(existingTranslationMap)) {
49
- if (localeCode === fallbackLocaleCode) continue;
50
- if (!currentValue.match(/\d+(?!.*\d)/)) continue;
51
- updatedTranslationMap[localeCode] = currentValue.replace(/(\d+)(?!.*\d)/, newTrailingNumber);
52
- }
53
- return updatedTranslationMap;
54
- };
55
- /**
56
- * Safely formats a key for use in object literals.
57
- * Handles special cases like reserved keywords and non-identifier keys.
58
- *
59
- * @param objectKey - The key to format
60
- * @returns Properly formatted key string
61
- */
62
- const stringifyKey = (objectKey) => {
63
- if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(objectKey)) return JSON.stringify(objectKey);
64
- if (objectKey === "true" || objectKey === "false") return JSON.stringify(objectKey);
65
- return objectKey;
66
- };
67
- /**
68
- * Builds an enumeration initializer string for the 'enu' function call.
69
- * Creates a properly formatted enumeration object with key-value pairs.
70
- *
71
- * @param enumerationMap - Map of enumeration keys to string values
72
- * @returns Formatted string for the enumeration function call, or empty string if invalid
73
- */
74
- const buildEnumerationInitializer = (enumerationMap) => {
75
- const enumerationParts = [];
76
- for (const [enumerationKey, enumerationValue] of Object.entries(enumerationMap)) {
77
- if (typeof enumerationValue !== "string") return "";
78
- enumerationParts.push(`${stringifyKey(enumerationKey)}: ${JSON.stringify(enumerationValue)}`);
79
- }
80
- return `enu({ ${enumerationParts.join(", ")} })`;
81
- };
82
- /**
83
- * Builds a condition initializer string for the 'cond' function call.
84
- * Creates a properly formatted condition object with key-value pairs.
85
- *
86
- * @param conditionMap - Map of condition keys to string values
87
- * @returns Formatted string for the condition function call, or empty string if invalid
88
- */
89
- const buildConditionInitializer = (conditionMap) => {
90
- const conditionParts = [];
91
- for (const [conditionKey, conditionValue] of Object.entries(conditionMap)) {
92
- if (typeof conditionValue !== "string") return "";
93
- conditionParts.push(`${stringifyKey(conditionKey)}: ${JSON.stringify(conditionValue)}`);
94
- }
95
- return `cond({ ${conditionParts.join(", ")} })`;
96
- };
97
- /**
98
- * Builds a gender initializer string for the 'gender' function call.
99
- * Creates a properly formatted gender object with key-value pairs.
100
- *
101
- * @param genderMap - Map of gender keys to string values
102
- * @returns Formatted string for the gender function call, or empty string if invalid
103
- */
104
- const buildGenderInitializer = (genderMap) => {
105
- const genderParts = [];
106
- for (const [genderKey, genderValue] of Object.entries(genderMap)) {
107
- if (typeof genderValue !== "string") return "";
108
- genderParts.push(`${stringifyKey(genderKey)}: ${JSON.stringify(genderValue)}`);
109
- }
110
- return `gender({ ${genderParts.join(", ")} })`;
111
- };
112
- /**
113
- * Builds an insertion initializer string for the 'insert' function call.
114
- * Handles both string content and translation content for insertions.
115
- *
116
- * @param insertionContent - The content to be inserted (string or translation)
117
- * @returns Formatted string for the insertion function call, or undefined if invalid
118
- */
119
- const buildInsertionInitializer = (insertionContent) => {
120
- if (typeof insertionContent === "string") return `insert(${JSON.stringify(insertionContent)})`;
121
- if (getNodeType(insertionContent) === NodeType.Translation) {
122
- const translationMap = insertionContent[NodeType.Translation] ?? {};
123
- if (!Object.values(translationMap).every((translationValue) => typeof translationValue === "string")) return void 0;
124
- return `insert(${buildTranslationInitializer(translationMap)})`;
125
- }
126
- };
127
- /**
128
- * Builds a file initializer string for the 'file' function call.
129
- * Creates a properly formatted file path reference.
130
- *
131
- * @param filePath - The file path to reference
132
- * @returns Formatted string for the file function call, or undefined if invalid
133
- */
134
- const buildFileInitializer = (filePath) => {
135
- if (typeof filePath === "string") return `file(${JSON.stringify(filePath)})`;
136
- };
137
- /**
138
- * Builds an html initializer string for the 'html' function call.
139
- * Handles both string content and translation content for html.
140
- *
141
- * @param htmlContent - The content to be rendered as html (string or translation)
142
- * @returns Formatted string for the html function call, or undefined if invalid
143
- */
144
- const buildHTMLInitializer = (htmlContent) => {
145
- if (typeof htmlContent === "string") return `html(${JSON.stringify(htmlContent)})`;
146
- if (getNodeType(htmlContent) === NodeType.Translation) {
147
- const translationMap = htmlContent[NodeType.Translation] ?? {};
148
- if (!Object.values(translationMap).every((translationValue) => typeof translationValue === "string")) return void 0;
149
- return `html(${buildTranslationInitializer(translationMap)})`;
150
- }
151
- };
152
- /**
153
- * Builds a markdown initializer string for the 'md' function call.
154
- * Handles string content, translation content, and file references for markdown.
155
- *
156
- * @param markdownContent - The markdown content (string, translation, or file reference)
157
- * @returns Formatted string for the markdown function call, or undefined if invalid
158
- */
159
- const buildMarkdownInitializer = (markdownContent) => {
160
- if (typeof markdownContent === "string") return `md(${JSON.stringify(markdownContent)})`;
161
- if (getNodeType(markdownContent) === NodeType.Translation) {
162
- const translationMap = markdownContent[NodeType.Translation] ?? {};
163
- if (!Object.values(translationMap).every((translationValue) => typeof translationValue === "string")) return void 0;
164
- return `md(${buildTranslationInitializer(translationMap)})`;
165
- }
166
- if (getNodeType(markdownContent) === NodeType.File) {
167
- const filePath = markdownContent[NodeType.File];
168
- const fileInitializer = buildFileInitializer(filePath);
169
- if (!fileInitializer) return void 0;
170
- return `md(${fileInitializer})`;
171
- }
172
- };
173
- /**
174
- * Builds a nested initializer string for the 'nest' function call.
175
- * Creates a properly formatted nested dictionary reference.
176
- *
177
- * @param nestedContent - The nested content with dictionary key and optional path
178
- * @returns Formatted string for the nested function call, or undefined if invalid
179
- */
180
- const buildNestedInitializer = (nestedContent) => {
181
- if (!nestedContent || typeof nestedContent.dictionaryKey !== "string") return void 0;
182
- if (nestedContent.path && typeof nestedContent.path === "string") return `nest(${JSON.stringify(nestedContent.dictionaryKey)}, ${JSON.stringify(nestedContent.path)})`;
183
- return `nest(${JSON.stringify(nestedContent.dictionaryKey)})`;
184
- };
185
- /**
186
- * Reads an existing translation map from a property in a content object.
187
- * Parses the 't' function call and extracts the translation key-value pairs.
188
- *
189
- * @param contentObject - The object containing the property
190
- * @param propertyName - The name of the property to read
191
- * @returns Translation map with locale keys and values, or undefined if not found
192
- */
193
- const readExistingTranslationMap = (contentObject, propertyName) => {
194
- const property = contentObject.getProperty(propertyName);
195
- if (!property || !Node.isPropertyAssignment(property)) return void 0;
196
- const propertyInitializer = property.getInitializer();
197
- if (!propertyInitializer) return void 0;
198
- if (!Node.isCallExpression(propertyInitializer)) return void 0;
199
- const callExpression = propertyInitializer.getExpression();
200
- if (!Node.isIdentifier(callExpression) || callExpression.getText() !== "t") return void 0;
201
- const translationArgument = propertyInitializer.getArguments()[0];
202
- if (!translationArgument || !Node.isObjectLiteralExpression(translationArgument)) return void 0;
203
- const translationMap = {};
204
- for (const propertyAssignment of translationArgument.getProperties()) {
205
- if (!Node.isPropertyAssignment(propertyAssignment)) continue;
206
- const cleanPropertyName = propertyAssignment.getNameNode().getText().replace(/^['"]|['"]$/g, "");
207
- const valueInitializer = propertyAssignment.getInitializer();
208
- if (valueInitializer && Node.isStringLiteral(valueInitializer)) translationMap[cleanPropertyName] = valueInitializer.getLiteralValue();
209
- else if (valueInitializer && Node.isArrayLiteralExpression(valueInitializer)) {
210
- const stringArray = [];
211
- for (const arrayElement of valueInitializer.getElements()) {
212
- if (!Node.isStringLiteral(arrayElement)) return void 0;
213
- stringArray.push(arrayElement.getLiteralValue());
214
- }
215
- translationMap[cleanPropertyName] = stringArray;
216
- } else return;
217
- }
218
- return translationMap;
219
- };
220
- /**
221
- * Reads an existing map from a function call (enu, cond, or gender).
222
- * Parses the function call and extracts the key-value pairs.
223
- *
224
- * @param contentObject - The object containing the property
225
- * @param propertyName - The name of the property to read
226
- * @param functionName - The name of the function to look for ('enu', 'cond', or 'gender')
227
- * @returns Map with keys and string values, or undefined if not found
228
- */
229
- const readExistingMapFromCall = (contentObject, propertyName, functionName) => {
230
- const property = contentObject.getProperty(propertyName);
231
- if (!property || !Node.isPropertyAssignment(property)) return void 0;
232
- const propertyInitializer = property.getInitializer();
233
- if (!propertyInitializer || !Node.isCallExpression(propertyInitializer)) return void 0;
234
- const callExpression = propertyInitializer.getExpression();
235
- if (!Node.isIdentifier(callExpression) || callExpression.getText() !== functionName) return void 0;
236
- const functionArgument = propertyInitializer.getArguments()[0];
237
- if (!functionArgument || !Node.isObjectLiteralExpression(functionArgument)) return void 0;
238
- const keyValueMap = {};
239
- for (const propertyAssignment of functionArgument.getProperties()) {
240
- if (!Node.isPropertyAssignment(propertyAssignment)) continue;
241
- const cleanPropertyName = propertyAssignment.getNameNode().getText().replace(/^['"]|['"]$/g, "");
242
- const valueInitializer = propertyAssignment.getInitializer();
243
- if (valueInitializer && Node.isStringLiteral(valueInitializer)) keyValueMap[cleanPropertyName] = valueInitializer.getLiteralValue();
244
- }
245
- return keyValueMap;
246
- };
247
- /**
248
- * Extracts generic type arguments text from a call expression.
249
- * Returns the type arguments as a string (e.g., "<string[]>").
250
- *
251
- * @param callExpression - The call expression to extract type arguments from
252
- * @returns Type arguments as a string, or undefined if none found
253
- */
254
- const getCallExpressionTypeArgsText = (callExpression) => {
255
- try {
256
- const typeArguments = callExpression.getTypeArguments();
257
- if (!typeArguments || typeArguments.length === 0) return void 0;
258
- return `<${typeArguments.map((typeArgument) => typeArgument.getText()).join(", ")}>`;
259
- } catch {
260
- return;
261
- }
262
- };
263
- /**
264
- * Reads existing type arguments used in a specific property call.
265
- * Supports both direct calls and nested calls (e.g., md(t<...>(...))).
266
- *
267
- * @param contentObject - The object containing the property
268
- * @param propertyName - The name of the property to read
269
- * @param functionName - The name of the function to look for
270
- * @returns Type arguments as a string, or undefined if not found
271
- */
272
- const readExistingTypeArgsForCall = (contentObject, propertyName, functionName) => {
273
- const property = contentObject.getProperty(propertyName);
274
- if (!property || !Node.isPropertyAssignment(property)) return void 0;
275
- const propertyInitializer = property.getInitializer();
276
- if (!propertyInitializer || !Node.isCallExpression(propertyInitializer)) return void 0;
277
- const callExpression = propertyInitializer.getExpression();
278
- if (Node.isIdentifier(callExpression) && callExpression.getText() === functionName) return getCallExpressionTypeArgsText(propertyInitializer);
279
- if (functionName === "t" && Node.isIdentifier(callExpression) && callExpression.getText() === "md") {
280
- const markdownArgument = propertyInitializer.getArguments()[0];
281
- if (markdownArgument && Node.isCallExpression(markdownArgument)) {
282
- const innerExpression = markdownArgument.getExpression();
283
- if (Node.isIdentifier(innerExpression) && innerExpression.getText() === "t") return getCallExpressionTypeArgsText(markdownArgument);
284
- }
285
- }
286
- };
287
- /**
288
- * Compares two string maps for equality.
289
- * Filters out non-string values from the first map before comparison.
290
- *
291
- * @param firstMap - First map to compare (may contain non-string values)
292
- * @param secondMap - Second map to compare (should contain only strings)
293
- * @returns True if the string values in both maps are equal
294
- */
295
- const areStringMapsEqual = (firstMap, secondMap) => {
296
- if (!secondMap) return false;
297
- const firstMapStringEntries = Object.entries(firstMap).filter(([, value]) => typeof value === "string");
298
- if (firstMapStringEntries.length !== Object.keys(firstMap).length) return false;
299
- if (firstMapStringEntries.length !== Object.keys(secondMap).length) return false;
300
- for (const [key, value] of firstMapStringEntries) {
301
- if (!(key in secondMap)) return false;
302
- if (secondMap[key] !== value) return false;
303
- }
304
- return true;
305
- };
306
- /**
307
- * Compares translation maps for equality.
308
- * Handles both string and array values in translations.
309
- *
310
- * @param desiredTranslationMap - The desired translation map
311
- * @param existingTranslationMap - The existing translation map to compare against
312
- * @returns True if both translation maps are equal
313
- */
314
- const areTranslationsEqual = (desiredTranslationMap, existingTranslationMap) => {
315
- if (!existingTranslationMap) return false;
316
- for (const [localeCode, desiredValue] of Object.entries(desiredTranslationMap)) {
317
- if (!(localeCode in existingTranslationMap)) return false;
318
- const existingValue = existingTranslationMap[localeCode];
319
- if (typeof desiredValue === "string") {
320
- if (typeof existingValue !== "string") return false;
321
- if (existingValue !== desiredValue) return false;
322
- } else if (Array.isArray(desiredValue)) {
323
- if (!Array.isArray(existingValue)) return false;
324
- if (existingValue.length !== desiredValue.length) return false;
325
- for (let arrayIndex = 0; arrayIndex < desiredValue.length; arrayIndex++) if (existingValue[arrayIndex] !== desiredValue[arrayIndex]) return false;
326
- } else return false;
327
- }
328
- return true;
329
- };
330
- /**
331
- * Gets existing property names from the content object.
332
- * Handles both regular property assignments and shorthand properties.
333
- *
334
- * @param contentObject - The object literal expression to extract property names from
335
- * @returns Set of existing property names
336
- */
337
- const getExistingPropertyNames = (contentObject) => {
338
- const existingPropertyNames = /* @__PURE__ */ new Set();
339
- for (const property of contentObject.getProperties()) {
340
- if (Node.isPropertyAssignment(property)) {
341
- const propertyName = property.getName();
342
- if (propertyName) existingPropertyNames.add(propertyName.replace(/^['"]|['"]$/g, ""));
343
- continue;
344
- }
345
- if (Node.isShorthandPropertyAssignment(property)) {
346
- const shorthandPropertyName = property.getNameNode().getText();
347
- if (shorthandPropertyName) existingPropertyNames.add(shorthandPropertyName);
348
- }
349
- }
350
- return existingPropertyNames;
351
- };
352
- /**
353
- * Processes array content entries.
354
- * Handles arrays of various content types including strings, objects, and complex content nodes.
355
- * Supports nested objects within arrays and maintains existing translation structures.
356
- *
357
- * @param contentObject - The object containing the array property
358
- * @param propertyKey - The key of the array property
359
- * @param arrayValue - The array of values to process
360
- * @param existingPropertyKeys - Set of existing property names in the content object
361
- * @param effectiveFallbackLocale - The fallback locale for translations
362
- * @param requiredImports - Set to track required imports
363
- * @param sourceFile - The source file being processed
364
- * @returns True if the content was modified
365
- */
366
- const processArrayContent = (contentObject, propertyKey, arrayValue, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile) => {
367
- if (!existingPropertyKeys.has(propertyKey)) {
368
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
369
- if (spreadTargetObject) return processArrayContent(spreadTargetObject, propertyKey, arrayValue, getExistingPropertyNames(spreadTargetObject), effectiveFallbackLocale, requiredImports, sourceFile);
370
- }
371
- const serializedArrayElements = [];
372
- let hasUnsupportedContent = false;
373
- let existingArrayElements;
374
- let existingArrayHasTranslation = false;
375
- let existingArrayTypeArguments;
376
- let arrayWasChanged = false;
377
- const existingProperty = contentObject.getProperty(propertyKey);
378
- if (existingProperty && Node.isPropertyAssignment(existingProperty)) {
379
- const propertyInitializer = existingProperty.getInitializer();
380
- let existingPropertyTypeArguments;
381
- const areAllDesiredValuesStrings = arrayValue.every((arrayElement) => typeof arrayElement === "string");
382
- if (propertyInitializer && Node.isCallExpression(propertyInitializer) && Node.isIdentifier(propertyInitializer.getExpression()) && propertyInitializer.getExpression().getText() === "t" && areAllDesiredValuesStrings) {
383
- existingPropertyTypeArguments = getCallExpressionTypeArgsText(propertyInitializer);
384
- const existingTranslationMap = readExistingTranslationMap(contentObject, propertyKey);
385
- if (existingTranslationMap) {
386
- const translationInitializerText = buildTranslationInitializer({
387
- ...existingTranslationMap,
388
- [effectiveFallbackLocale]: arrayValue
389
- }, existingPropertyTypeArguments);
390
- requiredImports.add("t");
391
- const property = contentObject.getProperty(propertyKey);
392
- if (property && Node.isPropertyAssignment(property)) {
393
- if (property.getInitializer()?.getText() !== translationInitializerText) {
394
- property.setInitializer(translationInitializerText);
395
- return true;
396
- }
397
- }
398
- return false;
399
- }
400
- }
401
- if (propertyInitializer && Node.isArrayLiteralExpression(propertyInitializer)) {
402
- existingArrayElements = propertyInitializer.getElements();
403
- existingArrayHasTranslation = propertyInitializer.getElements().some((arrayElement) => {
404
- if (!Node.isCallExpression(arrayElement)) return false;
405
- const callExpression = arrayElement.getExpression();
406
- return Node.isIdentifier(callExpression) && callExpression.getText() === "t";
407
- });
408
- if (existingArrayHasTranslation) {
409
- for (const arrayElement of existingArrayElements) if (Node.isCallExpression(arrayElement)) {
410
- const callExpression = arrayElement.getExpression();
411
- if (Node.isIdentifier(callExpression) && callExpression.getText() === "t") {
412
- existingArrayTypeArguments = getCallExpressionTypeArgsText(arrayElement);
413
- if (existingArrayTypeArguments) break;
414
- }
415
- }
416
- }
417
- }
418
- }
419
- for (let elementIndex = 0; elementIndex < arrayValue.length; elementIndex++) {
420
- const currentElement = arrayValue[elementIndex];
421
- if (currentElement === null || currentElement === void 0 || typeof currentElement === "string" || typeof currentElement === "number" || typeof currentElement === "boolean") {
422
- let serializedElementValue = serializeValue(currentElement);
423
- if (typeof currentElement === "string" && existingArrayElements && elementIndex < existingArrayElements.length) {
424
- const existingArrayElement = existingArrayElements[elementIndex];
425
- if (Node.isCallExpression(existingArrayElement)) {
426
- const callExpression = existingArrayElement.getExpression();
427
- if (Node.isIdentifier(callExpression) && callExpression.getText() === "t") {
428
- const translationArgument = existingArrayElement.getArguments()[0];
429
- if (translationArgument && Node.isObjectLiteralExpression(translationArgument)) {
430
- const translationMap = {};
431
- for (const propertyAssignment of translationArgument.getProperties()) {
432
- if (!Node.isPropertyAssignment(propertyAssignment)) continue;
433
- const cleanPropertyName = propertyAssignment.getNameNode().getText().replace(/^['"]|['"]$/g, "");
434
- const propertyValue = propertyAssignment.getInitializer();
435
- if (propertyValue && Node.isStringLiteral(propertyValue)) translationMap[cleanPropertyName] = propertyValue.getLiteralValue();
436
- }
437
- serializedElementValue = buildTranslationInitializer(syncNumericSuffixAcrossLocales(translationMap, effectiveFallbackLocale, currentElement), getCallExpressionTypeArgsText(existingArrayElement));
438
- requiredImports.add("t");
439
- }
440
- }
441
- }
442
- }
443
- if (typeof currentElement === "string" && existingArrayHasTranslation && serializedElementValue && serializedElementValue.startsWith("\"")) {
444
- serializedElementValue = buildTranslationInitializer({ [effectiveFallbackLocale]: currentElement }, existingArrayTypeArguments);
445
- requiredImports.add("t");
446
- }
447
- if (serializedElementValue === void 0) {
448
- hasUnsupportedContent = true;
449
- break;
450
- }
451
- serializedArrayElements.push(serializedElementValue);
452
- } else if (typeof currentElement === "object" && currentElement !== null) {
453
- if (existingArrayElements && elementIndex < existingArrayElements.length) {
454
- const existingArrayElement = existingArrayElements[elementIndex];
455
- if (Node.isObjectLiteralExpression(existingArrayElement)) {
456
- if (processContentEntries(existingArrayElement, currentElement, effectiveFallbackLocale, requiredImports, sourceFile)) arrayWasChanged = true;
457
- serializedArrayElements.push(existingArrayElement.getText());
458
- } else {
459
- const serializedElementValue = serializeValue(currentElement);
460
- if (serializedElementValue === void 0) {
461
- hasUnsupportedContent = true;
462
- break;
463
- }
464
- serializedArrayElements.push(serializedElementValue);
465
- }
466
- } else {
467
- const serializedElementValue = serializeValue(currentElement);
468
- if (serializedElementValue === void 0) {
469
- hasUnsupportedContent = true;
470
- break;
471
- }
472
- serializedArrayElements.push(serializedElementValue);
473
- }
474
- const elementNodeType = getNodeType(currentElement);
475
- if (elementNodeType === NodeType.Translation) requiredImports.add("t");
476
- else if (elementNodeType === NodeType.Enumeration) requiredImports.add("enu");
477
- else if (elementNodeType === NodeType.Condition) requiredImports.add("cond");
478
- else if (elementNodeType === NodeType.Gender) requiredImports.add("gender");
479
- else if (elementNodeType === NodeType.HTML) {
480
- requiredImports.add("html");
481
- const htmlContent = currentElement[NodeType.HTML];
482
- if (typeof htmlContent === "object" && htmlContent !== null && getNodeType(htmlContent) === NodeType.Translation) requiredImports.add("t");
483
- } else if (elementNodeType === NodeType.Insertion) {
484
- requiredImports.add("insert");
485
- const insertionContent = currentElement[NodeType.Insertion];
486
- if (typeof insertionContent === "object" && insertionContent !== null && getNodeType(insertionContent) === NodeType.Translation) requiredImports.add("t");
487
- } else if (elementNodeType === NodeType.Markdown) {
488
- requiredImports.add("md");
489
- const markdownContent = currentElement[NodeType.Markdown];
490
- if (typeof markdownContent === "object" && markdownContent !== null && getNodeType(markdownContent) === NodeType.File) requiredImports.add("file");
491
- } else if (elementNodeType === NodeType.File) requiredImports.add("file");
492
- else if (elementNodeType === NodeType.Nested) requiredImports.add("nest");
493
- } else {
494
- hasUnsupportedContent = true;
495
- break;
496
- }
497
- }
498
- if (hasUnsupportedContent) return false;
499
- if (arrayWasChanged) return true;
500
- const arrayInitializerText = `[ ${serializedArrayElements.join(", ")} ]`;
501
- if (!existingPropertyKeys.has(propertyKey)) {
502
- contentObject.addPropertyAssignment({
503
- name: propertyKey,
504
- initializer: arrayInitializerText
505
- });
506
- return true;
507
- }
508
- const property = contentObject.getProperty(propertyKey);
509
- if (property && Node.isPropertyAssignment(property)) {
510
- const existingSerializedArray = readExistingArraySerialized(contentObject, propertyKey);
511
- if (!(existingSerializedArray !== void 0 && existingSerializedArray.length === serializedArrayElements.length && existingSerializedArray.every((existingElement, elementIndex) => existingElement === serializedArrayElements[elementIndex]))) {
512
- property.setInitializer(arrayInitializerText);
513
- return true;
514
- }
515
- }
516
- return false;
517
- };
518
- /**
519
- * Processes primitive content entries (string, number, boolean, null).
520
- * Handles simple value types and updates existing translation maps when appropriate.
521
- *
522
- * @param contentObject - The object containing the property
523
- * @param propertyKey - The key of the property to process
524
- * @param primitiveValue - The primitive value to process
525
- * @param existingPropertyKeys - Set of existing property names
526
- * @param effectiveFallbackLocale - The fallback locale for translations
527
- * @param requiredImports - Set to track required imports
528
- * @param sourceFile - The source file being processed
529
- * @returns True if the content was modified
530
- */
531
- const processPrimitiveContent = (contentObject, propertyKey, primitiveValue, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile) => {
532
- if (typeof primitiveValue === "string" && existingPropertyKeys.has(propertyKey)) {
533
- const property = contentObject.getProperty(propertyKey);
534
- if (property && Node.isPropertyAssignment(property)) {
535
- const propertyInitializer = property.getInitializer();
536
- if (propertyInitializer && !Node.isStringLiteral(propertyInitializer) && !Node.isCallExpression(propertyInitializer)) {
537
- console.log(`Skipping update for key "${propertyKey}" because existing value is not a string literal`);
538
- return false;
539
- }
540
- }
541
- const existingTranslationMap = readExistingTranslationMap(contentObject, propertyKey);
542
- if (existingTranslationMap) {
543
- const translationInitializerText = buildTranslationInitializer({
544
- ...existingTranslationMap,
545
- [effectiveFallbackLocale]: primitiveValue
546
- }, readExistingTypeArgsForCall(contentObject, propertyKey, "t"));
547
- requiredImports.add("t");
548
- if (property && Node.isPropertyAssignment(property)) {
549
- property.setInitializer(translationInitializerText);
550
- return true;
551
- }
552
- return false;
553
- }
554
- }
555
- if (!existingPropertyKeys.has(propertyKey)) {
556
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
557
- if (spreadTargetObject) return processPrimitiveContent(spreadTargetObject, propertyKey, primitiveValue, getExistingPropertyNames(spreadTargetObject), effectiveFallbackLocale, requiredImports, sourceFile);
558
- contentObject.addPropertyAssignment({
559
- name: propertyKey,
560
- initializer: typeof primitiveValue === "string" ? JSON.stringify(primitiveValue) : String(primitiveValue)
561
- });
562
- return true;
563
- }
564
- const property = contentObject.getProperty(propertyKey);
565
- if (property && Node.isPropertyAssignment(property)) {
566
- const propertyInitializer = property.getInitializer();
567
- const isPrimitiveLiteral = propertyInitializer && (Node.isStringLiteral(propertyInitializer) || Node.isNumericLiteral(propertyInitializer) || propertyInitializer.getKind() === SyntaxKind.TrueKeyword || propertyInitializer.getKind() === SyntaxKind.FalseKeyword || Node.isNullLiteral(propertyInitializer) || Node.isCallExpression(propertyInitializer));
568
- if (propertyInitializer && !isPrimitiveLiteral) {
569
- console.log(`Skipping update for key "${propertyKey}" because existing value is not a primitive literal`);
570
- return false;
571
- }
572
- const currentInitializerText = propertyInitializer?.getText();
573
- const desiredInitializerText = typeof primitiveValue === "string" ? JSON.stringify(primitiveValue) : String(primitiveValue);
574
- if (currentInitializerText !== desiredInitializerText) {
575
- property.setInitializer(desiredInitializerText);
576
- return true;
577
- }
578
- }
579
- return false;
580
- };
581
- /**
582
- * Processes complex content entries (translation, enumeration, condition, etc.).
583
- * Routes content to the appropriate specialized processor based on node type.
584
- *
585
- * @param contentObject - The object containing the property
586
- * @param propertyKey - The key of the property to process
587
- * @param contentNode - The complex content node to process
588
- * @param existingPropertyKeys - Set of existing property names
589
- * @param effectiveFallbackLocale - The fallback locale for translations
590
- * @param requiredImports - Set to track required imports
591
- * @param sourceFile - The source file being processed
592
- * @returns True if the content was modified
593
- */
594
- const processComplexContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile) => {
595
- switch (getNodeType(contentNode)) {
596
- case NodeType.Translation: return processTranslationContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
597
- case NodeType.Enumeration: return processEnumerationContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
598
- case NodeType.Condition: return processConditionContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
599
- case NodeType.Gender: return processGenderContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
600
- case NodeType.Insertion: return processInsertionContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
601
- case NodeType.HTML: return processHTMLContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
602
- case NodeType.Markdown: return processMarkdownContent(contentObject, propertyKey, contentNode, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile);
603
- case NodeType.File: return processFileContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
604
- case NodeType.Nested: return processNestedContent(contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile);
605
- default: return false;
606
- }
607
- };
608
- /**
609
- * Processes translation content.
610
- * Handles translation objects with locale keys and string/array values.
611
- *
612
- * @param contentObject - The object containing the property
613
- * @param propertyKey - The key of the property to process
614
- * @param contentNode - The translation content node
615
- * @param existingPropertyKeys - Set of existing property names
616
- * @param requiredImports - Set to track required imports
617
- * @param sourceFile - The source file being processed
618
- * @returns True if the content was modified
619
- */
620
- const processTranslationContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
621
- const translationMap = contentNode[NodeType.Translation] ?? {};
622
- const areAllValuesStringsOrArrays = Object.values(translationMap).every((translationValue) => typeof translationValue === "string" || Array.isArray(translationValue));
623
- if (Object.values(translationMap).some((translationValue) => typeof translationValue === "object" && translationValue !== null && !Array.isArray(translationValue) && getNodeType(translationValue) !== NodeType.Text) && !areAllValuesStringsOrArrays) {
624
- if (!existingPropertyKeys.has(propertyKey)) {
625
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
626
- if (spreadTargetObject) return processTranslationContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
627
- }
628
- const translationParts = [];
629
- let hasUnsupportedValue = false;
630
- for (const [localeCode, translationValue] of Object.entries(translationMap)) {
631
- const formattedLocaleKey = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(localeCode) ? localeCode : JSON.stringify(localeCode);
632
- if (typeof translationValue === "object" && translationValue !== null && !Array.isArray(translationValue)) {
633
- const serializedValue = serializeValue(translationValue);
634
- if (serializedValue === void 0) {
635
- hasUnsupportedValue = true;
636
- break;
637
- }
638
- translationParts.push(`${formattedLocaleKey}: ${serializedValue}`);
639
- const nodeType = getNodeType(translationValue);
640
- if (nodeType === NodeType.Markdown) {
641
- requiredImports.add("md");
642
- const markdownContent = translationValue[NodeType.Markdown];
643
- if (typeof markdownContent === "object" && markdownContent !== null && getNodeType(markdownContent) === NodeType.File) requiredImports.add("file");
644
- } else if (nodeType === NodeType.File) requiredImports.add("file");
645
- else if (nodeType === NodeType.Insertion) requiredImports.add("insert");
646
- else if (nodeType === NodeType.Enumeration) requiredImports.add("enu");
647
- else if (nodeType === NodeType.Condition) requiredImports.add("cond");
648
- else if (nodeType === NodeType.Gender) requiredImports.add("gender");
649
- else if (nodeType === NodeType.Nested) requiredImports.add("nest");
650
- } else if (typeof translationValue === "string") translationParts.push(`${formattedLocaleKey}: ${JSON.stringify(translationValue)}`);
651
- else if (Array.isArray(translationValue)) {
652
- const serializedArrayElements = translationValue.map((arrayElement) => JSON.stringify(arrayElement)).join(", ");
653
- translationParts.push(`${formattedLocaleKey}: [ ${serializedArrayElements} ]`);
654
- }
655
- }
656
- if (hasUnsupportedValue) return false;
657
- const translationInitializerText = `t${readExistingTypeArgsForCall(contentObject, propertyKey, "t") ?? ""}({ ${translationParts.join(", ")} })`;
658
- if (!existingPropertyKeys.has(propertyKey)) {
659
- requiredImports.add("t");
660
- contentObject.addPropertyAssignment({
661
- name: propertyKey,
662
- initializer: translationInitializerText
663
- });
664
- return true;
665
- }
666
- const property = contentObject.getProperty(propertyKey);
667
- if (property && Node.isPropertyAssignment(property)) {
668
- if (property.getInitializer()?.getText() !== translationInitializerText) {
669
- requiredImports.add("t");
670
- property.setInitializer(translationInitializerText);
671
- return true;
672
- }
673
- }
674
- return false;
675
- }
676
- if (!areAllValuesStringsOrArrays) return false;
677
- if (!existingPropertyKeys.has(propertyKey)) {
678
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
679
- if (spreadTargetObject) return processTranslationContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
680
- }
681
- const translationParts = [];
682
- for (const [localeCode, translationValue] of Object.entries(translationMap)) {
683
- const formattedLocaleKey = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(localeCode) ? localeCode : JSON.stringify(localeCode);
684
- if (typeof translationValue === "string") translationParts.push(`${formattedLocaleKey}: ${JSON.stringify(translationValue)}`);
685
- else if (Array.isArray(translationValue)) {
686
- const serializedArrayElements = translationValue.map((arrayElement) => JSON.stringify(arrayElement)).join(", ");
687
- translationParts.push(`${formattedLocaleKey}: [ ${serializedArrayElements} ]`);
688
- }
689
- }
690
- const translationInitializerText = `t${readExistingTypeArgsForCall(contentObject, propertyKey, "t") ?? ""}({ ${translationParts.join(", ")} })`;
691
- if (!existingPropertyKeys.has(propertyKey)) {
692
- requiredImports.add("t");
693
- contentObject.addPropertyAssignment({
694
- name: propertyKey,
695
- initializer: translationInitializerText
696
- });
697
- return true;
698
- }
699
- if (!areTranslationsEqual(translationMap, readExistingTranslationMap(contentObject, propertyKey))) {
700
- requiredImports.add("t");
701
- const property = contentObject.getProperty(propertyKey);
702
- if (property && Node.isPropertyAssignment(property)) {
703
- property.setInitializer(translationInitializerText);
704
- return true;
705
- }
706
- }
707
- return false;
708
- };
709
- /**
710
- * Processes enumeration content.
711
- * Handles enumeration objects with key-value string pairs.
712
- *
713
- * @param contentObject - The object containing the property
714
- * @param propertyKey - The key of the property to process
715
- * @param contentNode - The enumeration content node
716
- * @param existingPropertyKeys - Set of existing property names
717
- * @param requiredImports - Set to track required imports
718
- * @param sourceFile - The source file being processed
719
- * @returns True if the content was modified
720
- */
721
- const processEnumerationContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
722
- const enumerationMap = contentNode[NodeType.Enumeration];
723
- if (!Object.values(enumerationMap).every((enumerationValue) => typeof enumerationValue === "string")) return false;
724
- const enumerationInitializerText = buildEnumerationInitializer(enumerationMap);
725
- if (!enumerationInitializerText) return false;
726
- if (!existingPropertyKeys.has(propertyKey)) {
727
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
728
- if (spreadTargetObject) return processEnumerationContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
729
- requiredImports.add("enu");
730
- contentObject.addPropertyAssignment({
731
- name: propertyKey,
732
- initializer: enumerationInitializerText
733
- });
734
- return true;
735
- }
736
- if (!areStringMapsEqual(enumerationMap, readExistingMapFromCall(contentObject, propertyKey, "enu"))) {
737
- requiredImports.add("enu");
738
- const property = contentObject.getProperty(propertyKey);
739
- if (property && Node.isPropertyAssignment(property)) {
740
- property.setInitializer(enumerationInitializerText);
741
- return true;
742
- }
743
- }
744
- return false;
745
- };
746
- /**
747
- * Processes condition content.
748
- * Handles condition objects with key-value string pairs.
749
- *
750
- * @param contentObject - The object containing the property
751
- * @param propertyKey - The key of the property to process
752
- * @param contentNode - The condition content node
753
- * @param existingPropertyKeys - Set of existing property names
754
- * @param requiredImports - Set to track required imports
755
- * @param sourceFile - The source file being processed
756
- * @returns True if the content was modified
757
- */
758
- const processConditionContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
759
- const conditionMap = contentNode[NodeType.Condition];
760
- if (Object.values(conditionMap).every((conditionValue) => typeof conditionValue === "string")) {
761
- const conditionInitializerText = buildConditionInitializer(conditionMap);
762
- if (!conditionInitializerText) return false;
763
- if (!existingPropertyKeys.has(propertyKey)) {
764
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
765
- if (spreadTargetObject) return processConditionContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
766
- requiredImports.add("cond");
767
- contentObject.addPropertyAssignment({
768
- name: propertyKey,
769
- initializer: conditionInitializerText
770
- });
771
- return true;
772
- }
773
- if (!areStringMapsEqual(conditionMap, readExistingMapFromCall(contentObject, propertyKey, "cond"))) {
774
- requiredImports.add("cond");
775
- const property = contentObject.getProperty(propertyKey);
776
- if (property && Node.isPropertyAssignment(property)) {
777
- property.setInitializer(conditionInitializerText);
778
- return true;
779
- }
780
- }
781
- return false;
782
- }
783
- if (!existingPropertyKeys.has(propertyKey)) {
784
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
785
- if (spreadTargetObject) return processConditionContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
786
- return false;
787
- }
788
- const property = contentObject.getProperty(propertyKey);
789
- if (!property || !Node.isPropertyAssignment(property)) return false;
790
- const propertyInitializer = property.getInitializer();
791
- if (!propertyInitializer || !Node.isCallExpression(propertyInitializer)) return false;
792
- const callExpression = propertyInitializer.getExpression();
793
- if (!Node.isIdentifier(callExpression) || callExpression.getText() !== "cond") return false;
794
- const condArgument = propertyInitializer.getArguments()[0];
795
- if (!condArgument || !Node.isObjectLiteralExpression(condArgument)) return false;
796
- requiredImports.add("cond");
797
- let hasModifications = false;
798
- for (const [conditionKey, conditionValue] of Object.entries(conditionMap)) {
799
- const nodeType = getNodeType(conditionValue);
800
- if (!nodeType) continue;
801
- let condProperty = condArgument.getProperty(conditionKey);
802
- if (!condProperty) condProperty = condArgument.getProperty(stringifyKey(conditionKey));
803
- if (!condProperty || !Node.isPropertyAssignment(condProperty)) continue;
804
- const condValueInitializer = condProperty.getInitializer();
805
- if (!condValueInitializer) continue;
806
- if (nodeType === NodeType.Translation) {
807
- if (!Node.isCallExpression(condValueInitializer)) continue;
808
- const tCallExpression = condValueInitializer.getExpression();
809
- if (!Node.isIdentifier(tCallExpression) || tCallExpression.getText() !== "t") continue;
810
- const tArgument = condValueInitializer.getArguments()[0];
811
- if (!tArgument || !Node.isObjectLiteralExpression(tArgument)) continue;
812
- const translationMap = conditionValue[NodeType.Translation];
813
- if (!translationMap || typeof translationMap !== "object") continue;
814
- const existingTranslationMap = {};
815
- for (const propertyAssignment of tArgument.getProperties()) {
816
- if (!Node.isPropertyAssignment(propertyAssignment)) continue;
817
- const cleanPropertyName = propertyAssignment.getNameNode().getText().replace(/^['"]|['"]$/g, "");
818
- const valueInitializer = propertyAssignment.getInitializer();
819
- if (valueInitializer && Node.isStringLiteral(valueInitializer)) existingTranslationMap[cleanPropertyName] = valueInitializer.getLiteralValue();
820
- else if (valueInitializer && Node.isArrayLiteralExpression(valueInitializer)) {
821
- const stringArray = [];
822
- for (const arrayElement of valueInitializer.getElements()) if (Node.isStringLiteral(arrayElement)) stringArray.push(arrayElement.getLiteralValue());
823
- existingTranslationMap[cleanPropertyName] = stringArray;
824
- }
825
- }
826
- if (!areTranslationsEqual(translationMap, existingTranslationMap)) {
827
- requiredImports.add("t");
828
- for (const [locale, localeValue] of Object.entries(translationMap)) {
829
- const isLocaleCodeValidIdentifier = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(locale);
830
- const formattedLocaleKey = isLocaleCodeValidIdentifier ? locale : JSON.stringify(locale);
831
- let existingProperty = tArgument.getProperty(locale);
832
- if (!existingProperty && !isLocaleCodeValidIdentifier) existingProperty = tArgument.getProperty(JSON.stringify(locale));
833
- if (existingProperty && Node.isPropertyAssignment(existingProperty)) {
834
- const currentValue = existingProperty.getInitializer();
835
- const newValue = Array.isArray(localeValue) ? `[${localeValue.map((v) => JSON.stringify(v)).join(", ")}]` : JSON.stringify(localeValue);
836
- if (currentValue?.getText() !== newValue) {
837
- existingProperty.setInitializer(newValue);
838
- hasModifications = true;
839
- }
840
- } else if (!existingProperty) {
841
- const newValue = Array.isArray(localeValue) ? `[${localeValue.map((v) => JSON.stringify(v)).join(", ")}]` : JSON.stringify(localeValue);
842
- tArgument.addPropertyAssignment({
843
- name: formattedLocaleKey,
844
- initializer: newValue
845
- });
846
- hasModifications = true;
847
- }
848
- }
849
- }
850
- }
851
- }
852
- return hasModifications;
853
- };
854
- /**
855
- * Processes gender content.
856
- * Handles gender objects with key-value string pairs.
857
- *
858
- * @param contentObject - The object containing the property
859
- * @param propertyKey - The key of the property to process
860
- * @param contentNode - The gender content node
861
- * @param existingPropertyKeys - Set of existing property names
862
- * @param requiredImports - Set to track required imports
863
- * @param sourceFile - The source file being processed
864
- * @returns True if the content was modified
865
- */
866
- const processGenderContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
867
- const genderMap = contentNode[NodeType.Gender];
868
- if (!Object.values(genderMap).every((genderValue) => typeof genderValue === "string")) return false;
869
- const genderInitializerText = buildGenderInitializer(genderMap);
870
- if (!genderInitializerText) return false;
871
- if (!existingPropertyKeys.has(propertyKey)) {
872
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
873
- if (spreadTargetObject) return processGenderContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
874
- requiredImports.add("gender");
875
- contentObject.addPropertyAssignment({
876
- name: propertyKey,
877
- initializer: genderInitializerText
878
- });
879
- return true;
880
- }
881
- if (!areStringMapsEqual(genderMap, readExistingMapFromCall(contentObject, propertyKey, "gender"))) {
882
- requiredImports.add("gender");
883
- const property = contentObject.getProperty(propertyKey);
884
- if (property && Node.isPropertyAssignment(property)) {
885
- property.setInitializer(genderInitializerText);
886
- return true;
887
- }
888
- }
889
- return false;
890
- };
891
- /**
892
- * Processes insertion content.
893
- * Handles insertion objects with string or translation content.
894
- *
895
- * @param contentObject - The object containing the property
896
- * @param propertyKey - The key of the property to process
897
- * @param contentNode - The insertion content node
898
- * @param existingPropertyKeys - Set of existing property names
899
- * @param requiredImports - Set to track required imports
900
- * @param sourceFile - The source file being processed
901
- * @returns True if the content was modified
902
- */
903
- const processInsertionContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
904
- const insertionContent = contentNode[NodeType.Insertion];
905
- const insertionInitializerText = buildInsertionInitializer(insertionContent);
906
- if (!insertionInitializerText) return false;
907
- if (!existingPropertyKeys.has(propertyKey)) {
908
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
909
- if (spreadTargetObject) return processInsertionContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
910
- requiredImports.add("insert");
911
- contentObject.addPropertyAssignment({
912
- name: propertyKey,
913
- initializer: insertionInitializerText
914
- });
915
- return true;
916
- }
917
- const property = contentObject.getProperty(propertyKey);
918
- if (property && Node.isPropertyAssignment(property)) {
919
- if (property.getInitializer()?.getText() !== insertionInitializerText) {
920
- requiredImports.add("insert");
921
- property.setInitializer(insertionInitializerText);
922
- return true;
923
- }
924
- }
925
- return false;
926
- };
927
- /**
928
- * Processes html content.
929
- * Handles html node with string or translation content.
930
- *
931
- * @param contentObject - The object containing the property
932
- * @param propertyKey - The key of the property to process
933
- * @param contentNode - The html content node
934
- * @param existingPropertyKeys - Set of existing property names
935
- * @param requiredImports - Set to track required imports
936
- * @param sourceFile - The source file being processed
937
- * @returns True if the content was modified
938
- */
939
- const processHTMLContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
940
- const htmlContent = contentNode[NodeType.HTML];
941
- const htmlInitializerText = buildHTMLInitializer(htmlContent);
942
- if (!htmlInitializerText) return false;
943
- if (!existingPropertyKeys.has(propertyKey)) {
944
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
945
- if (spreadTargetObject) return processHTMLContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
946
- requiredImports.add("html");
947
- contentObject.addPropertyAssignment({
948
- name: propertyKey,
949
- initializer: htmlInitializerText
950
- });
951
- return true;
952
- }
953
- const property = contentObject.getProperty(propertyKey);
954
- if (property && Node.isPropertyAssignment(property)) {
955
- if (property.getInitializer()?.getText() !== htmlInitializerText) {
956
- requiredImports.add("html");
957
- property.setInitializer(htmlInitializerText);
958
- return true;
959
- }
960
- }
961
- return false;
962
- };
963
- /**
964
- * Processes markdown content.
965
-
966
- * Handles markdown objects with string, translation, or file content.
967
- *
968
- * @param contentObject - The object containing the property
969
- * @param propertyKey - The key of the property to process
970
- * @param contentNode - The markdown content node
971
- * @param existingPropertyKeys - Set of existing property names
972
- * @param effectiveFallbackLocale - The fallback locale for translations
973
- * @param requiredImports - Set to track required imports
974
- * @param sourceFile - The source file being processed
975
- * @returns True if the content was modified
976
- */
977
- const processMarkdownContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile) => {
978
- const markdownContent = contentNode[NodeType.Markdown];
979
- const markdownInitializerText = buildMarkdownInitializer(markdownContent);
980
- if (!markdownInitializerText) return false;
981
- if (!existingPropertyKeys.has(propertyKey)) {
982
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
983
- if (spreadTargetObject) return processMarkdownContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), effectiveFallbackLocale, requiredImports, sourceFile);
984
- requiredImports.add("md");
985
- const markdownNodeType = getNodeType(markdownContent);
986
- if (markdownNodeType === NodeType.File) requiredImports.add("file");
987
- else if (markdownNodeType === NodeType.Translation) requiredImports.add("t");
988
- contentObject.addPropertyAssignment({
989
- name: propertyKey,
990
- initializer: markdownInitializerText
991
- });
992
- return true;
993
- }
994
- const markdownNodeType = getNodeType(markdownContent);
995
- const existingSimpleMarkdown = readExistingMarkdown(contentObject, propertyKey);
996
- const existingMarkdownTranslationMap = readExistingMarkdownTranslationMap(contentObject, propertyKey);
997
- const existingTranslationTypeArguments = readExistingTypeArgsForCall(contentObject, propertyKey, "t");
998
- if (typeof markdownContent === "string" && existingMarkdownTranslationMap && effectiveFallbackLocale) {
999
- const updatedTranslationMap = {
1000
- ...existingMarkdownTranslationMap,
1001
- [effectiveFallbackLocale]: markdownContent
1002
- };
1003
- requiredImports.add("md");
1004
- requiredImports.add("t");
1005
- const property = contentObject.getProperty(propertyKey);
1006
- if (property && Node.isPropertyAssignment(property)) {
1007
- property.setInitializer(`md(${buildTranslationInitializer(updatedTranslationMap, existingTranslationTypeArguments)})`);
1008
- return true;
1009
- }
1010
- return false;
1011
- }
1012
- if (markdownNodeType === NodeType.Translation) {
1013
- const markdownTranslationMap = markdownContent[NodeType.Translation];
1014
- if (!Object.values(markdownTranslationMap).every((translationValue) => typeof translationValue === "string")) return false;
1015
- if (!areStringMapsEqual(markdownTranslationMap, existingMarkdownTranslationMap)) {
1016
- requiredImports.add("md");
1017
- requiredImports.add("t");
1018
- const property = contentObject.getProperty(propertyKey);
1019
- if (property && Node.isPropertyAssignment(property)) {
1020
- property.setInitializer(`md(${buildTranslationInitializer(markdownTranslationMap, existingTranslationTypeArguments)})`);
1021
- return true;
1022
- }
1023
- }
1024
- return false;
1025
- }
1026
- if (!(typeof markdownContent === "string" && existingSimpleMarkdown?.kind === "string" && existingSimpleMarkdown.value === markdownContent || markdownNodeType === NodeType.File && existingSimpleMarkdown?.kind === "file" && existingSimpleMarkdown.path === markdownContent[NodeType.File])) {
1027
- requiredImports.add("md");
1028
- if (markdownNodeType === NodeType.File) requiredImports.add("file");
1029
- const property = contentObject.getProperty(propertyKey);
1030
- if (property && Node.isPropertyAssignment(property)) {
1031
- property.setInitializer(markdownInitializerText);
1032
- return true;
1033
- }
1034
- }
1035
- return false;
1036
- };
1037
- /**
1038
- * Processes file content.
1039
- * Handles file objects with file path references.
1040
- *
1041
- * @param contentObject - The object containing the property
1042
- * @param propertyKey - The key of the property to process
1043
- * @param contentNode - The file content node
1044
- * @param existingPropertyKeys - Set of existing property names
1045
- * @param requiredImports - Set to track required imports
1046
- * @param sourceFile - The source file being processed
1047
- * @returns True if the content was modified
1048
- */
1049
- const processFileContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
1050
- const filePath = contentNode[NodeType.File];
1051
- const fileInitializerText = buildFileInitializer(filePath);
1052
- if (!fileInitializerText) return false;
1053
- if (!existingPropertyKeys.has(propertyKey)) {
1054
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
1055
- if (spreadTargetObject) return processFileContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
1056
- requiredImports.add("file");
1057
- contentObject.addPropertyAssignment({
1058
- name: propertyKey,
1059
- initializer: fileInitializerText
1060
- });
1061
- return true;
1062
- }
1063
- if (readExistingFilePath(contentObject, propertyKey) !== filePath) {
1064
- requiredImports.add("file");
1065
- const property = contentObject.getProperty(propertyKey);
1066
- if (property && Node.isPropertyAssignment(property)) {
1067
- property.setInitializer(fileInitializerText);
1068
- return true;
1069
- }
1070
- }
1071
- return false;
1072
- };
1073
- /**
1074
- * Processes nested content.
1075
- * Handles nested objects with dictionary key and optional path references.
1076
- *
1077
- * @param contentObject - The object containing the property
1078
- * @param propertyKey - The key of the property to process
1079
- * @param contentNode - The nested content node
1080
- * @param existingPropertyKeys - Set of existing property names
1081
- * @param requiredImports - Set to track required imports
1082
- * @param sourceFile - The source file being processed
1083
- * @returns True if the content was modified
1084
- */
1085
- const processNestedContent = (contentObject, propertyKey, contentNode, existingPropertyKeys, requiredImports, sourceFile) => {
1086
- const nestedContent = contentNode[NodeType.Nested];
1087
- const nestedInitializerText = buildNestedInitializer(nestedContent);
1088
- if (!nestedInitializerText) return false;
1089
- if (!existingPropertyKeys.has(propertyKey)) {
1090
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
1091
- if (spreadTargetObject) return processNestedContent(spreadTargetObject, propertyKey, contentNode, getExistingPropertyNames(spreadTargetObject), requiredImports, sourceFile);
1092
- requiredImports.add("nest");
1093
- contentObject.addPropertyAssignment({
1094
- name: propertyKey,
1095
- initializer: nestedInitializerText
1096
- });
1097
- return true;
1098
- }
1099
- const existingNestedContent = readExistingNest(contentObject, propertyKey);
1100
- if (!(!!nestedContent && existingNestedContent?.dictionaryKey === nestedContent.dictionaryKey && existingNestedContent?.path === nestedContent.path)) {
1101
- requiredImports.add("nest");
1102
- const property = contentObject.getProperty(propertyKey);
1103
- if (property && Node.isPropertyAssignment(property)) {
1104
- property.setInitializer(nestedInitializerText);
1105
- return true;
1106
- }
1107
- }
1108
- return false;
1109
- };
1110
- /**
1111
- * Processes nested object content.
1112
- * Handles nested objects within content structures.
1113
- *
1114
- * @param contentObject - The object containing the property
1115
- * @param propertyKey - The key of the property to process
1116
- * @param nestedObjectValue - The nested object value to process
1117
- * @param _existingPropertyKeys - Set of existing property names (unused)
1118
- * @param effectiveFallbackLocale - The fallback locale for translations
1119
- * @param requiredImports - Set to track required imports
1120
- * @param sourceFile - The source file being processed
1121
- * @returns True if the content was modified
1122
- */
1123
- const processNestedObjectContent = (contentObject, propertyKey, nestedObjectValue, _existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile) => {
1124
- let childObject;
1125
- const existingProperty = contentObject.getProperty(propertyKey);
1126
- if (existingProperty && Node.isPropertyAssignment(existingProperty)) childObject = existingProperty.getInitializerIfKind(SyntaxKind.ObjectLiteralExpression);
1127
- if (!childObject) {
1128
- const shorthandProperty = contentObject.getProperty(propertyKey);
1129
- if (shorthandProperty && Node.isShorthandPropertyAssignment(shorthandProperty)) childObject = resolveNameToObjectLiteral(contentObject.getSourceFile(), propertyKey);
1130
- else if (existingProperty && Node.isPropertyAssignment(existingProperty)) {
1131
- const propertyInitializer = existingProperty.getInitializer();
1132
- if (propertyInitializer) {
1133
- if (Node.isIdentifier(propertyInitializer)) childObject = resolveNameToObjectLiteral(sourceFile, propertyInitializer.getText());
1134
- else if (Node.isPropertyAccessExpression(propertyInitializer)) childObject = resolveExpressionToObjectLiteral(sourceFile, propertyInitializer);
1135
- }
1136
- }
1137
- }
1138
- if (!childObject) {
1139
- const spreadTargetObject = findSpreadTargetObjectForKey(contentObject, propertyKey, sourceFile);
1140
- if (spreadTargetObject) return processNestedObjectContent(spreadTargetObject, propertyKey, nestedObjectValue, getExistingPropertyNames(spreadTargetObject), effectiveFallbackLocale, requiredImports, sourceFile);
1141
- contentObject.addPropertyAssignment({
1142
- name: propertyKey,
1143
- initializer: "{ }"
1144
- });
1145
- const newProperty = contentObject.getProperty(propertyKey);
1146
- if (newProperty && Node.isPropertyAssignment(newProperty)) childObject = newProperty.getInitializerIfKind(SyntaxKind.ObjectLiteralExpression);
1147
- }
1148
- if (childObject) return processContentEntries(childObject, nestedObjectValue, effectiveFallbackLocale, requiredImports, sourceFile);
1149
- return false;
1150
- };
1151
- /**
1152
- * Processes content entries in a dictionary object.
1153
- * Routes different content types to appropriate processors.
1154
- *
1155
- * @param contentObject - The object containing the content
1156
- * @param dictionaryContent - The dictionary content to process
1157
- * @param effectiveFallbackLocale - The fallback locale for translations
1158
- * @param requiredImports - Set to track required imports
1159
- * @param sourceFile - The source file being processed
1160
- * @returns True if any content was modified
1161
- */
1162
- const processContentEntries = (contentObject, dictionaryContent, effectiveFallbackLocale, requiredImports, sourceFile) => {
1163
- let contentWasChanged = false;
1164
- const existingPropertyKeys = getExistingPropertyNames(contentObject);
1165
- for (const [propertyKey, propertyValue] of Object.entries(dictionaryContent)) {
1166
- if (Array.isArray(propertyValue)) {
1167
- if (processArrayContent(contentObject, propertyKey, propertyValue, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile)) contentWasChanged = true;
1168
- continue;
1169
- }
1170
- if (typeof propertyValue === "string" || typeof propertyValue === "number" || typeof propertyValue === "boolean" || propertyValue === null) {
1171
- if (processPrimitiveContent(contentObject, propertyKey, propertyValue, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile)) contentWasChanged = true;
1172
- continue;
1173
- }
1174
- const nodeType = getNodeType(propertyValue);
1175
- if (nodeType !== NodeType.Text && nodeType !== NodeType.Number && nodeType !== NodeType.Boolean && nodeType !== NodeType.Null) {
1176
- if (processComplexContent(contentObject, propertyKey, propertyValue, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile)) {
1177
- contentWasChanged = true;
1178
- continue;
1179
- }
1180
- }
1181
- if (propertyValue && typeof propertyValue === "object" && !Array.isArray(propertyValue) && !propertyValue.nodeType) {
1182
- if (processNestedObjectContent(contentObject, propertyKey, propertyValue, existingPropertyKeys, effectiveFallbackLocale, requiredImports, sourceFile)) contentWasChanged = true;
1183
- }
1184
- }
1185
- return contentWasChanged;
1186
- };
1187
- const readExistingMarkdown = (contentObject, propName) => {
1188
- const property = contentObject.getProperty(propName);
1189
- if (!property || !Node.isPropertyAssignment(property)) return void 0;
1190
- const initializer = property.getInitializer();
1191
- if (!initializer) return void 0;
1192
- if (Node.isCallExpression(initializer)) {
1193
- const expression = initializer.getExpression();
1194
- if (!Node.isIdentifier(expression)) return void 0;
1195
- if (expression.getText() === "md") {
1196
- const argument = initializer.getArguments()[0];
1197
- if (!argument) return void 0;
1198
- if (Node.isStringLiteral(argument)) return {
1199
- kind: "string",
1200
- value: argument.getLiteralValue()
1201
- };
1202
- if (Node.isCallExpression(argument)) {
1203
- const argumentExpression = argument.getExpression();
1204
- if (Node.isIdentifier(argumentExpression) && argumentExpression.getText() === "file") {
1205
- const fileArgument = argument.getArguments()[0];
1206
- if (fileArgument && Node.isStringLiteral(fileArgument)) return {
1207
- kind: "file",
1208
- path: fileArgument.getLiteralValue()
1209
- };
1210
- }
1211
- }
1212
- }
1213
- }
1214
- };
1215
- const readExistingFilePath = (contentObject, propName) => {
1216
- const property = contentObject.getProperty(propName);
1217
- if (!property || !Node.isPropertyAssignment(property)) return void 0;
1218
- const initializer = property.getInitializer();
1219
- if (!initializer || !Node.isCallExpression(initializer)) return void 0;
1220
- const expression = initializer.getExpression();
1221
- if (!Node.isIdentifier(expression) || expression.getText() !== "file") return void 0;
1222
- const argument = initializer.getArguments()[0];
1223
- if (argument && Node.isStringLiteral(argument)) return argument.getLiteralValue();
1224
- };
1225
- const readExistingMarkdownTranslationMap = (contentObject, propName) => {
1226
- const property = contentObject.getProperty(propName);
1227
- if (!property || !Node.isPropertyAssignment(property)) return void 0;
1228
- const initializer = property.getInitializer();
1229
- if (!initializer) return void 0;
1230
- if (Node.isCallExpression(initializer)) {
1231
- const exp = initializer.getExpression();
1232
- if (Node.isIdentifier(exp) && exp.getText() === "md") {
1233
- const arg = initializer.getArguments()[0];
1234
- if (arg && Node.isCallExpression(arg)) {
1235
- const tExp = arg.getExpression();
1236
- if (Node.isIdentifier(tExp) && tExp.getText() === "t") {
1237
- const tArg = arg.getArguments()[0];
1238
- if (tArg && Node.isObjectLiteralExpression(tArg)) {
1239
- const map = {};
1240
- for (const prop of tArg.getProperties()) {
1241
- if (!Node.isPropertyAssignment(prop)) continue;
1242
- const name = prop.getNameNode().getText().replace(/^['"]|['"]$/g, "");
1243
- const valueInit = prop.getInitializer();
1244
- if (valueInit && Node.isStringLiteral(valueInit)) map[name] = valueInit.getLiteralValue();
1245
- else return;
1246
- }
1247
- return map;
1248
- }
1249
- }
1250
- }
1251
- }
1252
- }
1253
- if (Node.isCallExpression(initializer)) {
1254
- const exp = initializer.getExpression();
1255
- if (Node.isIdentifier(exp) && exp.getText() === "t") {
1256
- const tArg = initializer.getArguments()[0];
1257
- if (tArg && Node.isObjectLiteralExpression(tArg)) {
1258
- const map = {};
1259
- for (const prop of tArg.getProperties()) {
1260
- if (!Node.isPropertyAssignment(prop)) continue;
1261
- const name = prop.getNameNode().getText().replace(/^['"]|['"]$/g, "");
1262
- const valueInit = prop.getInitializer();
1263
- if (valueInit && Node.isCallExpression(valueInit) && Node.isIdentifier(valueInit.getExpression()) && valueInit.getExpression().getText() === "md") {
1264
- const mdArg = valueInit.getArguments()[0];
1265
- if (mdArg && Node.isStringLiteral(mdArg)) map[name] = mdArg.getLiteralValue();
1266
- else return;
1267
- } else return;
1268
- }
1269
- return map;
1270
- }
1271
- }
1272
- }
1273
- };
1274
- const readExistingNest = (contentObject, propName) => {
1275
- const property = contentObject.getProperty(propName);
1276
- if (!property || !Node.isPropertyAssignment(property)) return void 0;
1277
- let initializer = property.getInitializer();
1278
- if (!initializer) return void 0;
1279
- let safetyCounter = 0;
1280
- while (safetyCounter++ < 5) {
1281
- if (Node.isCallExpression(initializer)) break;
1282
- const nextExpression = initializer.getExpression?.();
1283
- if (nextExpression && typeof nextExpression === "object" && nextExpression !== initializer) {
1284
- initializer = nextExpression;
1285
- continue;
1286
- }
1287
- break;
1288
- }
1289
- if (!Node.isCallExpression(initializer)) return void 0;
1290
- const expression = initializer.getExpression();
1291
- if (!Node.isIdentifier(expression) || expression.getText() !== "nest") return void 0;
1292
- const [firstArgument, secondArgument] = initializer.getArguments();
1293
- if (!firstArgument || !Node.isStringLiteral(firstArgument)) return void 0;
1294
- const dictionaryKey = firstArgument.getLiteralValue();
1295
- let path;
1296
- if (secondArgument && Node.isStringLiteral(secondArgument)) path = secondArgument.getLiteralValue();
1297
- return {
1298
- dictionaryKey,
1299
- path
1300
- };
1301
- };
1302
- const unwrapToObjectLiteral = (node) => {
1303
- if (!node || typeof node !== "object") return void 0;
1304
- let current = node;
1305
- let safetyCounter = 0;
1306
- while (safetyCounter++ < 8) {
1307
- if (Node.isObjectLiteralExpression(current)) return current;
1308
- const next = current?.getExpression?.();
1309
- if (next && typeof next === "object" && next !== current) {
1310
- current = next;
1311
- continue;
1312
- }
1313
- break;
1314
- }
1315
- };
1316
- const resolveNameToObjectLiteral = (sourceFile, name) => {
1317
- const varDecl = sourceFile.getVariableDeclaration(name);
1318
- if (varDecl) {
1319
- const obj = unwrapToObjectLiteral(varDecl.getInitializer());
1320
- if (obj) return obj;
1321
- }
1322
- const decl = sourceFile.getDescendants().find((n) => {
1323
- return Node.isIdentifier(n) && n.getText() === name;
1324
- })?.getSymbol()?.getDeclarations()?.[0];
1325
- if (decl && Node.isVariableDeclaration(decl)) {
1326
- const obj = unwrapToObjectLiteral(decl.getInitializer());
1327
- if (obj) return obj;
1328
- }
1329
- };
1330
- const resolveExpressionToObjectLiteral = (sourceFile, expr) => {
1331
- if (Node.isIdentifier(expr)) return resolveNameToObjectLiteral(sourceFile, expr.getText());
1332
- if (Node.isPropertyAccessExpression(expr)) {
1333
- const leftResolved = resolveExpressionToObjectLiteral(sourceFile, expr.getExpression());
1334
- if (!leftResolved) return void 0;
1335
- const propName = expr.getName();
1336
- const prop = leftResolved.getProperty(propName);
1337
- if (prop && Node.isPropertyAssignment(prop)) {
1338
- const init = prop.getInitializer();
1339
- const obj = unwrapToObjectLiteral(init);
1340
- if (obj) return obj;
1341
- if (init && Node.isIdentifier(init)) return resolveNameToObjectLiteral(sourceFile, init.getText());
1342
- }
1343
- }
1344
- };
1345
- const getSpreadSourceObjects = (contentObject, sourceFile) => {
1346
- const spreads = [];
1347
- for (const prop of contentObject.getProperties()) if (Node.isSpreadAssignment(prop)) {
1348
- const resolved = resolveExpressionToObjectLiteral(sourceFile, prop.getExpression());
1349
- if (resolved) spreads.push(resolved);
1350
- }
1351
- return spreads;
1352
- };
1353
- const findSpreadTargetObjectForKey = (contentObject, key, sourceFile) => {
1354
- const spreads = getSpreadSourceObjects(contentObject, sourceFile);
1355
- for (let i = spreads.length - 1; i >= 0; i--) {
1356
- const spreadObj = spreads[i];
1357
- const prop = spreadObj.getProperty(key);
1358
- if (prop && Node.isPropertyAssignment(prop)) return spreadObj;
1359
- }
1360
- };
1361
- const readExistingArraySerialized = (contentObject, propName) => {
1362
- const property = contentObject.getProperty(propName);
1363
- if (!property || !Node.isPropertyAssignment(property)) return void 0;
1364
- const initializer = property.getInitializer();
1365
- if (!initializer || !Node.isArrayLiteralExpression(initializer)) return void 0;
1366
- const serialized = [];
1367
- for (const element of initializer.getElements()) {
1368
- if (Node.isStringLiteral(element)) {
1369
- serialized.push(JSON.stringify(element.getLiteralValue()));
1370
- continue;
1371
- }
1372
- if (Node.isNumericLiteral(element)) {
1373
- serialized.push(element.getText());
1374
- continue;
1375
- }
1376
- if (element.getKind() === SyntaxKind.TrueKeyword || element.getKind() === SyntaxKind.FalseKeyword) {
1377
- serialized.push(element.getText());
1378
- continue;
1379
- }
1380
- if (Node.isNullLiteral(element)) {
1381
- serialized.push("null");
1382
- continue;
1383
- }
1384
- if (Node.isCallExpression(element)) {
1385
- const expression = element.getExpression();
1386
- if (Node.isIdentifier(expression) && expression.getText() === "t") {
1387
- const argument = element.getArguments()[0];
1388
- if (argument && Node.isObjectLiteralExpression(argument)) {
1389
- const map = {};
1390
- for (const propertyAssignment of argument.getProperties()) {
1391
- if (!Node.isPropertyAssignment(propertyAssignment)) return void 0;
1392
- const name = propertyAssignment.getNameNode().getText().replace(/^['"]|['"]$/g, "");
1393
- const valueInitializer = propertyAssignment.getInitializer();
1394
- if (valueInitializer && Node.isStringLiteral(valueInitializer)) map[name] = valueInitializer.getLiteralValue();
1395
- else return;
1396
- }
1397
- serialized.push(buildTranslationInitializer(map));
1398
- continue;
1399
- }
1400
- }
1401
- }
1402
- return;
1403
- }
1404
- return serialized;
1405
- };
1406
- const serializeValue = (value) => {
1407
- const nodeType = getNodeType(value);
1408
- if (nodeType === NodeType.Text) return JSON.stringify(value);
1409
- if (nodeType === NodeType.Number || nodeType === NodeType.Boolean) return String(value);
1410
- if (nodeType === NodeType.Null) return "null";
1411
- if (nodeType === NodeType.Translation) {
1412
- const translations = value[NodeType.Translation] ?? {};
1413
- if (!Object.values(translations).every((v) => typeof v === "string")) return void 0;
1414
- return buildTranslationInitializer(translations);
1415
- }
1416
- if (nodeType === NodeType.Enumeration) {
1417
- const map = value[NodeType.Enumeration];
1418
- return buildEnumerationInitializer(map);
1419
- }
1420
- if (nodeType === NodeType.Condition) {
1421
- const map = value[NodeType.Condition];
1422
- return buildConditionInitializer(map);
1423
- }
1424
- if (nodeType === NodeType.Gender) {
1425
- const map = value[NodeType.Gender];
1426
- return buildGenderInitializer(map);
1427
- }
1428
- if (nodeType === NodeType.Insertion) {
1429
- const content = value[NodeType.Insertion];
1430
- return buildInsertionInitializer(content);
1431
- }
1432
- if (nodeType === NodeType.HTML) {
1433
- const content = value[NodeType.HTML];
1434
- return buildHTMLInitializer(content);
1435
- }
1436
- if (nodeType === NodeType.Markdown) {
1437
- const content = value[NodeType.Markdown];
1438
- return buildMarkdownInitializer(content);
1439
- }
1440
- if (nodeType === NodeType.File) {
1441
- const path = value[NodeType.File];
1442
- return buildFileInitializer(path);
1443
- }
1444
- if (nodeType === NodeType.Nested) {
1445
- const content = value[NodeType.Nested];
1446
- return buildNestedInitializer(content);
1447
- }
1448
- };
1449
- /**
1450
- * Gets the existing imports from @intlayer/core in the source file
1451
- */
1452
- const getExistingIntlayerImports = (sourceFile) => {
1453
- const imported = /* @__PURE__ */ new Set();
1454
- for (const importDecl of sourceFile.getImportDeclarations()) {
1455
- const moduleSpecifier = importDecl.getModuleSpecifierValue();
1456
- if (moduleSpecifier === "intlayer") {
1457
- const namedImports = importDecl.getNamedImports();
1458
- for (const namedImport of namedImports) imported.add(namedImport.getName());
1459
- }
1460
- if (moduleSpecifier === "intlayer/file") {
1461
- const namedImports = importDecl.getNamedImports();
1462
- for (const namedImport of namedImports) {
1463
- const alias = namedImport.getAliasNode();
1464
- imported.add(alias ? alias.getText() : namedImport.getName());
1465
- }
1466
- }
1467
- }
1468
- return imported;
1469
- };
1470
- /**
1471
- * Adds missing imports to the source file
1472
- */
1473
- const addMissingImports = (sourceFile, requiredImports) => {
1474
- if (requiredImports.size === 0) return false;
1475
- const existingImports = getExistingIntlayerImports(sourceFile);
1476
- const missingImports = [...requiredImports].filter((imp) => !existingImports.has(imp));
1477
- if (missingImports.length === 0) return false;
1478
- const hasMissingFile = missingImports.includes("file");
1479
- const otherMissingImports = missingImports.filter((imp) => imp !== "file");
1480
- if (otherMissingImports.length > 0) {
1481
- const coreImport = sourceFile.getImportDeclarations().find((imp) => imp.getModuleSpecifierValue() === "intlayer");
1482
- if (coreImport) {
1483
- const existingNamedImports = coreImport.getNamedImports().map((ni) => ni.getName());
1484
- const allImports = [...new Set([...existingNamedImports, ...otherMissingImports])].sort();
1485
- coreImport.removeNamedImports();
1486
- coreImport.addNamedImports(allImports.map((name) => ({ name })));
1487
- } else sourceFile.insertImportDeclaration(0, {
1488
- moduleSpecifier: "intlayer",
1489
- namedImports: otherMissingImports.sort().map((name) => ({ name }))
1490
- });
1491
- }
1492
- if (hasMissingFile) {
1493
- if (!sourceFile.getImportDeclarations().find((imp) => imp.getModuleSpecifierValue() === "intlayer/file")) {
1494
- const coreImportIndex = sourceFile.getImportDeclarations().findIndex((imp) => imp.getModuleSpecifierValue() === "intlayer");
1495
- const insertIndex = coreImportIndex >= 0 ? coreImportIndex + 1 : 0;
1496
- sourceFile.insertImportDeclaration(insertIndex, {
1497
- moduleSpecifier: "intlayer/file",
1498
- namedImports: [{ name: "file" }]
1499
- });
1500
- }
1501
- }
1502
- return true;
1503
- };
1504
- /**
1505
- * Detect whether the current source file is written in CommonJS style.
1506
- * Prefers ESM when import/export syntax is present; otherwise detects CJS via require/module.exports.
1507
- */
1508
- const isCommonJS = (sourceFile) => {
1509
- if (sourceFile.getImportDeclarations().length > 0) return false;
1510
- if (sourceFile.getExportDeclarations().length > 0) return false;
1511
- if (sourceFile.getExportAssignments().length > 0) return false;
1512
- for (const statement of sourceFile.getStatements()) {
1513
- if (!Node.isExpressionStatement(statement)) continue;
1514
- const expression = statement.getExpression();
1515
- if (!Node.isBinaryExpression(expression)) continue;
1516
- const leftSide = expression.getLeft();
1517
- if (!Node.isPropertyAccessExpression(leftSide)) continue;
1518
- const leftExpression = leftSide.getExpression();
1519
- const leftName = leftSide.getName();
1520
- const isModuleExports = Node.isIdentifier(leftExpression) && leftExpression.getText() === "module" && leftName === "exports";
1521
- const isExportsDefault = Node.isIdentifier(leftExpression) && leftExpression.getText() === "exports";
1522
- if (isModuleExports || isExportsDefault) return true;
1523
- }
1524
- return sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression).some((call) => {
1525
- const exp = call.getExpression();
1526
- return Node.isIdentifier(exp) && exp.getText() === "require";
1527
- });
1528
- };
1529
- /**
1530
- * Adds missing CommonJS requires for intlayer helpers.
1531
- * - Core helpers (t, md, insert, enu, cond, gender, nest) come from require('intlayer') via destructuring
1532
- * - file helper comes from require('intlayer/file') via destructuring
1533
- * Existing destructured requires are respected to avoid duplicates.
1534
- */
1535
- const addMissingRequires = (sourceFile, requiredImports) => {
1536
- if (requiredImports.size === 0) return false;
1537
- const existingCoreNames = /* @__PURE__ */ new Set();
1538
- let hasFileHelper = false;
1539
- for (const varDecl of sourceFile.getVariableDeclarations()) {
1540
- const init = varDecl.getInitializer();
1541
- if (!init || !Node.isCallExpression(init)) continue;
1542
- const callee = init.getExpression();
1543
- if (!Node.isIdentifier(callee) || callee.getText() !== "require") continue;
1544
- const arg = init.getArguments()[0];
1545
- if (!arg || !Node.isStringLiteral(arg)) continue;
1546
- const spec = arg.getLiteralValue();
1547
- const nameNode = varDecl.getNameNode();
1548
- if (spec === "intlayer") {
1549
- if (Node.isObjectBindingPattern(nameNode)) for (const el of nameNode.getElements()) existingCoreNames.add(el.getNameNode().getText());
1550
- }
1551
- if (spec === "intlayer/file") {
1552
- if (Node.isObjectBindingPattern(nameNode)) {
1553
- for (const el of nameNode.getElements()) if (el.getNameNode().getText() === "file") hasFileHelper = true;
1554
- } else if (Node.isIdentifier(nameNode) && nameNode.getText() === "file") hasFileHelper = true;
1555
- }
1556
- }
1557
- const missingCore = Array.from(requiredImports).filter((n) => n !== "file").filter((n) => !existingCoreNames.has(n));
1558
- const needsFile = requiredImports.has("file") && !hasFileHelper;
1559
- if (missingCore.length === 0 && !needsFile) return false;
1560
- let insertIndex = 0;
1561
- const statements = sourceFile.getStatements();
1562
- for (const st of statements) {
1563
- if (Node.isExpressionStatement(st)) {
1564
- const expr = st.getExpression();
1565
- if (Node.isStringLiteral(expr)) {
1566
- insertIndex += 1;
1567
- continue;
1568
- }
1569
- }
1570
- break;
1571
- }
1572
- const lines = [];
1573
- if (missingCore.length > 0) {
1574
- const sorted = Array.from(new Set(missingCore)).sort();
1575
- lines.push(`const { ${sorted.join(", ")} } = require('intlayer');`);
1576
- }
1577
- if (needsFile) lines.push("const { file } = require('intlayer/file');");
1578
- if (lines.length > 0) {
1579
- sourceFile.insertStatements(insertIndex, lines.join("\n"));
1580
- return true;
1581
- }
1582
- return false;
1583
- };
1584
- /**
1585
- * Serializes a metadata value to its string representation for code generation
1586
- * Handles: boolean, number, string, and array of strings
1587
- */
1588
- const serializeMetadataValue = (value) => {
1589
- if (Array.isArray(value)) return `[${value.map((item) => JSON.stringify(item)).join(", ")}]`;
1590
- if (typeof value === "boolean" || typeof value === "number") return String(value);
1591
- return JSON.stringify(value);
1592
- };
1593
- /**
1594
- * Updates a single property in the root object if the value has changed
1595
- */
1596
- const updateMetadataProperty = (rootObject, propertyName, value) => {
1597
- const property = rootObject.getProperty(propertyName) || rootObject.getProperty(`'${propertyName}'`) || rootObject.getProperty(`"${propertyName}"`);
1598
- const serializedValue = serializeMetadataValue(value);
1599
- if (property && Node.isPropertyAssignment(property)) {
1600
- if (property.getInitializer()?.getText() !== serializedValue) {
1601
- property.setInitializer(serializedValue);
1602
- return true;
1603
- }
1604
- } else if (!property) {
1605
- rootObject.addPropertyAssignment({
1606
- name: propertyName,
1607
- initializer: serializedValue
1608
- });
1609
- return true;
1610
- }
1611
- return false;
1612
- };
1613
- /**
1614
- * Updates dictionary metadata properties in the root object
1615
- * Supports: id, locale, filled, fill, title, description, tags, version, priority, live
1616
- * and any future fields that may be added
1617
- */
1618
- const updateDictionaryMetadata = (rootObject, dictionary) => {
1619
- let changed = false;
1620
- for (const prop of [
1621
- "id",
1622
- "locale",
1623
- "filled",
1624
- "fill",
1625
- "title",
1626
- "description",
1627
- "tags",
1628
- "version",
1629
- "priority",
1630
- "contentAutoTransformation"
1631
- ]) {
1632
- const value = dictionary[prop];
1633
- if (value !== void 0) {
1634
- if (updateMetadataProperty(rootObject, prop, value)) changed = true;
1635
- }
1636
- }
1637
- return changed;
1638
- };
1639
- /**
1640
- * Locates the root dictionary object in the source file
1641
- */
1642
- const findRootDictionaryObject = (sourceFile) => {
1643
- const exportAssignment = sourceFile.getExportAssignment((_) => true);
1644
- if (exportAssignment) {
1645
- const expression = exportAssignment.getExpression();
1646
- if (Node.isIdentifier(expression)) {
1647
- const declarationByName = expression.getSymbol()?.getDeclarations()?.[0] ?? sourceFile.getVariableDeclaration(expression.getText());
1648
- if (declarationByName && Node.isVariableDeclaration(declarationByName)) {
1649
- const objectLiteral = unwrapToObjectLiteral(declarationByName.getInitializer());
1650
- if (objectLiteral) return objectLiteral;
1651
- }
1652
- } else {
1653
- const objectLiteral = unwrapToObjectLiteral(expression);
1654
- if (objectLiteral) return objectLiteral;
1655
- }
1656
- }
1657
- const variableDeclaration = sourceFile.getVariableDeclaration((variable) => {
1658
- try {
1659
- return variable.getType().getText().includes("Dictionary") || variable.getName() === "content" || variable.getName().toLowerCase().includes("dictionary");
1660
- } catch {
1661
- return variable.getName() === "content";
1662
- }
1663
- });
1664
- if (variableDeclaration) {
1665
- const objectLiteral = unwrapToObjectLiteral(variableDeclaration.getInitializer());
1666
- if (objectLiteral) return objectLiteral;
1667
- }
1668
- for (const statement of sourceFile.getStatements()) {
1669
- if (!Node.isExpressionStatement(statement)) continue;
1670
- const expression = statement.getExpression();
1671
- if (!Node.isBinaryExpression(expression)) continue;
1672
- if (expression.getOperatorToken().getText() !== "=") continue;
1673
- const leftSide = expression.getLeft();
1674
- if (!Node.isPropertyAccessExpression(leftSide)) continue;
1675
- const leftExpression = leftSide.getExpression();
1676
- const leftName = leftSide.getName();
1677
- const isModuleExports = Node.isIdentifier(leftExpression) && leftExpression.getText() === "module" && leftName === "exports";
1678
- const isExportsDefault = Node.isIdentifier(leftExpression) && leftExpression.getText() === "exports" && leftName === "default";
1679
- if (!isModuleExports && !isExportsDefault) continue;
1680
- const rightSide = expression.getRight();
1681
- if (Node.isObjectLiteralExpression(rightSide)) return rightSide;
1682
- if (Node.isIdentifier(rightSide)) {
1683
- const declaration = rightSide.getSymbol()?.getDeclarations()?.[0];
1684
- if (declaration && Node.isVariableDeclaration(declaration)) {
1685
- const objectLiteral = unwrapToObjectLiteral(declaration.getInitializer());
1686
- if (objectLiteral) return objectLiteral;
1687
- }
1688
- }
1689
- }
1690
- };
1691
- /**
1692
- * Updates a JavaScript/TypeScript file based on the provided dictionary.
1693
- * It targets a specific dictionary object within the file and updates its
1694
- * metadata (title, description, tags) and content entries.
1695
- *
1696
- * This function now supports inserting translation keys into nested objects
1697
- * within arrays. For example, if you have:
1698
- * ```
1699
- * content: [
1700
- * { question: t({ en: '...', fr: '...' }) }
1701
- * ]
1702
- * ```
1703
- *
1704
- * You can add a new locale (e.g., 'pl') by providing a dictionary with:
1705
- * ```
1706
- * {
1707
- * content: [
1708
- * { question: { [NodeType.Translation]: { en: '...', fr: '...', pl: '...' } } }
1709
- * ]
1710
- * }
1711
- * ```
1712
- *
1713
- * The function will:
1714
- * 1. Detect the existing array structure
1715
- * 2. Navigate into each array element (if it's an object)
1716
- * 3. Recursively process nested properties
1717
- * 4. Update translation maps while preserving existing locales
1718
- */
1719
- const transformJSFile = async (fileContent, dictionary, fallbackLocale) => {
1720
- try {
1721
- if (!dictionary || typeof dictionary !== "object") return fileContent;
1722
- const sourceFile = new Project({
1723
- useInMemoryFileSystem: true,
1724
- skipAddingFilesFromTsConfig: true,
1725
- skipFileDependencyResolution: true,
1726
- compilerOptions: {
1727
- allowJs: true,
1728
- jsx: ts.JsxEmit.Preserve
1729
- },
1730
- manipulationSettings: {
1731
- indentationText: IndentationText.TwoSpaces,
1732
- quoteKind: QuoteKind.Double,
1733
- newLineKind: NewLineKind.LineFeed
1734
- }
1735
- }).createSourceFile("file.tsx", fileContent, { overwrite: true });
1736
- const rootObject = findRootDictionaryObject(sourceFile);
1737
- if (!rootObject) return fileContent;
1738
- let changed = false;
1739
- const requiredImports = /* @__PURE__ */ new Set();
1740
- if (updateDictionaryMetadata(rootObject, dictionary)) changed = true;
1741
- if (dictionary.content) {
1742
- const contentProperty = rootObject.getProperty("content");
1743
- let contentObject;
1744
- let isContentArrayInSource = false;
1745
- let isContentCallExpression = false;
1746
- if (contentProperty && Node.isPropertyAssignment(contentProperty)) {
1747
- contentObject = contentProperty.getInitializerIfKind(SyntaxKind.ObjectLiteralExpression);
1748
- isContentArrayInSource = !!contentProperty.getInitializerIfKind(SyntaxKind.ArrayLiteralExpression);
1749
- const initializer = contentProperty.getInitializer();
1750
- isContentCallExpression = !!initializer && Node.isCallExpression(initializer);
1751
- }
1752
- const effectiveFallbackLocale = fallbackLocale ?? "en";
1753
- if (contentObject && !Array.isArray(dictionary.content)) {
1754
- const dictContent = dictionary.content ?? {};
1755
- if (processContentEntries(contentObject, dictContent, effectiveFallbackLocale, requiredImports, sourceFile)) changed = true;
1756
- } else if (Array.isArray(dictionary.content) && isContentArrayInSource) {
1757
- if (processArrayContent(rootObject, "content", dictionary.content ?? [], getExistingPropertyNames(rootObject), effectiveFallbackLocale, requiredImports, sourceFile)) changed = true;
1758
- } else if (isContentCallExpression) {
1759
- if (getNodeType(dictionary.content)) {
1760
- if (processComplexContent(rootObject, "content", dictionary.content, getExistingPropertyNames(rootObject), effectiveFallbackLocale, requiredImports, sourceFile)) changed = true;
1761
- }
1762
- }
1763
- }
1764
- if (!changed) return fileContent;
1765
- if ((isCommonJS(sourceFile) ? addMissingRequires(sourceFile, requiredImports) : addMissingImports(sourceFile, requiredImports)) || changed) return sourceFile.getFullText();
1766
- return fileContent;
1767
- } catch {
1768
- return fileContent;
1769
- }
1770
- };
1771
-
1772
- //#endregion
1773
- export { transformJSFile };
1
+ import{getNodeType as e}from"@intlayer/core/dictionaryManipulator";import{NodeType as t}from"@intlayer/types";import{IndentationText as n,NewLineKind as r,Node as i,Project as a,QuoteKind as o,SyntaxKind as s,ts as c}from"ts-morph";const l=(e,t)=>{let n=Object.entries(e).sort(([e],[t])=>e.localeCompare(t)),r=[];for(let[e,t]of n){let n=/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e)?e:JSON.stringify(e);if(typeof t==`string`)r.push(`${n}: ${JSON.stringify(t)}`);else if(Array.isArray(t)){let e=t.map(e=>JSON.stringify(e)).join(`, `);r.push(`${n}: [ ${e} ]`)}else r.push(`${n}: ${JSON.stringify(t)}`)}return`t${t??``}({ ${r.join(`, `)} })`},u=(e,t,n)=>{let r={...e,[t]:n},i=n.match(/\d+(?!.*\d)/);if(!i)return r;let a=i[0];for(let[n,i]of Object.entries(e))n!==t&&i.match(/\d+(?!.*\d)/)&&(r[n]=i.replace(/(\d+)(?!.*\d)/,a));return r},d=e=>!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e)||e===`true`||e===`false`?JSON.stringify(e):e,f=e=>{let t=[];for(let[n,r]of Object.entries(e)){if(typeof r!=`string`)return``;t.push(`${d(n)}: ${JSON.stringify(r)}`)}return`enu({ ${t.join(`, `)} })`},p=e=>{let t=[];for(let[n,r]of Object.entries(e)){if(typeof r!=`string`)return``;t.push(`${d(n)}: ${JSON.stringify(r)}`)}return`cond({ ${t.join(`, `)} })`},m=e=>{let t=[];for(let[n,r]of Object.entries(e)){if(typeof r!=`string`)return``;t.push(`${d(n)}: ${JSON.stringify(r)}`)}return`gender({ ${t.join(`, `)} })`},h=n=>{if(typeof n==`string`)return`insert(${JSON.stringify(n)})`;if(e(n)===t.Translation){let e=n[t.Translation]??{};return Object.values(e).every(e=>typeof e==`string`)?`insert(${l(e)})`:void 0}},g=e=>{if(typeof e==`string`)return`file(${JSON.stringify(e)})`},_=n=>{if(typeof n==`string`)return`html(${JSON.stringify(n)})`;if(e(n)===t.Translation){let e=n[t.Translation]??{};return Object.values(e).every(e=>typeof e==`string`)?`html(${l(e)})`:void 0}},v=n=>{if(typeof n==`string`)return`md(${JSON.stringify(n)})`;if(e(n)===t.Translation){let e=n[t.Translation]??{};return Object.values(e).every(e=>typeof e==`string`)?`md(${l(e)})`:void 0}if(e(n)===t.File){let e=n[t.File],r=g(e);return r?`md(${r})`:void 0}},y=e=>{if(!(!e||typeof e.dictionaryKey!=`string`))return e.path&&typeof e.path==`string`?`nest(${JSON.stringify(e.dictionaryKey)}, ${JSON.stringify(e.path)})`:`nest(${JSON.stringify(e.dictionaryKey)})`},b=(e,t)=>{let n=e.getProperty(t);if(!n||!i.isPropertyAssignment(n))return;let r=n.getInitializer();if(!r||!i.isCallExpression(r))return;let a=r.getExpression();if(!i.isIdentifier(a)||a.getText()!==`t`)return;let o=r.getArguments()[0];if(!o||!i.isObjectLiteralExpression(o))return;let s={};for(let e of o.getProperties()){if(!i.isPropertyAssignment(e))continue;let t=e.getNameNode().getText().replace(/^['"]|['"]$/g,``),n=e.getInitializer();if(n&&i.isStringLiteral(n))s[t]=n.getLiteralValue();else if(n&&i.isArrayLiteralExpression(n)){let e=[];for(let t of n.getElements()){if(!i.isStringLiteral(t))return;e.push(t.getLiteralValue())}s[t]=e}else return}return s},x=(e,t,n)=>{let r=e.getProperty(t);if(!r||!i.isPropertyAssignment(r))return;let a=r.getInitializer();if(!a||!i.isCallExpression(a))return;let o=a.getExpression();if(!i.isIdentifier(o)||o.getText()!==n)return;let s=a.getArguments()[0];if(!s||!i.isObjectLiteralExpression(s))return;let c={};for(let e of s.getProperties()){if(!i.isPropertyAssignment(e))continue;let t=e.getNameNode().getText().replace(/^['"]|['"]$/g,``),n=e.getInitializer();n&&i.isStringLiteral(n)&&(c[t]=n.getLiteralValue())}return c},S=e=>{try{let t=e.getTypeArguments();return!t||t.length===0?void 0:`<${t.map(e=>e.getText()).join(`, `)}>`}catch{return}},C=(e,t,n)=>{let r=e.getProperty(t);if(!r||!i.isPropertyAssignment(r))return;let a=r.getInitializer();if(!a||!i.isCallExpression(a))return;let o=a.getExpression();if(i.isIdentifier(o)&&o.getText()===n)return S(a);if(n===`t`&&i.isIdentifier(o)&&o.getText()===`md`){let e=a.getArguments()[0];if(e&&i.isCallExpression(e)){let t=e.getExpression();if(i.isIdentifier(t)&&t.getText()===`t`)return S(e)}}},w=(e,t)=>{if(!t)return!1;let n=Object.entries(e).filter(([,e])=>typeof e==`string`);if(n.length!==Object.keys(e).length||n.length!==Object.keys(t).length)return!1;for(let[e,r]of n)if(!(e in t)||t[e]!==r)return!1;return!0},T=(e,t)=>{if(!t)return!1;for(let[n,r]of Object.entries(e)){if(!(n in t))return!1;let e=t[n];if(typeof r==`string`){if(typeof e!=`string`||e!==r)return!1}else if(Array.isArray(r)){if(!Array.isArray(e)||e.length!==r.length)return!1;for(let t=0;t<r.length;t++)if(e[t]!==r[t])return!1}else return!1}return!0},E=e=>{let t=new Set;for(let n of e.getProperties()){if(i.isPropertyAssignment(n)){let e=n.getName();e&&t.add(e.replace(/^['"]|['"]$/g,``));continue}if(i.isShorthandPropertyAssignment(n)){let e=n.getNameNode().getText();e&&t.add(e)}}return t},D=(n,r,a,o,s,c,d)=>{if(!o.has(r)){let e=Y(n,r,d);if(e)return D(e,r,a,E(e),s,c,d)}let f=[],p=!1,m,h=!1,g,_=!1,v=n.getProperty(r);if(v&&i.isPropertyAssignment(v)){let e=v.getInitializer(),t,o=a.every(e=>typeof e==`string`);if(e&&i.isCallExpression(e)&&i.isIdentifier(e.getExpression())&&e.getExpression().getText()===`t`&&o){t=S(e);let o=b(n,r);if(o){let e=l({...o,[s]:a},t);c.add(`t`);let u=n.getProperty(r);return u&&i.isPropertyAssignment(u)&&u.getInitializer()?.getText()!==e?(u.setInitializer(e),!0):!1}}if(e&&i.isArrayLiteralExpression(e)&&(m=e.getElements(),h=e.getElements().some(e=>{if(!i.isCallExpression(e))return!1;let t=e.getExpression();return i.isIdentifier(t)&&t.getText()===`t`}),h)){for(let e of m)if(i.isCallExpression(e)){let t=e.getExpression();if(i.isIdentifier(t)&&t.getText()===`t`&&(g=S(e),g))break}}}for(let n=0;n<a.length;n++){let r=a[n];if(r==null||typeof r==`string`||typeof r==`number`||typeof r==`boolean`){let e=Z(r);if(typeof r==`string`&&m&&n<m.length){let t=m[n];if(i.isCallExpression(t)){let n=t.getExpression();if(i.isIdentifier(n)&&n.getText()===`t`){let n=t.getArguments()[0];if(n&&i.isObjectLiteralExpression(n)){let a={};for(let e of n.getProperties()){if(!i.isPropertyAssignment(e))continue;let t=e.getNameNode().getText().replace(/^['"]|['"]$/g,``),n=e.getInitializer();n&&i.isStringLiteral(n)&&(a[t]=n.getLiteralValue())}e=l(u(a,s,r),S(t)),c.add(`t`)}}}}if(typeof r==`string`&&h&&e&&e.startsWith(`"`)&&(e=l({[s]:r},g),c.add(`t`)),e===void 0){p=!0;break}f.push(e)}else if(typeof r==`object`&&r){if(m&&n<m.length){let e=m[n];if(i.isObjectLiteralExpression(e))B(e,r,s,c,d)&&(_=!0),f.push(e.getText());else{let e=Z(r);if(e===void 0){p=!0;break}f.push(e)}}else{let e=Z(r);if(e===void 0){p=!0;break}f.push(e)}let a=e(r);if(a===t.Translation)c.add(`t`);else if(a===t.Enumeration)c.add(`enu`);else if(a===t.Condition)c.add(`cond`);else if(a===t.Gender)c.add(`gender`);else if(a===t.HTML){c.add(`html`);let n=r[t.HTML];typeof n==`object`&&n&&e(n)===t.Translation&&c.add(`t`)}else if(a===t.Insertion){c.add(`insert`);let n=r[t.Insertion];typeof n==`object`&&n&&e(n)===t.Translation&&c.add(`t`)}else if(a===t.Markdown){c.add(`md`);let n=r[t.Markdown];typeof n==`object`&&n&&e(n)===t.File&&c.add(`file`)}else a===t.File?c.add(`file`):a===t.Nested&&c.add(`nest`)}else{p=!0;break}}if(p)return!1;if(_)return!0;let y=`[ ${f.join(`, `)} ]`;if(!o.has(r))return n.addPropertyAssignment({name:r,initializer:y}),!0;let x=n.getProperty(r);if(x&&i.isPropertyAssignment(x)){let e=X(n,r);if(!(e!==void 0&&e.length===f.length&&e.every((e,t)=>e===f[t])))return x.setInitializer(y),!0}return!1},O=(e,t,n,r,a,o,c)=>{if(typeof n==`string`&&r.has(t)){let r=e.getProperty(t);if(r&&i.isPropertyAssignment(r)){let e=r.getInitializer();if(e&&!i.isStringLiteral(e)&&!i.isCallExpression(e))return console.log(`Skipping update for key "${t}" because existing value is not a string literal`),!1}let s=b(e,t);if(s){let c=l({...s,[a]:n},C(e,t,`t`));return o.add(`t`),r&&i.isPropertyAssignment(r)?(r.setInitializer(c),!0):!1}}if(!r.has(t)){let r=Y(e,t,c);return r?O(r,t,n,E(r),a,o,c):(e.addPropertyAssignment({name:t,initializer:typeof n==`string`?JSON.stringify(n):String(n)}),!0)}let u=e.getProperty(t);if(u&&i.isPropertyAssignment(u)){let e=u.getInitializer(),r=e&&(i.isStringLiteral(e)||i.isNumericLiteral(e)||e.getKind()===s.TrueKeyword||e.getKind()===s.FalseKeyword||i.isNullLiteral(e)||i.isCallExpression(e));if(e&&!r)return console.log(`Skipping update for key "${t}" because existing value is not a primitive literal`),!1;let a=e?.getText(),o=typeof n==`string`?JSON.stringify(n):String(n);if(a!==o)return u.setInitializer(o),!0}return!1},k=(n,r,i,a,o,s,c)=>{switch(e(i)){case t.Translation:return A(n,r,i,a,s,c);case t.Enumeration:return j(n,r,i,a,s,c);case t.Condition:return M(n,r,i,a,s,c);case t.Gender:return N(n,r,i,a,s,c);case t.Insertion:return P(n,r,i,a,s,c);case t.HTML:return F(n,r,i,a,s,c);case t.Markdown:return I(n,r,i,a,o,s,c);case t.File:return L(n,r,i,a,s,c);case t.Nested:return R(n,r,i,a,s,c);default:return!1}},A=(n,r,a,o,s,c)=>{let l=a[t.Translation]??{},u=Object.values(l).every(e=>typeof e==`string`||Array.isArray(e));if(Object.values(l).some(n=>typeof n==`object`&&!!n&&!Array.isArray(n)&&e(n)!==t.Text)&&!u){if(!o.has(r)){let e=Y(n,r,c);if(e)return A(e,r,a,E(e),s,c)}let u=[],d=!1;for(let[n,r]of Object.entries(l)){let i=/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(n)?n:JSON.stringify(n);if(typeof r==`object`&&r&&!Array.isArray(r)){let n=Z(r);if(n===void 0){d=!0;break}u.push(`${i}: ${n}`);let a=e(r);if(a===t.Markdown){s.add(`md`);let n=r[t.Markdown];typeof n==`object`&&n&&e(n)===t.File&&s.add(`file`)}else a===t.File?s.add(`file`):a===t.Insertion?s.add(`insert`):a===t.Enumeration?s.add(`enu`):a===t.Condition?s.add(`cond`):a===t.Gender?s.add(`gender`):a===t.Nested&&s.add(`nest`)}else if(typeof r==`string`)u.push(`${i}: ${JSON.stringify(r)}`);else if(Array.isArray(r)){let e=r.map(e=>JSON.stringify(e)).join(`, `);u.push(`${i}: [ ${e} ]`)}}if(d)return!1;let f=`t${C(n,r,`t`)??``}({ ${u.join(`, `)} })`;if(!o.has(r))return s.add(`t`),n.addPropertyAssignment({name:r,initializer:f}),!0;let p=n.getProperty(r);return p&&i.isPropertyAssignment(p)&&p.getInitializer()?.getText()!==f?(s.add(`t`),p.setInitializer(f),!0):!1}if(!u)return!1;if(!o.has(r)){let e=Y(n,r,c);if(e)return A(e,r,a,E(e),s,c)}let d=[];for(let[e,t]of Object.entries(l)){let n=/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e)?e:JSON.stringify(e);if(typeof t==`string`)d.push(`${n}: ${JSON.stringify(t)}`);else if(Array.isArray(t)){let e=t.map(e=>JSON.stringify(e)).join(`, `);d.push(`${n}: [ ${e} ]`)}}let f=`t${C(n,r,`t`)??``}({ ${d.join(`, `)} })`;if(!o.has(r))return s.add(`t`),n.addPropertyAssignment({name:r,initializer:f}),!0;if(!T(l,b(n,r))){s.add(`t`);let e=n.getProperty(r);if(e&&i.isPropertyAssignment(e))return e.setInitializer(f),!0}return!1},j=(e,n,r,a,o,s)=>{let c=r[t.Enumeration];if(!Object.values(c).every(e=>typeof e==`string`))return!1;let l=f(c);if(!l)return!1;if(!a.has(n)){let t=Y(e,n,s);return t?j(t,n,r,E(t),o,s):(o.add(`enu`),e.addPropertyAssignment({name:n,initializer:l}),!0)}if(!w(c,x(e,n,`enu`))){o.add(`enu`);let t=e.getProperty(n);if(t&&i.isPropertyAssignment(t))return t.setInitializer(l),!0}return!1},M=(n,r,a,o,s,c)=>{let l=a[t.Condition];if(Object.values(l).every(e=>typeof e==`string`)){let e=p(l);if(!e)return!1;if(!o.has(r)){let t=Y(n,r,c);return t?M(t,r,a,E(t),s,c):(s.add(`cond`),n.addPropertyAssignment({name:r,initializer:e}),!0)}if(!w(l,x(n,r,`cond`))){s.add(`cond`);let t=n.getProperty(r);if(t&&i.isPropertyAssignment(t))return t.setInitializer(e),!0}return!1}if(!o.has(r)){let e=Y(n,r,c);return e?M(e,r,a,E(e),s,c):!1}let u=n.getProperty(r);if(!u||!i.isPropertyAssignment(u))return!1;let f=u.getInitializer();if(!f||!i.isCallExpression(f))return!1;let m=f.getExpression();if(!i.isIdentifier(m)||m.getText()!==`cond`)return!1;let h=f.getArguments()[0];if(!h||!i.isObjectLiteralExpression(h))return!1;s.add(`cond`);let g=!1;for(let[n,r]of Object.entries(l)){let a=e(r);if(!a)continue;let o=h.getProperty(n);if(o||=h.getProperty(d(n)),!o||!i.isPropertyAssignment(o))continue;let c=o.getInitializer();if(c&&a===t.Translation){if(!i.isCallExpression(c))continue;let e=c.getExpression();if(!i.isIdentifier(e)||e.getText()!==`t`)continue;let n=c.getArguments()[0];if(!n||!i.isObjectLiteralExpression(n))continue;let a=r[t.Translation];if(!a||typeof a!=`object`)continue;let o={};for(let e of n.getProperties()){if(!i.isPropertyAssignment(e))continue;let t=e.getNameNode().getText().replace(/^['"]|['"]$/g,``),n=e.getInitializer();if(n&&i.isStringLiteral(n))o[t]=n.getLiteralValue();else if(n&&i.isArrayLiteralExpression(n)){let e=[];for(let t of n.getElements())i.isStringLiteral(t)&&e.push(t.getLiteralValue());o[t]=e}}if(!T(a,o)){s.add(`t`);for(let[e,t]of Object.entries(a)){let r=/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(e),a=r?e:JSON.stringify(e),o=n.getProperty(e);if(!o&&!r&&(o=n.getProperty(JSON.stringify(e))),o&&i.isPropertyAssignment(o)){let e=o.getInitializer(),n=Array.isArray(t)?`[${t.map(e=>JSON.stringify(e)).join(`, `)}]`:JSON.stringify(t);e?.getText()!==n&&(o.setInitializer(n),g=!0)}else if(!o){let e=Array.isArray(t)?`[${t.map(e=>JSON.stringify(e)).join(`, `)}]`:JSON.stringify(t);n.addPropertyAssignment({name:a,initializer:e}),g=!0}}}}}return g},N=(e,n,r,a,o,s)=>{let c=r[t.Gender];if(!Object.values(c).every(e=>typeof e==`string`))return!1;let l=m(c);if(!l)return!1;if(!a.has(n)){let t=Y(e,n,s);return t?N(t,n,r,E(t),o,s):(o.add(`gender`),e.addPropertyAssignment({name:n,initializer:l}),!0)}if(!w(c,x(e,n,`gender`))){o.add(`gender`);let t=e.getProperty(n);if(t&&i.isPropertyAssignment(t))return t.setInitializer(l),!0}return!1},P=(e,n,r,a,o,s)=>{let c=r[t.Insertion],l=h(c);if(!l)return!1;if(!a.has(n)){let t=Y(e,n,s);return t?P(t,n,r,E(t),o,s):(o.add(`insert`),e.addPropertyAssignment({name:n,initializer:l}),!0)}let u=e.getProperty(n);return u&&i.isPropertyAssignment(u)&&u.getInitializer()?.getText()!==l?(o.add(`insert`),u.setInitializer(l),!0):!1},F=(e,n,r,a,o,s)=>{let c=r[t.HTML],l=_(c);if(!l)return!1;if(!a.has(n)){let t=Y(e,n,s);return t?F(t,n,r,E(t),o,s):(o.add(`html`),e.addPropertyAssignment({name:n,initializer:l}),!0)}let u=e.getProperty(n);return u&&i.isPropertyAssignment(u)&&u.getInitializer()?.getText()!==l?(o.add(`html`),u.setInitializer(l),!0):!1},I=(n,r,a,o,s,c,u)=>{let d=a[t.Markdown],f=v(d);if(!f)return!1;if(!o.has(r)){let i=Y(n,r,u);if(i)return I(i,r,a,E(i),s,c,u);c.add(`md`);let o=e(d);return o===t.File?c.add(`file`):o===t.Translation&&c.add(`t`),n.addPropertyAssignment({name:r,initializer:f}),!0}let p=e(d),m=V(n,r),h=U(n,r),g=C(n,r,`t`);if(typeof d==`string`&&h&&s){let e={...h,[s]:d};c.add(`md`),c.add(`t`);let t=n.getProperty(r);return t&&i.isPropertyAssignment(t)?(t.setInitializer(`md(${l(e,g)})`),!0):!1}if(p===t.Translation){let e=d[t.Translation];if(!Object.values(e).every(e=>typeof e==`string`))return!1;if(!w(e,h)){c.add(`md`),c.add(`t`);let t=n.getProperty(r);if(t&&i.isPropertyAssignment(t))return t.setInitializer(`md(${l(e,g)})`),!0}return!1}if(!(typeof d==`string`&&m?.kind===`string`&&m.value===d||p===t.File&&m?.kind===`file`&&m.path===d[t.File])){c.add(`md`),p===t.File&&c.add(`file`);let e=n.getProperty(r);if(e&&i.isPropertyAssignment(e))return e.setInitializer(f),!0}return!1},L=(e,n,r,a,o,s)=>{let c=r[t.File],l=g(c);if(!l)return!1;if(!a.has(n)){let t=Y(e,n,s);return t?L(t,n,r,E(t),o,s):(o.add(`file`),e.addPropertyAssignment({name:n,initializer:l}),!0)}if(H(e,n)!==c){o.add(`file`);let t=e.getProperty(n);if(t&&i.isPropertyAssignment(t))return t.setInitializer(l),!0}return!1},R=(e,n,r,a,o,s)=>{let c=r[t.Nested],l=y(c);if(!l)return!1;if(!a.has(n)){let t=Y(e,n,s);return t?R(t,n,r,E(t),o,s):(o.add(`nest`),e.addPropertyAssignment({name:n,initializer:l}),!0)}let u=W(e,n);if(!(c&&u?.dictionaryKey===c.dictionaryKey&&u?.path===c.path)){o.add(`nest`);let t=e.getProperty(n);if(t&&i.isPropertyAssignment(t))return t.setInitializer(l),!0}return!1},z=(e,t,n,r,a,o,c)=>{let l,u=e.getProperty(t);if(u&&i.isPropertyAssignment(u)&&(l=u.getInitializerIfKind(s.ObjectLiteralExpression)),!l){let n=e.getProperty(t);if(n&&i.isShorthandPropertyAssignment(n))l=K(e.getSourceFile(),t);else if(u&&i.isPropertyAssignment(u)){let e=u.getInitializer();e&&(i.isIdentifier(e)?l=K(c,e.getText()):i.isPropertyAccessExpression(e)&&(l=q(c,e)))}}if(!l){let r=Y(e,t,c);if(r)return z(r,t,n,E(r),a,o,c);e.addPropertyAssignment({name:t,initializer:`{ }`});let u=e.getProperty(t);u&&i.isPropertyAssignment(u)&&(l=u.getInitializerIfKind(s.ObjectLiteralExpression))}return l?B(l,n,a,o,c):!1},B=(n,r,i,a,o)=>{let s=!1,c=E(n);for(let[l,u]of Object.entries(r)){if(Array.isArray(u)){D(n,l,u,c,i,a,o)&&(s=!0);continue}if(typeof u==`string`||typeof u==`number`||typeof u==`boolean`||u===null){O(n,l,u,c,i,a,o)&&(s=!0);continue}let r=e(u);if(r!==t.Text&&r!==t.Number&&r!==t.Boolean&&r!==t.Null&&k(n,l,u,c,i,a,o)){s=!0;continue}u&&typeof u==`object`&&!Array.isArray(u)&&!u.nodeType&&z(n,l,u,c,i,a,o)&&(s=!0)}return s},V=(e,t)=>{let n=e.getProperty(t);if(!n||!i.isPropertyAssignment(n))return;let r=n.getInitializer();if(r&&i.isCallExpression(r)){let e=r.getExpression();if(!i.isIdentifier(e))return;if(e.getText()===`md`){let e=r.getArguments()[0];if(!e)return;if(i.isStringLiteral(e))return{kind:`string`,value:e.getLiteralValue()};if(i.isCallExpression(e)){let t=e.getExpression();if(i.isIdentifier(t)&&t.getText()===`file`){let t=e.getArguments()[0];if(t&&i.isStringLiteral(t))return{kind:`file`,path:t.getLiteralValue()}}}}}},H=(e,t)=>{let n=e.getProperty(t);if(!n||!i.isPropertyAssignment(n))return;let r=n.getInitializer();if(!r||!i.isCallExpression(r))return;let a=r.getExpression();if(!i.isIdentifier(a)||a.getText()!==`file`)return;let o=r.getArguments()[0];if(o&&i.isStringLiteral(o))return o.getLiteralValue()},U=(e,t)=>{let n=e.getProperty(t);if(!n||!i.isPropertyAssignment(n))return;let r=n.getInitializer();if(r){if(i.isCallExpression(r)){let e=r.getExpression();if(i.isIdentifier(e)&&e.getText()===`md`){let e=r.getArguments()[0];if(e&&i.isCallExpression(e)){let t=e.getExpression();if(i.isIdentifier(t)&&t.getText()===`t`){let t=e.getArguments()[0];if(t&&i.isObjectLiteralExpression(t)){let e={};for(let n of t.getProperties()){if(!i.isPropertyAssignment(n))continue;let t=n.getNameNode().getText().replace(/^['"]|['"]$/g,``),r=n.getInitializer();if(r&&i.isStringLiteral(r))e[t]=r.getLiteralValue();else return}return e}}}}}if(i.isCallExpression(r)){let e=r.getExpression();if(i.isIdentifier(e)&&e.getText()===`t`){let e=r.getArguments()[0];if(e&&i.isObjectLiteralExpression(e)){let t={};for(let n of e.getProperties()){if(!i.isPropertyAssignment(n))continue;let e=n.getNameNode().getText().replace(/^['"]|['"]$/g,``),r=n.getInitializer();if(r&&i.isCallExpression(r)&&i.isIdentifier(r.getExpression())&&r.getExpression().getText()===`md`){let n=r.getArguments()[0];if(n&&i.isStringLiteral(n))t[e]=n.getLiteralValue();else return}else return}return t}}}}},W=(e,t)=>{let n=e.getProperty(t);if(!n||!i.isPropertyAssignment(n))return;let r=n.getInitializer();if(!r)return;let a=0;for(;a++<5&&!i.isCallExpression(r);){let e=r.getExpression?.();if(e&&typeof e==`object`&&e!==r){r=e;continue}break}if(!i.isCallExpression(r))return;let o=r.getExpression();if(!i.isIdentifier(o)||o.getText()!==`nest`)return;let[s,c]=r.getArguments();if(!s||!i.isStringLiteral(s))return;let l=s.getLiteralValue(),u;return c&&i.isStringLiteral(c)&&(u=c.getLiteralValue()),{dictionaryKey:l,path:u}},G=e=>{if(!e||typeof e!=`object`)return;let t=e,n=0;for(;n++<8;){if(i.isObjectLiteralExpression(t))return t;let e=t?.getExpression?.();if(e&&typeof e==`object`&&e!==t){t=e;continue}break}},K=(e,t)=>{let n=e.getVariableDeclaration(t);if(n){let e=G(n.getInitializer());if(e)return e}let r=e.getDescendants().find(e=>i.isIdentifier(e)&&e.getText()===t)?.getSymbol()?.getDeclarations()?.[0];if(r&&i.isVariableDeclaration(r)){let e=G(r.getInitializer());if(e)return e}},q=(e,t)=>{if(i.isIdentifier(t))return K(e,t.getText());if(i.isPropertyAccessExpression(t)){let n=q(e,t.getExpression());if(!n)return;let r=t.getName(),a=n.getProperty(r);if(a&&i.isPropertyAssignment(a)){let t=a.getInitializer(),n=G(t);if(n)return n;if(t&&i.isIdentifier(t))return K(e,t.getText())}}},J=(e,t)=>{let n=[];for(let r of e.getProperties())if(i.isSpreadAssignment(r)){let e=q(t,r.getExpression());e&&n.push(e)}return n},Y=(e,t,n)=>{let r=J(e,n);for(let e=r.length-1;e>=0;e--){let n=r[e],a=n.getProperty(t);if(a&&i.isPropertyAssignment(a))return n}},X=(e,t)=>{let n=e.getProperty(t);if(!n||!i.isPropertyAssignment(n))return;let r=n.getInitializer();if(!r||!i.isArrayLiteralExpression(r))return;let a=[];for(let e of r.getElements()){if(i.isStringLiteral(e)){a.push(JSON.stringify(e.getLiteralValue()));continue}if(i.isNumericLiteral(e)){a.push(e.getText());continue}if(e.getKind()===s.TrueKeyword||e.getKind()===s.FalseKeyword){a.push(e.getText());continue}if(i.isNullLiteral(e)){a.push(`null`);continue}if(i.isCallExpression(e)){let t=e.getExpression();if(i.isIdentifier(t)&&t.getText()===`t`){let t=e.getArguments()[0];if(t&&i.isObjectLiteralExpression(t)){let e={};for(let n of t.getProperties()){if(!i.isPropertyAssignment(n))return;let t=n.getNameNode().getText().replace(/^['"]|['"]$/g,``),r=n.getInitializer();if(r&&i.isStringLiteral(r))e[t]=r.getLiteralValue();else return}a.push(l(e));continue}}}return}return a},Z=n=>{let r=e(n);if(r===t.Text)return JSON.stringify(n);if(r===t.Number||r===t.Boolean)return String(n);if(r===t.Null)return`null`;if(r===t.Translation){let e=n[t.Translation]??{};return Object.values(e).every(e=>typeof e==`string`)?l(e):void 0}if(r===t.Enumeration){let e=n[t.Enumeration];return f(e)}if(r===t.Condition){let e=n[t.Condition];return p(e)}if(r===t.Gender){let e=n[t.Gender];return m(e)}if(r===t.Insertion){let e=n[t.Insertion];return h(e)}if(r===t.HTML){let e=n[t.HTML];return _(e)}if(r===t.Markdown){let e=n[t.Markdown];return v(e)}if(r===t.File){let e=n[t.File];return g(e)}if(r===t.Nested){let e=n[t.Nested];return y(e)}},Q=e=>{let t=new Set;for(let n of e.getImportDeclarations()){let e=n.getModuleSpecifierValue();if(e===`intlayer`){let e=n.getNamedImports();for(let n of e)t.add(n.getName())}if(e===`intlayer/file`){let e=n.getNamedImports();for(let n of e){let e=n.getAliasNode();t.add(e?e.getText():n.getName())}}}return t},$=(e,t)=>{if(t.size===0)return!1;let n=Q(e),r=[...t].filter(e=>!n.has(e));if(r.length===0)return!1;let i=r.includes(`file`),a=r.filter(e=>e!==`file`);if(a.length>0){let t=e.getImportDeclarations().find(e=>e.getModuleSpecifierValue()===`intlayer`);if(t){let e=t.getNamedImports().map(e=>e.getName()),n=[...new Set([...e,...a])].sort();t.removeNamedImports(),t.addNamedImports(n.map(e=>({name:e})))}else e.insertImportDeclaration(0,{moduleSpecifier:`intlayer`,namedImports:a.sort().map(e=>({name:e}))})}if(i&&!e.getImportDeclarations().find(e=>e.getModuleSpecifierValue()===`intlayer/file`)){let t=e.getImportDeclarations().findIndex(e=>e.getModuleSpecifierValue()===`intlayer`),n=t>=0?t+1:0;e.insertImportDeclaration(n,{moduleSpecifier:`intlayer/file`,namedImports:[{name:`file`}]})}return!0},ee=e=>{if(e.getImportDeclarations().length>0||e.getExportDeclarations().length>0||e.getExportAssignments().length>0)return!1;for(let t of e.getStatements()){if(!i.isExpressionStatement(t))continue;let e=t.getExpression();if(!i.isBinaryExpression(e))continue;let n=e.getLeft();if(!i.isPropertyAccessExpression(n))continue;let r=n.getExpression(),a=n.getName(),o=i.isIdentifier(r)&&r.getText()===`module`&&a===`exports`,s=i.isIdentifier(r)&&r.getText()===`exports`;if(o||s)return!0}return e.getDescendantsOfKind(s.CallExpression).some(e=>{let t=e.getExpression();return i.isIdentifier(t)&&t.getText()===`require`})},te=(e,t)=>{if(t.size===0)return!1;let n=new Set,r=!1;for(let t of e.getVariableDeclarations()){let e=t.getInitializer();if(!e||!i.isCallExpression(e))continue;let a=e.getExpression();if(!i.isIdentifier(a)||a.getText()!==`require`)continue;let o=e.getArguments()[0];if(!o||!i.isStringLiteral(o))continue;let s=o.getLiteralValue(),c=t.getNameNode();if(s===`intlayer`&&i.isObjectBindingPattern(c))for(let e of c.getElements())n.add(e.getNameNode().getText());if(s===`intlayer/file`)if(i.isObjectBindingPattern(c))for(let e of c.getElements())e.getNameNode().getText()===`file`&&(r=!0);else i.isIdentifier(c)&&c.getText()===`file`&&(r=!0)}let a=Array.from(t).filter(e=>e!==`file`).filter(e=>!n.has(e)),o=t.has(`file`)&&!r;if(a.length===0&&!o)return!1;let s=0,c=e.getStatements();for(let e of c){if(i.isExpressionStatement(e)){let t=e.getExpression();if(i.isStringLiteral(t)){s+=1;continue}}break}let l=[];if(a.length>0){let e=Array.from(new Set(a)).sort();l.push(`const { ${e.join(`, `)} } = require('intlayer');`)}return o&&l.push(`const { file } = require('intlayer/file');`),l.length>0?(e.insertStatements(s,l.join(`
2
+ `)),!0):!1},ne=e=>Array.isArray(e)?`[${e.map(e=>JSON.stringify(e)).join(`, `)}]`:typeof e==`boolean`||typeof e==`number`?String(e):JSON.stringify(e),re=(e,t,n)=>{let r=e.getProperty(t)||e.getProperty(`'${t}'`)||e.getProperty(`"${t}"`),a=ne(n);if(r&&i.isPropertyAssignment(r)){if(r.getInitializer()?.getText()!==a)return r.setInitializer(a),!0}else if(!r)return e.addPropertyAssignment({name:t,initializer:a}),!0;return!1},ie=(e,t)=>{let n=!1;for(let r of[`id`,`locale`,`filled`,`fill`,`title`,`description`,`tags`,`version`,`priority`,`contentAutoTransformation`]){let i=t[r];i!==void 0&&re(e,r,i)&&(n=!0)}return n},ae=e=>{let t=e.getExportAssignment(e=>!0);if(t){let n=t.getExpression();if(i.isIdentifier(n)){let t=n.getSymbol()?.getDeclarations()?.[0]??e.getVariableDeclaration(n.getText());if(t&&i.isVariableDeclaration(t)){let e=G(t.getInitializer());if(e)return e}}else{let e=G(n);if(e)return e}}let n=e.getVariableDeclaration(e=>{try{return e.getType().getText().includes(`Dictionary`)||e.getName()===`content`||e.getName().toLowerCase().includes(`dictionary`)}catch{return e.getName()===`content`}});if(n){let e=G(n.getInitializer());if(e)return e}for(let t of e.getStatements()){if(!i.isExpressionStatement(t))continue;let e=t.getExpression();if(!i.isBinaryExpression(e)||e.getOperatorToken().getText()!==`=`)continue;let n=e.getLeft();if(!i.isPropertyAccessExpression(n))continue;let r=n.getExpression(),a=n.getName(),o=i.isIdentifier(r)&&r.getText()===`module`&&a===`exports`,s=i.isIdentifier(r)&&r.getText()===`exports`&&a===`default`;if(!o&&!s)continue;let c=e.getRight();if(i.isObjectLiteralExpression(c))return c;if(i.isIdentifier(c)){let e=c.getSymbol()?.getDeclarations()?.[0];if(e&&i.isVariableDeclaration(e)){let t=G(e.getInitializer());if(t)return t}}}},oe=async(t,l,u)=>{try{if(!l||typeof l!=`object`)return t;let d=new a({useInMemoryFileSystem:!0,skipAddingFilesFromTsConfig:!0,skipFileDependencyResolution:!0,compilerOptions:{allowJs:!0,jsx:c.JsxEmit.Preserve},manipulationSettings:{indentationText:n.TwoSpaces,quoteKind:o.Double,newLineKind:r.LineFeed}}).createSourceFile(`file.tsx`,t,{overwrite:!0}),f=ae(d);if(!f)return t;let p=!1,m=new Set;if(ie(f,l)&&(p=!0),l.content){let t=f.getProperty(`content`),n,r=!1,a=!1;if(t&&i.isPropertyAssignment(t)){n=t.getInitializerIfKind(s.ObjectLiteralExpression),r=!!t.getInitializerIfKind(s.ArrayLiteralExpression);let e=t.getInitializer();a=!!e&&i.isCallExpression(e)}let o=u??`en`;if(n&&!Array.isArray(l.content)){let e=l.content??{};B(n,e,o,m,d)&&(p=!0)}else Array.isArray(l.content)&&r?D(f,`content`,l.content??[],E(f),o,m,d)&&(p=!0):a&&e(l.content)&&k(f,`content`,l.content,E(f),o,m,d)&&(p=!0)}return p&&((ee(d)?te(d,m):$(d,m))||p)?d.getFullText():t}catch{return t}};export{oe as transformJSFile};
1774
3
  //# sourceMappingURL=transformJSFile.mjs.map