@intlayer/cli 6.1.6 → 7.0.0-canary.1

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 (287) hide show
  1. package/dist/cjs/IntlayerEventListener.cjs +187 -239
  2. package/dist/cjs/IntlayerEventListener.cjs.map +1 -1
  3. package/dist/cjs/_virtual/_utils_asset.cjs +104 -0
  4. package/dist/cjs/_virtual/rolldown_runtime.cjs +25 -0
  5. package/dist/cjs/build.cjs +23 -42
  6. package/dist/cjs/build.cjs.map +1 -1
  7. package/dist/cjs/cli.cjs +370 -431
  8. package/dist/cjs/cli.cjs.map +1 -1
  9. package/dist/cjs/config.cjs +10 -35
  10. package/dist/cjs/config.cjs.map +1 -1
  11. package/dist/cjs/editor.cjs +51 -66
  12. package/dist/cjs/editor.cjs.map +1 -1
  13. package/dist/cjs/fill/fill.cjs +73 -301
  14. package/dist/cjs/fill/fill.cjs.map +1 -1
  15. package/dist/cjs/fill/formatAutoFillData.cjs +48 -106
  16. package/dist/cjs/fill/formatAutoFillData.cjs.map +1 -1
  17. package/dist/cjs/fill/formatAutoFilledFilePath.cjs +27 -43
  18. package/dist/cjs/fill/formatAutoFilledFilePath.cjs.map +1 -1
  19. package/dist/cjs/fill/formatFillData.cjs +50 -0
  20. package/dist/cjs/fill/formatFillData.cjs.map +1 -0
  21. package/dist/cjs/fill/groupLimiter.cjs +42 -0
  22. package/dist/cjs/fill/groupLimiter.cjs.map +1 -0
  23. package/dist/cjs/fill/index.cjs +5 -25
  24. package/dist/cjs/fill/listTranslationsTasks.cjs +77 -0
  25. package/dist/cjs/fill/listTranslationsTasks.cjs.map +1 -0
  26. package/dist/cjs/fill/test-original-case.cjs +10 -0
  27. package/dist/cjs/fill/test-original-case.cjs.map +1 -0
  28. package/dist/cjs/fill/translateDictionary.cjs +148 -0
  29. package/dist/cjs/fill/translateDictionary.cjs.map +1 -0
  30. package/dist/cjs/fill/writeAutoFill.cjs +48 -0
  31. package/dist/cjs/fill/writeAutoFill.cjs.map +1 -0
  32. package/dist/cjs/fill/writeFill.cjs +50 -0
  33. package/dist/cjs/fill/writeFill.cjs.map +1 -0
  34. package/dist/cjs/getTargetDictionary.cjs +36 -76
  35. package/dist/cjs/getTargetDictionary.cjs.map +1 -1
  36. package/dist/cjs/index.cjs +31 -45
  37. package/dist/cjs/listContentDeclaration.cjs +36 -64
  38. package/dist/cjs/listContentDeclaration.cjs.map +1 -1
  39. package/dist/cjs/liveSync.cjs +146 -221
  40. package/dist/cjs/liveSync.cjs.map +1 -1
  41. package/dist/cjs/pull.cjs +132 -195
  42. package/dist/cjs/pull.cjs.map +1 -1
  43. package/dist/cjs/push/pullLog.cjs +101 -144
  44. package/dist/cjs/push/pullLog.cjs.map +1 -1
  45. package/dist/cjs/push/push.cjs +159 -249
  46. package/dist/cjs/push/push.cjs.map +1 -1
  47. package/dist/cjs/pushConfig.cjs +18 -45
  48. package/dist/cjs/pushConfig.cjs.map +1 -1
  49. package/dist/cjs/pushLog.cjs +87 -128
  50. package/dist/cjs/pushLog.cjs.map +1 -1
  51. package/dist/cjs/reviewDoc.cjs +183 -291
  52. package/dist/cjs/reviewDoc.cjs.map +1 -1
  53. package/dist/cjs/test/index.cjs +52 -91
  54. package/dist/cjs/test/index.cjs.map +1 -1
  55. package/dist/cjs/test/listMissingTranslations.cjs +35 -62
  56. package/dist/cjs/test/listMissingTranslations.cjs.map +1 -1
  57. package/dist/cjs/translateDoc.cjs +127 -221
  58. package/dist/cjs/translateDoc.cjs.map +1 -1
  59. package/dist/cjs/utils/calculateChunks.cjs +85 -115
  60. package/dist/cjs/utils/calculateChunks.cjs.map +1 -1
  61. package/dist/cjs/utils/checkAccess.cjs +33 -72
  62. package/dist/cjs/utils/checkAccess.cjs.map +1 -1
  63. package/dist/cjs/utils/checkFileModifiedRange.cjs +75 -91
  64. package/dist/cjs/utils/checkFileModifiedRange.cjs.map +1 -1
  65. package/dist/cjs/utils/checkLastUpdateTime.cjs +17 -30
  66. package/dist/cjs/utils/checkLastUpdateTime.cjs.map +1 -1
  67. package/dist/cjs/utils/chunkInference.cjs +28 -47
  68. package/dist/cjs/utils/chunkInference.cjs.map +1 -1
  69. package/dist/cjs/utils/fixChunkStartEndChars.cjs +23 -42
  70. package/dist/cjs/utils/fixChunkStartEndChars.cjs.map +1 -1
  71. package/dist/cjs/utils/formatTimeDiff.cjs +17 -43
  72. package/dist/cjs/utils/formatTimeDiff.cjs.map +1 -1
  73. package/dist/cjs/utils/getIsFileUpdatedRecently.cjs +13 -32
  74. package/dist/cjs/utils/getIsFileUpdatedRecently.cjs.map +1 -1
  75. package/dist/cjs/utils/getOutputFilePath.cjs +71 -86
  76. package/dist/cjs/utils/getOutputFilePath.cjs.map +1 -1
  77. package/dist/cjs/utils/getParentPackageJSON.cjs +19 -44
  78. package/dist/cjs/utils/getParentPackageJSON.cjs.map +1 -1
  79. package/dist/cjs/utils/listSpecialChars.cjs +49 -73
  80. package/dist/cjs/utils/listSpecialChars.cjs.map +1 -1
  81. package/dist/cjs/utils/mapChunksBetweenFiles.cjs +90 -132
  82. package/dist/cjs/utils/mapChunksBetweenFiles.cjs.map +1 -1
  83. package/dist/cjs/utils/reorderParagraphs.cjs +86 -118
  84. package/dist/cjs/utils/reorderParagraphs.cjs.map +1 -1
  85. package/dist/cjs/watch.cjs +21 -39
  86. package/dist/cjs/watch.cjs.map +1 -1
  87. package/dist/esm/IntlayerEventListener.mjs +178 -201
  88. package/dist/esm/IntlayerEventListener.mjs.map +1 -1
  89. package/dist/esm/_virtual/_utils_asset.mjs +100 -0
  90. package/dist/esm/_virtual/rolldown_runtime.mjs +8 -0
  91. package/dist/esm/build.mjs +20 -23
  92. package/dist/esm/build.mjs.map +1 -1
  93. package/dist/esm/cli.mjs +349 -380
  94. package/dist/esm/cli.mjs.map +1 -1
  95. package/dist/esm/config.mjs +8 -14
  96. package/dist/esm/config.mjs.map +1 -1
  97. package/dist/esm/editor.mjs +47 -41
  98. package/dist/esm/editor.mjs.map +1 -1
  99. package/dist/esm/fill/fill.mjs +68 -300
  100. package/dist/esm/fill/fill.mjs.map +1 -1
  101. package/dist/esm/fill/formatAutoFillData.mjs +47 -81
  102. package/dist/esm/fill/formatAutoFillData.mjs.map +1 -1
  103. package/dist/esm/fill/formatAutoFilledFilePath.mjs +25 -19
  104. package/dist/esm/fill/formatAutoFilledFilePath.mjs.map +1 -1
  105. package/dist/esm/fill/formatFillData.mjs +50 -0
  106. package/dist/esm/fill/formatFillData.mjs.map +1 -0
  107. package/dist/esm/fill/groupLimiter.mjs +40 -0
  108. package/dist/esm/fill/groupLimiter.mjs.map +1 -0
  109. package/dist/esm/fill/index.mjs +4 -3
  110. package/dist/esm/fill/listTranslationsTasks.mjs +70 -0
  111. package/dist/esm/fill/listTranslationsTasks.mjs.map +1 -0
  112. package/dist/esm/fill/test-original-case.mjs +10 -0
  113. package/dist/esm/fill/test-original-case.mjs.map +1 -0
  114. package/dist/esm/fill/translateDictionary.mjs +141 -0
  115. package/dist/esm/fill/translateDictionary.mjs.map +1 -0
  116. package/dist/esm/fill/writeAutoFill.mjs +44 -0
  117. package/dist/esm/fill/writeAutoFill.mjs.map +1 -0
  118. package/dist/esm/fill/writeFill.mjs +45 -0
  119. package/dist/esm/fill/writeFill.mjs.map +1 -0
  120. package/dist/esm/getTargetDictionary.mjs +27 -48
  121. package/dist/esm/getTargetDictionary.mjs.map +1 -1
  122. package/dist/esm/index.mjs +15 -13
  123. package/dist/esm/listContentDeclaration.mjs +28 -43
  124. package/dist/esm/listContentDeclaration.mjs.map +1 -1
  125. package/dist/esm/liveSync.mjs +135 -187
  126. package/dist/esm/liveSync.mjs.map +1 -1
  127. package/dist/esm/pull.mjs +125 -178
  128. package/dist/esm/pull.mjs.map +1 -1
  129. package/dist/esm/push/pullLog.mjs +99 -125
  130. package/dist/esm/push/pullLog.mjs.map +1 -1
  131. package/dist/esm/push/push.mjs +149 -221
  132. package/dist/esm/push/push.mjs.map +1 -1
  133. package/dist/esm/pushConfig.mjs +14 -23
  134. package/dist/esm/pushConfig.mjs.map +1 -1
  135. package/dist/esm/pushLog.mjs +85 -109
  136. package/dist/esm/pushLog.mjs.map +1 -1
  137. package/dist/esm/reviewDoc.mjs +167 -264
  138. package/dist/esm/reviewDoc.mjs.map +1 -1
  139. package/dist/esm/test/index.mjs +47 -73
  140. package/dist/esm/test/index.mjs.map +1 -1
  141. package/dist/esm/test/listMissingTranslations.mjs +30 -41
  142. package/dist/esm/test/listMissingTranslations.mjs.map +1 -1
  143. package/dist/esm/translateDoc.mjs +114 -193
  144. package/dist/esm/translateDoc.mjs.map +1 -1
  145. package/dist/esm/utils/calculateChunks.mjs +83 -91
  146. package/dist/esm/utils/calculateChunks.mjs.map +1 -1
  147. package/dist/esm/utils/checkAccess.mjs +28 -46
  148. package/dist/esm/utils/checkAccess.mjs.map +1 -1
  149. package/dist/esm/utils/checkFileModifiedRange.mjs +73 -65
  150. package/dist/esm/utils/checkFileModifiedRange.mjs.map +1 -1
  151. package/dist/esm/utils/checkLastUpdateTime.mjs +15 -6
  152. package/dist/esm/utils/checkLastUpdateTime.mjs.map +1 -1
  153. package/dist/esm/utils/chunkInference.mjs +24 -24
  154. package/dist/esm/utils/chunkInference.mjs.map +1 -1
  155. package/dist/esm/utils/fixChunkStartEndChars.mjs +22 -18
  156. package/dist/esm/utils/fixChunkStartEndChars.mjs.map +1 -1
  157. package/dist/esm/utils/formatTimeDiff.mjs +16 -19
  158. package/dist/esm/utils/formatTimeDiff.mjs.map +1 -1
  159. package/dist/esm/utils/getIsFileUpdatedRecently.mjs +11 -8
  160. package/dist/esm/utils/getIsFileUpdatedRecently.mjs.map +1 -1
  161. package/dist/esm/utils/getOutputFilePath.mjs +70 -62
  162. package/dist/esm/utils/getOutputFilePath.mjs.map +1 -1
  163. package/dist/esm/utils/getParentPackageJSON.mjs +16 -20
  164. package/dist/esm/utils/getParentPackageJSON.mjs.map +1 -1
  165. package/dist/esm/utils/listSpecialChars.mjs +48 -49
  166. package/dist/esm/utils/listSpecialChars.mjs.map +1 -1
  167. package/dist/esm/utils/mapChunksBetweenFiles.mjs +87 -107
  168. package/dist/esm/utils/mapChunksBetweenFiles.mjs.map +1 -1
  169. package/dist/esm/utils/reorderParagraphs.mjs +85 -93
  170. package/dist/esm/utils/reorderParagraphs.mjs.map +1 -1
  171. package/dist/esm/watch.mjs +17 -17
  172. package/dist/esm/watch.mjs.map +1 -1
  173. package/dist/types/IntlayerEventListener.d.ts +63 -59
  174. package/dist/types/IntlayerEventListener.d.ts.map +1 -1
  175. package/dist/types/build.d.ts +10 -7
  176. package/dist/types/build.d.ts.map +1 -1
  177. package/dist/types/cli.d.ts +13 -10
  178. package/dist/types/cli.d.ts.map +1 -1
  179. package/dist/types/config.d.ts +7 -4
  180. package/dist/types/config.d.ts.map +1 -1
  181. package/dist/types/editor.d.ts +6 -4
  182. package/dist/types/editor.d.ts.map +1 -1
  183. package/dist/types/fill/fill.d.ts +20 -14
  184. package/dist/types/fill/fill.d.ts.map +1 -1
  185. package/dist/types/fill/formatAutoFillData.d.ts +10 -7
  186. package/dist/types/fill/formatAutoFillData.d.ts.map +1 -1
  187. package/dist/types/fill/formatAutoFilledFilePath.d.ts +6 -2
  188. package/dist/types/fill/formatAutoFilledFilePath.d.ts.map +1 -1
  189. package/dist/types/fill/formatFillData.d.ts +12 -0
  190. package/dist/types/fill/formatFillData.d.ts.map +1 -0
  191. package/dist/types/fill/groupLimiter.d.ts +15 -0
  192. package/dist/types/fill/groupLimiter.d.ts.map +1 -0
  193. package/dist/types/fill/index.d.ts +3 -3
  194. package/dist/types/fill/listTranslationsTasks.d.ts +15 -0
  195. package/dist/types/fill/listTranslationsTasks.d.ts.map +1 -0
  196. package/dist/types/fill/test-original-case.d.ts +1 -0
  197. package/dist/types/fill/translateDictionary.d.ts +19 -0
  198. package/dist/types/fill/translateDictionary.d.ts.map +1 -0
  199. package/dist/types/fill/writeAutoFill.d.ts +7 -0
  200. package/dist/types/fill/writeAutoFill.d.ts.map +1 -0
  201. package/dist/types/fill/writeFill.d.ts +7 -0
  202. package/dist/types/fill/writeFill.d.ts.map +1 -0
  203. package/dist/types/getTargetDictionary.d.ts +17 -13
  204. package/dist/types/getTargetDictionary.d.ts.map +1 -1
  205. package/dist/types/index.d.ts +15 -14
  206. package/dist/types/listContentDeclaration.d.ts +10 -7
  207. package/dist/types/listContentDeclaration.d.ts.map +1 -1
  208. package/dist/types/liveSync.d.ts +8 -3
  209. package/dist/types/liveSync.d.ts.map +1 -1
  210. package/dist/types/pull.d.ts +9 -6
  211. package/dist/types/pull.d.ts.map +1 -1
  212. package/dist/types/push/pullLog.d.ts +25 -21
  213. package/dist/types/push/pullLog.d.ts.map +1 -1
  214. package/dist/types/push/push.d.ts +13 -9
  215. package/dist/types/push/push.d.ts.map +1 -1
  216. package/dist/types/pushConfig.d.ts +7 -4
  217. package/dist/types/pushConfig.d.ts.map +1 -1
  218. package/dist/types/pushLog.d.ts +23 -20
  219. package/dist/types/pushLog.d.ts.map +1 -1
  220. package/dist/types/reviewDoc.d.ts +34 -17
  221. package/dist/types/reviewDoc.d.ts.map +1 -1
  222. package/dist/types/test/index.d.ts +9 -5
  223. package/dist/types/test/index.d.ts.map +1 -1
  224. package/dist/types/test/listMissingTranslations.d.ts +14 -10
  225. package/dist/types/test/listMissingTranslations.d.ts.map +1 -1
  226. package/dist/types/translateDoc.d.ts +34 -17
  227. package/dist/types/translateDoc.d.ts.map +1 -1
  228. package/dist/types/utils/calculateChunks.d.ts +10 -7
  229. package/dist/types/utils/calculateChunks.d.ts.map +1 -1
  230. package/dist/types/utils/checkAccess.d.ts +8 -4
  231. package/dist/types/utils/checkAccess.d.ts.map +1 -1
  232. package/dist/types/utils/checkFileModifiedRange.d.ts +8 -6
  233. package/dist/types/utils/checkFileModifiedRange.d.ts.map +1 -1
  234. package/dist/types/utils/checkLastUpdateTime.d.ts +4 -1
  235. package/dist/types/utils/checkLastUpdateTime.d.ts.map +1 -1
  236. package/dist/types/utils/chunkInference.d.ts +9 -6
  237. package/dist/types/utils/chunkInference.d.ts.map +1 -1
  238. package/dist/types/utils/fixChunkStartEndChars.d.ts +4 -1
  239. package/dist/types/utils/fixChunkStartEndChars.d.ts.map +1 -1
  240. package/dist/types/utils/formatTimeDiff.d.ts +4 -1
  241. package/dist/types/utils/formatTimeDiff.d.ts.map +1 -1
  242. package/dist/types/utils/getIsFileUpdatedRecently.d.ts +4 -1
  243. package/dist/types/utils/getIsFileUpdatedRecently.d.ts.map +1 -1
  244. package/dist/types/utils/getOutputFilePath.d.ts +7 -2
  245. package/dist/types/utils/getOutputFilePath.d.ts.map +1 -1
  246. package/dist/types/utils/getParentPackageJSON.d.ts +30 -28
  247. package/dist/types/utils/getParentPackageJSON.d.ts.map +1 -1
  248. package/dist/types/utils/listSpecialChars.d.ts +9 -7
  249. package/dist/types/utils/listSpecialChars.d.ts.map +1 -1
  250. package/dist/types/utils/mapChunksBetweenFiles.d.ts +10 -6
  251. package/dist/types/utils/mapChunksBetweenFiles.d.ts.map +1 -1
  252. package/dist/types/utils/reorderParagraphs.d.ts +4 -1
  253. package/dist/types/utils/reorderParagraphs.d.ts.map +1 -1
  254. package/dist/types/watch.d.ts +9 -6
  255. package/dist/types/watch.d.ts.map +1 -1
  256. package/package.json +42 -47
  257. package/LICENSE +0 -202
  258. package/dist/cjs/fill/autoFill.cjs +0 -105
  259. package/dist/cjs/fill/autoFill.cjs.map +0 -1
  260. package/dist/cjs/fill/index.cjs.map +0 -1
  261. package/dist/cjs/index.cjs.map +0 -1
  262. package/dist/cjs/utils/getChunk.cjs +0 -77
  263. package/dist/cjs/utils/getChunk.cjs.map +0 -1
  264. package/dist/cjs/utils/splitTextByLine.cjs +0 -35
  265. package/dist/cjs/utils/splitTextByLine.cjs.map +0 -1
  266. package/dist/esm/fill/autoFill.mjs +0 -92
  267. package/dist/esm/fill/autoFill.mjs.map +0 -1
  268. package/dist/esm/fill/index.mjs.map +0 -1
  269. package/dist/esm/index.mjs.map +0 -1
  270. package/dist/esm/prompts/REVIEW_PROMPT.md +0 -37
  271. package/dist/esm/prompts/TRANSLATE_PROMPT.md +0 -38
  272. package/dist/esm/utils/calculrateChunkTest.md +0 -9
  273. package/dist/esm/utils/getChunk.mjs +0 -53
  274. package/dist/esm/utils/getChunk.mjs.map +0 -1
  275. package/dist/esm/utils/splitTextByLine.mjs +0 -11
  276. package/dist/esm/utils/splitTextByLine.mjs.map +0 -1
  277. package/dist/types/fill/autoFill.d.ts +0 -4
  278. package/dist/types/fill/autoFill.d.ts.map +0 -1
  279. package/dist/types/fill/index.d.ts.map +0 -1
  280. package/dist/types/index.d.ts.map +0 -1
  281. package/dist/types/utils/getChunk.d.ts +0 -9
  282. package/dist/types/utils/getChunk.d.ts.map +0 -1
  283. package/dist/types/utils/splitTextByLine.d.ts +0 -2
  284. package/dist/types/utils/splitTextByLine.d.ts.map +0 -1
  285. /package/dist/{cjs → assets}/prompts/REVIEW_PROMPT.md +0 -0
  286. /package/dist/{cjs → assets}/prompts/TRANSLATE_PROMPT.md +0 -0
  287. /package/dist/{cjs/utils/calculrateChunkTest.md → assets/utils/_calculateChunkTest.md} +0 -0
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/translateDoc.ts"],"sourcesContent":["import { AIOptions } from '@intlayer/api';\nimport {\n formatLocale,\n formatPath,\n listGitFiles,\n ListGitFilesOptions,\n parallelize,\n} from '@intlayer/chokidar';\nimport {\n ANSIColors,\n colon,\n colorize,\n colorizeNumber,\n getAppLogger,\n getConfiguration,\n GetConfigurationOptions,\n IntlayerConfig,\n Locales,\n retryManager,\n} from '@intlayer/config';\nimport fg from 'fast-glob';\nimport { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { readFile } from 'fs/promises';\nimport { dirname, join, relative } from 'path';\nimport { fileURLToPath } from 'url';\nimport { chunkText } from './utils/calculateChunks';\nimport { checkAIAccess } from './utils/checkAccess';\nimport { checkFileModifiedRange } from './utils/checkFileModifiedRange';\nimport { chunkInference } from './utils/chunkInference';\nimport { fixChunkStartEndChars } from './utils/fixChunkStartEndChars';\nimport { getChunk } from './utils/getChunk';\nimport { getOutputFilePath } from './utils/getOutputFilePath';\n\nconst isESModule = typeof import.meta.url === 'string';\n\nconst dir = isESModule ? dirname(fileURLToPath(import.meta.url)) : __dirname;\n\n/**\n * Translate a single file for a given locale\n */\nexport const translateFile = async (\n baseFilePath: string,\n outputFilePath: string,\n locale: Locales,\n baseLocale: Locales,\n aiOptions?: AIOptions,\n configuration: IntlayerConfig = getConfiguration(),\n customInstructions?: string\n) => {\n try {\n const appLogger = getAppLogger(configuration, {\n config: {\n prefix: '',\n },\n });\n\n // Determine the target locale file path\n const fileContent = await readFile(baseFilePath, 'utf-8');\n let fileResultContent = fileContent;\n\n // Prepare the base prompt for ChatGPT\n const basePrompt = (\n await readFile(join(dir, './prompts/TRANSLATE_PROMPT.md'), 'utf-8')\n )\n .replaceAll('{{localeName}}', `${formatLocale(locale, false)}`)\n .replaceAll('{{baseLocaleName}}', `${formatLocale(baseLocale, false)}`)\n .replace('{{applicationContext}}', aiOptions?.applicationContext ?? '-')\n .replace('{{customInstructions}}', customInstructions ?? '-');\n\n const filePrexixText = `${ANSIColors.GREY_DARK}[${formatPath(baseFilePath)}${ANSIColors.GREY_DARK}] `;\n const filePrefix = [\n colon(filePrexixText, { colSize: 40 }),\n `→ ${ANSIColors.RESET}`,\n ].join('');\n\n const prefixText = `${ANSIColors.GREY_DARK}[${formatPath(baseFilePath)}${ANSIColors.GREY_DARK}][${formatLocale(locale)}${ANSIColors.GREY_DARK}] `;\n const prefix = [\n colon(prefixText, { colSize: 40 }),\n `→ ${ANSIColors.RESET}`,\n ].join('');\n\n // 1. Chunk the file by number of lines instead of characters\n const chunks = chunkText(fileContent);\n appLogger(\n `${filePrefix}Base file splitted into ${colorizeNumber(chunks.length)} chunks`\n );\n\n for await (const [i, chunk] of chunks.entries()) {\n const isFirstChunk = i === 0;\n\n // Build the chunk-specific prompt\n const getPrevChunkPrompt = () =>\n `**CHUNK ${i} of ${chunks.length}** that has been translated in ${formatLocale(locale)}:\\n` +\n `///chunkStart///` +\n getChunk(fileResultContent, chunks[i - 1]) +\n `///chunkEnd///`;\n\n const getBaseChunkContextPrompt = () =>\n `**CHUNK ${i + 1} to ${Math.min(i + 3, chunks.length)} of ${chunks.length}** is the base chunk in ${formatLocale(baseLocale, false)} as reference.\\n` +\n `///chunksStart///` +\n (chunks[i - 1]?.content ?? '') +\n chunks[i].content +\n (chunks[i + 1]?.content ?? '') +\n `///chunksEnd///`;\n\n const fileToTranslateCurrentChunk = chunk.content;\n\n // Make the actual translation call\n let chunkTranslation = await retryManager(async () => {\n const result = await chunkInference(\n [\n { role: 'system', content: basePrompt },\n\n { role: 'system', content: getBaseChunkContextPrompt() },\n ...(isFirstChunk\n ? []\n : [{ role: 'system', content: getPrevChunkPrompt() } as const]),\n {\n role: 'system',\n content: `The next user message will be the **CHUNK ${colorizeNumber(i + 1)} of ${colorizeNumber(chunks.length)}** in ${formatLocale(baseLocale, false)} to translate in ${formatLocale(locale, false)}:`,\n },\n { role: 'user', content: fileToTranslateCurrentChunk },\n ],\n aiOptions,\n configuration\n );\n\n appLogger(\n [\n `${prefix}`,\n `${ANSIColors.GREY_DARK}[Chunk `,\n colorizeNumber(i + 1),\n `${ANSIColors.GREY_DARK} of `,\n colorizeNumber(chunks.length),\n `${ANSIColors.GREY_DARK}] →${ANSIColors.RESET} `,\n `${colorizeNumber(result.tokenUsed)} tokens used`,\n ].join('')\n );\n\n const fixedTranslatedChunkResult = fixChunkStartEndChars(\n result?.fileContent,\n fileToTranslateCurrentChunk\n );\n\n return fixedTranslatedChunkResult;\n })();\n\n // Replace the chunk in the file content\n fileResultContent = fileResultContent.replace(\n fileToTranslateCurrentChunk,\n chunkTranslation\n );\n }\n\n // 4. Write the final translation to the appropriate file path\n mkdirSync(dirname(outputFilePath), { recursive: true });\n writeFileSync(outputFilePath, fileResultContent);\n\n const relativePath = relative(\n configuration.content.baseDir,\n outputFilePath\n );\n\n appLogger(\n `${colorize('✔', ANSIColors.GREEN)} File ${formatPath(relativePath)} created/updated successfully.`\n );\n } catch (error) {\n console.error(error);\n }\n};\n\ntype TranslateDocOptions = {\n docPattern: string[];\n locales: Locales[];\n excludedGlobPattern: string[];\n baseLocale: Locales;\n aiOptions?: AIOptions;\n nbSimultaneousFileProcessed?: number;\n configOptions?: GetConfigurationOptions;\n customInstructions?: string;\n skipIfModifiedBefore?: number | string | Date;\n skipIfModifiedAfter?: number | string | Date;\n gitOptions?: ListGitFilesOptions;\n};\n\n/**\n * Main translate function: scans all .md files in \"en/\" (unless you specified DOC_LIST),\n * then translates them to each locale in LOCALE_LIST.\n */\nexport const translateDoc = async ({\n docPattern,\n locales,\n excludedGlobPattern,\n baseLocale,\n aiOptions,\n nbSimultaneousFileProcessed,\n configOptions,\n customInstructions,\n skipIfModifiedBefore,\n skipIfModifiedAfter,\n gitOptions,\n}: TranslateDocOptions) => {\n const configuration = getConfiguration(configOptions);\n const appLogger = getAppLogger(configuration, {\n config: {\n prefix: '',\n },\n });\n\n if (nbSimultaneousFileProcessed && nbSimultaneousFileProcessed > 10) {\n appLogger(\n `Warning: nbSimultaneousFileProcessed is set to ${nbSimultaneousFileProcessed}, which is greater than 10. Setting it to 10.`\n );\n nbSimultaneousFileProcessed = 10; // Limit the number of simultaneous file processed to 10\n }\n\n let docList: string[] = fg.sync(docPattern, {\n ignore: excludedGlobPattern,\n });\n\n const hasCMSAuth = await checkAIAccess(configuration, aiOptions);\n\n if (!hasCMSAuth) return;\n\n if (gitOptions) {\n const gitChangedFiles = await listGitFiles(gitOptions);\n\n if (gitChangedFiles) {\n // Convert dictionary file paths to be relative to git root for comparison\n\n // Filter dictionaries based on git changed files\n docList = docList.filter((path) =>\n gitChangedFiles.some((gitFile) => join(process.cwd(), path) === gitFile)\n );\n }\n }\n\n // OAuth handled by API proxy internally\n\n appLogger(`Base locale is ${formatLocale(baseLocale)}`);\n appLogger(\n `Translating ${colorizeNumber(locales.length)} locales: [ ${formatLocale(locales)} ]`\n );\n\n appLogger(`Translating ${colorizeNumber(docList.length)} files:`);\n appLogger(docList.map((path) => ` - ${formatPath(path)}\\n`));\n\n // Create all tasks to be processed\n const allTasks = docList.flatMap((docPath) =>\n locales.map((locale) => async () => {\n appLogger(\n `Translating file: ${formatPath(docPath)} to ${formatLocale(locale)}`\n );\n\n const absoluteBaseFilePath = join(configuration.content.baseDir, docPath);\n const outputFilePath = getOutputFilePath(\n absoluteBaseFilePath,\n locale,\n baseLocale\n );\n\n // check if the file exist, otherwise create it\n if (!existsSync(outputFilePath)) {\n appLogger(`File ${outputFilePath} does not exist, creating it...`);\n mkdirSync(dirname(outputFilePath), { recursive: true });\n writeFileSync(outputFilePath, '');\n }\n\n const fileModificationData = checkFileModifiedRange(outputFilePath, {\n skipIfModifiedBefore,\n skipIfModifiedAfter,\n });\n\n if (fileModificationData.isSkipped) {\n appLogger(fileModificationData.message);\n return;\n }\n\n await translateFile(\n absoluteBaseFilePath,\n outputFilePath,\n locale as Locales,\n baseLocale,\n aiOptions,\n configuration,\n customInstructions\n );\n })\n );\n\n await parallelize(\n allTasks,\n (task) => task(),\n nbSimultaneousFileProcessed ?? 3\n );\n};\n"],"mappings":"AACA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAIA;AAAA,OACK;AACP,OAAO,QAAQ;AACf,SAAS,YAAY,WAAW,qBAAqB;AACrD,SAAS,gBAAgB;AACzB,SAAS,SAAS,MAAM,gBAAgB;AACxC,SAAS,qBAAqB;AAC9B,SAAS,iBAAiB;AAC1B,SAAS,qBAAqB;AAC9B,SAAS,8BAA8B;AACvC,SAAS,sBAAsB;AAC/B,SAAS,6BAA6B;AACtC,SAAS,gBAAgB;AACzB,SAAS,yBAAyB;AAElC,MAAM,aAAa,OAAO,YAAY,QAAQ;AAE9C,MAAM,MAAM,aAAa,QAA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,EAAE;AACtD;AAAA,IACE,eAAe,eAAe,QAAQ,MAAM,CAAC,eAAe,aAAa,OAAO,CAAC;AAAA,EACnF;AAEA,YAAU,eAAe,eAAe,QAAQ,MAAM,CAAC,SAAS;AAChE,YAAU,QAAQ,IAAI,CAAC,SAAS,MAAM,WAAW,IAAI,CAAC;AAAA,CAAI,CAAC;AAG3D,QAAM,WAAW,QAAQ;AAAA,IAAQ,CAAC,YAChC,QAAQ,IAAI,CAAC,WAAW,YAAY;AAClC;AAAA,QACE,qBAAqB,WAAW,OAAO,CAAC,OAAO,aAAa,MAAM,CAAC;AAAA,MACrE;AAEA,YAAM,uBAAuB,KAAK,cAAc,QAAQ,SAAS,OAAO;AACxE,YAAM,iBAAiB;AAAA,QACrB;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAGA,UAAI,CAAC,WAAW,cAAc,GAAG;AAC/B,kBAAU,QAAQ,cAAc,iCAAiC;AACjE,kBAAU,QAAQ,cAAc,GAAG,EAAE,WAAW,KAAK,CAAC;AACtD,sBAAc,gBAAgB,EAAE;AAAA,MAClC;AAEA,YAAM,uBAAuB,uBAAuB,gBAAgB;AAAA,QAClE;AAAA,QACA;AAAA,MACF,CAAC;AAED,UAAI,qBAAqB,WAAW;AAClC,kBAAU,qBAAqB,OAAO;AACtC;AAAA,MACF;AAEA,YAAM;AAAA,QACJ;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AAEA,QAAM;AAAA,IACJ;AAAA,IACA,CAAC,SAAS,KAAK;AAAA,IACf,+BAA+B;AAAA,EACjC;AACF;","names":[]}
1
+ {"version":3,"file":"translateDoc.mjs","names":["docList: string[]"],"sources":["../../src/translateDoc.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'node:fs';\nimport { readFile } from 'node:fs/promises';\nimport { dirname, join, relative } from 'node:path';\nimport { readAsset } from 'utils:asset';\nimport type { AIOptions } from '@intlayer/api';\nimport {\n formatLocale,\n formatPath,\n getChunk,\n type ListGitFilesOptions,\n listGitFiles,\n parallelize,\n} from '@intlayer/chokidar';\nimport {\n ANSIColors,\n colon,\n colorize,\n colorizeNumber,\n type GetConfigurationOptions,\n getAppLogger,\n getConfiguration,\n retryManager,\n} from '@intlayer/config';\nimport type { IntlayerConfig, Locale, Locales } from '@intlayer/types';\nimport fg from 'fast-glob';\nimport { chunkText } from './utils/calculateChunks';\nimport { checkAIAccess } from './utils/checkAccess';\nimport { checkFileModifiedRange } from './utils/checkFileModifiedRange';\nimport { chunkInference } from './utils/chunkInference';\nimport { fixChunkStartEndChars } from './utils/fixChunkStartEndChars';\nimport { getOutputFilePath } from './utils/getOutputFilePath';\n\n/**\n * Translate a single file for a given locale\n */\nexport const translateFile = async (\n baseFilePath: string,\n outputFilePath: string,\n locale: Locale,\n baseLocale: Locale,\n configuration: IntlayerConfig,\n aiOptions?: AIOptions,\n customInstructions?: string\n) => {\n try {\n const appLogger = getAppLogger(configuration, {\n config: {\n prefix: '',\n },\n });\n\n // Determine the target locale file path\n const fileContent = await readFile(baseFilePath, 'utf-8');\n\n let fileResultContent = fileContent;\n\n // Prepare the base prompt for ChatGPT\n const basePrompt = readAsset('./prompts/TRANSLATE_PROMPT.md', 'utf-8')\n .replaceAll('{{localeName}}', `${formatLocale(locale, false)}`)\n .replaceAll('{{baseLocaleName}}', `${formatLocale(baseLocale, false)}`)\n .replace('{{applicationContext}}', aiOptions?.applicationContext ?? '-')\n .replace('{{customInstructions}}', customInstructions ?? '-');\n\n const filePrefixText = `${ANSIColors.GREY_DARK}[${formatPath(baseFilePath)}${ANSIColors.GREY_DARK}] `;\n const filePrefix = [\n colon(filePrefixText, { colSize: 40 }),\n `→ ${ANSIColors.RESET}`,\n ].join('');\n\n const prefixText = `${ANSIColors.GREY_DARK}[${formatPath(baseFilePath)}${ANSIColors.GREY_DARK}][${formatLocale(locale)}${ANSIColors.GREY_DARK}] `;\n const prefix = [\n colon(prefixText, { colSize: 40 }),\n `→ ${ANSIColors.RESET}`,\n ].join('');\n\n // 1. Chunk the file by number of lines instead of characters\n const chunks = chunkText(fileContent);\n appLogger(\n `${filePrefix}Base file splitted into ${colorizeNumber(chunks.length)} chunks`\n );\n\n for await (const [i, chunk] of chunks.entries()) {\n const isFirstChunk = i === 0;\n\n // Build the chunk-specific prompt\n const getPrevChunkPrompt = () =>\n `**CHUNK ${i} of ${chunks.length}** that has been translated in ${formatLocale(locale)}:\\n` +\n `///chunkStart///` +\n getChunk(fileResultContent, chunks[i - 1]) +\n `///chunkEnd///`;\n\n const getBaseChunkContextPrompt = () =>\n `**CHUNK ${i + 1} to ${Math.min(i + 3, chunks.length)} of ${chunks.length}** is the base chunk in ${formatLocale(baseLocale, false)} as reference.\\n` +\n `///chunksStart///` +\n (chunks[i - 1]?.content ?? '') +\n chunks[i].content +\n (chunks[i + 1]?.content ?? '') +\n `///chunksEnd///`;\n\n const fileToTranslateCurrentChunk = chunk.content;\n\n // Make the actual translation call\n const chunkTranslation = await retryManager(async () => {\n const result = await chunkInference(\n [\n { role: 'system', content: basePrompt },\n\n { role: 'system', content: getBaseChunkContextPrompt() },\n ...(isFirstChunk\n ? []\n : [{ role: 'system', content: getPrevChunkPrompt() } as const]),\n {\n role: 'system',\n content: `The next user message will be the **CHUNK ${colorizeNumber(i + 1)} of ${colorizeNumber(chunks.length)}** in ${formatLocale(baseLocale, false)} to translate in ${formatLocale(locale, false)}:`,\n },\n { role: 'user', content: fileToTranslateCurrentChunk },\n ],\n aiOptions,\n configuration\n );\n\n appLogger(\n [\n `${prefix}`,\n `${ANSIColors.GREY_DARK}[Chunk `,\n colorizeNumber(i + 1),\n `${ANSIColors.GREY_DARK} of `,\n colorizeNumber(chunks.length),\n `${ANSIColors.GREY_DARK}] →${ANSIColors.RESET} `,\n `${colorizeNumber(result.tokenUsed)} tokens used`,\n ].join('')\n );\n\n const fixedTranslatedChunkResult = fixChunkStartEndChars(\n result?.fileContent,\n fileToTranslateCurrentChunk\n );\n\n return fixedTranslatedChunkResult;\n })();\n\n // Replace the chunk in the file content\n fileResultContent = fileResultContent.replace(\n fileToTranslateCurrentChunk,\n chunkTranslation\n );\n }\n\n // 4. Write the final translation to the appropriate file path\n mkdirSync(dirname(outputFilePath), { recursive: true });\n writeFileSync(outputFilePath, fileResultContent);\n\n const relativePath = relative(\n configuration.content.baseDir,\n outputFilePath\n );\n\n appLogger(\n `${colorize('✔', ANSIColors.GREEN)} File ${formatPath(relativePath)} created/updated successfully.`\n );\n } catch (error) {\n console.error(error);\n }\n};\n\ntype TranslateDocOptions = {\n docPattern: string[];\n locales: Locale[];\n excludedGlobPattern: string[];\n baseLocale: Locale;\n aiOptions?: AIOptions;\n nbSimultaneousFileProcessed?: number;\n configOptions?: GetConfigurationOptions;\n customInstructions?: string;\n skipIfModifiedBefore?: number | string | Date;\n skipIfModifiedAfter?: number | string | Date;\n gitOptions?: ListGitFilesOptions;\n};\n\n/**\n * Main translate function: scans all .md files in \"en/\" (unless you specified DOC_LIST),\n * then translates them to each locale in LOCALE_LIST.\n */\nexport const translateDoc = async ({\n docPattern,\n locales,\n excludedGlobPattern,\n baseLocale,\n aiOptions,\n nbSimultaneousFileProcessed,\n configOptions,\n customInstructions,\n skipIfModifiedBefore,\n skipIfModifiedAfter,\n gitOptions,\n}: TranslateDocOptions) => {\n const configuration = getConfiguration(configOptions);\n const appLogger = getAppLogger(configuration);\n\n if (nbSimultaneousFileProcessed && nbSimultaneousFileProcessed > 10) {\n appLogger(\n `Warning: nbSimultaneousFileProcessed is set to ${nbSimultaneousFileProcessed}, which is greater than 10. Setting it to 10.`\n );\n nbSimultaneousFileProcessed = 10; // Limit the number of simultaneous file processed to 10\n }\n\n let docList: string[] = await fg(docPattern, {\n ignore: excludedGlobPattern,\n });\n\n const hasCMSAuth = await checkAIAccess(configuration, aiOptions);\n\n if (!hasCMSAuth) return;\n\n if (gitOptions) {\n const gitChangedFiles = await listGitFiles(gitOptions);\n\n if (gitChangedFiles) {\n // Convert dictionary file paths to be relative to git root for comparison\n\n // Filter dictionaries based on git changed files\n docList = docList.filter((path) =>\n gitChangedFiles.some((gitFile) => join(process.cwd(), path) === gitFile)\n );\n }\n }\n\n // OAuth handled by API proxy internally\n\n appLogger(`Base locale is ${formatLocale(baseLocale)}`);\n appLogger(\n `Translating ${colorizeNumber(locales.length)} locales: [ ${formatLocale(locales)} ]`\n );\n\n appLogger(`Translating ${colorizeNumber(docList.length)} files:`);\n appLogger(docList.map((path) => ` - ${formatPath(path)}\\n`));\n\n // Create all tasks to be processed\n const allTasks = docList.flatMap((docPath) =>\n locales.map((locale) => async () => {\n appLogger(\n `Translating file: ${formatPath(docPath)} to ${formatLocale(locale)}`\n );\n\n const absoluteBaseFilePath = join(configuration.content.baseDir, docPath);\n const outputFilePath = getOutputFilePath(\n absoluteBaseFilePath,\n locale,\n baseLocale\n );\n\n // check if the file exist, otherwise create it\n if (!existsSync(outputFilePath)) {\n appLogger(`File ${outputFilePath} does not exist, creating it...`);\n mkdirSync(dirname(outputFilePath), { recursive: true });\n writeFileSync(outputFilePath, '');\n }\n\n const fileModificationData = checkFileModifiedRange(outputFilePath, {\n skipIfModifiedBefore,\n skipIfModifiedAfter,\n });\n\n if (fileModificationData.isSkipped) {\n appLogger(fileModificationData.message);\n return;\n }\n\n await translateFile(\n absoluteBaseFilePath,\n outputFilePath,\n locale as Locale,\n baseLocale,\n configuration,\n aiOptions,\n customInstructions\n );\n })\n );\n\n await parallelize(\n allTasks,\n (task) => task(),\n nbSimultaneousFileProcessed ?? 3\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmCA,MAAa,gBAAgB,OAC3B,cACA,gBACA,QACA,YACA,eACA,WACA,uBACG;AACH,KAAI;EACF,MAAM,YAAY,aAAa,eAAe,EAC5C,QAAQ,EACN,QAAQ,IACT,EACF,CAAC;EAGF,MAAM,cAAc,MAAM,SAAS,cAAc,QAAQ;EAEzD,IAAI,oBAAoB;EAGxB,MAAM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,cAAc,eAAe,UAAU,CAE/C;AAEjB,KAAI,YAAY;EACd,MAAM,kBAAkB,MAAM,aAAa,WAAW;AAEtD,MAAI,gBAIF,WAAU,QAAQ,QAAQ,SACxB,gBAAgB,MAAM,YAAY,KAAK,QAAQ,KAAK,EAAE,KAAK,KAAK,QAAQ,CACzE;;AAML,WAAU,kBAAkB,aAAa,WAAW,GAAG;AACvD,WACE,eAAe,eAAe,QAAQ,OAAO,CAAC,cAAc,aAAa,QAAQ,CAAC,IACnF;AAED,WAAU,eAAe,eAAe,QAAQ,OAAO,CAAC,SAAS;AACjE,WAAU,QAAQ,KAAK,SAAS,MAAM,WAAW,KAAK,CAAC,IAAI,CAAC;AA6C5D,OAAM,YA1CW,QAAQ,SAAS,YAChC,QAAQ,KAAK,WAAW,YAAY;AAClC,YACE,qBAAqB,WAAW,QAAQ,CAAC,MAAM,aAAa,OAAO,GACpE;EAED,MAAM,uBAAuB,KAAK,cAAc,QAAQ,SAAS,QAAQ;EACzE,MAAM,iBAAiB,kBACrB,sBACA,QACA,WACD;AAGD,MAAI,CAAC,WAAW,eAAe,EAAE;AAC/B,aAAU,QAAQ,eAAe,iCAAiC;AAClE,aAAU,QAAQ,eAAe,EAAE,EAAE,WAAW,MAAM,CAAC;AACvD,iBAAc,gBAAgB,GAAG;;EAGnC,MAAM,uBAAuB,uBAAuB,gBAAgB;GAClE;GACA;GACD,CAAC;AAEF,MAAI,qBAAqB,WAAW;AAClC,aAAU,qBAAqB,QAAQ;AACvC;;AAGF,QAAM,cACJ,sBACA,gBACA,QACA,YACA,eACA,WACA,mBACD;GACD,CACH,GAIE,SAAS,MAAM,EAChB,+BAA+B,EAChC"}
@@ -1,96 +1,88 @@
1
- import { splitTextByLines } from "./splitTextByLine.mjs";
1
+ import { splitTextByLines } from "@intlayer/chokidar";
2
+
3
+ //#region src/utils/calculateChunks.ts
2
4
  const DEFAULT_MAX_CHARS_PER_CHUNK = 800;
3
5
  const DEFAULT_OVERLAP_CHARS = 0;
4
6
  const chunkText = (text, maxCharsPerChunk = DEFAULT_MAX_CHARS_PER_CHUNK, overlapChars = DEFAULT_OVERLAP_CHARS) => {
5
- if (maxCharsPerChunk <= 0) {
6
- throw new Error("maxCharsPerChunk must be greater than 0");
7
- }
8
- const splittedText = splitTextByLines(text);
9
- const lines = [];
10
- let charStartAcc = 0;
11
- splittedText.forEach((line, index) => {
12
- lines.push({
13
- content: line,
14
- lineStart: index,
15
- lineLength: 1,
16
- charStart: charStartAcc,
17
- charLength: line.length
18
- });
19
- charStartAcc += line.length;
20
- });
21
- const groupedLines = lines.reduce(
22
- (acc, line) => {
23
- if (line.content.length > maxCharsPerChunk) {
24
- acc.push(line);
25
- return acc;
26
- }
27
- if (acc.length === 0) {
28
- acc.push(line);
29
- return acc;
30
- }
31
- const lastChunk = acc[acc.length - 1];
32
- const combinedLength = lastChunk.content.length + line.content.length;
33
- if (combinedLength > maxCharsPerChunk) {
34
- acc.push(line);
35
- return acc;
36
- }
37
- const combinedContent = lastChunk.content + line.content;
38
- const updatedChunk = {
39
- content: combinedContent,
40
- lineStart: lastChunk.lineStart,
41
- lineLength: lastChunk.lineLength + line.lineLength,
42
- charStart: lastChunk.charStart,
43
- charLength: combinedContent.length
44
- };
45
- acc[acc.length - 1] = updatedChunk;
46
- return acc;
47
- },
48
- []
49
- );
50
- const splittedLines = groupedLines.flatMap((line) => {
51
- const chunk = [];
52
- if (line.content.length <= maxCharsPerChunk) {
53
- chunk.push(line);
54
- return chunk;
55
- }
56
- for (let i = 0; i < line.content.length; i += maxCharsPerChunk) {
57
- const slicedContent = line.content.slice(i, i + maxCharsPerChunk);
58
- chunk.push({
59
- content: slicedContent,
60
- lineStart: line.lineStart,
61
- lineLength: 1,
62
- charStart: line.charStart + i,
63
- charLength: slicedContent.length
64
- });
65
- }
66
- return chunk;
67
- });
68
- if (overlapChars === 0) return splittedLines;
69
- const overlapChunks = splittedLines.length > 0 ? [splittedLines[0]] : [];
70
- for (let i = 1; i < splittedLines.length; i++) {
71
- const previousChunk = splittedLines[i - 1];
72
- const chunk = splittedLines[i];
73
- const overlapContent = previousChunk.content.slice(-overlapChars);
74
- const overlapLineNb = splitTextByLines(overlapContent).length;
75
- const overlapContentWithoutPartialLine = overlapContent.slice(
76
- overlapLineNb > 1 ? overlapContent.indexOf("\n") + 1 : 0,
77
- overlapContent.length
78
- );
79
- const newContent = overlapContentWithoutPartialLine + chunk.content;
80
- const newLineLength = splitTextByLines(newContent).length;
81
- const lineDiff = chunk.lineLength - newLineLength;
82
- const overlappedChunk = {
83
- content: newContent,
84
- lineStart: chunk.lineStart + lineDiff,
85
- lineLength: chunk.lineLength - lineDiff,
86
- charStart: chunk.charStart - overlapContentWithoutPartialLine.length,
87
- charLength: newContent.length
88
- };
89
- overlapChunks.push(overlappedChunk);
90
- }
91
- return overlapChunks;
92
- };
93
- export {
94
- chunkText
7
+ if (maxCharsPerChunk <= 0) throw new Error("maxCharsPerChunk must be greater than 0");
8
+ const splittedText = splitTextByLines(text);
9
+ const lines = [];
10
+ let charStartAcc = 0;
11
+ splittedText.forEach((line, index) => {
12
+ lines.push({
13
+ content: line,
14
+ lineStart: index,
15
+ lineLength: 1,
16
+ charStart: charStartAcc,
17
+ charLength: line.length
18
+ });
19
+ charStartAcc += line.length;
20
+ });
21
+ const splittedLines = lines.reduce((acc, line) => {
22
+ if (line.content.length > maxCharsPerChunk) {
23
+ acc.push(line);
24
+ return acc;
25
+ }
26
+ if (acc.length === 0) {
27
+ acc.push(line);
28
+ return acc;
29
+ }
30
+ const lastChunk = acc[acc.length - 1];
31
+ if (lastChunk.content.length + line.content.length > maxCharsPerChunk) {
32
+ acc.push(line);
33
+ return acc;
34
+ }
35
+ const combinedContent = lastChunk.content + line.content;
36
+ const updatedChunk = {
37
+ content: combinedContent,
38
+ lineStart: lastChunk.lineStart,
39
+ lineLength: lastChunk.lineLength + line.lineLength,
40
+ charStart: lastChunk.charStart,
41
+ charLength: combinedContent.length
42
+ };
43
+ acc[acc.length - 1] = updatedChunk;
44
+ return acc;
45
+ }, []).flatMap((line) => {
46
+ const chunk = [];
47
+ if (line.content.length <= maxCharsPerChunk) {
48
+ chunk.push(line);
49
+ return chunk;
50
+ }
51
+ for (let i = 0; i < line.content.length; i += maxCharsPerChunk) {
52
+ const slicedContent = line.content.slice(i, i + maxCharsPerChunk);
53
+ chunk.push({
54
+ content: slicedContent,
55
+ lineStart: line.lineStart,
56
+ lineLength: 1,
57
+ charStart: line.charStart + i,
58
+ charLength: slicedContent.length
59
+ });
60
+ }
61
+ return chunk;
62
+ });
63
+ if (overlapChars === 0) return splittedLines;
64
+ const overlapChunks = splittedLines.length > 0 ? [splittedLines[0]] : [];
65
+ for (let i = 1; i < splittedLines.length; i++) {
66
+ const previousChunk = splittedLines[i - 1];
67
+ const chunk = splittedLines[i];
68
+ const overlapContent = previousChunk.content.slice(-overlapChars);
69
+ const overlapLineNb = splitTextByLines(overlapContent).length;
70
+ const overlapContentWithoutPartialLine = overlapContent.slice(overlapLineNb > 1 ? overlapContent.indexOf("\n") + 1 : 0, overlapContent.length);
71
+ const newContent = overlapContentWithoutPartialLine + chunk.content;
72
+ const newLineLength = splitTextByLines(newContent).length;
73
+ const lineDiff = chunk.lineLength - newLineLength;
74
+ const overlappedChunk = {
75
+ content: newContent,
76
+ lineStart: chunk.lineStart + lineDiff,
77
+ lineLength: chunk.lineLength - lineDiff,
78
+ charStart: chunk.charStart - overlapContentWithoutPartialLine.length,
79
+ charLength: newContent.length
80
+ };
81
+ overlapChunks.push(overlappedChunk);
82
+ }
83
+ return overlapChunks;
95
84
  };
85
+
86
+ //#endregion
87
+ export { chunkText };
96
88
  //# sourceMappingURL=calculateChunks.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/utils/calculateChunks.ts"],"sourcesContent":["import { splitTextByLines } from './splitTextByLine';\n\nexport type ChunkLineResult = {\n lineStart: number;\n lineLength: number;\n charStart: number;\n charLength: number;\n content: string;\n};\n\nconst DEFAULT_MAX_CHARS_PER_CHUNK = 800;\nconst DEFAULT_OVERLAP_CHARS = 0;\n\nexport const chunkText = (\n text: string,\n maxCharsPerChunk: number = DEFAULT_MAX_CHARS_PER_CHUNK,\n overlapChars: number = DEFAULT_OVERLAP_CHARS\n): ChunkLineResult[] => {\n if (maxCharsPerChunk <= 0) {\n throw new Error('maxCharsPerChunk must be greater than 0');\n }\n\n const splittedText = splitTextByLines(text);\n\n // Split text into lines to faciliate the translation\n const lines: ChunkLineResult[] = [];\n let charStartAcc = 0;\n\n splittedText.forEach((line, index) => {\n lines.push({\n content: line,\n lineStart: index,\n lineLength: 1,\n charStart: charStartAcc,\n charLength: line.length,\n });\n charStartAcc += line.length;\n });\n\n // Group lines\n // as long as the chunk length is less than maxCharsPerChunk\n // if a line longer than maxCharsPerChunk, keep it alone\n // if a line is not longer than maxCharsPerChunk, it is grouped\n const groupedLines: ChunkLineResult[] = lines.reduce(\n (acc: ChunkLineResult[], line) => {\n // If this line alone exceeds maxCharsPerChunk, keep it separate\n if (line.content.length > maxCharsPerChunk) {\n acc.push(line);\n return acc;\n }\n\n // If we have no chunks yet, start with this line\n if (acc.length === 0) {\n acc.push(line);\n return acc;\n }\n\n // Get the last chunk\n const lastChunk = acc[acc.length - 1];\n\n // Calculate what the combined length would be (including newline character)\n const combinedLength = lastChunk.content.length + line.content.length;\n\n // If combining would exceed the limit, start a new chunk\n if (combinedLength > maxCharsPerChunk) {\n acc.push(line);\n return acc;\n }\n\n // Otherwise, combine with the last chunk\n const combinedContent = lastChunk.content + line.content;\n const updatedChunk = {\n content: combinedContent,\n lineStart: lastChunk.lineStart,\n lineLength: lastChunk.lineLength + line.lineLength,\n charStart: lastChunk.charStart,\n charLength: combinedContent.length,\n };\n\n acc[acc.length - 1] = updatedChunk;\n return acc;\n },\n []\n );\n\n // If one line is longer than maxCharsPerChunk, split it into multiple chunks\n const splittedLines: ChunkLineResult[] = groupedLines.flatMap((line) => {\n const chunk: ChunkLineResult[] = [];\n\n if (line.content.length <= maxCharsPerChunk) {\n chunk.push(line);\n return chunk;\n }\n\n for (let i = 0; i < line.content.length; i += maxCharsPerChunk) {\n const slicedContent = line.content.slice(i, i + maxCharsPerChunk);\n chunk.push({\n content: slicedContent,\n lineStart: line.lineStart,\n lineLength: 1,\n charStart: line.charStart + i,\n charLength: slicedContent.length,\n });\n }\n return chunk;\n });\n\n if (overlapChars === 0) return splittedLines;\n\n const overlapChunks: ChunkLineResult[] =\n splittedLines.length > 0 ? [splittedLines[0]] : [];\n\n for (let i = 1; i < splittedLines.length; i++) {\n const previousChunk = splittedLines[i - 1];\n const chunk = splittedLines[i];\n\n const overlapContent = previousChunk.content.slice(-overlapChars);\n const overlapLineNb = splitTextByLines(overlapContent).length;\n\n const overlapContentWithoutPartialLine = overlapContent.slice(\n overlapLineNb > 1 ? overlapContent.indexOf('\\n') + 1 : 0,\n overlapContent.length\n );\n\n const newContent = overlapContentWithoutPartialLine + chunk.content;\n const newLineLength = splitTextByLines(newContent).length;\n const lineDiff = chunk.lineLength - newLineLength;\n\n const overlappedChunk = {\n content: newContent,\n lineStart: chunk.lineStart + lineDiff,\n lineLength: chunk.lineLength - lineDiff,\n charStart: chunk.charStart - overlapContentWithoutPartialLine.length,\n charLength: newContent.length,\n };\n\n overlapChunks.push(overlappedChunk);\n }\n\n return overlapChunks;\n};\n"],"mappings":"AAAA,SAAS,wBAAwB;AAUjC,MAAM,8BAA8B;AACpC,MAAM,wBAAwB;AAEvB,MAAM,YAAY,CACvB,MACA,mBAA2B,6BAC3B,eAAuB,0BACD;AACtB,MAAI,oBAAoB,GAAG;AACzB,UAAM,IAAI,MAAM,yCAAyC;AAAA,EAC3D;AAEA,QAAM,eAAe,iBAAiB,IAAI;AAG1C,QAAM,QAA2B,CAAC;AAClC,MAAI,eAAe;AAEnB,eAAa,QAAQ,CAAC,MAAM,UAAU;AACpC,UAAM,KAAK;AAAA,MACT,SAAS;AAAA,MACT,WAAW;AAAA,MACX,YAAY;AAAA,MACZ,WAAW;AAAA,MACX,YAAY,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AAG,QAAO;AAE/B,QAAM,gBACJ,cAAc,SAAS,IAAI,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC;AAEnD,WAAS,IAAI,GAAG,IAAI,cAAc,QAAQ,KAAK;AAC7C,UAAM,gBAAgB,cAAc,IAAI,CAAC;AACzC,UAAM,QAAQ,cAAc,CAAC;AAE7B,UAAM,iBAAiB,cAAc,QAAQ,MAAM,CAAC,YAAY;AAChE,UAAM,gBAAgB,iBAAiB,cAAc,EAAE;AAEvD,UAAM,mCAAmC,eAAe;AAAA,MACtD,gBAAgB,IAAI,eAAe,QAAQ,IAAI,IAAI,IAAI;AAAA,MACvD,eAAe;AAAA,IACjB;AAEA,UAAM,aAAa,mCAAmC,MAAM;AAC5D,UAAM,gBAAgB,iBAAiB,UAAU,EAAE;AACnD,UAAM,WAAW,MAAM,aAAa;AAEpC,UAAM,kBAAkB;AAAA,MACtB,SAAS;AAAA,MACT,WAAW,MAAM,YAAY;AAAA,MAC7B,YAAY,MAAM,aAAa;AAAA,MAC/B,WAAW,MAAM,YAAY,iCAAiC;AAAA,MAC9D,YAAY,WAAW;AAAA,IACzB;AAEA,kBAAc,KAAK,eAAe;AAAA,EACpC;AAEA,SAAO;AACT;","names":[]}
1
+ {"version":3,"file":"calculateChunks.mjs","names":["lines: ChunkLineResult[]","splittedLines: ChunkLineResult[]","chunk: ChunkLineResult[]","overlapChunks: ChunkLineResult[]"],"sources":["../../../src/utils/calculateChunks.ts"],"sourcesContent":["import { splitTextByLines } from '@intlayer/chokidar';\n\nexport type ChunkLineResult = {\n lineStart: number;\n lineLength: number;\n charStart: number;\n charLength: number;\n content: string;\n};\n\nconst DEFAULT_MAX_CHARS_PER_CHUNK = 800;\nconst DEFAULT_OVERLAP_CHARS = 0;\n\nexport const chunkText = (\n text: string,\n maxCharsPerChunk: number = DEFAULT_MAX_CHARS_PER_CHUNK,\n overlapChars: number = DEFAULT_OVERLAP_CHARS\n): ChunkLineResult[] => {\n if (maxCharsPerChunk <= 0) {\n throw new Error('maxCharsPerChunk must be greater than 0');\n }\n\n const splittedText = splitTextByLines(text);\n\n // Split text into lines to facilitate the translation\n const lines: ChunkLineResult[] = [];\n let charStartAcc = 0;\n\n splittedText.forEach((line, index) => {\n lines.push({\n content: line,\n lineStart: index,\n lineLength: 1,\n charStart: charStartAcc,\n charLength: line.length,\n });\n charStartAcc += line.length;\n });\n\n // Group lines\n // as long as the chunk length is less than maxCharsPerChunk\n // if a line longer than maxCharsPerChunk, keep it alone\n // if a line is not longer than maxCharsPerChunk, it is grouped\n const groupedLines: ChunkLineResult[] = lines.reduce(\n (acc: ChunkLineResult[], line) => {\n // If this line alone exceeds maxCharsPerChunk, keep it separate\n if (line.content.length > maxCharsPerChunk) {\n acc.push(line);\n return acc;\n }\n\n // If we have no chunks yet, start with this line\n if (acc.length === 0) {\n acc.push(line);\n return acc;\n }\n\n // Get the last chunk\n const lastChunk = acc[acc.length - 1];\n\n // Calculate what the combined length would be (including newline character)\n const combinedLength = lastChunk.content.length + line.content.length;\n\n // If combining would exceed the limit, start a new chunk\n if (combinedLength > maxCharsPerChunk) {\n acc.push(line);\n return acc;\n }\n\n // Otherwise, combine with the last chunk\n const combinedContent = lastChunk.content + line.content;\n const updatedChunk = {\n content: combinedContent,\n lineStart: lastChunk.lineStart,\n lineLength: lastChunk.lineLength + line.lineLength,\n charStart: lastChunk.charStart,\n charLength: combinedContent.length,\n };\n\n acc[acc.length - 1] = updatedChunk;\n return acc;\n },\n []\n );\n\n // If one line is longer than maxCharsPerChunk, split it into multiple chunks\n const splittedLines: ChunkLineResult[] = groupedLines.flatMap((line) => {\n const chunk: ChunkLineResult[] = [];\n\n if (line.content.length <= maxCharsPerChunk) {\n chunk.push(line);\n return chunk;\n }\n\n for (let i = 0; i < line.content.length; i += maxCharsPerChunk) {\n const slicedContent = line.content.slice(i, i + maxCharsPerChunk);\n chunk.push({\n content: slicedContent,\n lineStart: line.lineStart,\n lineLength: 1,\n charStart: line.charStart + i,\n charLength: slicedContent.length,\n });\n }\n return chunk;\n });\n\n if (overlapChars === 0) return splittedLines;\n\n const overlapChunks: ChunkLineResult[] =\n splittedLines.length > 0 ? [splittedLines[0]] : [];\n\n for (let i = 1; i < splittedLines.length; i++) {\n const previousChunk = splittedLines[i - 1];\n const chunk = splittedLines[i];\n\n const overlapContent = previousChunk.content.slice(-overlapChars);\n const overlapLineNb = splitTextByLines(overlapContent).length;\n\n const overlapContentWithoutPartialLine = overlapContent.slice(\n overlapLineNb > 1 ? overlapContent.indexOf('\\n') + 1 : 0,\n overlapContent.length\n );\n\n const newContent = overlapContentWithoutPartialLine + chunk.content;\n const newLineLength = splitTextByLines(newContent).length;\n const lineDiff = chunk.lineLength - newLineLength;\n\n const overlappedChunk = {\n content: newContent,\n lineStart: chunk.lineStart + lineDiff,\n lineLength: chunk.lineLength - lineDiff,\n charStart: chunk.charStart - overlapContentWithoutPartialLine.length,\n charLength: newContent.length,\n };\n\n overlapChunks.push(overlappedChunk);\n }\n\n return overlapChunks;\n};\n"],"mappings":";;;AAUA,MAAM,8BAA8B;AACpC,MAAM,wBAAwB;AAE9B,MAAa,aACX,MACA,mBAA2B,6BAC3B,eAAuB,0BACD;AACtB,KAAI,oBAAoB,EACtB,OAAM,IAAI,MAAM,0CAA0C;CAG5D,MAAM,eAAe,iBAAiB,KAAK;CAG3C,MAAMA,QAA2B,EAAE;CACnC,IAAI,eAAe;AAEnB,cAAa,SAAS,MAAM,UAAU;AACpC,QAAM,KAAK;GACT,SAAS;GACT,WAAW;GACX,YAAY;GACZ,WAAW;GACX,YAAY,KAAK;GAClB,CAAC;AACF,kBAAgB,KAAK;GACrB;CAiDF,MAAMC,gBA3CkC,MAAM,QAC3C,KAAwB,SAAS;AAEhC,MAAI,KAAK,QAAQ,SAAS,kBAAkB;AAC1C,OAAI,KAAK,KAAK;AACd,UAAO;;AAIT,MAAI,IAAI,WAAW,GAAG;AACpB,OAAI,KAAK,KAAK;AACd,UAAO;;EAIT,MAAM,YAAY,IAAI,IAAI,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,MAAM,kBAAkB;GACtB,SAAS;GACT,WAAW,MAAM,YAAY;GAC7B,YAAY,MAAM,aAAa;GAC/B,WAAW,MAAM,YAAY,iCAAiC;GAC9D,YAAY,WAAW;GACxB;AAED,gBAAc,KAAK,gBAAgB;;AAGrC,QAAO"}
@@ -1,52 +1,34 @@
1
1
  import { getIntlayerAPIProxy } from "@intlayer/api";
2
- import { extractErrorMessage } from "@intlayer/chokidar";
3
- import { getAppLogger } from "@intlayer/config";
2
+ import { extractErrorMessage, getAppLogger } from "@intlayer/config";
3
+
4
+ //#region src/utils/checkAccess.ts
4
5
  const checkCMSAuth = async (configuration) => {
5
- const appLogger = getAppLogger(configuration, {
6
- config: {
7
- prefix: ""
8
- }
9
- });
10
- const hasCMSAuth = configuration.editor.clientId && configuration.editor.clientSecret;
11
- if (!hasCMSAuth) {
12
- appLogger("CMS auth not provided.", {
13
- level: "error"
14
- });
15
- return false;
16
- }
17
- const intlayerAPI = getIntlayerAPIProxy(void 0, configuration);
18
- try {
19
- await intlayerAPI.oAuth.getOAuth2AccessToken();
20
- } catch (error) {
21
- const message = extractErrorMessage(error);
22
- appLogger(message, {
23
- level: "error"
24
- });
25
- return false;
26
- }
27
- return true;
6
+ const appLogger = getAppLogger(configuration, { config: { prefix: "" } });
7
+ if (!(configuration.editor.clientId && configuration.editor.clientSecret)) {
8
+ appLogger("CMS auth not provided.", { level: "error" });
9
+ return false;
10
+ }
11
+ const intlayerAPI = getIntlayerAPIProxy(void 0, configuration);
12
+ try {
13
+ await intlayerAPI.oAuth.getOAuth2AccessToken();
14
+ } catch (error) {
15
+ appLogger(extractErrorMessage(error), { level: "error" });
16
+ return false;
17
+ }
18
+ return true;
28
19
  };
29
20
  const checkAIAccess = async (configuration, aiOptions) => {
30
- const appLogger = getAppLogger(configuration, {
31
- config: {
32
- prefix: ""
33
- }
34
- });
35
- const hasCMSAuth = configuration.editor.clientId && configuration.editor.clientSecret;
36
- const hasHisOwnAIAPIKey = configuration.ai?.apiKey || aiOptions?.apiKey;
37
- if (!hasCMSAuth && !hasHisOwnAIAPIKey) {
38
- appLogger("AI options or API key not provided.", {
39
- level: "error"
40
- });
41
- return false;
42
- }
43
- if (!hasHisOwnAIAPIKey) {
44
- return await checkCMSAuth(configuration);
45
- }
46
- return true;
47
- };
48
- export {
49
- checkAIAccess,
50
- checkCMSAuth
21
+ const appLogger = getAppLogger(configuration);
22
+ const hasCMSAuth = configuration.editor.clientId && configuration.editor.clientSecret;
23
+ const hasHisOwnAIAPIKey = configuration.ai?.apiKey || aiOptions?.apiKey;
24
+ if (!hasCMSAuth && !hasHisOwnAIAPIKey) {
25
+ appLogger("AI options or API key not provided.", { level: "error" });
26
+ return false;
27
+ }
28
+ if (!hasHisOwnAIAPIKey) return await checkCMSAuth(configuration);
29
+ return true;
51
30
  };
31
+
32
+ //#endregion
33
+ export { checkAIAccess, checkCMSAuth };
52
34
  //# sourceMappingURL=checkAccess.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/utils/checkAccess.ts"],"sourcesContent":["import type { AIOptions } from '@intlayer/api';\nimport { getIntlayerAPIProxy } from '@intlayer/api';\nimport { extractErrorMessage } from '@intlayer/chokidar';\nimport { getAppLogger, type IntlayerConfig } from '@intlayer/config';\n\nexport const checkCMSAuth = async (\n configuration: IntlayerConfig\n): Promise<boolean> => {\n const appLogger = getAppLogger(configuration, {\n config: {\n prefix: '',\n },\n });\n\n const hasCMSAuth =\n configuration.editor.clientId && configuration.editor.clientSecret;\n if (!hasCMSAuth) {\n appLogger('CMS auth not provided.', {\n level: 'error',\n });\n\n return false;\n }\n const intlayerAPI = getIntlayerAPIProxy(undefined, configuration);\n\n try {\n await intlayerAPI.oAuth.getOAuth2AccessToken();\n } catch (error) {\n const message = extractErrorMessage(error);\n\n appLogger(message, {\n level: 'error',\n });\n return false;\n }\n\n return true;\n};\n\nexport const checkAIAccess = async (\n configuration: IntlayerConfig,\n aiOptions?: AIOptions\n): Promise<boolean> => {\n const appLogger = getAppLogger(configuration, {\n config: {\n prefix: '',\n },\n });\n\n const hasCMSAuth =\n configuration.editor.clientId && configuration.editor.clientSecret;\n const hasHisOwnAIAPIKey = configuration.ai?.apiKey || aiOptions?.apiKey;\n\n // User need to provide either his own AI API key or the CMS auth\n if (!hasCMSAuth && !hasHisOwnAIAPIKey) {\n appLogger('AI options or API key not provided.', {\n level: 'error',\n });\n\n return false;\n }\n\n // If the user do not have his own AI API key, we need to check the CMS auth\n if (!hasHisOwnAIAPIKey) {\n return await checkCMSAuth(configuration);\n }\n\n return true;\n};\n"],"mappings":"AACA,SAAS,2BAA2B;AACpC,SAAS,2BAA2B;AACpC,SAAS,oBAAyC;AAE3C,MAAM,eAAe,OAC1B,kBACqB;AACrB,QAAM,YAAY,aAAa,eAAe;AAAA,IAC5C,QAAQ;AAAA,MACN,QAAQ;AAAA,IACV;AAAA,EACF,CAAC;AAED,QAAM,aACJ,cAAc,OAAO,YAAY,cAAc,OAAO;AACxD,MAAI,CAAC,YAAY;AACf,cAAU,0BAA0B;AAAA,MAClC,OAAO;AAAA,IACT,CAAC;AAED,WAAO;AAAA,EACT;AACA,QAAM,cAAc,oBAAoB,QAAW,aAAa;AAEhE,MAAI;AACF,UAAM,YAAY,MAAM,qBAAqB;AAAA,EAC/C,SAAS,OAAO;AACd,UAAM,UAAU,oBAAoB,KAAK;AAEzC,cAAU,SAAS;AAAA,MACjB,OAAO;AAAA,IACT,CAAC;AACD,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAEO,MAAM,gBAAgB,OAC3B,eACA,cACqB;AACrB,QAAM,YAAY,aAAa,eAAe;AAAA,IAC5C,QAAQ;AAAA,MACN,QAAQ;AAAA,IACV;AAAA,EACF,CAAC;AAED,QAAM,aACJ,cAAc,OAAO,YAAY,cAAc,OAAO;AACxD,QAAM,oBAAoB,cAAc,IAAI,UAAU,WAAW;AAGjE,MAAI,CAAC,cAAc,CAAC,mBAAmB;AACrC,cAAU,uCAAuC;AAAA,MAC/C,OAAO;AAAA,IACT,CAAC;AAED,WAAO;AAAA,EACT;AAGA,MAAI,CAAC,mBAAmB;AACtB,WAAO,MAAM,aAAa,aAAa;AAAA,EACzC;AAEA,SAAO;AACT;","names":[]}
1
+ {"version":3,"file":"checkAccess.mjs","names":[],"sources":["../../../src/utils/checkAccess.ts"],"sourcesContent":["import type { AIOptions } from '@intlayer/api';\nimport { getIntlayerAPIProxy } from '@intlayer/api';\nimport { extractErrorMessage, getAppLogger } from '@intlayer/config';\nimport type { IntlayerConfig } from '@intlayer/types';\n\nexport const checkCMSAuth = async (\n configuration: IntlayerConfig\n): Promise<boolean> => {\n const appLogger = getAppLogger(configuration, {\n config: {\n prefix: '',\n },\n });\n\n const hasCMSAuth =\n configuration.editor.clientId && configuration.editor.clientSecret;\n if (!hasCMSAuth) {\n appLogger('CMS auth not provided.', {\n level: 'error',\n });\n\n return false;\n }\n const intlayerAPI = getIntlayerAPIProxy(undefined, configuration);\n\n try {\n await intlayerAPI.oAuth.getOAuth2AccessToken();\n } catch (error) {\n const message = extractErrorMessage(error);\n\n appLogger(message, {\n level: 'error',\n });\n return false;\n }\n\n return true;\n};\n\nexport const checkAIAccess = async (\n configuration: IntlayerConfig,\n aiOptions?: AIOptions\n): Promise<boolean> => {\n const appLogger = getAppLogger(configuration);\n\n const hasCMSAuth =\n configuration.editor.clientId && configuration.editor.clientSecret;\n const hasHisOwnAIAPIKey = configuration.ai?.apiKey || aiOptions?.apiKey;\n\n // User need to provide either his own AI API key or the CMS auth\n if (!hasCMSAuth && !hasHisOwnAIAPIKey) {\n appLogger('AI options or API key not provided.', {\n level: 'error',\n });\n\n return false;\n }\n\n // If the user do not have his own AI API key, we need to check the CMS auth\n if (!hasHisOwnAIAPIKey) {\n return await checkCMSAuth(configuration);\n }\n\n return true;\n};\n"],"mappings":";;;;AAKA,MAAa,eAAe,OAC1B,kBACqB;CACrB,MAAM,YAAY,aAAa,eAAe,EAC5C,QAAQ,EACN,QAAQ,IACT,EACF,CAAC;AAIF,KAAI,EADF,cAAc,OAAO,YAAY,cAAc,OAAO,eACvC;AACf,YAAU,0BAA0B,EAClC,OAAO,SACR,CAAC;AAEF,SAAO;;CAET,MAAM,cAAc,oBAAoB,QAAW,cAAc;AAEjE,KAAI;AACF,QAAM,YAAY,MAAM,sBAAsB;UACvC,OAAO;AAGd,YAFgB,oBAAoB,MAAM,EAEvB,EACjB,OAAO,SACR,CAAC;AACF,SAAO;;AAGT,QAAO;;AAGT,MAAa,gBAAgB,OAC3B,eACA,cACqB;CACrB,MAAM,YAAY,aAAa,cAAc;CAE7C,MAAM,aACJ,cAAc,OAAO,YAAY,cAAc,OAAO;CACxD,MAAM,oBAAoB,cAAc,IAAI,UAAU,WAAW;AAGjE,KAAI,CAAC,cAAc,CAAC,mBAAmB;AACrC,YAAU,uCAAuC,EAC/C,OAAO,SACR,CAAC;AAEF,SAAO;;AAIT,KAAI,CAAC,kBACH,QAAO,MAAM,aAAa,cAAc;AAG1C,QAAO"}
@@ -1,73 +1,81 @@
1
1
  import { checkLastUpdateTime } from "./checkLastUpdateTime.mjs";
2
2
  import { formatTimeDiff } from "./formatTimeDiff.mjs";
3
+
4
+ //#region src/utils/checkFileModifiedRange.ts
5
+ /**
6
+ * Threshold that helps us differentiate between a numeric *timestamp* (ms from epoch)
7
+ * and a numeric *duration* (ms ago).
8
+ * 50 years expressed in milliseconds is far greater than any reasonable
9
+ * "relative" duration we expect the helper to receive (e.g. a couple of years).
10
+ */
3
11
  const TIMESTAMP_THRESHOLD_MS = 50 * 365 * 24 * 60 * 60 * 1e3;
12
+ /**
13
+ * Normalises the input date representation into a pair:
14
+ * 1. `relativeTime` – a Date instance whose epoch-time equals the duration
15
+ * between `now` and the absolute date.
16
+ * 2. `absoluteTime` – the concrete point in time represented as a Date.
17
+ *
18
+ * Rules for interpreting the input:
19
+ * • Date => treated as an absolute time.
20
+ * • string => parsed via the Date constructor => absolute time.
21
+ * • number:
22
+ * – if the value is larger than the TIMESTAMP_THRESHOLD_MS we assume it
23
+ * is a unix timestamp (absolute time).
24
+ * – otherwise we treat it as a *relative* duration expressed in
25
+ * milliseconds.
26
+ */
4
27
  const normaliseInputDate = (date, now = /* @__PURE__ */ new Date()) => {
5
- if (date instanceof Date) {
6
- return {
7
- absoluteTime: date,
8
- relativeTime: new Date(now.getTime() - date.getTime())
9
- };
10
- }
11
- if (typeof date === "number") {
12
- if (date > TIMESTAMP_THRESHOLD_MS) {
13
- const absoluteTime = new Date(date);
14
- return {
15
- absoluteTime,
16
- relativeTime: new Date(now.getTime() - absoluteTime.getTime())
17
- };
18
- }
19
- const relativeMs = date;
20
- return {
21
- // Relative duration expressed as a Date object starting at the epoch
22
- relativeTime: new Date(relativeMs),
23
- // The concrete date obtained by subtracting the duration from *now*
24
- absoluteTime: new Date(now.getTime() - relativeMs)
25
- };
26
- }
27
- if (typeof date === "string") {
28
- const absoluteTime = new Date(date);
29
- if (Number.isNaN(absoluteTime.getTime())) {
30
- throw new Error(`Invalid date string provided: ${date}`);
31
- }
32
- return {
33
- absoluteTime,
34
- relativeTime: new Date(now.getTime() - absoluteTime.getTime())
35
- };
36
- }
37
- throw new Error(`Unsupported date format: ${date}`);
28
+ if (date instanceof Date) return {
29
+ absoluteTime: date,
30
+ relativeTime: new Date(now.getTime() - date.getTime())
31
+ };
32
+ if (typeof date === "number") {
33
+ if (date > TIMESTAMP_THRESHOLD_MS) {
34
+ const absoluteTime = new Date(date);
35
+ return {
36
+ absoluteTime,
37
+ relativeTime: new Date(now.getTime() - absoluteTime.getTime())
38
+ };
39
+ }
40
+ const relativeMs = date;
41
+ return {
42
+ relativeTime: new Date(relativeMs),
43
+ absoluteTime: new Date(now.getTime() - relativeMs)
44
+ };
45
+ }
46
+ if (typeof date === "string") {
47
+ const absoluteTime = new Date(date);
48
+ if (Number.isNaN(absoluteTime.getTime())) throw new Error(`Invalid date string provided: ${date}`);
49
+ return {
50
+ absoluteTime,
51
+ relativeTime: new Date(now.getTime() - absoluteTime.getTime())
52
+ };
53
+ }
54
+ throw new Error(`Unsupported date format: ${date}`);
38
55
  };
39
56
  const checkFileModifiedRange = (filePath, options) => {
40
- const fileLastUpdateTime = checkLastUpdateTime(filePath);
41
- const { skipIfModifiedBefore, skipIfModifiedAfter } = options;
42
- const now = /* @__PURE__ */ new Date();
43
- const minDate = skipIfModifiedBefore ? normaliseInputDate(skipIfModifiedBefore, now).absoluteTime : void 0;
44
- const maxDate = skipIfModifiedAfter ? normaliseInputDate(skipIfModifiedAfter, now).absoluteTime : void 0;
45
- let shouldSkip = false;
46
- if (minDate instanceof Date && maxDate instanceof Date) {
47
- shouldSkip = fileLastUpdateTime >= minDate && fileLastUpdateTime <= maxDate;
48
- } else if (minDate instanceof Date) {
49
- shouldSkip = fileLastUpdateTime >= minDate;
50
- } else if (maxDate) {
51
- shouldSkip = fileLastUpdateTime >= maxDate;
52
- }
53
- if (shouldSkip) {
54
- const referenceDate = minDate && maxDate ? (
55
- // When both bounds are present, the *range* caused the skip so we use
56
- // the distance between the two bounds as the relative duration.
57
- new Date(Math.abs(maxDate.getTime() - minDate.getTime()))
58
- ) : minDate ?? maxDate;
59
- const relativeTime = new Date(now.getTime() - referenceDate.getTime());
60
- return {
61
- isSkipped: true,
62
- message: `Skipping file because it has been modified within the last ${formatTimeDiff(relativeTime)} - ${filePath}`
63
- };
64
- }
65
- return {
66
- isSkipped: false,
67
- message: `File ${filePath} can be processed - ${filePath}`
68
- };
69
- };
70
- export {
71
- checkFileModifiedRange
57
+ const fileLastUpdateTime = checkLastUpdateTime(filePath);
58
+ const { skipIfModifiedBefore, skipIfModifiedAfter } = options;
59
+ const now = /* @__PURE__ */ new Date();
60
+ const minDate = skipIfModifiedBefore ? normaliseInputDate(skipIfModifiedBefore, now).absoluteTime : void 0;
61
+ const maxDate = skipIfModifiedAfter ? normaliseInputDate(skipIfModifiedAfter, now).absoluteTime : void 0;
62
+ let shouldSkip = false;
63
+ if (minDate instanceof Date && maxDate instanceof Date) shouldSkip = fileLastUpdateTime >= minDate && fileLastUpdateTime <= maxDate;
64
+ else if (minDate instanceof Date) shouldSkip = fileLastUpdateTime >= minDate;
65
+ else if (maxDate) shouldSkip = fileLastUpdateTime >= maxDate;
66
+ if (shouldSkip) {
67
+ const referenceDate = minDate && maxDate ? new Date(Math.abs(maxDate.getTime() - minDate.getTime())) : minDate ?? maxDate;
68
+ return {
69
+ isSkipped: true,
70
+ message: `Skipping file because it has been modified within the last ${formatTimeDiff(new Date(now.getTime() - referenceDate.getTime()))} - ${filePath}`
71
+ };
72
+ }
73
+ return {
74
+ isSkipped: false,
75
+ message: `File ${filePath} can be processed - ${filePath}`
76
+ };
72
77
  };
78
+
79
+ //#endregion
80
+ export { checkFileModifiedRange };
73
81
  //# sourceMappingURL=checkFileModifiedRange.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/utils/checkFileModifiedRange.ts"],"sourcesContent":["import { checkLastUpdateTime } from './checkLastUpdateTime';\nimport { formatTimeDiff } from './formatTimeDiff';\n\ntype GetTimeRangeResult = {\n relativeTime: Date;\n absoluteTime: Date;\n};\n\n/**\n * Threshold that helps us differentiate between a numeric *timestamp* (ms from epoch)\n * and a numeric *duration* (ms ago).\n * 50 years expressed in milliseconds is far greater than any reasonable\n * \"relative\" duration we expect the helper to receive (e.g. a couple of years).\n */\nconst TIMESTAMP_THRESHOLD_MS = 50 * 365 * 24 * 60 * 60 * 1000; // 50 years\n\n/**\n * Normalises the input date representation into a pair:\n * 1. `relativeTime` – a Date instance whose epoch-time equals the duration\n * between `now` and the absolute date.\n * 2. `absoluteTime` – the concrete point in time represented as a Date.\n *\n * Rules for interpreting the input:\n * • Date => treated as an absolute time.\n * • string => parsed via the Date constructor => absolute time.\n * • number:\n * – if the value is larger than the TIMESTAMP_THRESHOLD_MS we assume it\n * is a unix timestamp (absolute time).\n * – otherwise we treat it as a *relative* duration expressed in\n * milliseconds.\n */\nconst normaliseInputDate = (\n date: Date | number | string,\n now: Date = new Date()\n): GetTimeRangeResult => {\n // Case 1: Already a Date instance\n if (date instanceof Date) {\n return {\n absoluteTime: date,\n relativeTime: new Date(now.getTime() - date.getTime()),\n };\n }\n\n // Case 2: Numeric value – decide between timestamp vs relative ms.\n if (typeof date === 'number') {\n if (date > TIMESTAMP_THRESHOLD_MS) {\n // Treat as *unix timestamp* (absolute)\n const absoluteTime = new Date(date);\n return {\n absoluteTime,\n relativeTime: new Date(now.getTime() - absoluteTime.getTime()),\n };\n }\n\n // Treat as a *relative* duration (milliseconds in the past)\n const relativeMs = date;\n return {\n // Relative duration expressed as a Date object starting at the epoch\n relativeTime: new Date(relativeMs),\n // The concrete date obtained by subtracting the duration from *now*\n absoluteTime: new Date(now.getTime() - relativeMs),\n };\n }\n\n // Case 3: String representation – let Date parse it.\n if (typeof date === 'string') {\n const absoluteTime = new Date(date);\n if (Number.isNaN(absoluteTime.getTime())) {\n throw new Error(`Invalid date string provided: ${date}`);\n }\n\n return {\n absoluteTime,\n relativeTime: new Date(now.getTime() - absoluteTime.getTime()),\n };\n }\n\n throw new Error(`Unsupported date format: ${date}`);\n};\n\ntype CheckFileModifiedRangeResult = {\n isSkipped: boolean;\n message: string;\n};\n\ntype CheckFileModifiedRangeOptions = {\n skipIfModifiedBefore?: Date | number | string;\n skipIfModifiedAfter?: Date | number | string;\n};\n\nexport const checkFileModifiedRange = (\n filePath: string,\n options: CheckFileModifiedRangeOptions\n): CheckFileModifiedRangeResult => {\n const fileLastUpdateTime = checkLastUpdateTime(filePath);\n const { skipIfModifiedBefore, skipIfModifiedAfter } = options;\n\n // Normalise the provided thresholds to concrete dates.\n const now = new Date();\n const minDate = skipIfModifiedBefore\n ? normaliseInputDate(skipIfModifiedBefore, now).absoluteTime\n : undefined;\n const maxDate = skipIfModifiedAfter\n ? normaliseInputDate(skipIfModifiedAfter, now).absoluteTime\n : undefined;\n\n // Determine if the file should be skipped.\n let shouldSkip = false;\n\n if (minDate instanceof Date && maxDate instanceof Date) {\n // Skip when the modification time falls *within* the range [minDate, maxDate]\n shouldSkip = fileLastUpdateTime >= minDate && fileLastUpdateTime <= maxDate;\n } else if (minDate instanceof Date) {\n // Only lower bound – skip when the file was modified *after* minDate\n shouldSkip = fileLastUpdateTime >= minDate;\n } else if (maxDate) {\n // Only upper bound – skip when the file was modified *after* maxDate\n shouldSkip = fileLastUpdateTime >= maxDate;\n }\n\n if (shouldSkip) {\n // For the sake of the message we compute the relative time to *now* using\n // whichever bound was responsible for the skip logic.\n const referenceDate = (\n minDate && maxDate\n ? // When both bounds are present, the *range* caused the skip so we use\n // the distance between the two bounds as the relative duration.\n new Date(Math.abs(maxDate.getTime() - minDate.getTime()))\n : (minDate ?? maxDate)\n )!;\n\n const relativeTime = new Date(now.getTime() - referenceDate.getTime());\n\n return {\n isSkipped: true,\n message: `Skipping file because it has been modified within the last ${formatTimeDiff(relativeTime)} - ${filePath}`,\n };\n }\n\n return {\n isSkipped: false,\n message: `File ${filePath} can be processed - ${filePath}`,\n };\n};\n"],"mappings":"AAAA,SAAS,2BAA2B;AACpC,SAAS,sBAAsB;AAa/B,MAAM,yBAAyB,KAAK,MAAM,KAAK,KAAK,KAAK;AAiBzD,MAAM,qBAAqB,CACzB,MACA,MAAY,oBAAI,KAAK,MACE;AAEvB,MAAI,gBAAgB,MAAM;AACxB,WAAO;AAAA,MACL,cAAc;AAAA,MACd,cAAc,IAAI,KAAK,IAAI,QAAQ,IAAI,KAAK,QAAQ,CAAC;AAAA,IACvD;AAAA,EACF;AAGA,MAAI,OAAO,SAAS,UAAU;AAC5B,QAAI,OAAO,wBAAwB;AAEjC,YAAM,eAAe,IAAI,KAAK,IAAI;AAClC,aAAO;AAAA,QACL;AAAA,QACA,cAAc,IAAI,KAAK,IAAI,QAAQ,IAAI,aAAa,QAAQ,CAAC;AAAA,MAC/D;AAAA,IACF;AAGA,UAAM,aAAa;AACnB,WAAO;AAAA;AAAA,MAEL,cAAc,IAAI,KAAK,UAAU;AAAA;AAAA,MAEjC,cAAc,IAAI,KAAK,IAAI,QAAQ,IAAI,UAAU;AAAA,IACn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uB,QAAQ;AAAA,EAC1D;AACF;","names":[]}
1
+ {"version":3,"file":"checkFileModifiedRange.mjs","names":[],"sources":["../../../src/utils/checkFileModifiedRange.ts"],"sourcesContent":["import { checkLastUpdateTime } from './checkLastUpdateTime';\nimport { formatTimeDiff } from './formatTimeDiff';\n\ntype GetTimeRangeResult = {\n relativeTime: Date;\n absoluteTime: Date;\n};\n\n/**\n * Threshold that helps us differentiate between a numeric *timestamp* (ms from epoch)\n * and a numeric *duration* (ms ago).\n * 50 years expressed in milliseconds is far greater than any reasonable\n * \"relative\" duration we expect the helper to receive (e.g. a couple of years).\n */\nconst TIMESTAMP_THRESHOLD_MS = 50 * 365 * 24 * 60 * 60 * 1000; // 50 years\n\n/**\n * Normalises the input date representation into a pair:\n * 1. `relativeTime` – a Date instance whose epoch-time equals the duration\n * between `now` and the absolute date.\n * 2. `absoluteTime` – the concrete point in time represented as a Date.\n *\n * Rules for interpreting the input:\n * • Date => treated as an absolute time.\n * • string => parsed via the Date constructor => absolute time.\n * • number:\n * – if the value is larger than the TIMESTAMP_THRESHOLD_MS we assume it\n * is a unix timestamp (absolute time).\n * – otherwise we treat it as a *relative* duration expressed in\n * milliseconds.\n */\nconst normaliseInputDate = (\n date: Date | number | string,\n now: Date = new Date()\n): GetTimeRangeResult => {\n // Case 1: Already a Date instance\n if (date instanceof Date) {\n return {\n absoluteTime: date,\n relativeTime: new Date(now.getTime() - date.getTime()),\n };\n }\n\n // Case 2: Numeric value – decide between timestamp vs relative ms.\n if (typeof date === 'number') {\n if (date > TIMESTAMP_THRESHOLD_MS) {\n // Treat as *unix timestamp* (absolute)\n const absoluteTime = new Date(date);\n return {\n absoluteTime,\n relativeTime: new Date(now.getTime() - absoluteTime.getTime()),\n };\n }\n\n // Treat as a *relative* duration (milliseconds in the past)\n const relativeMs = date;\n return {\n // Relative duration expressed as a Date object starting at the epoch\n relativeTime: new Date(relativeMs),\n // The concrete date obtained by subtracting the duration from *now*\n absoluteTime: new Date(now.getTime() - relativeMs),\n };\n }\n\n // Case 3: String representation – let Date parse it.\n if (typeof date === 'string') {\n const absoluteTime = new Date(date);\n if (Number.isNaN(absoluteTime.getTime())) {\n throw new Error(`Invalid date string provided: ${date}`);\n }\n\n return {\n absoluteTime,\n relativeTime: new Date(now.getTime() - absoluteTime.getTime()),\n };\n }\n\n throw new Error(`Unsupported date format: ${date}`);\n};\n\ntype CheckFileModifiedRangeResult = {\n isSkipped: boolean;\n message: string;\n};\n\ntype CheckFileModifiedRangeOptions = {\n skipIfModifiedBefore?: Date | number | string;\n skipIfModifiedAfter?: Date | number | string;\n};\n\nexport const checkFileModifiedRange = (\n filePath: string,\n options: CheckFileModifiedRangeOptions\n): CheckFileModifiedRangeResult => {\n const fileLastUpdateTime = checkLastUpdateTime(filePath);\n const { skipIfModifiedBefore, skipIfModifiedAfter } = options;\n\n // Normalise the provided thresholds to concrete dates.\n const now = new Date();\n const minDate = skipIfModifiedBefore\n ? normaliseInputDate(skipIfModifiedBefore, now).absoluteTime\n : undefined;\n const maxDate = skipIfModifiedAfter\n ? normaliseInputDate(skipIfModifiedAfter, now).absoluteTime\n : undefined;\n\n // Determine if the file should be skipped.\n let shouldSkip = false;\n\n if (minDate instanceof Date && maxDate instanceof Date) {\n // Skip when the modification time falls *within* the range [minDate, maxDate]\n shouldSkip = fileLastUpdateTime >= minDate && fileLastUpdateTime <= maxDate;\n } else if (minDate instanceof Date) {\n // Only lower bound – skip when the file was modified *after* minDate\n shouldSkip = fileLastUpdateTime >= minDate;\n } else if (maxDate) {\n // Only upper bound – skip when the file was modified *after* maxDate\n shouldSkip = fileLastUpdateTime >= maxDate;\n }\n\n if (shouldSkip) {\n // For the sake of the message we compute the relative time to *now* using\n // whichever bound was responsible for the skip logic.\n const referenceDate = (\n minDate && maxDate\n ? // When both bounds are present, the *range* caused the skip so we use\n // the distance between the two bounds as the relative duration.\n new Date(Math.abs(maxDate.getTime() - minDate.getTime()))\n : (minDate ?? maxDate)\n )!;\n\n const relativeTime = new Date(now.getTime() - referenceDate.getTime());\n\n return {\n isSkipped: true,\n message: `Skipping file because it has been modified within the last ${formatTimeDiff(relativeTime)} - ${filePath}`,\n };\n }\n\n return {\n isSkipped: false,\n message: `File ${filePath} can be processed - ${filePath}`,\n };\n};\n"],"mappings":";;;;;;;;;;AAcA,MAAM,yBAAyB,KAAK,MAAM,KAAK,KAAK,KAAK;;;;;;;;;;;;;;;;AAiBzD,MAAM,sBACJ,MACA,sBAAY,IAAI,MAAM,KACC;AAEvB,KAAI,gBAAgB,KAClB,QAAO;EACL,cAAc;EACd,cAAc,IAAI,KAAK,IAAI,SAAS,GAAG,KAAK,SAAS,CAAC;EACvD;AAIH,KAAI,OAAO,SAAS,UAAU;AAC5B,MAAI,OAAO,wBAAwB;GAEjC,MAAM,eAAe,IAAI,KAAK,KAAK;AACnC,UAAO;IACL;IACA,cAAc,IAAI,KAAK,IAAI,SAAS,GAAG,aAAa,SAAS,CAAC;IAC/D;;EAIH,MAAM,aAAa;AACnB,SAAO;GAEL,cAAc,IAAI,KAAK,WAAW;GAElC,cAAc,IAAI,KAAK,IAAI,SAAS,GAAG,WAAW;GACnD;;AAIH,KAAI,OAAO,SAAS,UAAU;EAC5B,MAAM,eAAe,IAAI,KAAK,KAAK;AACnC,MAAI,OAAO,MAAM,aAAa,SAAS,CAAC,CACtC,OAAM,IAAI,MAAM,iCAAiC,OAAO;AAG1D,SAAO;GACL;GACA,cAAc,IAAI,KAAK,IAAI,SAAS,GAAG,aAAa,SAAS,CAAC;GAC/D;;AAGH,OAAM,IAAI,MAAM,4BAA4B,OAAO;;AAarD,MAAa,0BACX,UACA,YACiC;CACjC,MAAM,qBAAqB,oBAAoB,SAAS;CACxD,MAAM,EAAE,sBAAsB,wBAAwB;CAGtD,MAAM,sBAAM,IAAI,MAAM;CACtB,MAAM,UAAU,uBACZ,mBAAmB,sBAAsB,IAAI,CAAC,eAC9C;CACJ,MAAM,UAAU,sBACZ,mBAAmB,qBAAqB,IAAI,CAAC,eAC7C;CAGJ,IAAI,aAAa;AAEjB,KAAI,mBAAmB,QAAQ,mBAAmB,KAEhD,cAAa,sBAAsB,WAAW,sBAAsB;UAC3D,mBAAmB,KAE5B,cAAa,sBAAsB;UAC1B,QAET,cAAa,sBAAsB;AAGrC,KAAI,YAAY;EAGd,MAAM,gBACJ,WAAW,UAGP,IAAI,KAAK,KAAK,IAAI,QAAQ,SAAS,GAAG,QAAQ,SAAS,CAAC,CAAC,GACxD,WAAW;AAKlB,SAAO;GACL,WAAW;GACX,SAAS,8DAA8D,eAJpD,IAAI,KAAK,IAAI,SAAS,GAAG,cAAc,SAAS,CAAC,CAI+B,CAAC,KAAK;GAC1G;;AAGH,QAAO;EACL,WAAW;EACX,SAAS,QAAQ,SAAS,sBAAsB;EACjD"}
@@ -1,9 +1,18 @@
1
- import { statSync } from "fs";
1
+ import { statSync } from "node:fs";
2
+
3
+ //#region src/utils/checkLastUpdateTime.ts
4
+ /**
5
+ * Returns the last modification date of a file.
6
+ *
7
+ * @param filePath - Absolute or relative path to the file to inspect.
8
+ * @returns Date instance representing the file's last modified time (mtime).
9
+ * @throws Will propagate any error thrown by fs.statSync (e.g., file not found).
10
+ */
2
11
  const checkLastUpdateTime = (filePath) => {
3
- const stats = statSync(filePath);
4
- return new Date(stats.mtime);
5
- };
6
- export {
7
- checkLastUpdateTime
12
+ const stats = statSync(filePath);
13
+ return new Date(stats.mtime);
8
14
  };
15
+
16
+ //#endregion
17
+ export { checkLastUpdateTime };
9
18
  //# sourceMappingURL=checkLastUpdateTime.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/utils/checkLastUpdateTime.ts"],"sourcesContent":["import { statSync } from 'fs';\n\n/**\n * Returns the last modification date of a file.\n *\n * @param filePath - Absolute or relative path to the file to inspect.\n * @returns Date instance representing the file's last modified time (mtime).\n * @throws Will propagate any error thrown by fs.statSync (e.g., file not found).\n */\nexport const checkLastUpdateTime = (filePath: string): Date => {\n const stats = statSync(filePath);\n return new Date(stats.mtime);\n};\n"],"mappings":"AAAA,SAAS,gBAAgB;AASlB,MAAM,sBAAsB,CAAC,aAA2B;AAC7D,QAAM,QAAQ,SAAS,QAAQ;AAC/B,SAAO,IAAI,KAAK,MAAM,KAAK;AAC7B;","names":[]}
1
+ {"version":3,"file":"checkLastUpdateTime.mjs","names":[],"sources":["../../../src/utils/checkLastUpdateTime.ts"],"sourcesContent":["import { statSync } from 'node:fs';\n\n/**\n * Returns the last modification date of a file.\n *\n * @param filePath - Absolute or relative path to the file to inspect.\n * @returns Date instance representing the file's last modified time (mtime).\n * @throws Will propagate any error thrown by fs.statSync (e.g., file not found).\n */\nexport const checkLastUpdateTime = (filePath: string): Date => {\n const stats = statSync(filePath);\n return new Date(stats.mtime);\n};\n"],"mappings":";;;;;;;;;;AASA,MAAa,uBAAuB,aAA2B;CAC7D,MAAM,QAAQ,SAAS,SAAS;AAChC,QAAO,IAAI,KAAK,MAAM,MAAM"}