|
| 1 | +#!/usr/bin/env node |
| 2 | +// Mirrors every skills/<name>/SKILL.md to static/ so Docusaurus publishes them |
| 3 | +// at the site root: |
| 4 | +// skills/greptimedb-quickstart/SKILL.md -> static/SKILL.md |
| 5 | +// -> static/skills/greptimedb-quickstart/SKILL.md |
| 6 | +// skills/<other>/SKILL.md -> static/skills/<other>/SKILL.md |
| 7 | +// |
| 8 | +// The root /SKILL.md is the agent-onboarding entrypoint (copied from |
| 9 | +// greptimedb-quickstart). The /skills/<name>/SKILL.md endpoints let agents |
| 10 | +// fetch sister skills directly from the docs site instead of guessing GitHub |
| 11 | +// paths. |
| 12 | +// |
| 13 | +// Runs as a prestart / prebuild npm hook. Never edit anything under |
| 14 | +// static/SKILL.md or static/skills/ by hand. |
| 15 | + |
| 16 | +import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 17 | +import { dirname, resolve } from 'node:path'; |
| 18 | +import { fileURLToPath } from 'node:url'; |
| 19 | + |
| 20 | +const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..'); |
| 21 | +const skillsDir = resolve(repoRoot, 'skills'); |
| 22 | +const staticDir = resolve(repoRoot, 'static'); |
| 23 | +const ROOT_SKILL = 'greptimedb-quickstart'; |
| 24 | + |
| 25 | +// Wipe previous outputs so a renamed or deleted skill does not leave a stale |
| 26 | +// SKILL.md behind under static/skills/. |
| 27 | +const staticSkillsDir = resolve(staticDir, 'skills'); |
| 28 | +const staticRootSkill = resolve(staticDir, 'SKILL.md'); |
| 29 | +if (existsSync(staticSkillsDir)) rmSync(staticSkillsDir, { recursive: true, force: true }); |
| 30 | +if (existsSync(staticRootSkill)) rmSync(staticRootSkill, { force: true }); |
| 31 | + |
| 32 | +// Load version variables from variables/variables-<latestStable>.ts and resolve |
| 33 | +// VAR::name placeholders in each skill, mirroring what llms-txt-generator.ts |
| 34 | +// does for the .md endpoints. Without this, version-pinned commands in |
| 35 | +// SKILL.md (Docker tag, install.sh argument) would ship the literal |
| 36 | +// "VAR::greptimedbVersion" string to agents. |
| 37 | +const latestVersion = JSON.parse(readFileSync(resolve(repoRoot, 'versions.json'), 'utf8'))[0]; |
| 38 | +const variables = loadVariables(latestVersion); |
| 39 | + |
| 40 | +function loadVariables(version) { |
| 41 | + const file = resolve(repoRoot, 'variables', `variables-${version}.ts`); |
| 42 | + if (!existsSync(file)) { |
| 43 | + console.warn(`[sync-skill] variables-${version}.ts not found; VAR:: placeholders will not be resolved`); |
| 44 | + return {}; |
| 45 | + } |
| 46 | + const text = readFileSync(file, 'utf8'); |
| 47 | + const out = {}; |
| 48 | + for (const m of text.matchAll(/(\w+):\s*['"]([^'"]+)['"]/g)) { |
| 49 | + out[m[1]] = m[2]; |
| 50 | + } |
| 51 | + return out; |
| 52 | +} |
| 53 | + |
| 54 | +function resolveVariables(content) { |
| 55 | + return content.replace(/VAR::([A-Z_]+)/gi, (match, name) => variables[name] ?? match); |
| 56 | +} |
| 57 | + |
| 58 | +// Inject the breadcrumb as a YAML comment inside the frontmatter so strict |
| 59 | +// frontmatter parsers (Anthropic Skill loaders, etc.) still see `---` as the |
| 60 | +// first byte of the file. Falls back to an HTML comment prefix when the |
| 61 | +// source has no frontmatter. |
| 62 | +function withBanner(body, srcRel) { |
| 63 | + const yamlComment = `# Generated from ${srcRel}. Do not edit by hand.\n`; |
| 64 | + const match = body.match(/^---\r?\n/); |
| 65 | + if (match) { |
| 66 | + return body.slice(0, match[0].length) + yamlComment + body.slice(match[0].length); |
| 67 | + } |
| 68 | + return `<!-- Generated from ${srcRel}. Do not edit by hand. -->\n` + body; |
| 69 | +} |
| 70 | + |
| 71 | +function writeWithBanner(srcAbs, dstAbs, srcRel) { |
| 72 | + mkdirSync(dirname(dstAbs), { recursive: true }); |
| 73 | + const body = resolveVariables(readFileSync(srcAbs, 'utf8')); |
| 74 | + writeFileSync(dstAbs, withBanner(body, srcRel), 'utf8'); |
| 75 | + console.log(`[sync-skill] ${srcRel} -> ${dstAbs.slice(repoRoot.length + 1)}`); |
| 76 | +} |
| 77 | + |
| 78 | +for (const name of readdirSync(skillsDir, { withFileTypes: true })) { |
| 79 | + if (!name.isDirectory()) continue; |
| 80 | + const src = resolve(skillsDir, name.name, 'SKILL.md'); |
| 81 | + const srcRel = `skills/${name.name}/SKILL.md`; |
| 82 | + try { |
| 83 | + readFileSync(src); |
| 84 | + } catch { |
| 85 | + continue; |
| 86 | + } |
| 87 | + writeWithBanner(src, resolve(staticDir, 'skills', name.name, 'SKILL.md'), srcRel); |
| 88 | + if (name.name === ROOT_SKILL) { |
| 89 | + writeWithBanner(src, resolve(staticDir, 'SKILL.md'), srcRel); |
| 90 | + } |
| 91 | +} |
0 commit comments