@alauda/doom 0.14.5 → 0.14.7

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;
@@ -6,8 +6,10 @@ import { Command } from 'commander';
6
6
  import matter from 'gray-matter';
7
7
  import { AzureOpenAI } from 'openai';
8
8
  import { pRateLimit } from 'p-ratelimit';
9
+ import { glob } from 'tinyglobby';
9
10
  import { cyan } from 'yoctocolors';
10
11
  import { mdProcessor, mdxProcessor, normalizeImgSrc, } from '../plugins/index.js';
12
+ import { isDoc } from '../shared/index.js';
11
13
  import { pathExists } from '../utils/index.js';
12
14
  import { loadConfig } from './load-config.js';
13
15
  const LANGUAGE_CODES = {
@@ -88,8 +90,10 @@ export const translateCommand = new Command('translate')
88
90
  .argument('[root]', 'Root directory of the documentation')
89
91
  .option('-s, --source <language>', 'Document source language', 'zh')
90
92
  .option('-t, --target <language>', 'Document target language', 'en')
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)
91
95
  .action(async function (root) {
92
- const { source, target, ...globalOptions } = this.optsWithGlobals();
96
+ const { source, target, glob: globs, copy, ...globalOptions } = this.optsWithGlobals();
93
97
  if (!Object.hasOwn(LANGUAGE_CODES, source) ||
94
98
  !Object.hasOwn(LANGUAGE_CODES, target) ||
95
99
  source === target) {
@@ -106,17 +110,25 @@ export const translateCommand = new Command('translate')
106
110
  process.exitCode = 1;
107
111
  return;
108
112
  }
109
- const dirents = await fs.readdir(sourceDir, {
110
- recursive: true,
111
- withFileTypes: true,
113
+ const matched = await glob(globs, {
114
+ absolute: true,
115
+ cwd: sourceDir,
116
+ onlyFiles: false,
112
117
  });
113
- await Promise.all(dirents.map(async (d) => {
114
- if (!d.isFile() || !/\.mdx?$/.test(d.name)) {
115
- return;
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
+ });
116
125
  }
117
- const sourceFilePath = path.resolve(d.parentPath ||
118
- // eslint-disable-next-line @typescript-eslint/no-deprecated
119
- d.path, d.name);
126
+ if (stat.isFile() && isDoc(it)) {
127
+ return it;
128
+ }
129
+ return [];
130
+ }));
131
+ await Promise.all(sourceFilePaths.flat().map(async (sourceFilePath) => {
120
132
  const sourceContent = await fs.readFile(sourceFilePath, 'utf-8');
121
133
  const sourceFrontmatter = matter(sourceContent).data;
122
134
  if (sourceFrontmatter.i18n?.disableAutoTranslation) {
@@ -147,7 +159,7 @@ export const translateCommand = new Command('translate')
147
159
  localPublicBase: path.resolve(docsDir, 'public'),
148
160
  sourceBase: path.dirname(sourceFilePath),
149
161
  targetBase: path.dirname(targetFilePath),
150
- translating: [source, target],
162
+ translating: { source, target, copy },
151
163
  };
152
164
  targetContent = await translate({
153
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) => {
@@ -1,3 +1,5 @@
1
1
  export declare const normalizeVersion: (version?: string) => string;
2
2
  export declare const removeBothEndsSlashes: (str?: string) => string;
3
3
  export declare const getPdfName: (lang: string, userBase?: string, title?: string) => string;
4
+ export declare const DOC_PATTERN: RegExp;
5
+ export declare const isDoc: (filename: string) => boolean;
@@ -2,3 +2,5 @@ const PLAIN_VERSION_PATTERN = /^(\d+\.)*\d+$/;
2
2
  export const normalizeVersion = (version = '') => PLAIN_VERSION_PATTERN.test(version) ? `v${version}` : version;
3
3
  export const removeBothEndsSlashes = (str) => str?.replace(/^\/|\/$/g, '') || '';
4
4
  export const getPdfName = (lang, userBase, title) => `${removeBothEndsSlashes(userBase) || title || 'exported'}-${lang}.pdf`;
5
+ export const DOC_PATTERN = /\.mdx?$/;
6
+ export const isDoc = (filename) => DOC_PATTERN.test(filename);
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "0.14.5",
3
+ "version": "0.14.7",
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
  },
@@ -73,14 +73,14 @@
73
73
  "simple-git": "^3.27.0",
74
74
  "swagger2openapi": "^7.0.8",
75
75
  "tinyglobby": "^0.2.12",
76
- "type-fest": "^4.36.0",
76
+ "type-fest": "^4.37.0",
77
77
  "yaml": "^2.7.0",
78
78
  "yoctocolors": "^2.1.1"
79
79
  },
80
80
  "devDependencies": {
81
81
  "@eslint/js": "^9.21.0",
82
82
  "@swc-node/register": "^1.10.9",
83
- "@swc/core": "1.11.5",
83
+ "@swc/core": "1.11.7",
84
84
  "@types/cli-progress": "^3.11.6",
85
85
  "@types/ejs": "^3.1.5",
86
86
  "@types/fs-extra": "^11.0.4",