@intlayer/cli 5.8.1 → 6.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.
Files changed (114) hide show
  1. package/dist/cjs/IntlayerEventListener.cjs +240 -0
  2. package/dist/cjs/IntlayerEventListener.cjs.map +1 -0
  3. package/dist/cjs/cli.cjs +29 -7
  4. package/dist/cjs/cli.cjs.map +1 -1
  5. package/dist/cjs/config.cjs +5 -1
  6. package/dist/cjs/config.cjs.map +1 -1
  7. package/dist/cjs/fill/autoFill.cjs +105 -0
  8. package/dist/cjs/fill/autoFill.cjs.map +1 -0
  9. package/dist/cjs/fill/formatAutoFillData.cjs +108 -0
  10. package/dist/cjs/fill/formatAutoFillData.cjs.map +1 -0
  11. package/dist/cjs/fill/formatAutoFilledFilePath.cjs +46 -0
  12. package/dist/cjs/fill/formatAutoFilledFilePath.cjs.map +1 -0
  13. package/dist/cjs/fill/getTargetDictionary.cjs +86 -0
  14. package/dist/cjs/fill/getTargetDictionary.cjs.map +1 -0
  15. package/dist/cjs/fill/index.cjs +257 -0
  16. package/dist/cjs/fill/index.cjs.map +1 -0
  17. package/dist/cjs/index.cjs +4 -2
  18. package/dist/cjs/index.cjs.map +1 -1
  19. package/dist/cjs/listContentDeclaration.cjs +37 -19
  20. package/dist/cjs/listContentDeclaration.cjs.map +1 -1
  21. package/dist/cjs/liveSync.cjs +254 -0
  22. package/dist/cjs/liveSync.cjs.map +1 -0
  23. package/dist/cjs/pull.cjs +18 -22
  24. package/dist/cjs/pull.cjs.map +1 -1
  25. package/dist/cjs/push.cjs +28 -27
  26. package/dist/cjs/push.cjs.map +1 -1
  27. package/dist/cjs/pushConfig.cjs +8 -15
  28. package/dist/cjs/pushConfig.cjs.map +1 -1
  29. package/dist/cjs/reviewDoc.cjs +43 -26
  30. package/dist/cjs/reviewDoc.cjs.map +1 -1
  31. package/dist/cjs/test/index.cjs +91 -0
  32. package/dist/cjs/test/index.cjs.map +1 -0
  33. package/dist/cjs/test/listMissingTranslations.cjs +73 -0
  34. package/dist/cjs/test/listMissingTranslations.cjs.map +1 -0
  35. package/dist/cjs/translateDoc.cjs +40 -24
  36. package/dist/cjs/translateDoc.cjs.map +1 -1
  37. package/dist/cjs/utils/checkAIAccess.cjs +5 -1
  38. package/dist/cjs/utils/checkAIAccess.cjs.map +1 -1
  39. package/dist/esm/IntlayerEventListener.mjs +206 -0
  40. package/dist/esm/IntlayerEventListener.mjs.map +1 -0
  41. package/dist/esm/cli.mjs +26 -4
  42. package/dist/esm/cli.mjs.map +1 -1
  43. package/dist/esm/config.mjs +5 -1
  44. package/dist/esm/config.mjs.map +1 -1
  45. package/dist/esm/fill/autoFill.mjs +92 -0
  46. package/dist/esm/fill/autoFill.mjs.map +1 -0
  47. package/dist/esm/fill/formatAutoFillData.mjs +84 -0
  48. package/dist/esm/fill/formatAutoFillData.mjs.map +1 -0
  49. package/dist/esm/fill/formatAutoFilledFilePath.mjs +22 -0
  50. package/dist/esm/fill/formatAutoFilledFilePath.mjs.map +1 -0
  51. package/dist/esm/fill/getTargetDictionary.mjs +51 -0
  52. package/dist/esm/fill/getTargetDictionary.mjs.map +1 -0
  53. package/dist/esm/fill/index.mjs +240 -0
  54. package/dist/esm/fill/index.mjs.map +1 -0
  55. package/dist/esm/index.mjs +2 -1
  56. package/dist/esm/index.mjs.map +1 -1
  57. package/dist/esm/listContentDeclaration.mjs +38 -17
  58. package/dist/esm/listContentDeclaration.mjs.map +1 -1
  59. package/dist/esm/liveSync.mjs +220 -0
  60. package/dist/esm/liveSync.mjs.map +1 -0
  61. package/dist/esm/pull.mjs +19 -21
  62. package/dist/esm/pull.mjs.map +1 -1
  63. package/dist/esm/push.mjs +33 -27
  64. package/dist/esm/push.mjs.map +1 -1
  65. package/dist/esm/pushConfig.mjs +8 -15
  66. package/dist/esm/pushConfig.mjs.map +1 -1
  67. package/dist/esm/reviewDoc.mjs +53 -28
  68. package/dist/esm/reviewDoc.mjs.map +1 -1
  69. package/dist/esm/test/index.mjs +74 -0
  70. package/dist/esm/test/index.mjs.map +1 -0
  71. package/dist/esm/test/listMissingTranslations.mjs +41 -0
  72. package/dist/esm/test/listMissingTranslations.mjs.map +1 -0
  73. package/dist/esm/translateDoc.mjs +50 -27
  74. package/dist/esm/translateDoc.mjs.map +1 -1
  75. package/dist/esm/utils/checkAIAccess.mjs +5 -1
  76. package/dist/esm/utils/checkAIAccess.mjs.map +1 -1
  77. package/dist/types/IntlayerEventListener.d.ts +85 -0
  78. package/dist/types/IntlayerEventListener.d.ts.map +1 -0
  79. package/dist/types/cli.d.ts.map +1 -1
  80. package/dist/types/config.d.ts.map +1 -1
  81. package/dist/types/fill/autoFill.d.ts +4 -0
  82. package/dist/types/fill/autoFill.d.ts.map +1 -0
  83. package/dist/types/fill/formatAutoFillData.d.ts +9 -0
  84. package/dist/types/fill/formatAutoFillData.d.ts.map +1 -0
  85. package/dist/types/fill/formatAutoFilledFilePath.d.ts +3 -0
  86. package/dist/types/fill/formatAutoFilledFilePath.d.ts.map +1 -0
  87. package/dist/types/fill/getTargetDictionary.d.ts +4 -0
  88. package/dist/types/fill/getTargetDictionary.d.ts.map +1 -0
  89. package/dist/types/{fill.d.ts → fill/index.d.ts} +2 -5
  90. package/dist/types/fill/index.d.ts.map +1 -0
  91. package/dist/types/index.d.ts +1 -0
  92. package/dist/types/index.d.ts.map +1 -1
  93. package/dist/types/listContentDeclaration.d.ts +4 -5
  94. package/dist/types/listContentDeclaration.d.ts.map +1 -1
  95. package/dist/types/liveSync.d.ts +6 -0
  96. package/dist/types/liveSync.d.ts.map +1 -0
  97. package/dist/types/pull.d.ts.map +1 -1
  98. package/dist/types/push.d.ts +1 -1
  99. package/dist/types/push.d.ts.map +1 -1
  100. package/dist/types/pushConfig.d.ts +0 -1
  101. package/dist/types/pushConfig.d.ts.map +1 -1
  102. package/dist/types/reviewDoc.d.ts.map +1 -1
  103. package/dist/types/test/index.d.ts +8 -0
  104. package/dist/types/test/index.d.ts.map +1 -0
  105. package/dist/types/test/listMissingTranslations.d.ts +12 -0
  106. package/dist/types/test/listMissingTranslations.d.ts.map +1 -0
  107. package/dist/types/translateDoc.d.ts.map +1 -1
  108. package/dist/types/utils/checkAIAccess.d.ts.map +1 -1
  109. package/package.json +17 -15
  110. package/dist/cjs/fill.cjs +0 -405
  111. package/dist/cjs/fill.cjs.map +0 -1
  112. package/dist/esm/fill.mjs +0 -385
  113. package/dist/esm/fill.mjs.map +0 -1
  114. package/dist/types/fill.d.ts.map +0 -1
@@ -35,7 +35,6 @@ module.exports = __toCommonJS(translateDoc_exports);
35
35
  var import_api = require("@intlayer/api");
36
36
  var import_chokidar = require("@intlayer/chokidar");
37
37
  var import_config = require("@intlayer/config");
38
- var import_core = require("@intlayer/core");
39
38
  var import_fast_glob = __toESM(require("fast-glob"));
40
39
  var import_fs = require("fs");
41
40
  var import_promises = require("fs/promises");
@@ -55,23 +54,33 @@ const dir = isESModule ? (0, import_path.dirname)((0, import_url.fileURLToPath)(
55
54
  const translateFile = async (baseFilePath, outputFilePath, locale, baseLocale, aiOptions, configOptions, oAuth2AccessToken, customInstructions) => {
56
55
  try {
57
56
  const configuration = (0, import_config.getConfiguration)(configOptions);
58
- const appLogger = (0, import_config.getAppLogger)(configuration);
57
+ const appLogger = (0, import_config.getAppLogger)(configuration, {
58
+ config: {
59
+ prefix: ""
60
+ }
61
+ });
59
62
  const fileContent = await (0, import_promises.readFile)(baseFilePath, "utf-8");
60
63
  let fileResultContent = fileContent;
61
- const basePrompt = (await (0, import_promises.readFile)((0, import_path.join)(dir, "./prompts/TRANSLATE_PROMPT.md"), "utf-8")).replaceAll(
62
- "{{localeName}}",
63
- `${(0, import_core.getLocaleName)(locale, import_config.Locales.ENGLISH)} (${locale})`
64
- ).replaceAll(
65
- "{{baseLocaleName}}",
66
- `${(0, import_core.getLocaleName)(baseLocale, import_config.Locales.ENGLISH)} (${baseLocale})`
67
- ).replace("{{applicationContext}}", aiOptions?.applicationContext ?? "-").replace("{{customInstructions}}", customInstructions ?? "-");
64
+ 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 ?? "-");
65
+ const filePrexixText = `${import_config.ANSIColors.GREY_DARK}[${(0, import_chokidar.formatPath)(baseFilePath)}${import_config.ANSIColors.GREY_DARK}] `;
66
+ const filePrefix = [
67
+ (0, import_config.colon)(filePrexixText, { colSize: 40 }),
68
+ `\u2192 ${import_config.ANSIColors.RESET}`
69
+ ].join("");
70
+ 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}] `;
71
+ const prefix = [
72
+ (0, import_config.colon)(prefixText, { colSize: 40 }),
73
+ `\u2192 ${import_config.ANSIColors.RESET}`
74
+ ].join("");
68
75
  const chunks = (0, import_calculateChunks.chunkText)(fileContent);
69
- appLogger(`Base file splitted into ${chunks.length} chunks`);
76
+ appLogger(
77
+ `${filePrefix}Base file splitted into ${(0, import_config.colorizeNumber)(chunks.length)} chunks`
78
+ );
70
79
  for await (const [i, chunk] of chunks.entries()) {
71
80
  const isFirstChunk = i === 0;
72
- const getPrevChunkPrompt = () => `**CHUNK ${i} of ${chunks.length}** that has been translated in ${(0, import_core.getLocaleName)(locale, import_config.Locales.ENGLISH)} (${locale}):
81
+ const getPrevChunkPrompt = () => `**CHUNK ${i} of ${chunks.length}** that has been translated in ${(0, import_chokidar.formatLocale)(locale)}:
73
82
  ///chunkStart///` + (0, import_getChunk.getChunk)(fileResultContent, chunks[i - 1]) + `///chunkEnd///`;
74
- const getBaseChunkContextPrompt = () => `**CHUNK ${i + 1} to ${Math.min(i + 3, chunks.length)} of ${chunks.length}** is the base chunk in ${(0, import_core.getLocaleName)(baseLocale, import_config.Locales.ENGLISH)} (${baseLocale}) as reference.
83
+ 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.
75
84
  ///chunksStart///` + (chunks[i - 1]?.content ?? "") + chunks[i].content + (chunks[i + 1]?.content ?? "") + `///chunksEnd///`;
76
85
  const fileToTranslateCurrentChunk = chunk.content;
77
86
  let chunkTranslation = await (0, import_config.retryManager)(async () => {
@@ -82,7 +91,7 @@ const translateFile = async (baseFilePath, outputFilePath, locale, baseLocale, a
82
91
  ...isFirstChunk ? [] : [{ role: "system", content: getPrevChunkPrompt() }],
83
92
  {
84
93
  role: "system",
85
- content: `The next user message will be the **CHUNK ${i + 1} of ${chunks.length}** in ${(0, import_core.getLocaleName)(baseLocale, import_config.Locales.ENGLISH)} (${baseLocale}) to translate in ${(0, import_core.getLocaleName)(locale, import_config.Locales.ENGLISH)} (${locale}):`
94
+ 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)}:`
86
95
  },
87
96
  { role: "user", content: fileToTranslateCurrentChunk }
88
97
  ],
@@ -90,7 +99,7 @@ const translateFile = async (baseFilePath, outputFilePath, locale, baseLocale, a
90
99
  oAuth2AccessToken
91
100
  );
92
101
  appLogger(
93
- ` -> ${result.tokenUsed} tokens used - CHUNK ${i + 1} of ${chunks.length}`
102
+ `${prefix}${(0, import_config.colorizeNumber)(result.tokenUsed)} tokens used - Chunk ${(0, import_config.colorizeNumber)(i + 1)} of ${(0, import_config.colorizeNumber)(chunks.length)}`
94
103
  );
95
104
  const fixedTranslatedChunkResult = (0, import_fixChunkStartEndChars.fixChunkStartEndChars)(
96
105
  result?.fileContent,
@@ -105,7 +114,13 @@ const translateFile = async (baseFilePath, outputFilePath, locale, baseLocale, a
105
114
  }
106
115
  (0, import_fs.mkdirSync)((0, import_path.dirname)(outputFilePath), { recursive: true });
107
116
  (0, import_fs.writeFileSync)(outputFilePath, fileResultContent);
108
- appLogger(`File ${outputFilePath} created/updated successfully.`);
117
+ const relativePath = (0, import_path.relative)(
118
+ configuration.content.baseDir,
119
+ outputFilePath
120
+ );
121
+ appLogger(
122
+ `${(0, import_config.colorize)("\u2714", import_config.ANSIColors.GREEN)} File ${(0, import_chokidar.formatPath)(relativePath)} created/updated successfully.`
123
+ );
109
124
  } catch (error) {
110
125
  console.error(error);
111
126
  }
@@ -124,7 +139,11 @@ const translateDoc = async ({
124
139
  gitOptions
125
140
  }) => {
126
141
  const configuration = (0, import_config.getConfiguration)(configOptions);
127
- const appLogger = (0, import_config.getAppLogger)(configuration);
142
+ const appLogger = (0, import_config.getAppLogger)(configuration, {
143
+ config: {
144
+ prefix: ""
145
+ }
146
+ });
128
147
  if (nbSimultaneousFileProcessed && nbSimultaneousFileProcessed > 10) {
129
148
  appLogger(
130
149
  `Warning: nbSimultaneousFileProcessed is set to ${nbSimultaneousFileProcessed}, which is greater than 10. Setting it to 10.`
@@ -150,21 +169,18 @@ const translateDoc = async ({
150
169
  const oAuth2TokenResult = await intlayerAuthAPI.getOAuth2AccessToken();
151
170
  oAuth2AccessToken = oAuth2TokenResult.data?.accessToken;
152
171
  }
153
- appLogger(`Base locale is ${(0, import_core.getLocaleName)(baseLocale)} (${baseLocale})`);
172
+ appLogger(`Base locale is ${(0, import_chokidar.formatLocale)(baseLocale)}`);
154
173
  appLogger(
155
- `Translating ${locales.length} locales: [ ${locales.map((locale) => `${(0, import_core.getLocaleName)(locale, baseLocale)} (${locale})`).join(", ")} ]`
174
+ `Translating ${(0, import_config.colorizeNumber)(locales.length)} locales: [ ${(0, import_chokidar.formatLocale)(locales)} ]`
156
175
  );
157
- appLogger(`Translating ${docList.length} files:`);
158
- appLogger(docList.map((path) => ` - ${path}
176
+ appLogger(`Translating ${(0, import_config.colorizeNumber)(docList.length)} files:`);
177
+ appLogger(docList.map((path) => ` - ${(0, import_chokidar.formatPath)(path)}
159
178
  `));
160
179
  const tasks = docList.map(
161
180
  (docPath) => locales.flatMap(
162
181
  (locale) => limit(async () => {
163
182
  appLogger(
164
- `Translating file: ${docPath} to ${(0, import_core.getLocaleName)(
165
- locale,
166
- import_config.Locales.ENGLISH
167
- )} (${locale})`
183
+ `Translating file: ${(0, import_chokidar.formatPath)(docPath)} to ${(0, import_chokidar.formatLocale)(locale)}`
168
184
  );
169
185
  const absoluteBaseFilePath = (0, import_path.join)(
170
186
  configuration.content.baseDir,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/translateDoc.ts"],"sourcesContent":["import { AIOptions, getOAuthAPI } from '@intlayer/api';\nimport { listGitFiles, ListGitFilesOptions } from '@intlayer/chokidar';\nimport {\n getAppLogger,\n getConfiguration,\n GetConfigurationOptions,\n Locales,\n retryManager,\n} from '@intlayer/config';\nimport { getLocaleName } from '@intlayer/core';\nimport fg from 'fast-glob';\nimport { existsSync, mkdirSync, writeFileSync } from 'fs';\nimport { readFile } from 'fs/promises';\nimport pLimit from 'p-limit';\nimport { dirname, join } from 'path';\nimport { fileURLToPath } from 'url';\nimport { chunkText } from './utils/calculateChunks';\nimport { checkAIAccess } from './utils/checkAIAccess';\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 configOptions?: GetConfigurationOptions,\n oAuth2AccessToken?: string,\n customInstructions?: string\n) => {\n try {\n const configuration = getConfiguration(configOptions);\n const appLogger = getAppLogger(configuration);\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(\n '{{localeName}}',\n `${getLocaleName(locale, Locales.ENGLISH)} (${locale})`\n )\n .replaceAll(\n '{{baseLocaleName}}',\n `${getLocaleName(baseLocale, Locales.ENGLISH)} (${baseLocale})`\n )\n .replace('{{applicationContext}}', aiOptions?.applicationContext ?? '-')\n .replace('{{customInstructions}}', customInstructions ?? '-');\n\n // 1. Chunk the file by number of lines instead of characters\n const chunks = chunkText(fileContent);\n appLogger(`Base file splitted into ${chunks.length} chunks`);\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 ${getLocaleName(locale, Locales.ENGLISH)} (${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 ${getLocaleName(baseLocale, Locales.ENGLISH)} (${baseLocale}) 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 ${i + 1} of ${chunks.length}** in ${getLocaleName(baseLocale, Locales.ENGLISH)} (${baseLocale}) to translate in ${getLocaleName(locale, Locales.ENGLISH)} (${locale}):`,\n },\n { role: 'user', content: fileToTranslateCurrentChunk },\n ],\n aiOptions,\n oAuth2AccessToken\n );\n\n appLogger(\n ` -> ${result.tokenUsed} tokens used - CHUNK ${i + 1} of ${chunks.length}`\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 appLogger(`File ${outputFilePath} created/updated successfully.`);\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\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 const limit = pLimit(nbSimultaneousFileProcessed ?? 3);\n\n let docList: string[] = fg.sync(docPattern, {\n ignore: excludedGlobPattern,\n });\n\n checkAIAccess(configuration, aiOptions);\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 let oAuth2AccessToken: string | undefined;\n if (configuration.editor.clientId) {\n const intlayerAuthAPI = getOAuthAPI(configuration);\n const oAuth2TokenResult = await intlayerAuthAPI.getOAuth2AccessToken();\n\n oAuth2AccessToken = oAuth2TokenResult.data?.accessToken;\n }\n\n appLogger(`Base locale is ${getLocaleName(baseLocale)} (${baseLocale})`);\n appLogger(\n `Translating ${locales.length} locales: [ ${locales\n .map((locale) => `${getLocaleName(locale, baseLocale)} (${locale})`)\n .join(', ')} ]`\n );\n\n appLogger(`Translating ${docList.length} files:`);\n appLogger(docList.map((path) => ` - ${path}\\n`));\n\n const tasks = docList.map((docPath) =>\n locales.flatMap((locale) =>\n limit(async () => {\n appLogger(\n `Translating file: ${docPath} to ${getLocaleName(\n locale,\n Locales.ENGLISH\n )} (${locale})`\n );\n\n const absoluteBaseFilePath = join(\n configuration.content.baseDir,\n docPath\n );\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 configOptions,\n oAuth2AccessToken,\n customInstructions\n );\n })\n )\n );\n\n await Promise.all(tasks);\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAAuC;AACvC,sBAAkD;AAClD,oBAMO;AACP,kBAA8B;AAC9B,uBAAe;AACf,gBAAqD;AACrD,sBAAyB;AACzB,qBAAmB;AACnB,kBAA8B;AAC9B,iBAA8B;AAC9B,6BAA0B;AAC1B,2BAA8B;AAC9B,oCAAuC;AACvC,4BAA+B;AAC/B,mCAAsC;AACtC,sBAAyB;AACzB,+BAAkC;AAtBlC;AAwBA,MAAM,aAAa,OAAO,YAAY,QAAQ;AAE9C,MAAM,MAAM,iBAAa,yBAAQ,0BAAc,YAAY,GAAG,CAAC,IAAI;AAK5D,MAAM,gBAAgB,OAC3B,cACA,gBACA,QACA,YACA,WACA,eACA,mBACA,uBACG;AACH,MAAI;AACF,UAAM,oBAAgB,gCAAiB,aAAa;AACpD,UAAM,gBAAY,4BAAa,aAAa;AAG5C,UAAM,cAAc,UAAM,0BAAS,cAAc,OAAO;AACxD,QAAI,oBAAoB;AAGxB,UAAM,cACJ,UAAM,8BAAS,kBAAK,KAAK,+BAA+B,GAAG,OAAO,GAEjE;AAAA,MACC;AAAA,MACA,OAAG,2BAAc,QAAQ,sBAAQ,OAAO,CAAC,KAAK,MAAM;AAAA,IACtD,EACC;AAAA,MACC;AAAA,MACA,OAAG,2BAAc,YAAY,sBAAQ,OAAO,CAAC,KAAK,UAAU;AAAA,IAC9D,EACC,QAAQ,0BAA0B,WAAW,sBAAsB,GAAG,EACtE,QAAQ,0BAA0B,sBAAsB,GAAG;AAG9D,UAAM,aAAS,kCAAU,WAAW;AACpC,cAAU,2BA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B,2BAAc,UAAU,CAAC,KAAK,UAAU,GAAG;AACvE;AAAA,IACE,eAAe,QAAQ,MAAM,eAAe,QACzC,IAAI,CAAC,WAAW,OAAG,2BAAc,QAAQ,UAAU,CAAC,KAAK,MAAM,GAAG,EAClE,KAAK,IAAI,CAAC;AAAA,EACf;AAEA,YAAU,eAAe,QAAQ,MAAM,SAAS;AAChD,YAAU,QAAQ,IAAI,CAAC,SAAS,MAAM,IAAI;AAAA,CAAI,CAAC;AAE/C,QAAM,QAAQ,QAAQ;AAAA,IAAI,CAAC,YACzB,QAAQ;AAAA,MAAQ,CAAC,WACf,MAAM,YAAY;AAChB;AAAA,UACE,qBAAqB,OAAO,WAAO;AAAA,YACjC;AAAA,YACA,sBAAQ;AAAA,UACV,CAAC,KAAK,MAAM;AAAA,QACd;AAEA,cAAM,2BAAuB;AAAA,UAC3B,cAAc,QAAQ;AAAA,UACtB;AAAA,QACF;AACA,cAAM,qBAAiB;AAAA,UACrB;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAGA,YAAI,KAAC,sBAAW,cAAc,GAAG;AAC/B,oBAAU,QAAQ,cAAc,iCAAiC;AACjE,uCAAU,qBAAQ,cAAc,GAAG,EAAE,WAAW,KAAK,CAAC;AACtD,uCAAc,gBAAgB,EAAE;AAAA,QAClC;AAEA,cAAM,2BAAuB,sDAAuB,gBAAgB;AAAA,UAClE;AAAA,UACA;AAAA,QACF,CAAC;AAED,YAAI,qBAAqB,WAAW;AAClC,oBAAU,qBAAqB,OAAO;AACtC;AAAA,QACF;AAEA,cAAM;AAAA,UACJ;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAEA,QAAM,QAAQ,IAAI,KAAK;AACzB;","names":["pLimit","fg"]}
1
+ {"version":3,"sources":["../../src/translateDoc.ts"],"sourcesContent":["import { AIOptions, getOAuthAPI } from '@intlayer/api';\nimport {\n formatLocale,\n formatPath,\n listGitFiles,\n ListGitFilesOptions,\n} from '@intlayer/chokidar';\nimport {\n ANSIColors,\n colon,\n colorize,\n colorizeNumber,\n getAppLogger,\n getConfiguration,\n GetConfigurationOptions,\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 pLimit from 'p-limit';\nimport { dirname, join, relative } from 'path';\nimport { fileURLToPath } from 'url';\nimport { chunkText } from './utils/calculateChunks';\nimport { checkAIAccess } from './utils/checkAIAccess';\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 configOptions?: GetConfigurationOptions,\n oAuth2AccessToken?: string,\n customInstructions?: string\n) => {\n try {\n const configuration = getConfiguration(configOptions);\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 oAuth2AccessToken\n );\n\n appLogger(\n `${prefix}${colorizeNumber(result.tokenUsed)} tokens used - Chunk ${colorizeNumber(i + 1)} of ${colorizeNumber(chunks.length)}`\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 const limit = pLimit(nbSimultaneousFileProcessed ?? 3);\n\n let docList: string[] = fg.sync(docPattern, {\n ignore: excludedGlobPattern,\n });\n\n checkAIAccess(configuration, aiOptions);\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 let oAuth2AccessToken: string | undefined;\n if (configuration.editor.clientId) {\n const intlayerAuthAPI = getOAuthAPI(configuration);\n const oAuth2TokenResult = await intlayerAuthAPI.getOAuth2AccessToken();\n\n oAuth2AccessToken = oAuth2TokenResult.data?.accessToken;\n }\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 const tasks = docList.map((docPath) =>\n locales.flatMap((locale) =>\n limit(async () => {\n appLogger(\n `Translating file: ${formatPath(docPath)} to ${formatLocale(locale)}`\n );\n\n const absoluteBaseFilePath = join(\n configuration.content.baseDir,\n docPath\n );\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 configOptions,\n oAuth2AccessToken,\n customInstructions\n );\n })\n )\n );\n\n await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uB,gBAAgB;AAAA,UAClE;AAAA,UACA;AAAA,QACF,CAAC;AAED,YAAI,qBAAqB,WAAW;AAClC,oBAAU,qBAAqB,OAAO;AACtC;AAAA,QACF;AAEA,cAAM;AAAA,UACJ;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAEA,QAAM,QAAQ,IAAI,KAAK;AACzB;","names":["pLimit","fg"]}
@@ -23,7 +23,11 @@ __export(checkAIAccess_exports, {
23
23
  module.exports = __toCommonJS(checkAIAccess_exports);
24
24
  var import_config = require("@intlayer/config");
25
25
  const checkAIAccess = (configuration, aiOptions) => {
26
- const appLogger = (0, import_config.getAppLogger)(configuration);
26
+ const appLogger = (0, import_config.getAppLogger)(configuration, {
27
+ config: {
28
+ prefix: ""
29
+ }
30
+ });
27
31
  if (!configuration.editor.clientId && !configuration.editor.clientSecret && !configuration.ai?.apiKey && !aiOptions?.apiKey) {
28
32
  appLogger("AI options or API key not provided. Skipping AI translation.", {
29
33
  level: "error"
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/utils/checkAIAccess.ts"],"sourcesContent":["import type { AIOptions } from '@intlayer/api';\nimport { getAppLogger, type IntlayerConfig } from '@intlayer/config';\n\nexport const checkAIAccess = (\n configuration: IntlayerConfig,\n aiOptions?: AIOptions\n) => {\n const appLogger = getAppLogger(configuration);\n\n if (\n !configuration.editor.clientId &&\n !configuration.editor.clientSecret &&\n !configuration.ai?.apiKey &&\n !aiOptions?.apiKey\n ) {\n appLogger('AI options or API key not provided. Skipping AI translation.', {\n level: 'error',\n });\n // Potentially handle this case differently, e.g., by using a different translation method or stopping.\n\n throw new Error(\n 'AI options or API key not provided. Skipping AI translation.'\n );\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAAkD;AAE3C,MAAM,gBAAgB,CAC3B,eACA,cACG;AACH,QAAM,gBAAY,4BAAa,aAAa;AAE5C,MACE,CAAC,cAAc,OAAO,YACtB,CAAC,cAAc,OAAO,gBACtB,CAAC,cAAc,IAAI,UACnB,CAAC,WAAW,QACZ;AACA,cAAU,gEAAgE;AAAA,MACxE,OAAO;AAAA,IACT,CAAC;AAGD,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../../../src/utils/checkAIAccess.ts"],"sourcesContent":["import type { AIOptions } from '@intlayer/api';\nimport { getAppLogger, type IntlayerConfig } from '@intlayer/config';\n\nexport const checkAIAccess = (\n configuration: IntlayerConfig,\n aiOptions?: AIOptions\n) => {\n const appLogger = getAppLogger(configuration, {\n config: {\n prefix: '',\n },\n });\n\n if (\n !configuration.editor.clientId &&\n !configuration.editor.clientSecret &&\n !configuration.ai?.apiKey &&\n !aiOptions?.apiKey\n ) {\n appLogger('AI options or API key not provided. Skipping AI translation.', {\n level: 'error',\n });\n // Potentially handle this case differently, e.g., by using a different translation method or stopping.\n\n throw new Error(\n 'AI options or API key not provided. Skipping AI translation.'\n );\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAAkD;AAE3C,MAAM,gBAAgB,CAC3B,eACA,cACG;AACH,QAAM,gBAAY,4BAAa,eAAe;AAAA,IAC5C,QAAQ;AAAA,MACN,QAAQ;AAAA,IACV;AAAA,EACF,CAAC;AAED,MACE,CAAC,cAAc,OAAO,YACtB,CAAC,cAAc,OAAO,gBACtB,CAAC,cAAc,IAAI,UACnB,CAAC,WAAW,QACZ;AACA,cAAU,gEAAgE;AAAA,MACxE,OAAO;AAAA,IACT,CAAC;AAGD,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
@@ -0,0 +1,206 @@
1
+ import { getOAuthAPI } from "@intlayer/api";
2
+ import configuration from "@intlayer/config/built";
3
+ import { getAppLogger } from "@intlayer/config/client";
4
+ import { EventSource } from "eventsource";
5
+ class IntlayerEventListener {
6
+ constructor(intlayerConfig = configuration) {
7
+ this.intlayerConfig = intlayerConfig;
8
+ this.appLogger = getAppLogger(this.intlayerConfig);
9
+ }
10
+ appLogger = getAppLogger(configuration);
11
+ eventSource = null;
12
+ reconnectAttempts = 0;
13
+ maxReconnectAttempts = 5;
14
+ reconnectDelay = 1e3;
15
+ // Start with 1 second
16
+ isManuallyDisconnected = false;
17
+ reconnectTimeout = null;
18
+ /**
19
+ * Callback triggered when a Dictionary is ADDED.
20
+ */
21
+ onDictionaryAdded;
22
+ /**
23
+ * Callback triggered when a Dictionary is UPDATED.
24
+ */
25
+ onDictionaryChange;
26
+ /**
27
+ * Callback triggered when a Dictionary is DELETED.
28
+ */
29
+ onDictionaryDeleted;
30
+ /**
31
+ * Callback triggered when connection is established or re-established.
32
+ */
33
+ onConnectionOpen;
34
+ /**
35
+ * Callback triggered when connection encounters an error.
36
+ */
37
+ onConnectionError;
38
+ /**
39
+ * Initializes the EventSource connection using the given intlayerConfig
40
+ * (or the default config if none was provided).
41
+ */
42
+ async initialize() {
43
+ this.isManuallyDisconnected = false;
44
+ await this.connect();
45
+ }
46
+ /**
47
+ * Establishes the EventSource connection with automatic reconnection support.
48
+ */
49
+ async connect() {
50
+ try {
51
+ const backendURL = this.intlayerConfig.editor.backendURL;
52
+ const accessToken = await getOAuthAPI(
53
+ this.intlayerConfig
54
+ ).getOAuth2AccessToken();
55
+ if (!accessToken) {
56
+ throw new Error("Failed to retrieve access token");
57
+ }
58
+ const API_ROUTE = `${backendURL}/api/event-listener`;
59
+ if (this.eventSource) {
60
+ this.eventSource.close();
61
+ }
62
+ this.eventSource = new EventSource(API_ROUTE, {
63
+ fetch: (input, init) => fetch(input, {
64
+ ...init,
65
+ headers: {
66
+ ...init?.headers ?? {},
67
+ Authorization: `Bearer ${accessToken.data?.accessToken}`
68
+ }
69
+ })
70
+ });
71
+ this.eventSource.onopen = () => {
72
+ this.reconnectAttempts = 0;
73
+ this.reconnectDelay = 1e3;
74
+ this.onConnectionOpen?.();
75
+ };
76
+ this.eventSource.onmessage = (event) => this.handleMessage(event);
77
+ this.eventSource.onerror = (event) => this.handleError(event);
78
+ } catch (error) {
79
+ this.appLogger("Failed to establish EventSource connection:", {
80
+ level: "error"
81
+ });
82
+ this.scheduleReconnect();
83
+ }
84
+ }
85
+ /**
86
+ * Cleans up (closes) the EventSource connection.
87
+ */
88
+ cleanup() {
89
+ this.isManuallyDisconnected = true;
90
+ if (this.reconnectTimeout) {
91
+ clearTimeout(this.reconnectTimeout);
92
+ this.reconnectTimeout = null;
93
+ }
94
+ if (this.eventSource) {
95
+ this.eventSource.close();
96
+ this.eventSource = null;
97
+ }
98
+ }
99
+ /**
100
+ * Schedules a reconnection attempt with exponential backoff.
101
+ */
102
+ scheduleReconnect() {
103
+ if (this.isManuallyDisconnected || this.reconnectAttempts >= this.maxReconnectAttempts) {
104
+ if (this.reconnectAttempts >= this.maxReconnectAttempts) {
105
+ this.appLogger(
106
+ [
107
+ `Max reconnection attempts (${this.maxReconnectAttempts}) reached. Giving up.`
108
+ ],
109
+ {
110
+ level: "error"
111
+ }
112
+ );
113
+ }
114
+ return;
115
+ }
116
+ this.reconnectAttempts++;
117
+ const delay = this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1);
118
+ this.appLogger(
119
+ `Scheduling reconnection attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts} in ${delay}ms`
120
+ );
121
+ this.reconnectTimeout = setTimeout(async () => {
122
+ if (!this.isManuallyDisconnected) {
123
+ await this.connect();
124
+ }
125
+ }, delay);
126
+ }
127
+ /**
128
+ * Handles incoming SSE messages, parses the event data,
129
+ * and invokes the appropriate callback.
130
+ */
131
+ async handleMessage(event) {
132
+ try {
133
+ const { data } = event;
134
+ const dataJSON = JSON.parse(data);
135
+ for (const dataEl of dataJSON) {
136
+ switch (dataEl.object) {
137
+ case "DICTIONARY":
138
+ switch (dataEl.status) {
139
+ case "ADDED":
140
+ await this.onDictionaryAdded?.(dataEl.data);
141
+ break;
142
+ case "UPDATED":
143
+ await this.onDictionaryChange?.(dataEl.data);
144
+ break;
145
+ case "DELETED":
146
+ await this.onDictionaryDeleted?.(dataEl.data);
147
+ break;
148
+ default:
149
+ this.appLogger(
150
+ ["Unhandled dictionary status:", dataEl.status],
151
+ {
152
+ level: "error"
153
+ }
154
+ );
155
+ break;
156
+ }
157
+ break;
158
+ default:
159
+ this.appLogger(["Unknown object type:", dataEl.object], {
160
+ level: "error"
161
+ });
162
+ break;
163
+ }
164
+ }
165
+ } catch (error) {
166
+ this.appLogger(["Error processing dictionary update:", error], {
167
+ level: "error"
168
+ });
169
+ }
170
+ }
171
+ /**
172
+ * Handles any SSE errors and attempts reconnection if appropriate.
173
+ */
174
+ handleError(event) {
175
+ const errorEvent = event;
176
+ this.appLogger(
177
+ [
178
+ "EventSource error:",
179
+ {
180
+ type: errorEvent.type,
181
+ message: errorEvent.message,
182
+ code: errorEvent.code,
183
+ readyState: this.eventSource?.readyState,
184
+ url: this.eventSource?.url
185
+ }
186
+ ],
187
+ {
188
+ level: "error"
189
+ }
190
+ );
191
+ this.onConnectionError?.(event);
192
+ const isConnectionClosed = errorEvent.type === "error" && (errorEvent.message?.includes("terminated") || errorEvent.message?.includes("closed") || this.eventSource?.readyState === EventSource.CLOSED);
193
+ if (isConnectionClosed && !this.isManuallyDisconnected) {
194
+ this.appLogger(
195
+ "Connection was terminated by server, attempting to reconnect..."
196
+ );
197
+ this.scheduleReconnect();
198
+ } else {
199
+ this.cleanup();
200
+ }
201
+ }
202
+ }
203
+ export {
204
+ IntlayerEventListener
205
+ };
206
+ //# sourceMappingURL=IntlayerEventListener.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/IntlayerEventListener.ts"],"sourcesContent":["import { getOAuthAPI } from '@intlayer/api';\n// @ts-ignore: @intlayer/backend is not built yet\nimport type { DictionaryAPI, MessageEventData } from '@intlayer/backend';\nimport configuration from '@intlayer/config/built';\nimport { type IntlayerConfig, getAppLogger } from '@intlayer/config/client';\nimport { EventSource } from 'eventsource';\n\nexport type IntlayerMessageEvent = MessageEvent;\n\n/**\n * IntlayerEventListener class to listen for dictionary changes via SSE (Server-Sent Events).\n *\n * Usage example:\n *\n * import { buildIntlayerDictionary } from './transpiler/declaration_file_to_dictionary/intlayer_dictionary';\n * import { IntlayerEventListener } from '@intlayer/api';\n *\n * export const checkDictionaryChanges = async () => {\n * // Instantiate the listener\n * const eventListener = new IntlayerEventListener();\n *\n * // Set up your callbacks\n * eventListener.onDictionaryChange = async (dictionary) => {\n * await buildIntlayerDictionary(dictionary);\n * };\n *\n * // Initialize the listener\n * await eventListener.initialize();\n *\n * // Optionally, clean up later when you’re done\n * // eventListener.cleanup();\n * };\n */\nexport class IntlayerEventListener {\n private appLogger = getAppLogger(configuration);\n\n private eventSource: EventSource | null = null;\n private reconnectAttempts = 0;\n private maxReconnectAttempts = 5;\n private reconnectDelay = 1000; // Start with 1 second\n private isManuallyDisconnected = false;\n private reconnectTimeout: NodeJS.Timeout | null = null;\n\n /**\n * Callback triggered when a Dictionary is ADDED.\n */\n public onDictionaryAdded?: (dictionary: DictionaryAPI) => any;\n\n /**\n * Callback triggered when a Dictionary is UPDATED.\n */\n public onDictionaryChange?: (dictionary: DictionaryAPI) => any;\n\n /**\n * Callback triggered when a Dictionary is DELETED.\n */\n public onDictionaryDeleted?: (dictionary: DictionaryAPI) => any;\n\n /**\n * Callback triggered when connection is established or re-established.\n */\n public onConnectionOpen?: () => any;\n\n /**\n * Callback triggered when connection encounters an error.\n */\n public onConnectionError?: (error: Event) => any;\n\n constructor(private intlayerConfig: IntlayerConfig = configuration) {\n this.appLogger = getAppLogger(this.intlayerConfig);\n }\n\n /**\n * Initializes the EventSource connection using the given intlayerConfig\n * (or the default config if none was provided).\n */\n public async initialize(): Promise<void> {\n this.isManuallyDisconnected = false;\n await this.connect();\n }\n\n /**\n * Establishes the EventSource connection with automatic reconnection support.\n */\n private async connect(): Promise<void> {\n try {\n const backendURL = this.intlayerConfig.editor.backendURL;\n\n // Retrieve the access token\n const accessToken = await getOAuthAPI(\n this.intlayerConfig\n ).getOAuth2AccessToken();\n\n if (!accessToken) {\n throw new Error('Failed to retrieve access token');\n }\n\n const API_ROUTE = `${backendURL}/api/event-listener`;\n\n // Close existing connection if any\n if (this.eventSource) {\n this.eventSource.close();\n }\n\n this.eventSource = new EventSource(API_ROUTE, {\n fetch: (input, init) =>\n fetch(input, {\n ...init,\n headers: {\n ...(init?.headers ?? {}),\n Authorization: `Bearer ${accessToken.data?.accessToken}`,\n },\n }),\n });\n\n this.eventSource.onopen = () => {\n this.reconnectAttempts = 0;\n this.reconnectDelay = 1000; // Reset delay\n this.onConnectionOpen?.();\n };\n\n this.eventSource.onmessage = (event) => this.handleMessage(event);\n this.eventSource.onerror = (event) => this.handleError(event);\n } catch (error) {\n this.appLogger('Failed to establish EventSource connection:', {\n level: 'error',\n });\n this.scheduleReconnect();\n }\n }\n\n /**\n * Cleans up (closes) the EventSource connection.\n */\n public cleanup(): void {\n this.isManuallyDisconnected = true;\n\n if (this.reconnectTimeout) {\n clearTimeout(this.reconnectTimeout);\n this.reconnectTimeout = null;\n }\n\n if (this.eventSource) {\n this.eventSource.close();\n this.eventSource = null;\n }\n }\n\n /**\n * Schedules a reconnection attempt with exponential backoff.\n */\n private scheduleReconnect(): void {\n if (\n this.isManuallyDisconnected ||\n this.reconnectAttempts >= this.maxReconnectAttempts\n ) {\n if (this.reconnectAttempts >= this.maxReconnectAttempts) {\n this.appLogger(\n [\n `Max reconnection attempts (${this.maxReconnectAttempts}) reached. Giving up.`,\n ],\n {\n level: 'error',\n }\n );\n }\n return;\n }\n\n this.reconnectAttempts++;\n const delay = this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1); // Exponential backoff\n\n this.appLogger(\n `Scheduling reconnection attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts} in ${delay}ms`\n );\n\n this.reconnectTimeout = setTimeout(async () => {\n if (!this.isManuallyDisconnected) {\n await this.connect();\n }\n }, delay);\n }\n\n /**\n * Handles incoming SSE messages, parses the event data,\n * and invokes the appropriate callback.\n */\n private async handleMessage(event: IntlayerMessageEvent): Promise<void> {\n try {\n const { data } = event;\n\n const dataJSON: MessageEventData[] = JSON.parse(data);\n\n for (const dataEl of dataJSON) {\n switch (dataEl.object) {\n case 'DICTIONARY':\n switch (dataEl.status) {\n case 'ADDED':\n await this.onDictionaryAdded?.(dataEl.data);\n break;\n case 'UPDATED':\n await this.onDictionaryChange?.(dataEl.data);\n break;\n case 'DELETED':\n await this.onDictionaryDeleted?.(dataEl.data);\n break;\n default:\n this.appLogger(\n ['Unhandled dictionary status:', dataEl.status],\n {\n level: 'error',\n }\n );\n break;\n }\n break;\n default:\n this.appLogger(['Unknown object type:', dataEl.object], {\n level: 'error',\n });\n break;\n }\n }\n } catch (error) {\n this.appLogger(['Error processing dictionary update:', error], {\n level: 'error',\n });\n }\n }\n\n /**\n * Handles any SSE errors and attempts reconnection if appropriate.\n */\n private handleError(event: Event): void {\n const errorEvent = event as any;\n\n // Log detailed error information\n this.appLogger(\n [\n 'EventSource error:',\n {\n type: errorEvent.type,\n message: errorEvent.message,\n code: errorEvent.code,\n readyState: this.eventSource?.readyState,\n url: this.eventSource?.url,\n },\n ],\n {\n level: 'error',\n }\n );\n\n // Notify error callback\n this.onConnectionError?.(event);\n\n // Check if this is a connection close error\n const isConnectionClosed =\n errorEvent.type === 'error' &&\n (errorEvent.message?.includes('terminated') ||\n errorEvent.message?.includes('closed') ||\n this.eventSource?.readyState === EventSource.CLOSED);\n\n if (isConnectionClosed && !this.isManuallyDisconnected) {\n this.appLogger(\n 'Connection was terminated by server, attempting to reconnect...'\n );\n this.scheduleReconnect();\n } else {\n // For other types of errors, close the connection\n this.cleanup();\n }\n }\n}\n"],"mappings":"AAAA,SAAS,mBAAmB;AAG5B,OAAO,mBAAmB;AAC1B,SAA8B,oBAAoB;AAClD,SAAS,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AACF;","names":[]}
package/dist/esm/cli.mjs CHANGED
@@ -1,18 +1,20 @@
1
- import { isESModule } from "@intlayer/config";
2
1
  import configuration from "@intlayer/config/built";
3
2
  import { Command } from "commander";
4
3
  import { dirname as pathDirname } from "path";
5
4
  import { fileURLToPath } from "url";
6
5
  import { build } from "./build.mjs";
7
6
  import { getConfig } from "./config.mjs";
8
- import { fill } from "./fill.mjs";
7
+ import { fill } from "./fill/index.mjs";
9
8
  import { listContentDeclaration } from "./listContentDeclaration.mjs";
9
+ import { liveSync } from "./liveSync.mjs";
10
10
  import { pull } from "./pull.mjs";
11
11
  import { push } from "./push.mjs";
12
12
  import { pushConfig } from "./pushConfig.mjs";
13
13
  import { reviewDoc } from "./reviewDoc.mjs";
14
+ import { testMissingTranslations } from "./test/index.mjs";
14
15
  import { translateDoc } from "./translateDoc.mjs";
15
16
  import { getParentPackageJSON } from "./utils/getParentPackageJSON.mjs";
17
+ const isESModule = typeof import.meta.url === "string";
16
18
  const dirname = isESModule ? pathDirname(fileURLToPath(import.meta.url)) : __dirname;
17
19
  const packageJson = getParentPackageJSON(dirname);
18
20
  const logOptions = [
@@ -242,20 +244,32 @@ const setAPI = () => {
242
244
  configOptions: extractConfigOptions(options)
243
245
  });
244
246
  });
245
- program.command("content list").description("List the content declaration files").action(listContentDeclaration);
247
+ const contentProgram = program.command("content").description("Content declaration operations");
248
+ contentProgram.command("list").description("List the content declaration files").action(listContentDeclaration);
249
+ const testProgram = contentProgram.command("test").description("Test if there are missing translations");
250
+ applyConfigOptions(testProgram);
251
+ testProgram.action((options) => {
252
+ testMissingTranslations({
253
+ ...options,
254
+ configOptions: extractConfigOptions(options)
255
+ });
256
+ });
246
257
  const fillProgram = program.command("fill").description("Fill the dictionaries").option("-f, --file [files...]", "List of Dictionary files to fill").option("--source-locale [sourceLocale]", "Source locale to translate from").option(
247
258
  "--output-locales [outputLocales...]",
248
259
  "Target locales to translate to"
249
260
  ).option(
250
261
  "--mode [mode]",
251
262
  "Fill mode: complete, review. Complete will fill all missing content, review will fill missing content and review existing keys",
252
- "review"
263
+ "complete"
253
264
  ).option("-k, --keys [keys...]", "Filter dictionaries based on keys").option(
254
265
  "--excluded-keys [excludedKeys...]",
255
266
  "Filter out dictionaries based on keys"
256
267
  ).option(
257
268
  "--path-filter [pathFilters...]",
258
269
  "Filter dictionaries based on glob pattern"
270
+ ).option(
271
+ "--build [build]",
272
+ "Build the dictionaries before filling to ensure the content is up to date"
259
273
  );
260
274
  applyConfigOptions(fillProgram);
261
275
  applyAIOptions(fillProgram);
@@ -334,6 +348,14 @@ const setAPI = () => {
334
348
  skipIfModifiedAfter: options.skipIfModifiedAfter
335
349
  })
336
350
  );
351
+ const liveOptions = [
352
+ ["--process [process]", "Start command in parallel with the live sync"]
353
+ ];
354
+ const liveCmd = program.command("live").description(
355
+ "Live sync - Watch for changes made on the CMS and update the application content accordingly"
356
+ );
357
+ applyOptions(liveCmd, liveOptions);
358
+ liveCmd.action((options) => liveSync(options));
337
359
  program.parse(process.argv);
338
360
  return program;
339
361
  };