@alauda/doom 0.15.11 → 0.16.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/lib/cli/translate.js +29 -4
- package/package.json +2 -2
package/lib/cli/translate.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import crypto from 'node:crypto';
|
|
2
2
|
import fs from 'node:fs/promises';
|
|
3
3
|
import path from 'node:path';
|
|
4
|
+
import { setTimeout } from 'node:timers/promises';
|
|
4
5
|
import { removeLeadingSlash } from '@rspress/shared';
|
|
5
6
|
import { logger } from '@rspress/shared/logger';
|
|
6
7
|
import { Command } from 'commander';
|
|
7
8
|
import { render } from 'ejs';
|
|
8
9
|
import matter from 'gray-matter';
|
|
9
|
-
import { AzureOpenAI } from 'openai';
|
|
10
|
+
import { AzureOpenAI, RateLimitError } from 'openai';
|
|
10
11
|
import { pRateLimit } from 'p-ratelimit';
|
|
11
12
|
import { glob } from 'tinyglobby';
|
|
12
13
|
import { cyan } from 'yoctocolors';
|
|
@@ -25,7 +26,7 @@ const DEFAULT_SYSTEM_PROMPT = `
|
|
|
25
26
|
|
|
26
27
|
## 规则
|
|
27
28
|
- 第一条消息为需要翻译的最新<%= sourceLang %>文档,第二条消息为之前翻译过的但内容可能过期的<%= targetLang %>文档,如果没有翻译过则为空
|
|
28
|
-
- 输入格式为 MDX 格式,输出格式也必须保留原始 MDX
|
|
29
|
+
- 输入格式为 MDX 格式,输出格式也必须保留原始 MDX 格式,不要翻译其中的 jsx 组件名称,如 <Overview />,且不要额外包装在不必要的代码块中
|
|
29
30
|
- 文档中的资源链接不要翻译和替换
|
|
30
31
|
- MDX 组件中包含的内容需要翻译,MDX 组件参数的值不需要翻译,但以下这些特殊的 MDX 组件参数值需要翻译
|
|
31
32
|
* 组件示例: <Tab label="参数值">组件包含的内容</Tab>,label 是 key 不用翻译,"参数值" 需要翻译
|
|
@@ -33,6 +34,7 @@ const DEFAULT_SYSTEM_PROMPT = `
|
|
|
33
34
|
* ACP -> ACP
|
|
34
35
|
* 灵雀云 -> Alauda
|
|
35
36
|
* 容器组 -> Pods
|
|
37
|
+
* global 集群 -> global cluster
|
|
36
38
|
- 移除 {/* reference-start */}, {/* reference-end */}, <!-- reference-start --> 和 <!-- reference-end --> 相关的注释
|
|
37
39
|
- 翻译过程中务必保留原文中的 \\< 转义字符不要做任何转义变更
|
|
38
40
|
|
|
@@ -133,10 +135,13 @@ export const translateCommand = new Command('translate')
|
|
|
133
135
|
}
|
|
134
136
|
return [];
|
|
135
137
|
}));
|
|
136
|
-
|
|
138
|
+
const allSourceFilePaths = new Set(sourceFilePaths.flat());
|
|
139
|
+
const executor = async () => await Promise.all([...allSourceFilePaths].map(async (sourceFilePath) => {
|
|
137
140
|
const sourceContent = await fs.readFile(sourceFilePath, 'utf-8');
|
|
138
|
-
const sourceFrontmatter = matter(sourceContent)
|
|
141
|
+
const sourceFrontmatter = matter(sourceContent)
|
|
142
|
+
.data;
|
|
139
143
|
if (sourceFrontmatter.i18n?.disableAutoTranslation) {
|
|
144
|
+
allSourceFilePaths.delete(sourceFilePath);
|
|
140
145
|
return;
|
|
141
146
|
}
|
|
142
147
|
const sourceSHA = crypto
|
|
@@ -151,6 +156,7 @@ export const translateCommand = new Command('translate')
|
|
|
151
156
|
targetFrontmatter = matter(targetContent).data;
|
|
152
157
|
if (targetFrontmatter.i18n?.disableAutoTranslation ||
|
|
153
158
|
(!force && targetFrontmatter.sourceSHA === sourceSHA)) {
|
|
159
|
+
allSourceFilePaths.delete(sourceFilePath);
|
|
154
160
|
return;
|
|
155
161
|
}
|
|
156
162
|
}
|
|
@@ -188,6 +194,25 @@ export const translateCommand = new Command('translate')
|
|
|
188
194
|
await fs.mkdir(path.dirname(targetFilePath), { recursive: true });
|
|
189
195
|
await fs.writeFile(targetFilePath, targetContent);
|
|
190
196
|
logger.info(`${cyan(relativePath)} translated`);
|
|
197
|
+
allSourceFilePaths.delete(sourceFilePath);
|
|
191
198
|
});
|
|
192
199
|
}));
|
|
200
|
+
let retry = 0;
|
|
201
|
+
while (retry < 15) {
|
|
202
|
+
try {
|
|
203
|
+
await executor();
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
if (error instanceof RateLimitError) {
|
|
208
|
+
const retryAfter = Number(error.headers.get('retry-after')) || 60 * ++retry;
|
|
209
|
+
logger.warn(`Rate limit exceeded, retrying in ${retryAfter}s...`);
|
|
210
|
+
await setTimeout(retryAfter);
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
logger.error(`Failed to translate after ${retry} retries, please try again later.`);
|
|
217
|
+
process.exitCode = 1;
|
|
193
218
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alauda/doom",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Doctor Doom making docs.",
|
|
6
6
|
"repository": {
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"prepublishOnly": "yarn build",
|
|
41
41
|
"serve": "yarn doom serve",
|
|
42
42
|
"swc-node": "node --enable-source-maps --import @swc-node/register/esm-register",
|
|
43
|
-
"translate": "yarn doom translate fixture-docs"
|
|
43
|
+
"translate": "yarn doom translate fixture-docs -s en -t zh"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@inquirer/prompts": "^7.4.0",
|