import { spawnSync } from "node:child_process"; import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync, } from "node:fs"; import { basename, dirname, extname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { tmpdir } from "node:os"; // Resolve paths relative to this script so it works regardless of CWD. const siteRoot = resolve(dirname(fileURLToPath(import.meta.url)), ".."); const repoRoot = resolve(siteRoot, ".."); const outDir = join(siteRoot, "src/content/docs/reference/commands"); // Prefer the repo-root build output (`go build -o tg ./cmd/tg`), else PATH. const localBin = join(repoRoot, "tg"); const tg = existsSync(localBin) ? localBin : "tg"; function run(args) { return spawnSync(tg, args, { encoding: "utf8" }); } const canary = run(["--help"]); if (canary.error || canary.status !== 0) { console.error( `gen-commands: could not run ${tg} — build it first with \`go build -o tg ./cmd/tg\` from the repo root, or install tg on your PATH.`, ); if (canary.stderr) console.error(canary.stderr.trim()); process.exit(1); } // Stage raw Cobra markdown in a temp dir, then transform into Starlight content. const stage = mkdtempSync(join(tmpdir(), "tg-commands-")); const gen = run(["man", stage, "--markdown"]); if (gen.error || gen.status !== 0) { console.error("gen-commands: `tg man --markdown` failed:"); if (gen.stderr) console.error(gen.stderr.trim()); rmSync(stage, { recursive: true, force: true }); process.exit(1); } rmSync(outDir, { recursive: true, force: true }); mkdirSync(outDir, { recursive: true }); // SEE ALSO links like [tg auth](tg_auth.md) -> absolute Starlight routes. const seeAlsoRe = /\((tg[\w-]*)\.md\)/g; // Strip the non-deterministic "Auto generated by spf13/cobra on " footer. const autoTagRe = /^###### Auto generated by spf13\/cobra.*$\n?/m; function yaml(s) { return `"${String(s).replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`; } let count = 0; for (const file of readdirSync(stage)) { if (extname(file) !== ".md") continue; let body = readFileSync(join(stage, file), "utf8").replace(autoTagRe, ""); let title = basename(file, ".md").replace(/_/g, " "); let description = ""; // Pull the title + short description from Cobra's leading "## \n\n". const lines = body.split("\n"); let i = 0; while (i < lines.length && !lines[i].startsWith("## ")) i++; if (i < lines.length) { title = lines[i].slice(3).trim(); i++; // drop the heading; the frontmatter title renders as H1 while (i < lines.length && lines[i].trim() === "") i++; if (i < lines.length && !lines[i].startsWith("#")) { description = lines[i].trim(); body = lines.slice(i).join("\n"); } else { body = lines.slice(i).join("\n"); } } body = body.replace(seeAlsoRe, "(/reference/commands/$1/)"); const frontmatter = [ "---", `title: ${yaml(title)}`, description ? `description: ${yaml(description)}` : null, "---", "", ] .filter(Boolean) .join("\n"); writeFileSync(join(outDir, file), `${frontmatter}\n${body}`); count++; } rmSync(stage, { recursive: true, force: true }); console.log( `gen-commands: wrote ${count} command pages to src/content/docs/reference/commands/`, );