|
| 1 | +#!/usr/bin/env node |
| 2 | +// Reads all skills SKILL.md files, parses YAML frontmatter, |
| 3 | +// and writes src/skills-data.js so the site auto-discovers skills. |
| 4 | +// Run: node scripts/generate-skills.js |
| 5 | + |
| 6 | +import { readdirSync, readFileSync, writeFileSync, statSync } from "fs"; |
| 7 | +import { execFileSync } from "child_process"; |
| 8 | +import { join, dirname } from "path"; |
| 9 | +import { fileURLToPath } from "url"; |
| 10 | + |
| 11 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 12 | +const ROOT = join(__dirname, ".."); |
| 13 | +const SKILLS_DIR = join(ROOT, "skills"); |
| 14 | +const OUTPUT = join(ROOT, "src", "skills-data.js"); |
| 15 | + |
| 16 | +function parseFrontmatter(content) { |
| 17 | + const match = content.match(/^---\n([\s\S]*?)\n---/); |
| 18 | + if (!match) return null; |
| 19 | + |
| 20 | + const yaml = match[1]; |
| 21 | + const data = {}; |
| 22 | + |
| 23 | + for (const line of yaml.split("\n")) { |
| 24 | + const idx = line.indexOf(":"); |
| 25 | + if (idx === -1) continue; |
| 26 | + const key = line.slice(0, idx).trim(); |
| 27 | + let val = line.slice(idx + 1).trim(); |
| 28 | + |
| 29 | + // Parse arrays: [a, b, c] |
| 30 | + if (val.startsWith("[") && val.endsWith("]")) { |
| 31 | + val = val.slice(1, -1).split(",").map((s) => s.trim()).filter(Boolean); |
| 32 | + } |
| 33 | + // Parse numbers |
| 34 | + else if (/^\d+$/.test(val)) { |
| 35 | + val = parseInt(val, 10); |
| 36 | + } |
| 37 | + // Strip quotes |
| 38 | + else if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) { |
| 39 | + val = val.slice(1, -1); |
| 40 | + } |
| 41 | + |
| 42 | + data[key] = val; |
| 43 | + } |
| 44 | + |
| 45 | + return data; |
| 46 | +} |
| 47 | + |
| 48 | +// Use git log for accurate dates (file mtime is unreliable on CI) |
| 49 | +function getLastUpdated(filePath) { |
| 50 | + try { |
| 51 | + const date = execFileSync("git", ["log", "-1", "--format=%cs", "--", filePath], { cwd: ROOT, encoding: "utf-8" }).trim(); |
| 52 | + if (date) return date; |
| 53 | + } catch {} |
| 54 | + // Fallback to file mtime if not in a git repo |
| 55 | + return statSync(filePath).mtime.toISOString().split("T")[0]; |
| 56 | +} |
| 57 | + |
| 58 | +const dirs = readdirSync(SKILLS_DIR).filter((d) => { |
| 59 | + try { |
| 60 | + return statSync(join(SKILLS_DIR, d, "SKILL.md")).isFile(); |
| 61 | + } catch { |
| 62 | + return false; |
| 63 | + } |
| 64 | +}); |
| 65 | + |
| 66 | +const skills = []; |
| 67 | +const errors = []; |
| 68 | + |
| 69 | +for (const dir of dirs) { |
| 70 | + const filePath = join(SKILLS_DIR, dir, "SKILL.md"); |
| 71 | + const content = readFileSync(filePath, "utf-8"); |
| 72 | + const meta = parseFrontmatter(content); |
| 73 | + |
| 74 | + if (!meta) { |
| 75 | + errors.push(`${dir}/SKILL.md: missing YAML frontmatter (---)`); |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + const missing = ["id", "name", "category"].filter((f) => !meta[f]); |
| 80 | + if (missing.length) { |
| 81 | + errors.push(`${dir}/SKILL.md: missing required fields: ${missing.join(", ")}`); |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + skills.push({ |
| 86 | + id: meta.id, |
| 87 | + name: meta.name, |
| 88 | + category: meta.category, |
| 89 | + description: meta.description || "", |
| 90 | + endpoints: meta.endpoints || 0, |
| 91 | + lastUpdated: getLastUpdated(filePath), |
| 92 | + version: meta.version || "1.0.0", |
| 93 | + status: meta.status || "stable", |
| 94 | + dependencies: Array.isArray(meta.dependencies) ? meta.dependencies : [], |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +if (errors.length) { |
| 99 | + console.error("ERRORS in skill files:"); |
| 100 | + errors.forEach((e) => console.error(` - ${e}`)); |
| 101 | + process.exit(1); |
| 102 | +} |
| 103 | + |
| 104 | +// Sort alphabetically by name for consistent output |
| 105 | +skills.sort((a, b) => a.name.localeCompare(b.name)); |
| 106 | + |
| 107 | +const output = `// Auto-generated by scripts/generate-skills.js — do not edit manually |
| 108 | +// To update: edit the YAML frontmatter in skills/*/SKILL.md and rebuild |
| 109 | +export const SKILLS = ${JSON.stringify(skills, null, 2)}; |
| 110 | +`; |
| 111 | + |
| 112 | +writeFileSync(OUTPUT, output); |
| 113 | +console.log(`Generated ${skills.length} skills -> src/skills-data.js`); |
0 commit comments