// Writes docs/snippets/plugin-catalog.jsx, the snapshot of the plugin marketplace the // documentation site shows, from the release's plugins-index.json (or a local file). // // node scripts/gen-plugin-catalog.ts [index.json | url] import { readFileSync, writeFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; const OFFICIAL_INDEX = "https://github.com/l0ng-ai/openidea/releases/latest/download/plugins-index.json"; const OUT = fileURLToPath(new URL("../docs/snippets/plugin-catalog.jsx", import.meta.url)); interface IndexPlugin { id: string; name: string; version: string; description: string; publisher: string; categories?: string[]; icon?: { light: string | null; dark: string | null } | null; homepage?: string | null; downloads?: number | null; } interface Index { publishers: { id: string; name: string }[]; plugins: IndexPlugin[]; } async function load(source: string): Promise { if (/^https?:\/\//.test(source)) { const res = await fetch(source); if (!res.ok) throw new Error(`${source}: HTTP ${res.status}`); return (await res.json()) as Index; } return JSON.parse(readFileSync(source, "utf8")) as Index; } function compareVersions(a: string, b: string): number { const pa = a.split(/[.-]/).map(Number); const pb = b.split(/[.-]/).map(Number); for (let i = 0; i < Math.max(pa.length, pb.length); i++) { const d = (pa[i] || 0) - (pb[i] || 0); if (d) return d; } return 0; } const svgUri = (svg: string) => `data:image/svg+xml;base64,${Buffer.from(svg).toString("base64")}`; function icon(p: IndexPlugin) { const light = p.icon?.light ?? p.icon?.dark; const dark = p.icon?.dark ?? p.icon?.light; if (!light || !dark) return null; return light === dark ? { light: svgUri(light) } : { light: svgUri(light), dark: svgUri(dark) }; } const index = await load(process.argv[2] ?? OFFICIAL_INDEX); const publishers = new Map(index.publishers.map((p) => [p.id, p.name])); const latest = new Map(); for (const plugin of index.plugins) { const seen = latest.get(plugin.id); if (!seen || compareVersions(plugin.version, seen.version) > 0) latest.set(plugin.id, plugin); } const plugins = [...latest.values()] // API examples document the plugin API; they are not offered to users here. .filter((p) => !/\(API Example\)$/.test(p.name)) .map((p) => ({ id: p.id, name: p.name, version: p.version, description: p.description, publisher: publishers.get(p.publisher) ?? p.publisher, official: p.publisher === "openidea", categories: p.categories?.length ? p.categories : ["other"], downloads: p.downloads ?? 0, homepage: p.homepage ?? null, icon: icon(p), })) .sort((a, b) => b.downloads - a.downloads || a.name.localeCompare(b.name)); const catalog = { generatedAt: new Date().toISOString().slice(0, 10), publishers: new Set(plugins.map((p) => p.publisher)).size, plugins, }; writeFileSync( OUT, `// Generated by scripts/gen-plugin-catalog.ts. Do not edit.\nexport const pluginCatalog = ${JSON.stringify(catalog)};\n`, ); console.log(`${OUT}: ${plugins.length} plugins from ${catalog.publishers} publishers`);