

























@@ -0,0 +1,100 @@
1+#!/usr/bin/env node
2+3+import { execFileSync, spawnSync } from "node:child_process";
4+import fs from "node:fs";
5+import os from "node:os";
6+import path from "node:path";
7+import { repairMintlifyAccordionIndentation } from "./lib/mintlify-accordion.mjs";
8+9+const ROOT = path.resolve(import.meta.dirname, "..");
10+const CHECK = process.argv.includes("--check");
11+const OXFMT_BIN = path.join(ROOT, "node_modules", "oxfmt", "bin", "oxfmt");
12+const OXFMT_CONFIG = path.join(ROOT, ".oxfmtrc.jsonc");
13+14+function docsFiles() {
15+const output = execFileSync("git", ["ls-files", "docs/**/*.md", "docs/**/*.mdx", "README.md"], {
16+cwd: ROOT,
17+encoding: "utf8",
18+});
19+return output.split("\n").filter(Boolean);
20+}
21+22+function runOxfmt(files) {
23+const result = spawnSync(
24+process.execPath,
25+[OXFMT_BIN, "--write", "--threads=1", "--config", OXFMT_CONFIG, ...files],
26+{
27+cwd: ROOT,
28+encoding: "utf8",
29+maxBuffer: 1024 * 1024 * 16,
30+},
31+);
32+33+if (result.status !== 0) {
34+const stderr = result.stderr.trim();
35+throw new Error(`oxfmt failed${stderr ? `:\n${stderr}` : ""}`);
36+}
37+}
38+39+function repairFiles(root, files) {
40+const changed = [];
41+for (const relativePath of files) {
42+const absolutePath = path.join(root, relativePath);
43+const raw = fs.readFileSync(absolutePath, "utf8");
44+const formatted = repairMintlifyAccordionIndentation(raw);
45+if (formatted === raw) {
46+continue;
47+}
48+fs.writeFileSync(absolutePath, formatted);
49+changed.push(relativePath);
50+}
51+return changed;
52+}
53+54+function copyDocsToTemp(files) {
55+const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-docs-format-"));
56+for (const relativePath of files) {
57+const source = path.join(ROOT, relativePath);
58+const target = path.join(tempRoot, relativePath);
59+fs.mkdirSync(path.dirname(target), { recursive: true });
60+fs.copyFileSync(source, target);
61+}
62+return tempRoot;
63+}
64+65+const changed = [];
66+const files = docsFiles();
67+68+if (CHECK) {
69+const tempRoot = copyDocsToTemp(files);
70+try {
71+runOxfmt(files.map((relativePath) => path.join(tempRoot, relativePath)));
72+repairFiles(tempRoot, files);
73+for (const relativePath of files) {
74+const raw = fs.readFileSync(path.join(ROOT, relativePath), "utf8");
75+const formatted = fs.readFileSync(path.join(tempRoot, relativePath), "utf8");
76+if (formatted !== raw) {
77+changed.push(relativePath);
78+}
79+}
80+} finally {
81+fs.rmSync(tempRoot, { recursive: true, force: true });
82+}
83+} else {
84+runOxfmt(files);
85+changed.push(...repairFiles(ROOT, files));
86+}
87+88+if (CHECK && changed.length > 0) {
89+console.error(`Format issues found in ${changed.length} docs file(s):`);
90+for (const relativePath of changed) {
91+console.error(`- ${relativePath}`);
92+}
93+process.exit(1);
94+}
95+96+if (changed.length > 0) {
97+console.log(`Formatted ${changed.length} docs file(s).`);
98+} else {
99+console.log(`Docs formatting clean (${files.length} files).`);
100+}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。