#!/usr/bin/env node // ipal-skills — copies the ipal-kit agent skills into the project. // pnpm ipal-skills → ./.agents/skills/ipal-* // pnpm ipal-skills --dry-run → show what would change // pnpm ipal-skills --dest .claude/skills // pnpm ipal-skills --no-pointer → skills only, leave AGENTS.md / CLAUDE.md alone // Only folders named ipal-* are touched; the project's own skills stay as they are. // ipal-* folders that no longer exist in the package are removed (renamed/retired skills). // AGENTS.md (and CLAUDE.md) get a marked ipal-kit block pointing at the guide — only the // text between the markers is ever rewritten; the project's own content stays as it is. import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs' import { dirname, join, relative, resolve } from 'node:path' import { fileURLToPath } from 'node:url' const args = process.argv.slice(2) if (args.includes('--help') || args.includes('-h')) { console.log('Usage: ipal-skills [--dest ] [--dry-run] [--no-pointer]\nDefault dest: .agents/skills') process.exit(0) } const dryRun = args.includes('--dry-run') const pointer = !args.includes('--no-pointer') const destArg = args.indexOf('--dest') const destValue = destArg >= 0 ? args[destArg + 1] : '.agents/skills' // A missing value would resolve to cwd and remove every ipal-* folder there. if (!destValue || destValue.startsWith('-')) { console.error('ipal-skills: --dest needs a directory, e.g. --dest .claude/skills') process.exit(1) } const dest = resolve(process.cwd(), destValue) if (dest === resolve(process.cwd())) { console.error('ipal-skills: --dest must be a subdirectory, not the project root') process.exit(1) } const pkgRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..') const src = join(pkgRoot, 'docs', 'agent', 'skills') if (!existsSync(src)) { console.error(`ipal-skills: no skills found in ${src} — is @intecion/ipal-kit installed with docs?`) process.exit(1) } const isSkillDir = (base, name) => name.startsWith('ipal-') && statSync(join(base, name)).isDirectory() const shipped = readdirSync(src).filter((n) => isSkillDir(src, n)) const existing = existsSync(dest) ? readdirSync(dest).filter((n) => isSkillDir(dest, n)) : [] const retired = existing.filter((n) => !shipped.includes(n)) if (!dryRun) mkdirSync(dest, { recursive: true }) for (const name of shipped) { const action = existing.includes(name) ? 'update' : 'add ' console.log(`${action} ${name}`) if (!dryRun) { rmSync(join(dest, name), { recursive: true, force: true }) // drop files removed upstream cpSync(join(src, name), join(dest, name), { recursive: true }) } } for (const name of retired) { console.log(`remove ${name} (no longer in ipal-kit)`) if (!dryRun) rmSync(join(dest, name), { recursive: true, force: true }) } console.log(`${dryRun ? '[dry-run] ' : ''}${shipped.length} skill(s) → ${dest}`) // Pointer block: an agent opening the project must find the guide without being told. // AGENTS.md is read by Codex/Cursor/Antigravity, CLAUDE.md by Claude Code (which also // follows `@AGENTS.md` imports, so a CLAUDE.md that imports AGENTS.md needs no copy). if (pointer) { const START = '' const END = '' const version = JSON.parse(readFileSync(join(pkgRoot, 'package.json'), 'utf8')).version const skillsDir = relative(process.cwd(), dest).split('\\').join('/') const block = [ START, `## ipal-kit ${version} — agent guide`, '', '', 'This project is built on `@intecion/ipal-kit` (a Payload CMS 3 plugin). Before you write code:', '', '1. Rules: `node_modules/@intecion/ipal-kit/docs/agent/AGENT.md`.', `2. Everything the plugin provides (options, env vars, routes, layout, hooks, collections, globals, every export): the \`ipal-reference\` skill — \`${skillsDir}/ipal-reference/SKILL.md\`.`, `3. New project or missing foundation: the \`ipal-setup\` skill; a whole site: \`ipal-site\`. All skills: \`${skillsDir}/\`.`, '4. Module details: `node_modules/@intecion/ipal-kit/docs/.md`.', '5. If the plugin has it, use it. If an option is missing, use the `ipal-plugin-gap` skill — never patch `node_modules`.', '6. ipal-kit docs, this guide and the skills are in English; write rules, AGENTS.md entries and gap reports in English.', END, ].join('\n') const upsert = (file, { create }) => { const path = join(process.cwd(), file) const text = existsSync(path) ? readFileSync(path, 'utf8') : null let next if (text === null) { if (!create) return next = create === 'import' ? '@AGENTS.md\n' : `# ${file.replace('.md', '')}\n\n${block}\n` } else if (text.includes(START) && text.includes(END)) { next = text.slice(0, text.indexOf(START)) + block + text.slice(text.indexOf(END) + END.length) } else { next = `${text.replace(/\s*$/, '')}\n\n${block}\n` } if (next === text) return console.log(`ok ${file}`) console.log(`${text === null ? 'create' : 'update'} ${file}`) if (!dryRun) writeFileSync(path, next) } upsert('AGENTS.md', { create: 'block' }) const claude = join(process.cwd(), 'CLAUDE.md') const importsAgents = existsSync(claude) && /^@AGENTS\.md\s*$/m.test(readFileSync(claude, 'utf8')) // CLAUDE.md that imports AGENTS.md already sees the block — don't duplicate it. if (!importsAgents) upsert('CLAUDE.md', { create: 'import' }) } // Skills ship .ts/.tsx templates and scripts. A Next tsconfig ("include": ["**/*.tsx"]) would // type-check them as project code and break `tsc` / `next build` → they must be excluded. const tsconfig = join(process.cwd(), 'tsconfig.json') const top = relative(process.cwd(), dest).split(/[\\/]/)[0] if (existsSync(tsconfig) && top && !top.startsWith('..')) { const text = readFileSync(tsconfig, 'utf8') const exclude = text.match(/"exclude"\s*:\s*\[([^\]]*)\]/)?.[1] ?? '' const entries = [...exclude.matchAll(/"([^"]*)"/g)].map((m) => m[1].replace(/^\.\//, '').replace(/\/(\*\*)?$/, '')) if (!entries.includes(top)) { console.warn(`\n⚠ tsconfig.json does not exclude "${top}" — skill templates would be type-checked and break the build.`) console.warn(` Add it: "exclude": ["node_modules", "${top}"]`) process.exitCode = 1 } }