Files
agent-skills/scripts/sync-shared-references.js
T
Anton YakutovichandClaude Opus 5.5 50f1dbc5cb fix(skills): keep generated checklist copies in references/shared/
The copies sat next to each skill's own reference files and filled PR
diffs. They now live in skills/<name>/references/shared/, which holds
only generated files, and .gitattributes marks that directory
linguist-generated so GitHub collapses it in diffs and language stats.
The sync script refuses to overwrite or delete a hand-written file there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XTTwR4HS4sWgZ992ThEwmP
2026-10-19 11:10:55 +04:00

144 lines
5.2 KiB
JavaScript

#!/usr/bin/env node
/**
* sync-shared-references.js
*
* Copies the shared checklists in the repo-root `references/` into each skill
* that links them, as `skills/<name>/references/shared/<file>.md`.
*
* A per-skill install (`npx skills add ... --skill <name>`) copies only
* `skills/<name>/`, so a link to `../../references/<file>.md` breaks after
* install. A copy inside the skill travels with it. Symlinks would avoid the
* copies, but Git for Windows checks them out as text files by default.
*
* The root file stays the one to edit. `shared/` holds only generated copies
* (`.gitattributes` marks it `linguist-generated`), each starting with
* GENERATED_HEADER. `--check` fails when a copy is missing, differs from its
* root file, or is no longer linked, so CI keeps the copies from drifting.
*
* A skill needs a copy when its SKILL.md links `references/shared/<file>.md`,
* or a file in its `references/` links `../references/shared/<file>.md`, and
* the repo-root `references/<file>.md` exists. Links inside fenced code blocks
* are examples and need no copy.
*
* Usage:
* node scripts/sync-shared-references.js write and prune the copies
* node scripts/sync-shared-references.js --check report drift, change nothing
*
* Exit codes: 0 = copies match, 1 = drift (with --check) or a hand-written
* file in `shared/`.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const { stripFencedCodeBlocks } = require('./lib/skill-lint');
const ROOT = path.resolve(__dirname, '..');
const SKILLS_DIR = path.join(ROOT, 'skills');
const SHARED_DIR = path.join(ROOT, 'references');
// The header names no references/ path: the link validator would resolve it.
const GENERATED_MARKER = '<!-- Generated by scripts/sync-shared-references.js';
const GENERATED_HEADER =
`${GENERATED_MARKER} from the repo-root file of the same name. ` +
'Edit that file and rerun the script; do not edit this copy. -->\n\n';
const SKILL_LINK_RE = /(?<![A-Za-z0-9._/-])references\/shared\/([A-Za-z0-9._-]+\.md)/g;
const REFERENCE_FILE_LINK_RE = /(?<![A-Za-z0-9._/-])\.\.\/references\/shared\/([A-Za-z0-9._-]+\.md)/g;
function isGenerated(file) {
return fs.readFileSync(file, 'utf8').startsWith(GENERATED_MARKER);
}
function linkedNames(file, re) {
const content = stripFencedCodeBlocks(fs.readFileSync(file, 'utf8'));
return [...content.matchAll(re)].map((match) => match[1]);
}
// Every copy the skills need, keyed by its path: { file -> content }.
function expectedCopies() {
const copies = new Map();
if (!fs.existsSync(SKILLS_DIR)) return copies;
for (const name of fs.readdirSync(SKILLS_DIR).sort()) {
const skillDir = path.join(SKILLS_DIR, name);
const skillFile = path.join(skillDir, 'SKILL.md');
if (!fs.statSync(skillDir).isDirectory() || !fs.existsSync(skillFile)) continue;
const names = linkedNames(skillFile, SKILL_LINK_RE);
for (const file of markdownFiles(path.join(skillDir, 'references'))) {
names.push(...linkedNames(file, REFERENCE_FILE_LINK_RE));
}
for (const shared of new Set(names)) {
const source = path.join(SHARED_DIR, shared);
if (!fs.existsSync(source)) continue;
copies.set(
path.join(skillDir, 'references', 'shared', shared),
GENERATED_HEADER + fs.readFileSync(source, 'utf8'),
);
}
}
return copies;
}
function markdownFiles(dir) {
if (!fs.existsSync(dir)) return [];
return fs.readdirSync(dir)
.filter((name) => name.endsWith('.md'))
.map((name) => path.join(dir, name));
}
function toPosix(file) {
return path.relative(ROOT, file).split(path.sep).join('/');
}
function refuseHandWritten(file) {
console.log(` ✗ ${toPosix(file)} has no generated header; shared/ holds only copies the script writes.`);
console.log(" Move the skill's own file out of shared/.");
process.exit(1);
}
function main() {
const check = process.argv.includes('--check');
const copies = expectedCopies();
const problems = [];
for (const [file, content] of copies) {
if (!fs.existsSync(file)) {
problems.push(`missing ${toPosix(file)}`);
if (!check) {
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, content);
}
} else if (!isGenerated(file)) {
refuseHandWritten(file);
} else if (fs.readFileSync(file, 'utf8') !== content) {
problems.push(`stale ${toPosix(file)}`);
if (!check) fs.writeFileSync(file, content);
}
}
if (fs.existsSync(SKILLS_DIR)) {
for (const name of fs.readdirSync(SKILLS_DIR).sort()) {
for (const file of markdownFiles(path.join(SKILLS_DIR, name, 'references', 'shared'))) {
if (copies.has(file)) continue;
if (!isGenerated(file)) refuseHandWritten(file);
problems.push(`unused ${toPosix(file)}`);
if (!check) fs.rmSync(file);
}
}
}
for (const problem of problems) console.log(` ${check ? '✗' : '✓'} ${problem}`);
if (check && problems.length > 0) {
console.log(`\n${problems.length} shared-reference copies out of date. Run: node scripts/sync-shared-references.js`);
process.exit(1);
}
console.log(`\n${copies.size} shared-reference copies ${check ? 'match' : 'in sync'}.`);
}
main();