opencode/services/site/script/docs-file-icons.ts
2026-11-06 00:17:30 -05:00

59 lines
2.7 KiB
TypeScript

// Writes src/component/site/docs-file-icons.ts: the file-type icons the docs use, cut from the
// desktop app's sprite (@opencode/ui), so the site bundles a dozen icons instead of 1,089.
// The names come from the shared mapping (chooseIconName) applied to every code block title in
// the docs content and the filenames on /dev/docs-components, plus each icon's _light variant.
// Run after adding a new kind of file to the docs: bun script/docs-file-icons.ts
import { chooseIconName } from "@opencode/ui/icons/file-type-name"
import { readdirSync } from "node:fs"
import path from "node:path"
const app = path.resolve(import.meta.dir, "..")
const content = path.resolve(app, "src/docs/content")
const sprite = await Bun.file(path.resolve(app, "../../ui/src/components/file-icons/sprite.svg")).text()
const walk = (dir: string): string[] =>
readdirSync(dir, { withFileTypes: true }).flatMap((entry) =>
entry.isDirectory()
? walk(path.join(dir, entry.name))
: entry.name.endsWith(".mdx")
? [path.join(dir, entry.name)]
: [],
)
const titles = (
await Promise.all(
walk(content).map(async (file) => [...(await Bun.file(file).text()).matchAll(/```\w*\s+title="([^"]+)"/g)]),
)
)
.flat()
.map((match) => match[1])
// Filenames and folders shown on the site that aren't titles in the docs content.
const site = [".env", ".github/workflows/opencode.yml", "mise.toml", "install.sh", "count-lines.ts", "release.md"]
const folders = [".opencode", "agents", "commands", "plugins", "tools", ".github", "workflows"]
const names = new Set([
...[...titles, ...site].map((name) => chooseIconName(name, "file", false)),
...folders.map((name) => chooseIconName(name, "directory", false)),
// The fallbacks.
chooseIconName("unknown", "file", false),
chooseIconName("unknown", "directory", false),
])
const symbol = (name: string) => sprite.match(new RegExp(`<symbol([^>]*)id="${name}"([^>]*)>([\\s\\S]*?)</symbol>`))
const icons = [...names, ...[...names].map((name) => `${name}_light`)].flatMap((name) => {
const match = symbol(name)
if (!match) return []
const attrs = `${match[1]}${match[2]}`.replace(/\s+/g, " ").trim()
const body = match[3].replace(/\s+/g, " ").replace(/>\s+</g, "><").trim()
return [[name, `<svg xmlns="http://www.w3.org/2000/svg" ${attrs}>${body}</svg>`] as const]
})
await Bun.write(
path.join(app, "src/component/site/docs-file-icons.ts"),
`// Generated by script/docs-file-icons.ts from @opencode/ui's file-type sprite. Do not edit.
import type { IconName } from "@opencode/ui/icons/file-type"
export const docsFileIcons: Partial<Record<IconName, string>> = {
${icons.map(([name, svg]) => ` ${name}: ${JSON.stringify(svg)},`).join("\n")}
}
`,
)
console.log(`${icons.length} icons: ${icons.map((icon) => icon[0]).join(", ")}`)