























@@ -275,47 +275,66 @@ export function sanitizeDocsConfigForEnglishOnly(value) {
275275return Object.keys(sanitized).length > 0 ? sanitized : undefined;
276276}
277277278-function prepareMirroredDocsDir(sourceDir = DOCS_DIR) {
278+/**
279+ * @param {string} [sourceDir]
280+ * @param {{
281+ * resolveClawHubRepoPathImpl?: typeof resolveClawHubRepoPath;
282+ * syncClawHubDocsTreeImpl?: typeof syncClawHubDocsTree;
283+ * }} [options]
284+ */
285+export function prepareMirroredDocsDir(sourceDir = DOCS_DIR, options = {}) {
279286const sourceRoot = path.resolve(sourceDir);
280287if (sourceRoot !== path.resolve(DOCS_DIR)) {
281288return { dir: sourceRoot, mirroredClawHub: false, cleanup: () => {} };
282289}
283290284-const clawhubRepo = resolveClawHubRepoPath("", { required: false });
291+const resolveClawHubRepoPathImpl = options.resolveClawHubRepoPathImpl ?? resolveClawHubRepoPath;
292+const syncClawHubDocsTreeImpl = options.syncClawHubDocsTreeImpl ?? syncClawHubDocsTree;
293+const clawhubRepo = resolveClawHubRepoPathImpl("", { required: false });
285294if (!clawhubRepo) {
286295return { dir: sourceRoot, mirroredClawHub: false, cleanup: () => {} };
287296}
288297289298const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-docs-link-audit-"));
290-fs.cpSync(sourceRoot, tempDir, { recursive: true });
291-syncClawHubDocsTree(tempDir, { repoPath: clawhubRepo, required: false });
292-return {
293-dir: tempDir,
294-mirroredClawHub: true,
295-cleanup: () => fs.rmSync(tempDir, { recursive: true, force: true }),
296-};
299+try {
300+fs.cpSync(sourceRoot, tempDir, { recursive: true });
301+syncClawHubDocsTreeImpl(tempDir, { repoPath: clawhubRepo, required: false });
302+return {
303+dir: tempDir,
304+mirroredClawHub: true,
305+cleanup: () => fs.rmSync(tempDir, { recursive: true, force: true }),
306+};
307+} catch (error) {
308+fs.rmSync(tempDir, { recursive: true, force: true });
309+throw error;
310+}
297311}
298312299313export function prepareAnchorAuditDocsDir(sourceDir = DOCS_DIR) {
300314const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-docs-anchor-audit-"));
301-fs.cpSync(sourceDir, tempDir, { recursive: true });
315+try {
316+fs.cpSync(sourceDir, tempDir, { recursive: true });
302317303-for (const entry of fs.readdirSync(tempDir, { withFileTypes: true })) {
304-if (!entry.isDirectory()) {
305-continue;
306-}
307-if (!isGeneratedTranslatedDoc(`${entry.name}/`)) {
308-continue;
318+for (const entry of fs.readdirSync(tempDir, { withFileTypes: true })) {
319+if (!entry.isDirectory()) {
320+continue;
321+}
322+if (!isGeneratedTranslatedDoc(`${entry.name}/`)) {
323+continue;
324+}
325+fs.rmSync(path.join(tempDir, entry.name), { recursive: true, force: true });
309326}
310-fs.rmSync(path.join(tempDir, entry.name), { recursive: true, force: true });
311-}
312327313-const docsJsonPath = path.join(tempDir, "docs.json");
314-const docsConfig = JSON.parse(fs.readFileSync(docsJsonPath, "utf8"));
315-const sanitized = sanitizeDocsConfigForEnglishOnly(docsConfig);
316-fs.writeFileSync(docsJsonPath, `${JSON.stringify(sanitized, null, 2)}\n`, "utf8");
328+ const docsJsonPath = path.join(tempDir, "docs.json");
329+ const docsConfig = JSON.parse(fs.readFileSync(docsJsonPath, "utf8"));
330+ const sanitized = sanitizeDocsConfigForEnglishOnly(docsConfig);
331+ fs.writeFileSync(docsJsonPath, `${JSON.stringify(sanitized, null, 2)}\n`, "utf8");
317332318-return tempDir;
333+return tempDir;
334+} catch (error) {
335+fs.rmSync(tempDir, { recursive: true, force: true });
336+throw error;
337+}
319338}
320339321340/** @param {string} version */
@@ -528,6 +547,11 @@ export function auditDocsLinks(options = {}) {
528547 * platform?: NodeJS.Platform;
529548 * spawnSyncImpl?: typeof spawnSync;
530549 * prepareAnchorAuditDocsDirImpl?: (sourceDir?: string) => string;
550+ * prepareMirroredDocsDirImpl?: (sourceDir?: string) => {
551+ * dir: string;
552+ * mirroredClawHub: boolean;
553+ * cleanup: () => void;
554+ * };
531555 * cleanupAnchorAuditDocsDirImpl?: (dir: string) => void;
532556 * }} [options]
533557 */
@@ -540,10 +564,12 @@ export function runDocsLinkAuditCli(options = {}) {
540564const cleanupAnchorAuditDocsDirImpl =
541565options.cleanupAnchorAuditDocsDirImpl ??
542566((dir) => fs.rmSync(dir, { recursive: true, force: true }));
543-const mirroredDocsDir = prepareMirroredDocsDir(DOCS_DIR);
544-const anchorDocsDir = prepareAnchorAuditDocsDirImpl(mirroredDocsDir.dir);
567+const prepareMirroredDocsDirImpl = options.prepareMirroredDocsDirImpl ?? prepareMirroredDocsDir;
568+const mirroredDocsDir = prepareMirroredDocsDirImpl(DOCS_DIR);
569+let anchorDocsDir;
545570546571try {
572+anchorDocsDir = prepareAnchorAuditDocsDirImpl(mirroredDocsDir.dir);
547573// Use the npm Mintlify package explicitly. Some developer machines also
548574// have the Swift Package Manager tool named `mint` on PATH, and that
549575// binary exits with "command 'broken-links' not found".
@@ -565,7 +591,9 @@ export function runDocsLinkAuditCli(options = {}) {
565591566592return result.status ?? 1;
567593} finally {
568-cleanupAnchorAuditDocsDirImpl(anchorDocsDir);
594+if (anchorDocsDir) {
595+cleanupAnchorAuditDocsDirImpl(anchorDocsDir);
596+}
569597mirroredDocsDir.cleanup();
570598}
571599}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。