|
| 1 | +/** |
| 2 | + * Syncs `skills/bailian-cli/SKILL.md` frontmatter `metadata.version` from |
| 3 | + * `packages/cli/package.json` (single source of truth for CLI release version). |
| 4 | + * |
| 5 | + * Run: pnpm --filter bailian-cli run sync:skill-version |
| 6 | + * Invoked automatically by `pnpm --filter bailian-cli run build`. |
| 7 | + */ |
| 8 | +import { readFileSync, writeFileSync } from "node:fs"; |
| 9 | +import { dirname, join } from "node:path"; |
| 10 | +import { fileURLToPath } from "node:url"; |
| 11 | + |
| 12 | +const VERSION_LINE_RE = /^(\s*version:\s*")[^"]*("\s*)$/m; |
| 13 | + |
| 14 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 15 | +const PKG_PATH = join(__dirname, "../packages/cli/package.json"); |
| 16 | +const SKILL_PATH = join(__dirname, "../skills/bailian-cli/SKILL.md"); |
| 17 | + |
| 18 | +const { version } = JSON.parse(readFileSync(PKG_PATH, "utf-8")) as { version: string }; |
| 19 | +const body = readFileSync(SKILL_PATH, "utf-8"); |
| 20 | + |
| 21 | +const lines = body.split(/\r?\n/); |
| 22 | +if (lines[0] !== "---") { |
| 23 | + throw new Error("skills/bailian-cli/SKILL.md: must start with --- YAML frontmatter"); |
| 24 | +} |
| 25 | +const closeIdx = lines.findIndex((line, i) => i > 0 && line === "---"); |
| 26 | +if (closeIdx === -1) { |
| 27 | + throw new Error("skills/bailian-cli/SKILL.md: missing closing --- frontmatter delimiter"); |
| 28 | +} |
| 29 | + |
| 30 | +const frontmatterLines = lines.slice(0, closeIdx + 1); |
| 31 | +const restLines = lines.slice(closeIdx + 1); |
| 32 | +const frontmatter = frontmatterLines.join("\n"); |
| 33 | + |
| 34 | +if (!VERSION_LINE_RE.test(frontmatter)) { |
| 35 | + throw new Error( |
| 36 | + 'skills/bailian-cli/SKILL.md: could not find metadata.version (expected a line like ` version: "…"` in frontmatter)', |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +const updatedFrontmatter = frontmatter.replace( |
| 41 | + VERSION_LINE_RE, |
| 42 | + (_m, g1: string, g2: string) => `${g1}${version}${g2}`, |
| 43 | +); |
| 44 | + |
| 45 | +const newBody = updatedFrontmatter + "\n" + restLines.join("\n"); |
| 46 | +if (newBody !== body) { |
| 47 | + writeFileSync(SKILL_PATH, newBody, "utf-8"); |
| 48 | + console.log(`Synced skills/bailian-cli/SKILL.md metadata.version → ${version}`); |
| 49 | +} else { |
| 50 | + console.log(`skills/bailian-cli/SKILL.md metadata.version already ${version}`); |
| 51 | +} |
0 commit comments