@alauda/doom 0.15.10 → 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.
@@ -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
- await Promise.all(sourceFilePaths.flat().map(async (sourceFilePath) => {
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).data;
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
  });
@@ -45,10 +45,10 @@ export const OpenAPIResponses = ({ responses, openapi, }) => {
45
45
  if (type === 'object' &&
46
46
  'properties' in schema &&
47
47
  schema.properties) {
48
- extraNode = (_jsxs("div", { className: "my-4", children: ["Properties:", _jsx(OpenAPIProperties, { properties: schema.properties, openapi: openapi })] }));
48
+ extraNode = (_jsxs("div", { className: "my-4", children: [_jsx("em", { children: "Properties:" }), _jsx(OpenAPIProperties, { properties: schema.properties, openapi: openapi })] }));
49
49
  }
50
50
  else if (type === 'array' && 'items' in schema && schema.items) {
51
- extraNode = (_jsxs("div", { className: "my-4", children: ["Items:", _jsx(OpenAPIProperty, { property: schema.items, openapi: openapi })] }));
51
+ extraNode = (_jsxs("div", { className: "my-4", children: [_jsx("em", { children: "Items:" }), _jsx(OpenAPIProperty, { property: schema.items, openapi: openapi })] }));
52
52
  }
53
53
  }
54
54
  else if (type && '$ref' in type) {
@@ -11,6 +11,7 @@ export const OpenAPIProperty = ({ name, property, openapi, }) => {
11
11
  const propObj = '$ref' in property ? resolveRef(openapi, property.$ref) : property;
12
12
  const type = propObj.type;
13
13
  let typeNode;
14
+ let extraNode;
14
15
  if (type === 'array') {
15
16
  const { items } = propObj;
16
17
  const itemsObj = '$ref' in items ? resolveRef(openapi, items.$ref) : items;
@@ -18,6 +19,9 @@ export const OpenAPIProperty = ({ name, property, openapi, }) => {
18
19
  typeNode = (_jsxs("code", { children: ["[]", '$ref' in items ? _jsx(RefLink, { "$ref": items.$ref }) : itemsType] }));
19
20
  }
20
21
  else if (type === 'object') {
22
+ if ('properties' in property && property.properties) {
23
+ extraNode = (_jsxs("div", { className: "my-4", children: [_jsx("em", { children: "Properties:" }), _jsx(OpenAPIProperties, { properties: property.properties, openapi: openapi })] }));
24
+ }
21
25
  if (typeof propObj.additionalProperties === 'object') {
22
26
  const props = propObj.additionalProperties;
23
27
  const propsObj = '$ref' in props ? resolveRef(openapi, props.$ref) : props;
@@ -36,7 +40,7 @@ export const OpenAPIProperty = ({ name, property, openapi, }) => {
36
40
  else if ('$ref' in property) {
37
41
  typeNode = _jsx(RefLink, { "$ref": property.$ref });
38
42
  }
39
- return (_jsxs(_Fragment, { children: [name && (_jsxs(_Fragment, { children: [_jsx("code", { children: name }), ":", ' '] })), typeNode, _jsx(Markdown, { children: propObj.description })] }));
43
+ return (_jsxs(_Fragment, { children: [name && (_jsxs(_Fragment, { children: [_jsx("code", { children: name }), ":", ' '] })), typeNode, _jsx(Markdown, { children: propObj.description }), extraNode] }));
40
44
  };
41
45
  export const OpenAPIProperties = ({ properties, openapi, }) => {
42
46
  const [X] = useState(getCustomMDXComponent);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "0.15.10",
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",