@intlayer/cli 6.1.6-canary.0 → 7.0.0-canary.0
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.
- package/dist/cjs/IntlayerEventListener.cjs +187 -239
- package/dist/cjs/IntlayerEventListener.cjs.map +1 -1
- package/dist/cjs/_virtual/_utils_asset.cjs +104 -0
- package/dist/cjs/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/cjs/build.cjs +23 -42
- package/dist/cjs/build.cjs.map +1 -1
- package/dist/cjs/cli.cjs +370 -431
- package/dist/cjs/cli.cjs.map +1 -1
- package/dist/cjs/config.cjs +10 -35
- package/dist/cjs/config.cjs.map +1 -1
- package/dist/cjs/editor.cjs +51 -66
- package/dist/cjs/editor.cjs.map +1 -1
- package/dist/cjs/fill/fill.cjs +73 -301
- package/dist/cjs/fill/fill.cjs.map +1 -1
- package/dist/cjs/fill/formatAutoFillData.cjs +48 -106
- package/dist/cjs/fill/formatAutoFillData.cjs.map +1 -1
- package/dist/cjs/fill/formatAutoFilledFilePath.cjs +27 -43
- package/dist/cjs/fill/formatAutoFilledFilePath.cjs.map +1 -1
- package/dist/cjs/fill/formatFillData.cjs +50 -0
- package/dist/cjs/fill/formatFillData.cjs.map +1 -0
- package/dist/cjs/fill/groupLimiter.cjs +42 -0
- package/dist/cjs/fill/groupLimiter.cjs.map +1 -0
- package/dist/cjs/fill/index.cjs +5 -25
- package/dist/cjs/fill/listTranslationsTasks.cjs +77 -0
- package/dist/cjs/fill/listTranslationsTasks.cjs.map +1 -0
- package/dist/cjs/fill/test-original-case.cjs +10 -0
- package/dist/cjs/fill/test-original-case.cjs.map +1 -0
- package/dist/cjs/fill/translateDictionary.cjs +148 -0
- package/dist/cjs/fill/translateDictionary.cjs.map +1 -0
- package/dist/cjs/fill/writeAutoFill.cjs +48 -0
- package/dist/cjs/fill/writeAutoFill.cjs.map +1 -0
- package/dist/cjs/fill/writeFill.cjs +50 -0
- package/dist/cjs/fill/writeFill.cjs.map +1 -0
- package/dist/cjs/getTargetDictionary.cjs +36 -76
- package/dist/cjs/getTargetDictionary.cjs.map +1 -1
- package/dist/cjs/index.cjs +31 -45
- package/dist/cjs/listContentDeclaration.cjs +36 -64
- package/dist/cjs/listContentDeclaration.cjs.map +1 -1
- package/dist/cjs/liveSync.cjs +146 -221
- package/dist/cjs/liveSync.cjs.map +1 -1
- package/dist/cjs/pull.cjs +132 -195
- package/dist/cjs/pull.cjs.map +1 -1
- package/dist/cjs/push/pullLog.cjs +101 -144
- package/dist/cjs/push/pullLog.cjs.map +1 -1
- package/dist/cjs/push/push.cjs +159 -249
- package/dist/cjs/push/push.cjs.map +1 -1
- package/dist/cjs/pushConfig.cjs +18 -45
- package/dist/cjs/pushConfig.cjs.map +1 -1
- package/dist/cjs/pushLog.cjs +87 -128
- package/dist/cjs/pushLog.cjs.map +1 -1
- package/dist/cjs/reviewDoc.cjs +183 -291
- package/dist/cjs/reviewDoc.cjs.map +1 -1
- package/dist/cjs/test/index.cjs +52 -91
- package/dist/cjs/test/index.cjs.map +1 -1
- package/dist/cjs/test/listMissingTranslations.cjs +35 -62
- package/dist/cjs/test/listMissingTranslations.cjs.map +1 -1
- package/dist/cjs/translateDoc.cjs +127 -221
- package/dist/cjs/translateDoc.cjs.map +1 -1
- package/dist/cjs/utils/calculateChunks.cjs +85 -115
- package/dist/cjs/utils/calculateChunks.cjs.map +1 -1
- package/dist/cjs/utils/checkAccess.cjs +33 -72
- package/dist/cjs/utils/checkAccess.cjs.map +1 -1
- package/dist/cjs/utils/checkFileModifiedRange.cjs +75 -91
- package/dist/cjs/utils/checkFileModifiedRange.cjs.map +1 -1
- package/dist/cjs/utils/checkLastUpdateTime.cjs +17 -30
- package/dist/cjs/utils/checkLastUpdateTime.cjs.map +1 -1
- package/dist/cjs/utils/chunkInference.cjs +28 -47
- package/dist/cjs/utils/chunkInference.cjs.map +1 -1
- package/dist/cjs/utils/fixChunkStartEndChars.cjs +23 -42
- package/dist/cjs/utils/fixChunkStartEndChars.cjs.map +1 -1
- package/dist/cjs/utils/formatTimeDiff.cjs +17 -43
- package/dist/cjs/utils/formatTimeDiff.cjs.map +1 -1
- package/dist/cjs/utils/getIsFileUpdatedRecently.cjs +13 -32
- package/dist/cjs/utils/getIsFileUpdatedRecently.cjs.map +1 -1
- package/dist/cjs/utils/getOutputFilePath.cjs +71 -86
- package/dist/cjs/utils/getOutputFilePath.cjs.map +1 -1
- package/dist/cjs/utils/getParentPackageJSON.cjs +19 -44
- package/dist/cjs/utils/getParentPackageJSON.cjs.map +1 -1
- package/dist/cjs/utils/listSpecialChars.cjs +49 -73
- package/dist/cjs/utils/listSpecialChars.cjs.map +1 -1
- package/dist/cjs/utils/mapChunksBetweenFiles.cjs +90 -132
- package/dist/cjs/utils/mapChunksBetweenFiles.cjs.map +1 -1
- package/dist/cjs/utils/reorderParagraphs.cjs +86 -118
- package/dist/cjs/utils/reorderParagraphs.cjs.map +1 -1
- package/dist/cjs/watch.cjs +21 -39
- package/dist/cjs/watch.cjs.map +1 -1
- package/dist/esm/IntlayerEventListener.mjs +178 -201
- package/dist/esm/IntlayerEventListener.mjs.map +1 -1
- package/dist/esm/_virtual/_utils_asset.mjs +100 -0
- package/dist/esm/_virtual/rolldown_runtime.mjs +8 -0
- package/dist/esm/build.mjs +20 -23
- package/dist/esm/build.mjs.map +1 -1
- package/dist/esm/cli.mjs +349 -380
- package/dist/esm/cli.mjs.map +1 -1
- package/dist/esm/config.mjs +8 -14
- package/dist/esm/config.mjs.map +1 -1
- package/dist/esm/editor.mjs +47 -41
- package/dist/esm/editor.mjs.map +1 -1
- package/dist/esm/fill/fill.mjs +68 -300
- package/dist/esm/fill/fill.mjs.map +1 -1
- package/dist/esm/fill/formatAutoFillData.mjs +47 -81
- package/dist/esm/fill/formatAutoFillData.mjs.map +1 -1
- package/dist/esm/fill/formatAutoFilledFilePath.mjs +25 -19
- package/dist/esm/fill/formatAutoFilledFilePath.mjs.map +1 -1
- package/dist/esm/fill/formatFillData.mjs +50 -0
- package/dist/esm/fill/formatFillData.mjs.map +1 -0
- package/dist/esm/fill/groupLimiter.mjs +40 -0
- package/dist/esm/fill/groupLimiter.mjs.map +1 -0
- package/dist/esm/fill/index.mjs +4 -3
- package/dist/esm/fill/listTranslationsTasks.mjs +70 -0
- package/dist/esm/fill/listTranslationsTasks.mjs.map +1 -0
- package/dist/esm/fill/test-original-case.mjs +10 -0
- package/dist/esm/fill/test-original-case.mjs.map +1 -0
- package/dist/esm/fill/translateDictionary.mjs +141 -0
- package/dist/esm/fill/translateDictionary.mjs.map +1 -0
- package/dist/esm/fill/writeAutoFill.mjs +44 -0
- package/dist/esm/fill/writeAutoFill.mjs.map +1 -0
- package/dist/esm/fill/writeFill.mjs +45 -0
- package/dist/esm/fill/writeFill.mjs.map +1 -0
- package/dist/esm/getTargetDictionary.mjs +27 -48
- package/dist/esm/getTargetDictionary.mjs.map +1 -1
- package/dist/esm/index.mjs +15 -13
- package/dist/esm/listContentDeclaration.mjs +28 -43
- package/dist/esm/listContentDeclaration.mjs.map +1 -1
- package/dist/esm/liveSync.mjs +135 -187
- package/dist/esm/liveSync.mjs.map +1 -1
- package/dist/esm/pull.mjs +125 -178
- package/dist/esm/pull.mjs.map +1 -1
- package/dist/esm/push/pullLog.mjs +99 -125
- package/dist/esm/push/pullLog.mjs.map +1 -1
- package/dist/esm/push/push.mjs +149 -221
- package/dist/esm/push/push.mjs.map +1 -1
- package/dist/esm/pushConfig.mjs +14 -23
- package/dist/esm/pushConfig.mjs.map +1 -1
- package/dist/esm/pushLog.mjs +85 -109
- package/dist/esm/pushLog.mjs.map +1 -1
- package/dist/esm/reviewDoc.mjs +167 -264
- package/dist/esm/reviewDoc.mjs.map +1 -1
- package/dist/esm/test/index.mjs +47 -73
- package/dist/esm/test/index.mjs.map +1 -1
- package/dist/esm/test/listMissingTranslations.mjs +30 -41
- package/dist/esm/test/listMissingTranslations.mjs.map +1 -1
- package/dist/esm/translateDoc.mjs +114 -193
- package/dist/esm/translateDoc.mjs.map +1 -1
- package/dist/esm/utils/calculateChunks.mjs +83 -91
- package/dist/esm/utils/calculateChunks.mjs.map +1 -1
- package/dist/esm/utils/checkAccess.mjs +28 -46
- package/dist/esm/utils/checkAccess.mjs.map +1 -1
- package/dist/esm/utils/checkFileModifiedRange.mjs +73 -65
- package/dist/esm/utils/checkFileModifiedRange.mjs.map +1 -1
- package/dist/esm/utils/checkLastUpdateTime.mjs +15 -6
- package/dist/esm/utils/checkLastUpdateTime.mjs.map +1 -1
- package/dist/esm/utils/chunkInference.mjs +24 -24
- package/dist/esm/utils/chunkInference.mjs.map +1 -1
- package/dist/esm/utils/fixChunkStartEndChars.mjs +22 -18
- package/dist/esm/utils/fixChunkStartEndChars.mjs.map +1 -1
- package/dist/esm/utils/formatTimeDiff.mjs +16 -19
- package/dist/esm/utils/formatTimeDiff.mjs.map +1 -1
- package/dist/esm/utils/getIsFileUpdatedRecently.mjs +11 -8
- package/dist/esm/utils/getIsFileUpdatedRecently.mjs.map +1 -1
- package/dist/esm/utils/getOutputFilePath.mjs +70 -62
- package/dist/esm/utils/getOutputFilePath.mjs.map +1 -1
- package/dist/esm/utils/getParentPackageJSON.mjs +16 -20
- package/dist/esm/utils/getParentPackageJSON.mjs.map +1 -1
- package/dist/esm/utils/listSpecialChars.mjs +48 -49
- package/dist/esm/utils/listSpecialChars.mjs.map +1 -1
- package/dist/esm/utils/mapChunksBetweenFiles.mjs +87 -107
- package/dist/esm/utils/mapChunksBetweenFiles.mjs.map +1 -1
- package/dist/esm/utils/reorderParagraphs.mjs +85 -93
- package/dist/esm/utils/reorderParagraphs.mjs.map +1 -1
- package/dist/esm/watch.mjs +17 -17
- package/dist/esm/watch.mjs.map +1 -1
- package/dist/types/IntlayerEventListener.d.ts +63 -59
- package/dist/types/IntlayerEventListener.d.ts.map +1 -1
- package/dist/types/build.d.ts +10 -7
- package/dist/types/build.d.ts.map +1 -1
- package/dist/types/cli.d.ts +13 -10
- package/dist/types/cli.d.ts.map +1 -1
- package/dist/types/config.d.ts +7 -4
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/editor.d.ts +6 -4
- package/dist/types/editor.d.ts.map +1 -1
- package/dist/types/fill/fill.d.ts +20 -14
- package/dist/types/fill/fill.d.ts.map +1 -1
- package/dist/types/fill/formatAutoFillData.d.ts +10 -7
- package/dist/types/fill/formatAutoFillData.d.ts.map +1 -1
- package/dist/types/fill/formatAutoFilledFilePath.d.ts +6 -2
- package/dist/types/fill/formatAutoFilledFilePath.d.ts.map +1 -1
- package/dist/types/fill/formatFillData.d.ts +12 -0
- package/dist/types/fill/formatFillData.d.ts.map +1 -0
- package/dist/types/fill/groupLimiter.d.ts +15 -0
- package/dist/types/fill/groupLimiter.d.ts.map +1 -0
- package/dist/types/fill/index.d.ts +3 -3
- package/dist/types/fill/listTranslationsTasks.d.ts +15 -0
- package/dist/types/fill/listTranslationsTasks.d.ts.map +1 -0
- package/dist/types/fill/test-original-case.d.ts +1 -0
- package/dist/types/fill/translateDictionary.d.ts +19 -0
- package/dist/types/fill/translateDictionary.d.ts.map +1 -0
- package/dist/types/fill/writeAutoFill.d.ts +7 -0
- package/dist/types/fill/writeAutoFill.d.ts.map +1 -0
- package/dist/types/fill/writeFill.d.ts +7 -0
- package/dist/types/fill/writeFill.d.ts.map +1 -0
- package/dist/types/getTargetDictionary.d.ts +17 -13
- package/dist/types/getTargetDictionary.d.ts.map +1 -1
- package/dist/types/index.d.ts +15 -14
- package/dist/types/listContentDeclaration.d.ts +10 -7
- package/dist/types/listContentDeclaration.d.ts.map +1 -1
- package/dist/types/liveSync.d.ts +8 -3
- package/dist/types/liveSync.d.ts.map +1 -1
- package/dist/types/pull.d.ts +9 -6
- package/dist/types/pull.d.ts.map +1 -1
- package/dist/types/push/pullLog.d.ts +25 -21
- package/dist/types/push/pullLog.d.ts.map +1 -1
- package/dist/types/push/push.d.ts +13 -9
- package/dist/types/push/push.d.ts.map +1 -1
- package/dist/types/pushConfig.d.ts +7 -4
- package/dist/types/pushConfig.d.ts.map +1 -1
- package/dist/types/pushLog.d.ts +23 -20
- package/dist/types/pushLog.d.ts.map +1 -1
- package/dist/types/reviewDoc.d.ts +34 -17
- package/dist/types/reviewDoc.d.ts.map +1 -1
- package/dist/types/test/index.d.ts +9 -5
- package/dist/types/test/index.d.ts.map +1 -1
- package/dist/types/test/listMissingTranslations.d.ts +14 -10
- package/dist/types/test/listMissingTranslations.d.ts.map +1 -1
- package/dist/types/translateDoc.d.ts +34 -17
- package/dist/types/translateDoc.d.ts.map +1 -1
- package/dist/types/utils/calculateChunks.d.ts +10 -7
- package/dist/types/utils/calculateChunks.d.ts.map +1 -1
- package/dist/types/utils/checkAccess.d.ts +8 -4
- package/dist/types/utils/checkAccess.d.ts.map +1 -1
- package/dist/types/utils/checkFileModifiedRange.d.ts +8 -6
- package/dist/types/utils/checkFileModifiedRange.d.ts.map +1 -1
- package/dist/types/utils/checkLastUpdateTime.d.ts +4 -1
- package/dist/types/utils/checkLastUpdateTime.d.ts.map +1 -1
- package/dist/types/utils/chunkInference.d.ts +9 -6
- package/dist/types/utils/chunkInference.d.ts.map +1 -1
- package/dist/types/utils/fixChunkStartEndChars.d.ts +4 -1
- package/dist/types/utils/fixChunkStartEndChars.d.ts.map +1 -1
- package/dist/types/utils/formatTimeDiff.d.ts +4 -1
- package/dist/types/utils/formatTimeDiff.d.ts.map +1 -1
- package/dist/types/utils/getIsFileUpdatedRecently.d.ts +4 -1
- package/dist/types/utils/getIsFileUpdatedRecently.d.ts.map +1 -1
- package/dist/types/utils/getOutputFilePath.d.ts +7 -2
- package/dist/types/utils/getOutputFilePath.d.ts.map +1 -1
- package/dist/types/utils/getParentPackageJSON.d.ts +30 -28
- package/dist/types/utils/getParentPackageJSON.d.ts.map +1 -1
- package/dist/types/utils/listSpecialChars.d.ts +9 -7
- package/dist/types/utils/listSpecialChars.d.ts.map +1 -1
- package/dist/types/utils/mapChunksBetweenFiles.d.ts +10 -6
- package/dist/types/utils/mapChunksBetweenFiles.d.ts.map +1 -1
- package/dist/types/utils/reorderParagraphs.d.ts +4 -1
- package/dist/types/utils/reorderParagraphs.d.ts.map +1 -1
- package/dist/types/watch.d.ts +9 -6
- package/dist/types/watch.d.ts.map +1 -1
- package/package.json +42 -47
- package/LICENSE +0 -202
- package/dist/cjs/fill/autoFill.cjs +0 -105
- package/dist/cjs/fill/autoFill.cjs.map +0 -1
- package/dist/cjs/fill/index.cjs.map +0 -1
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/utils/getChunk.cjs +0 -77
- package/dist/cjs/utils/getChunk.cjs.map +0 -1
- package/dist/cjs/utils/splitTextByLine.cjs +0 -35
- package/dist/cjs/utils/splitTextByLine.cjs.map +0 -1
- package/dist/esm/fill/autoFill.mjs +0 -92
- package/dist/esm/fill/autoFill.mjs.map +0 -1
- package/dist/esm/fill/index.mjs.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
- package/dist/esm/prompts/REVIEW_PROMPT.md +0 -37
- package/dist/esm/prompts/TRANSLATE_PROMPT.md +0 -38
- package/dist/esm/utils/calculrateChunkTest.md +0 -9
- package/dist/esm/utils/getChunk.mjs +0 -53
- package/dist/esm/utils/getChunk.mjs.map +0 -1
- package/dist/esm/utils/splitTextByLine.mjs +0 -11
- package/dist/esm/utils/splitTextByLine.mjs.map +0 -1
- package/dist/types/fill/autoFill.d.ts +0 -4
- package/dist/types/fill/autoFill.d.ts.map +0 -1
- package/dist/types/fill/index.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/utils/getChunk.d.ts +0 -9
- package/dist/types/utils/getChunk.d.ts.map +0 -1
- package/dist/types/utils/splitTextByLine.d.ts +0 -2
- package/dist/types/utils/splitTextByLine.d.ts.map +0 -1
- /package/dist/{cjs → assets}/prompts/REVIEW_PROMPT.md +0 -0
- /package/dist/{cjs → assets}/prompts/TRANSLATE_PROMPT.md +0 -0
- /package/dist/{cjs/utils/calculrateChunkTest.md → assets/utils/_calculateChunkTest.md} +0 -0
|
@@ -1,224 +1,130 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_utils_checkAccess = require('./utils/checkAccess.cjs');
|
|
3
|
+
const require__utils_asset = require('./_virtual/_utils_asset.cjs');
|
|
4
|
+
const require_utils_calculateChunks = require('./utils/calculateChunks.cjs');
|
|
5
|
+
const require_utils_checkFileModifiedRange = require('./utils/checkFileModifiedRange.cjs');
|
|
6
|
+
const require_utils_chunkInference = require('./utils/chunkInference.cjs');
|
|
7
|
+
const require_utils_fixChunkStartEndChars = require('./utils/fixChunkStartEndChars.cjs');
|
|
8
|
+
const require_utils_getOutputFilePath = require('./utils/getOutputFilePath.cjs');
|
|
9
|
+
let __intlayer_chokidar = require("@intlayer/chokidar");
|
|
10
|
+
__intlayer_chokidar = require_rolldown_runtime.__toESM(__intlayer_chokidar);
|
|
11
|
+
let __intlayer_config = require("@intlayer/config");
|
|
12
|
+
__intlayer_config = require_rolldown_runtime.__toESM(__intlayer_config);
|
|
13
|
+
let node_path = require("node:path");
|
|
14
|
+
node_path = require_rolldown_runtime.__toESM(node_path);
|
|
15
|
+
let node_fs = require("node:fs");
|
|
16
|
+
node_fs = require_rolldown_runtime.__toESM(node_fs);
|
|
17
|
+
let node_fs_promises = require("node:fs/promises");
|
|
18
|
+
node_fs_promises = require_rolldown_runtime.__toESM(node_fs_promises);
|
|
19
|
+
let fast_glob = require("fast-glob");
|
|
20
|
+
fast_glob = require_rolldown_runtime.__toESM(fast_glob);
|
|
21
|
+
|
|
22
|
+
//#region src/translateDoc.ts
|
|
23
|
+
/**
|
|
24
|
+
* Translate a single file for a given locale
|
|
25
|
+
*/
|
|
26
|
+
const translateFile = async (baseFilePath, outputFilePath, locale, baseLocale, configuration, aiOptions, customInstructions) => {
|
|
27
|
+
try {
|
|
28
|
+
const appLogger = (0, __intlayer_config.getAppLogger)(configuration, { config: { prefix: "" } });
|
|
29
|
+
const fileContent = await (0, node_fs_promises.readFile)(baseFilePath, "utf-8");
|
|
30
|
+
let fileResultContent = fileContent;
|
|
31
|
+
const basePrompt = require__utils_asset.readAsset("./prompts/TRANSLATE_PROMPT.md", "utf-8").replaceAll("{{localeName}}", `${(0, __intlayer_chokidar.formatLocale)(locale, false)}`).replaceAll("{{baseLocaleName}}", `${(0, __intlayer_chokidar.formatLocale)(baseLocale, false)}`).replace("{{applicationContext}}", aiOptions?.applicationContext ?? "-").replace("{{customInstructions}}", customInstructions ?? "-");
|
|
32
|
+
const filePrefix = [(0, __intlayer_config.colon)(`${__intlayer_config.ANSIColors.GREY_DARK}[${(0, __intlayer_chokidar.formatPath)(baseFilePath)}${__intlayer_config.ANSIColors.GREY_DARK}] `, { colSize: 40 }), `→ ${__intlayer_config.ANSIColors.RESET}`].join("");
|
|
33
|
+
const prefix = [(0, __intlayer_config.colon)(`${__intlayer_config.ANSIColors.GREY_DARK}[${(0, __intlayer_chokidar.formatPath)(baseFilePath)}${__intlayer_config.ANSIColors.GREY_DARK}][${(0, __intlayer_chokidar.formatLocale)(locale)}${__intlayer_config.ANSIColors.GREY_DARK}] `, { colSize: 40 }), `→ ${__intlayer_config.ANSIColors.RESET}`].join("");
|
|
34
|
+
const chunks = require_utils_calculateChunks.chunkText(fileContent);
|
|
35
|
+
appLogger(`${filePrefix}Base file splitted into ${(0, __intlayer_config.colorizeNumber)(chunks.length)} chunks`);
|
|
36
|
+
for await (const [i, chunk] of chunks.entries()) {
|
|
37
|
+
const isFirstChunk = i === 0;
|
|
38
|
+
const getPrevChunkPrompt = () => `**CHUNK ${i} of ${chunks.length}** that has been translated in ${(0, __intlayer_chokidar.formatLocale)(locale)}:\n///chunkStart///` + (0, __intlayer_chokidar.getChunk)(fileResultContent, chunks[i - 1]) + `///chunkEnd///`;
|
|
39
|
+
const getBaseChunkContextPrompt = () => `**CHUNK ${i + 1} to ${Math.min(i + 3, chunks.length)} of ${chunks.length}** is the base chunk in ${(0, __intlayer_chokidar.formatLocale)(baseLocale, false)} as reference.\n///chunksStart///` + (chunks[i - 1]?.content ?? "") + chunks[i].content + (chunks[i + 1]?.content ?? "") + `///chunksEnd///`;
|
|
40
|
+
const fileToTranslateCurrentChunk = chunk.content;
|
|
41
|
+
const chunkTranslation = await (0, __intlayer_config.retryManager)(async () => {
|
|
42
|
+
const result = await require_utils_chunkInference.chunkInference([
|
|
43
|
+
{
|
|
44
|
+
role: "system",
|
|
45
|
+
content: basePrompt
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
role: "system",
|
|
49
|
+
content: getBaseChunkContextPrompt()
|
|
50
|
+
},
|
|
51
|
+
...isFirstChunk ? [] : [{
|
|
52
|
+
role: "system",
|
|
53
|
+
content: getPrevChunkPrompt()
|
|
54
|
+
}],
|
|
55
|
+
{
|
|
56
|
+
role: "system",
|
|
57
|
+
content: `The next user message will be the **CHUNK ${(0, __intlayer_config.colorizeNumber)(i + 1)} of ${(0, __intlayer_config.colorizeNumber)(chunks.length)}** in ${(0, __intlayer_chokidar.formatLocale)(baseLocale, false)} to translate in ${(0, __intlayer_chokidar.formatLocale)(locale, false)}:`
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
role: "user",
|
|
61
|
+
content: fileToTranslateCurrentChunk
|
|
62
|
+
}
|
|
63
|
+
], aiOptions, configuration);
|
|
64
|
+
appLogger([
|
|
65
|
+
`${prefix}`,
|
|
66
|
+
`${__intlayer_config.ANSIColors.GREY_DARK}[Chunk `,
|
|
67
|
+
(0, __intlayer_config.colorizeNumber)(i + 1),
|
|
68
|
+
`${__intlayer_config.ANSIColors.GREY_DARK} of `,
|
|
69
|
+
(0, __intlayer_config.colorizeNumber)(chunks.length),
|
|
70
|
+
`${__intlayer_config.ANSIColors.GREY_DARK}] →${__intlayer_config.ANSIColors.RESET} `,
|
|
71
|
+
`${(0, __intlayer_config.colorizeNumber)(result.tokenUsed)} tokens used`
|
|
72
|
+
].join(""));
|
|
73
|
+
return require_utils_fixChunkStartEndChars.fixChunkStartEndChars(result?.fileContent, fileToTranslateCurrentChunk);
|
|
74
|
+
})();
|
|
75
|
+
fileResultContent = fileResultContent.replace(fileToTranslateCurrentChunk, chunkTranslation);
|
|
76
|
+
}
|
|
77
|
+
(0, node_fs.mkdirSync)((0, node_path.dirname)(outputFilePath), { recursive: true });
|
|
78
|
+
(0, node_fs.writeFileSync)(outputFilePath, fileResultContent);
|
|
79
|
+
const relativePath = (0, node_path.relative)(configuration.content.baseDir, outputFilePath);
|
|
80
|
+
appLogger(`${(0, __intlayer_config.colorize)("✔", __intlayer_config.ANSIColors.GREEN)} File ${(0, __intlayer_chokidar.formatPath)(relativePath)} created/updated successfully.`);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
console.error(error);
|
|
83
|
+
}
|
|
11
84
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
85
|
+
/**
|
|
86
|
+
* Main translate function: scans all .md files in "en/" (unless you specified DOC_LIST),
|
|
87
|
+
* then translates them to each locale in LOCALE_LIST.
|
|
88
|
+
*/
|
|
89
|
+
const translateDoc = async ({ docPattern, locales, excludedGlobPattern, baseLocale, aiOptions, nbSimultaneousFileProcessed, configOptions, customInstructions, skipIfModifiedBefore, skipIfModifiedAfter, gitOptions }) => {
|
|
90
|
+
const configuration = (0, __intlayer_config.getConfiguration)(configOptions);
|
|
91
|
+
const appLogger = (0, __intlayer_config.getAppLogger)(configuration);
|
|
92
|
+
if (nbSimultaneousFileProcessed && nbSimultaneousFileProcessed > 10) {
|
|
93
|
+
appLogger(`Warning: nbSimultaneousFileProcessed is set to ${nbSimultaneousFileProcessed}, which is greater than 10. Setting it to 10.`);
|
|
94
|
+
nbSimultaneousFileProcessed = 10;
|
|
95
|
+
}
|
|
96
|
+
let docList = await (0, fast_glob.default)(docPattern, { ignore: excludedGlobPattern });
|
|
97
|
+
if (!await require_utils_checkAccess.checkAIAccess(configuration, aiOptions)) return;
|
|
98
|
+
if (gitOptions) {
|
|
99
|
+
const gitChangedFiles = await (0, __intlayer_chokidar.listGitFiles)(gitOptions);
|
|
100
|
+
if (gitChangedFiles) docList = docList.filter((path) => gitChangedFiles.some((gitFile) => (0, node_path.join)(process.cwd(), path) === gitFile));
|
|
101
|
+
}
|
|
102
|
+
appLogger(`Base locale is ${(0, __intlayer_chokidar.formatLocale)(baseLocale)}`);
|
|
103
|
+
appLogger(`Translating ${(0, __intlayer_config.colorizeNumber)(locales.length)} locales: [ ${(0, __intlayer_chokidar.formatLocale)(locales)} ]`);
|
|
104
|
+
appLogger(`Translating ${(0, __intlayer_config.colorizeNumber)(docList.length)} files:`);
|
|
105
|
+
appLogger(docList.map((path) => ` - ${(0, __intlayer_chokidar.formatPath)(path)}\n`));
|
|
106
|
+
await (0, __intlayer_chokidar.parallelize)(docList.flatMap((docPath) => locales.map((locale) => async () => {
|
|
107
|
+
appLogger(`Translating file: ${(0, __intlayer_chokidar.formatPath)(docPath)} to ${(0, __intlayer_chokidar.formatLocale)(locale)}`);
|
|
108
|
+
const absoluteBaseFilePath = (0, node_path.join)(configuration.content.baseDir, docPath);
|
|
109
|
+
const outputFilePath = require_utils_getOutputFilePath.getOutputFilePath(absoluteBaseFilePath, locale, baseLocale);
|
|
110
|
+
if (!(0, node_fs.existsSync)(outputFilePath)) {
|
|
111
|
+
appLogger(`File ${outputFilePath} does not exist, creating it...`);
|
|
112
|
+
(0, node_fs.mkdirSync)((0, node_path.dirname)(outputFilePath), { recursive: true });
|
|
113
|
+
(0, node_fs.writeFileSync)(outputFilePath, "");
|
|
114
|
+
}
|
|
115
|
+
const fileModificationData = require_utils_checkFileModifiedRange.checkFileModifiedRange(outputFilePath, {
|
|
116
|
+
skipIfModifiedBefore,
|
|
117
|
+
skipIfModifiedAfter
|
|
118
|
+
});
|
|
119
|
+
if (fileModificationData.isSkipped) {
|
|
120
|
+
appLogger(fileModificationData.message);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
await translateFile(absoluteBaseFilePath, outputFilePath, locale, baseLocale, configuration, aiOptions, customInstructions);
|
|
124
|
+
})), (task) => task(), nbSimultaneousFileProcessed ?? 3);
|
|
19
125
|
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
var translateDoc_exports = {};
|
|
30
|
-
__export(translateDoc_exports, {
|
|
31
|
-
translateDoc: () => translateDoc,
|
|
32
|
-
translateFile: () => translateFile
|
|
33
|
-
});
|
|
34
|
-
module.exports = __toCommonJS(translateDoc_exports);
|
|
35
|
-
var import_chokidar = require("@intlayer/chokidar");
|
|
36
|
-
var import_config = require("@intlayer/config");
|
|
37
|
-
var import_fast_glob = __toESM(require("fast-glob"));
|
|
38
|
-
var import_fs = require("fs");
|
|
39
|
-
var import_promises = require("fs/promises");
|
|
40
|
-
var import_path = require("path");
|
|
41
|
-
var import_url = require("url");
|
|
42
|
-
var import_calculateChunks = require('./utils/calculateChunks.cjs');
|
|
43
|
-
var import_checkAccess = require('./utils/checkAccess.cjs');
|
|
44
|
-
var import_checkFileModifiedRange = require('./utils/checkFileModifiedRange.cjs');
|
|
45
|
-
var import_chunkInference = require('./utils/chunkInference.cjs');
|
|
46
|
-
var import_fixChunkStartEndChars = require('./utils/fixChunkStartEndChars.cjs');
|
|
47
|
-
var import_getChunk = require('./utils/getChunk.cjs');
|
|
48
|
-
var import_getOutputFilePath = require('./utils/getOutputFilePath.cjs');
|
|
49
|
-
const import_meta = {};
|
|
50
|
-
const isESModule = typeof import_meta.url === "string";
|
|
51
|
-
const dir = isESModule ? (0, import_path.dirname)((0, import_url.fileURLToPath)(import_meta.url)) : __dirname;
|
|
52
|
-
const translateFile = async (baseFilePath, outputFilePath, locale, baseLocale, aiOptions, configuration = (0, import_config.getConfiguration)(), customInstructions) => {
|
|
53
|
-
try {
|
|
54
|
-
const appLogger = (0, import_config.getAppLogger)(configuration, {
|
|
55
|
-
config: {
|
|
56
|
-
prefix: ""
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
const fileContent = await (0, import_promises.readFile)(baseFilePath, "utf-8");
|
|
60
|
-
let fileResultContent = fileContent;
|
|
61
|
-
const basePrompt = (await (0, import_promises.readFile)((0, import_path.join)(dir, "./prompts/TRANSLATE_PROMPT.md"), "utf-8")).replaceAll("{{localeName}}", `${(0, import_chokidar.formatLocale)(locale, false)}`).replaceAll("{{baseLocaleName}}", `${(0, import_chokidar.formatLocale)(baseLocale, false)}`).replace("{{applicationContext}}", aiOptions?.applicationContext ?? "-").replace("{{customInstructions}}", customInstructions ?? "-");
|
|
62
|
-
const filePrexixText = `${import_config.ANSIColors.GREY_DARK}[${(0, import_chokidar.formatPath)(baseFilePath)}${import_config.ANSIColors.GREY_DARK}] `;
|
|
63
|
-
const filePrefix = [
|
|
64
|
-
(0, import_config.colon)(filePrexixText, { colSize: 40 }),
|
|
65
|
-
`\u2192 ${import_config.ANSIColors.RESET}`
|
|
66
|
-
].join("");
|
|
67
|
-
const prefixText = `${import_config.ANSIColors.GREY_DARK}[${(0, import_chokidar.formatPath)(baseFilePath)}${import_config.ANSIColors.GREY_DARK}][${(0, import_chokidar.formatLocale)(locale)}${import_config.ANSIColors.GREY_DARK}] `;
|
|
68
|
-
const prefix = [
|
|
69
|
-
(0, import_config.colon)(prefixText, { colSize: 40 }),
|
|
70
|
-
`\u2192 ${import_config.ANSIColors.RESET}`
|
|
71
|
-
].join("");
|
|
72
|
-
const chunks = (0, import_calculateChunks.chunkText)(fileContent);
|
|
73
|
-
appLogger(
|
|
74
|
-
`${filePrefix}Base file splitted into ${(0, import_config.colorizeNumber)(chunks.length)} chunks`
|
|
75
|
-
);
|
|
76
|
-
for await (const [i, chunk] of chunks.entries()) {
|
|
77
|
-
const isFirstChunk = i === 0;
|
|
78
|
-
const getPrevChunkPrompt = () => `**CHUNK ${i} of ${chunks.length}** that has been translated in ${(0, import_chokidar.formatLocale)(locale)}:
|
|
79
|
-
///chunkStart///` + (0, import_getChunk.getChunk)(fileResultContent, chunks[i - 1]) + `///chunkEnd///`;
|
|
80
|
-
const getBaseChunkContextPrompt = () => `**CHUNK ${i + 1} to ${Math.min(i + 3, chunks.length)} of ${chunks.length}** is the base chunk in ${(0, import_chokidar.formatLocale)(baseLocale, false)} as reference.
|
|
81
|
-
///chunksStart///` + (chunks[i - 1]?.content ?? "") + chunks[i].content + (chunks[i + 1]?.content ?? "") + `///chunksEnd///`;
|
|
82
|
-
const fileToTranslateCurrentChunk = chunk.content;
|
|
83
|
-
let chunkTranslation = await (0, import_config.retryManager)(async () => {
|
|
84
|
-
const result = await (0, import_chunkInference.chunkInference)(
|
|
85
|
-
[
|
|
86
|
-
{ role: "system", content: basePrompt },
|
|
87
|
-
{ role: "system", content: getBaseChunkContextPrompt() },
|
|
88
|
-
...isFirstChunk ? [] : [{ role: "system", content: getPrevChunkPrompt() }],
|
|
89
|
-
{
|
|
90
|
-
role: "system",
|
|
91
|
-
content: `The next user message will be the **CHUNK ${(0, import_config.colorizeNumber)(i + 1)} of ${(0, import_config.colorizeNumber)(chunks.length)}** in ${(0, import_chokidar.formatLocale)(baseLocale, false)} to translate in ${(0, import_chokidar.formatLocale)(locale, false)}:`
|
|
92
|
-
},
|
|
93
|
-
{ role: "user", content: fileToTranslateCurrentChunk }
|
|
94
|
-
],
|
|
95
|
-
aiOptions,
|
|
96
|
-
configuration
|
|
97
|
-
);
|
|
98
|
-
appLogger(
|
|
99
|
-
[
|
|
100
|
-
`${prefix}`,
|
|
101
|
-
`${import_config.ANSIColors.GREY_DARK}[Chunk `,
|
|
102
|
-
(0, import_config.colorizeNumber)(i + 1),
|
|
103
|
-
`${import_config.ANSIColors.GREY_DARK} of `,
|
|
104
|
-
(0, import_config.colorizeNumber)(chunks.length),
|
|
105
|
-
`${import_config.ANSIColors.GREY_DARK}] \u2192${import_config.ANSIColors.RESET} `,
|
|
106
|
-
`${(0, import_config.colorizeNumber)(result.tokenUsed)} tokens used`
|
|
107
|
-
].join("")
|
|
108
|
-
);
|
|
109
|
-
const fixedTranslatedChunkResult = (0, import_fixChunkStartEndChars.fixChunkStartEndChars)(
|
|
110
|
-
result?.fileContent,
|
|
111
|
-
fileToTranslateCurrentChunk
|
|
112
|
-
);
|
|
113
|
-
return fixedTranslatedChunkResult;
|
|
114
|
-
})();
|
|
115
|
-
fileResultContent = fileResultContent.replace(
|
|
116
|
-
fileToTranslateCurrentChunk,
|
|
117
|
-
chunkTranslation
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
|
-
(0, import_fs.mkdirSync)((0, import_path.dirname)(outputFilePath), { recursive: true });
|
|
121
|
-
(0, import_fs.writeFileSync)(outputFilePath, fileResultContent);
|
|
122
|
-
const relativePath = (0, import_path.relative)(
|
|
123
|
-
configuration.content.baseDir,
|
|
124
|
-
outputFilePath
|
|
125
|
-
);
|
|
126
|
-
appLogger(
|
|
127
|
-
`${(0, import_config.colorize)("\u2714", import_config.ANSIColors.GREEN)} File ${(0, import_chokidar.formatPath)(relativePath)} created/updated successfully.`
|
|
128
|
-
);
|
|
129
|
-
} catch (error) {
|
|
130
|
-
console.error(error);
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
const translateDoc = async ({
|
|
134
|
-
docPattern,
|
|
135
|
-
locales,
|
|
136
|
-
excludedGlobPattern,
|
|
137
|
-
baseLocale,
|
|
138
|
-
aiOptions,
|
|
139
|
-
nbSimultaneousFileProcessed,
|
|
140
|
-
configOptions,
|
|
141
|
-
customInstructions,
|
|
142
|
-
skipIfModifiedBefore,
|
|
143
|
-
skipIfModifiedAfter,
|
|
144
|
-
gitOptions
|
|
145
|
-
}) => {
|
|
146
|
-
const configuration = (0, import_config.getConfiguration)(configOptions);
|
|
147
|
-
const appLogger = (0, import_config.getAppLogger)(configuration, {
|
|
148
|
-
config: {
|
|
149
|
-
prefix: ""
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
if (nbSimultaneousFileProcessed && nbSimultaneousFileProcessed > 10) {
|
|
153
|
-
appLogger(
|
|
154
|
-
`Warning: nbSimultaneousFileProcessed is set to ${nbSimultaneousFileProcessed}, which is greater than 10. Setting it to 10.`
|
|
155
|
-
);
|
|
156
|
-
nbSimultaneousFileProcessed = 10;
|
|
157
|
-
}
|
|
158
|
-
let docList = import_fast_glob.default.sync(docPattern, {
|
|
159
|
-
ignore: excludedGlobPattern
|
|
160
|
-
});
|
|
161
|
-
const hasCMSAuth = await (0, import_checkAccess.checkAIAccess)(configuration, aiOptions);
|
|
162
|
-
if (!hasCMSAuth) return;
|
|
163
|
-
if (gitOptions) {
|
|
164
|
-
const gitChangedFiles = await (0, import_chokidar.listGitFiles)(gitOptions);
|
|
165
|
-
if (gitChangedFiles) {
|
|
166
|
-
docList = docList.filter(
|
|
167
|
-
(path) => gitChangedFiles.some((gitFile) => (0, import_path.join)(process.cwd(), path) === gitFile)
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
appLogger(`Base locale is ${(0, import_chokidar.formatLocale)(baseLocale)}`);
|
|
172
|
-
appLogger(
|
|
173
|
-
`Translating ${(0, import_config.colorizeNumber)(locales.length)} locales: [ ${(0, import_chokidar.formatLocale)(locales)} ]`
|
|
174
|
-
);
|
|
175
|
-
appLogger(`Translating ${(0, import_config.colorizeNumber)(docList.length)} files:`);
|
|
176
|
-
appLogger(docList.map((path) => ` - ${(0, import_chokidar.formatPath)(path)}
|
|
177
|
-
`));
|
|
178
|
-
const allTasks = docList.flatMap(
|
|
179
|
-
(docPath) => locales.map((locale) => async () => {
|
|
180
|
-
appLogger(
|
|
181
|
-
`Translating file: ${(0, import_chokidar.formatPath)(docPath)} to ${(0, import_chokidar.formatLocale)(locale)}`
|
|
182
|
-
);
|
|
183
|
-
const absoluteBaseFilePath = (0, import_path.join)(configuration.content.baseDir, docPath);
|
|
184
|
-
const outputFilePath = (0, import_getOutputFilePath.getOutputFilePath)(
|
|
185
|
-
absoluteBaseFilePath,
|
|
186
|
-
locale,
|
|
187
|
-
baseLocale
|
|
188
|
-
);
|
|
189
|
-
if (!(0, import_fs.existsSync)(outputFilePath)) {
|
|
190
|
-
appLogger(`File ${outputFilePath} does not exist, creating it...`);
|
|
191
|
-
(0, import_fs.mkdirSync)((0, import_path.dirname)(outputFilePath), { recursive: true });
|
|
192
|
-
(0, import_fs.writeFileSync)(outputFilePath, "");
|
|
193
|
-
}
|
|
194
|
-
const fileModificationData = (0, import_checkFileModifiedRange.checkFileModifiedRange)(outputFilePath, {
|
|
195
|
-
skipIfModifiedBefore,
|
|
196
|
-
skipIfModifiedAfter
|
|
197
|
-
});
|
|
198
|
-
if (fileModificationData.isSkipped) {
|
|
199
|
-
appLogger(fileModificationData.message);
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
await translateFile(
|
|
203
|
-
absoluteBaseFilePath,
|
|
204
|
-
outputFilePath,
|
|
205
|
-
locale,
|
|
206
|
-
baseLocale,
|
|
207
|
-
aiOptions,
|
|
208
|
-
configuration,
|
|
209
|
-
customInstructions
|
|
210
|
-
);
|
|
211
|
-
})
|
|
212
|
-
);
|
|
213
|
-
await (0, import_chokidar.parallelize)(
|
|
214
|
-
allTasks,
|
|
215
|
-
(task) => task(),
|
|
216
|
-
nbSimultaneousFileProcessed ?? 3
|
|
217
|
-
);
|
|
218
|
-
};
|
|
219
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
220
|
-
0 && (module.exports = {
|
|
221
|
-
translateDoc,
|
|
222
|
-
translateFile
|
|
223
|
-
});
|
|
126
|
+
|
|
127
|
+
//#endregion
|
|
128
|
+
exports.translateDoc = translateDoc;
|
|
129
|
+
exports.translateFile = translateFile;
|
|
224
130
|
//# sourceMappingURL=translateDoc.cjs.map
|
|
@@ -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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,sBAMO;AACP,oBAWO;AACP,uBAAe;AACf,gBAAqD;AACrD,sBAAyB;AACzB,kBAAwC;AACxC,iBAA8B;AAC9B,6BAA0B;AAC1B,yBAA8B;AAC9B,oCAAuC;AACvC,4BAA+B;AAC/B,mCAAsC;AACtC,sBAAyB;AACzB,+BAAkC;AA/BlC;AAiCA,MAAM,aAAa,OAAO,YAAY,QAAQ;AAE9C,MAAM,MAAM,iBAAa,yBAAQ,0BAAc,YAAY,GAAG,CAAC,IAAI;AAK5D,MAAM,gBAAgB,OAC3B,cACA,gBACA,QACA,YACA,WACA,oBAAgC,gCAAiB,GACjD,uBACG;AACH,MAAI;AACF,UAAM,gBAAY,4BAAa,eAAe;AAAA,MAC5C,QAAQ;AAAA,QACN,QAAQ;AAAA,MACV;AAAA,IACF,CAAC;AAGD,UAAM,cAAc,UAAM,0BAAS,cAAc,OAAO;AACxD,QAAI,oBAAoB;AAGxB,UAAM,cACJ,UAAM,8BAAS,kBAAK,KAAK,+BAA+B,GAAG,OAAO,GAEjE,WAAW,kBAAkB,OAAG,8BAAa,QAAQ,KAAK,CAAC,EAAE,EAC7D,WAAW,sBAAsB,OAAG,8BAAa,YAAY,KAAK,CAAC,EAAE,EACrE,QAAQ,0BAA0B,WAAW,sBAAsB,GAAG,EACtE,QAAQ,0BAA0B,sBAAsB,GAAG;AAE9D,UAAM,iBAAiB,GAAG,yBAAW,SAAS,QAAI,4BAAW,YAAY,CAAC,GAAG,yBAAW,SAAS;AACjG,UAAM,aAAa;AAAA,UACjB,qBAAM,gBAAgB,EAAE,SAAS,GAAG,CAAC;AAAA,MACrC,UAA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c,gBAAgB,EAAE;AAAA,MAClC;AAEA,YAAM,2BAAuB,sDAAuB,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,YAAM;AAAA,IACJ;AAAA,IACA,CAAC,SAAS,KAAK;AAAA,IACf,+BAA+B;AAAA,EACjC;AACF;","names":["fg"]}
|
|
1
|
+
{"version":3,"file":"translateDoc.cjs","names":["readAsset","ANSIColors","chunkText","chunkInference","fixChunkStartEndChars","docList: string[]","checkAIAccess","getOutputFilePath","checkFileModifiedRange"],"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,gDAAyB,eAAe,EAC5C,QAAQ,EACN,QAAQ,IACT,EACF,CAAC;EAGF,MAAM,cAAc,qCAAe,cAAc,QAAQ;EAEzD,IAAI,oBAAoB;EAGxB,MAAM,aAAaA,+BAAU,iCAAiC,QAAQ,CACnE,WAAW,kBAAkB,yCAAgB,QAAQ,MAAM,GAAG,CAC9D,WAAW,sBAAsB,yCAAgB,YAAY,MAAM,GAAG,CACtE,QAAQ,0BAA0B,WAAW,sBAAsB,IAAI,CACvE,QAAQ,0BAA0B,sBAAsB,IAAI;EAG/D,MAAM,aAAa,8BADI,GAAGC,6BAAW,UAAU,uCAAc,aAAa,GAAGA,6BAAW,UAAU,KAE1E,EAAE,SAAS,IAAI,CAAC,EACtC,KAAKA,6BAAW,QACjB,CAAC,KAAK,GAAG;EAGV,MAAM,SAAS,8BADI,GAAGA,6BAAW,UAAU,uCAAc,aAAa,GAAGA,6BAAW,UAAU,0CAAiB,OAAO,GAAGA,6BAAW,UAAU,KAE1H,EAAE,SAAS,IAAI,CAAC,EAClC,KAAKA,6BAAW,QACjB,CAAC,KAAK,GAAG;EA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U,QAAQ,eAAe,iCAAiC;AAClE,iDAAkB,eAAe,EAAE,EAAE,WAAW,MAAM,CAAC;AACvD,8BAAc,gBAAgB,GAAG;;EAGnC,MAAM,uBAAuBC,4DAAuB,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,120 +1,90 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var calculateChunks_exports = {};
|
|
20
|
-
__export(calculateChunks_exports, {
|
|
21
|
-
chunkText: () => chunkText
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(calculateChunks_exports);
|
|
24
|
-
var import_splitTextByLine = require('./splitTextByLine.cjs');
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let __intlayer_chokidar = require("@intlayer/chokidar");
|
|
3
|
+
__intlayer_chokidar = require_rolldown_runtime.__toESM(__intlayer_chokidar);
|
|
4
|
+
|
|
5
|
+
//#region src/utils/calculateChunks.ts
|
|
25
6
|
const DEFAULT_MAX_CHARS_PER_CHUNK = 800;
|
|
26
7
|
const DEFAULT_OVERLAP_CHARS = 0;
|
|
27
8
|
const chunkText = (text, maxCharsPerChunk = DEFAULT_MAX_CHARS_PER_CHUNK, overlapChars = DEFAULT_OVERLAP_CHARS) => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const overlappedChunk = {
|
|
106
|
-
content: newContent,
|
|
107
|
-
lineStart: chunk.lineStart + lineDiff,
|
|
108
|
-
lineLength: chunk.lineLength - lineDiff,
|
|
109
|
-
charStart: chunk.charStart - overlapContentWithoutPartialLine.length,
|
|
110
|
-
charLength: newContent.length
|
|
111
|
-
};
|
|
112
|
-
overlapChunks.push(overlappedChunk);
|
|
113
|
-
}
|
|
114
|
-
return overlapChunks;
|
|
9
|
+
if (maxCharsPerChunk <= 0) throw new Error("maxCharsPerChunk must be greater than 0");
|
|
10
|
+
const splittedText = (0, __intlayer_chokidar.splitTextByLines)(text);
|
|
11
|
+
const lines = [];
|
|
12
|
+
let charStartAcc = 0;
|
|
13
|
+
splittedText.forEach((line, index) => {
|
|
14
|
+
lines.push({
|
|
15
|
+
content: line,
|
|
16
|
+
lineStart: index,
|
|
17
|
+
lineLength: 1,
|
|
18
|
+
charStart: charStartAcc,
|
|
19
|
+
charLength: line.length
|
|
20
|
+
});
|
|
21
|
+
charStartAcc += line.length;
|
|
22
|
+
});
|
|
23
|
+
const splittedLines = lines.reduce((acc, line) => {
|
|
24
|
+
if (line.content.length > maxCharsPerChunk) {
|
|
25
|
+
acc.push(line);
|
|
26
|
+
return acc;
|
|
27
|
+
}
|
|
28
|
+
if (acc.length === 0) {
|
|
29
|
+
acc.push(line);
|
|
30
|
+
return acc;
|
|
31
|
+
}
|
|
32
|
+
const lastChunk = acc[acc.length - 1];
|
|
33
|
+
if (lastChunk.content.length + line.content.length > 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
|
+
}, []).flatMap((line) => {
|
|
48
|
+
const chunk = [];
|
|
49
|
+
if (line.content.length <= maxCharsPerChunk) {
|
|
50
|
+
chunk.push(line);
|
|
51
|
+
return chunk;
|
|
52
|
+
}
|
|
53
|
+
for (let i = 0; i < line.content.length; i += maxCharsPerChunk) {
|
|
54
|
+
const slicedContent = line.content.slice(i, i + maxCharsPerChunk);
|
|
55
|
+
chunk.push({
|
|
56
|
+
content: slicedContent,
|
|
57
|
+
lineStart: line.lineStart,
|
|
58
|
+
lineLength: 1,
|
|
59
|
+
charStart: line.charStart + i,
|
|
60
|
+
charLength: slicedContent.length
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return chunk;
|
|
64
|
+
});
|
|
65
|
+
if (overlapChars === 0) return splittedLines;
|
|
66
|
+
const overlapChunks = splittedLines.length > 0 ? [splittedLines[0]] : [];
|
|
67
|
+
for (let i = 1; i < splittedLines.length; i++) {
|
|
68
|
+
const previousChunk = splittedLines[i - 1];
|
|
69
|
+
const chunk = splittedLines[i];
|
|
70
|
+
const overlapContent = previousChunk.content.slice(-overlapChars);
|
|
71
|
+
const overlapLineNb = (0, __intlayer_chokidar.splitTextByLines)(overlapContent).length;
|
|
72
|
+
const overlapContentWithoutPartialLine = overlapContent.slice(overlapLineNb > 1 ? overlapContent.indexOf("\n") + 1 : 0, overlapContent.length);
|
|
73
|
+
const newContent = overlapContentWithoutPartialLine + chunk.content;
|
|
74
|
+
const newLineLength = (0, __intlayer_chokidar.splitTextByLines)(newContent).length;
|
|
75
|
+
const lineDiff = chunk.lineLength - newLineLength;
|
|
76
|
+
const overlappedChunk = {
|
|
77
|
+
content: newContent,
|
|
78
|
+
lineStart: chunk.lineStart + lineDiff,
|
|
79
|
+
lineLength: chunk.lineLength - lineDiff,
|
|
80
|
+
charStart: chunk.charStart - overlapContentWithoutPartialLine.length,
|
|
81
|
+
charLength: newContent.length
|
|
82
|
+
};
|
|
83
|
+
overlapChunks.push(overlappedChunk);
|
|
84
|
+
}
|
|
85
|
+
return overlapChunks;
|
|
115
86
|
};
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
});
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
exports.chunkText = chunkText;
|
|
120
90
|
//# sourceMappingURL=calculateChunks.cjs.map
|