@alauda/doom 0.14.6 → 0.14.8

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.
@@ -15,4 +15,10 @@ export declare const translate: ({ source, sourceContent, target, targetContent,
15
15
  targetContent?: string;
16
16
  additionalPrompts?: string;
17
17
  }) => Promise<string>;
18
+ export interface TranslateCommandOptions {
19
+ source: string;
20
+ target: string;
21
+ glob: string[];
22
+ copy?: boolean;
23
+ }
18
24
  export declare const translateCommand: Command;
@@ -90,9 +90,10 @@ export const translateCommand = new Command('translate')
90
90
  .argument('[root]', 'Root directory of the documentation')
91
91
  .option('-s, --source <language>', 'Document source language', 'zh')
92
92
  .option('-t, --target <language>', 'Document target language', 'en')
93
- .option('-g, --glob <path...>', 'Glob pattern for source files')
93
+ .requiredOption('-g, --glob <path...>', 'Glob patterns for source dirs/files')
94
+ .option('-C, --copy [boolean]', 'Wether to copy relative assets to the target directory instead of following links', (value) => !!value && value !== 'false', false)
94
95
  .action(async function (root) {
95
- const { source, target, glob: globs, ...globalOptions } = this.optsWithGlobals();
96
+ const { source, target, glob: globs, copy, ...globalOptions } = this.optsWithGlobals();
96
97
  if (!Object.hasOwn(LANGUAGE_CODES, source) ||
97
98
  !Object.hasOwn(LANGUAGE_CODES, target) ||
98
99
  source === target) {
@@ -109,32 +110,25 @@ export const translateCommand = new Command('translate')
109
110
  process.exitCode = 1;
110
111
  return;
111
112
  }
112
- let sourceFilePaths;
113
- if (globs?.length) {
114
- sourceFilePaths = await glob(globs.map((g) => g.split('/').at(-1).includes('.') ? g : `${g}.md{,x}`), {
115
- absolute: true,
116
- cwd: sourceDir,
117
- });
118
- }
119
- else {
120
- const dirents = await fs.readdir(sourceDir, {
121
- recursive: true,
122
- withFileTypes: true,
123
- });
124
- sourceFilePaths = dirents.reduce((acc, d) => {
125
- if (!d.isFile() || !isDoc(d.name)) {
126
- return acc;
127
- }
128
- acc.push(path.resolve(d.parentPath ||
129
- // eslint-disable-next-line @typescript-eslint/no-deprecated
130
- d.path, d.name));
131
- return acc;
132
- }, []);
133
- }
134
- await Promise.all(sourceFilePaths.map(async (sourceFilePath) => {
135
- if (!isDoc(sourceFilePath)) {
136
- return;
113
+ const matched = await glob(globs, {
114
+ absolute: true,
115
+ cwd: sourceDir,
116
+ onlyFiles: false,
117
+ });
118
+ const sourceFilePaths = await Promise.all(matched.map(async (it) => {
119
+ const stat = await fs.stat(it);
120
+ if (stat.isDirectory()) {
121
+ return glob('**/*.md{,x}', {
122
+ absolute: true,
123
+ cwd: it,
124
+ });
137
125
  }
126
+ if (stat.isFile() && isDoc(it)) {
127
+ return it;
128
+ }
129
+ return [];
130
+ }));
131
+ await Promise.all(sourceFilePaths.flat().map(async (sourceFilePath) => {
138
132
  const sourceContent = await fs.readFile(sourceFilePath, 'utf-8');
139
133
  const sourceFrontmatter = matter(sourceContent).data;
140
134
  if (sourceFrontmatter.i18n?.disableAutoTranslation) {
@@ -165,7 +159,7 @@ export const translateCommand = new Command('translate')
165
159
  localPublicBase: path.resolve(docsDir, 'public'),
166
160
  sourceBase: path.dirname(sourceFilePath),
167
161
  targetBase: path.dirname(targetFilePath),
168
- translating: [source, target],
162
+ translating: { source, target, copy },
169
163
  };
170
164
  targetContent = await translate({
171
165
  source,
@@ -7,6 +7,10 @@ export interface NormalizeImgSrcOptions {
7
7
  sourceBase: string;
8
8
  targetBase: string;
9
9
  force?: boolean;
10
- translating?: readonly [string, string];
10
+ translating?: {
11
+ source: string;
12
+ target: string;
13
+ copy?: boolean;
14
+ };
11
15
  }
12
16
  export declare const normalizeImgSrc: (content: Content, { refSource, localPublicBase, publicBase, sourceBase, targetBase, force, translating, }: NormalizeImgSrcOptions) => Content;
@@ -23,27 +23,58 @@ export const normalizeImgSrc = (content, { refSource, localPublicBase, publicBas
23
23
  sourcePath = path.resolve(publicBase, imageSrc);
24
24
  }
25
25
  const targetPath = path.resolve(targetDir, imageSrc);
26
- fs.mkdirSync(path.dirname(targetPath), { recursive: true });
27
26
  if (force || !fs.existsSync(targetPath)) {
27
+ fs.mkdirSync(path.dirname(targetPath), { recursive: true });
28
28
  fs.copyFileSync(sourcePath, targetPath);
29
29
  }
30
30
  return `/_remotes/${refSource.name}/${imageSrc}`;
31
31
  }
32
32
  else if (!RELATIVE_URL_PATTERN.test(src)) {
33
33
  if (translating) {
34
- const [source, target] = translating;
34
+ const { source, target, copy } = translating;
35
35
  const sourcePrefix = `/${source}/`;
36
- if (src.startsWith(sourcePrefix) &&
37
- fs.existsSync(path.resolve(localPublicBase, target, src.replace(sourcePrefix, '')))) {
38
- return src.replace(sourcePrefix, `/${target}/`);
36
+ if (src.startsWith(sourcePrefix)) {
37
+ const filename = src.slice(sourcePrefix.length);
38
+ const targetPath = path.resolve(localPublicBase, target, filename);
39
+ let targetExisted = fs.existsSync(targetPath);
40
+ if (!targetExisted && copy) {
41
+ fs.mkdirSync(path.dirname(targetPath), { recursive: true });
42
+ fs.copyFileSync(path.resolve(localPublicBase, source, filename), targetPath);
43
+ targetExisted = true;
44
+ }
45
+ if (targetExisted) {
46
+ return `/${target}/${filename}`;
47
+ }
39
48
  }
40
49
  }
41
50
  return src;
42
51
  }
43
- if (translating && fs.existsSync(path.resolve(targetBase, src))) {
44
- return src;
52
+ let sourcePath = path.resolve(sourceBase, src);
53
+ let targetPath = path.resolve(targetBase, src);
54
+ if (translating) {
55
+ const { source, target, copy } = translating;
56
+ const assetsSourcePrefix = `./assets/${source}/`;
57
+ if (src.startsWith(assetsSourcePrefix)) {
58
+ const filename = src.slice(assetsSourcePrefix.length);
59
+ const targetSrc = `./assets/${target}/${filename}`;
60
+ const sourceTargetPath = path.resolve(sourceBase, targetSrc);
61
+ if (fs.existsSync(sourceTargetPath)) {
62
+ src = targetSrc;
63
+ sourcePath = sourceTargetPath;
64
+ targetPath = path.resolve(targetBase, src);
65
+ }
66
+ }
67
+ let targetExisted = fs.existsSync(targetPath);
68
+ if (!targetExisted && copy) {
69
+ fs.mkdirSync(path.dirname(targetPath), { recursive: true });
70
+ fs.copyFileSync(sourcePath, targetPath);
71
+ targetExisted = true;
72
+ }
73
+ if (targetExisted) {
74
+ return src;
75
+ }
45
76
  }
46
- const targetSrc = path.relative(targetBase, path.resolve(sourceBase, src));
77
+ const targetSrc = path.relative(targetBase, sourcePath);
47
78
  return RELATIVE_URL_PATTERN.test(targetSrc) ? targetSrc : `./${targetSrc}`;
48
79
  };
49
80
  visit(content, 'image', (node) => {
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "0.14.6",
3
+ "version": "0.14.8",
4
4
  "type": "module",
5
5
  "description": "Doctor Doom making docs.",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git+https://gitlab-ce.alauda.cn/idp/Doom.git"
9
9
  },
10
- "packageManager": "yarn@4.6.0",
10
+ "packageManager": "yarn@4.7.0",
11
11
  "engines": {
12
12
  "node": ">=18.17.0 <20 || >=20.1.0"
13
13
  },
@@ -57,7 +57,7 @@
57
57
  "fs-extra": "^11.3.0",
58
58
  "html-tag-names": "^2.1.0",
59
59
  "mermaid": "^11.4.1",
60
- "openai": "^5.0.0-alpha.0",
60
+ "openai": "^5.0.0-beta.0",
61
61
  "openapi-types": "^12.1.3",
62
62
  "p-ratelimit": "^1.0.1",
63
63
  "pdf-lib": "^1.17.1",