-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsync-skill-metadata.ts
More file actions
64 lines (53 loc) · 2.34 KB
/
Copy pathsync-skill-metadata.ts
File metadata and controls
64 lines (53 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Syncs frontmatter `metadata.version` for every `skills/<member>/SKILL.md` from
* `packages/cli/package.json` (single source of truth for CLI release version).
*
* Run: pnpm --filter bailian-cli run sync:skill-version
* Invoked via `pnpm run sync:skill-assets` or the repo pre-commit hook (see `.vite-hooks/pre-commit`).
*/
import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const VERSION_LINE_RE = /^(\s*version:\s*")[^"]*("\s*)$/m;
const __dirname = dirname(fileURLToPath(import.meta.url));
const PKG_PATH = join(__dirname, "../packages/cli/package.json");
const SKILLS_DIR = join(__dirname, "../skills");
const { version } = JSON.parse(readFileSync(PKG_PATH, "utf-8")) as { version: string };
const skillPaths = readdirSync(SKILLS_DIR, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.map((entry) => join(SKILLS_DIR, entry.name, "SKILL.md"))
.filter((skillPath) => existsSync(skillPath));
if (skillPaths.length === 0) {
throw new Error("skills/: no <member>/SKILL.md found");
}
for (const skillPath of skillPaths) {
const relPath = skillPath.slice(join(__dirname, "..").length + 1);
const body = readFileSync(skillPath, "utf-8");
const lines = body.split(/\r?\n/);
if (lines[0] !== "---") {
throw new Error(`${relPath}: must start with --- YAML frontmatter`);
}
const closeIdx = lines.findIndex((line, i) => i > 0 && line === "---");
if (closeIdx === -1) {
throw new Error(`${relPath}: missing closing --- frontmatter delimiter`);
}
const frontmatterLines = lines.slice(0, closeIdx + 1);
const restLines = lines.slice(closeIdx + 1);
const frontmatter = frontmatterLines.join("\n");
if (!VERSION_LINE_RE.test(frontmatter)) {
throw new Error(
`${relPath}: could not find metadata.version (expected a line like \` version: "…"\` in frontmatter)`,
);
}
const updatedFrontmatter = frontmatter.replace(
VERSION_LINE_RE,
(_m, g1: string, g2: string) => `${g1}${version}${g2}`,
);
const newBody = updatedFrontmatter + "\n" + restLines.join("\n");
if (newBody !== body) {
writeFileSync(skillPath, newBody, "utf-8");
console.log(`Synced ${relPath} metadata.version → ${version}`);
} else {
console.log(`${relPath} metadata.version already ${version}`);
}
}