#!/usr/bin/env node // iapl-blocks — the block library's command line. // pnpm exec iapl-blocks list → the library blocks // pnpm exec iapl-blocks eject faq → src/blocks/Faq/{config.ts,Component.tsx} // pnpm exec iapl-blocks eject faq --dest src/blocks --dry-run // Eject copies one block's source into the project, with its imports pointing at the // package entries, so the project owns it: same slug and data shape, no migration. // Then: use the ejected builder in Pages.layout instead of the library one, pass the ejected // component as createBlockRegistry({ components: { faq: FaqBlock } }), record it in AGENTS.md. import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs' import { dirname, join, relative, resolve } from 'node:path' import { fileURLToPath } from 'node:url' const args = process.argv.slice(2) const [command, slug] = args const dryRun = args.includes('--dry-run') const destArg = args.indexOf('--dest') const dest = resolve(process.cwd(), destArg >= 0 ? args[destArg + 1] : 'src/blocks') const pkgRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..') const libRoot = join(pkgRoot, 'src/modules/block-library') const blocksRoot = join(libRoot, 'blocks') if (!command || command === '--help' || command === '-h') { console.log('Usage: iapl-blocks list | iapl-blocks eject [--dest src/blocks] [--dry-run]') process.exit(0) } const slugs = existsSync(blocksRoot) ? readdirSync(blocksRoot).sort() : [] if (command === 'list') { console.log(slugs.join('\n')) process.exit(0) } if (command !== 'eject' || !slug) { console.error('Usage: iapl-blocks eject ') process.exit(1) } if (!slugs.includes(slug)) { console.error(`Unknown block "${slug}". Blocks: ${slugs.join(', ')}`) process.exit(1) } /** * Where an import inside the library goes in a project, by the file it points at. * Config files (no React) take the Payload-side entries; components take /rsc and /client. */ const ROOT = '@intecion/ipal-kit' const BLOCKS = '@intecion/ipal-kit/blocks' const RSC = '@intecion/ipal-kit/blocks/rsc' function target(file, isComponent) { const rel = relative(join(pkgRoot, 'src/modules'), file).replace(/\\/g, '/').replace(/\.js$/, '') if (rel.startsWith('block-library/')) { const inner = rel.slice('block-library/'.length) if (/^(define|fields|options|shared|types|faq|library|manifest|thumbnails)$/.test(inner)) {return BLOCKS} return RSC // context, data, texts, ui/* } if (rel === 'lightbox/LightboxProvider' || rel === 'richtext/TableOfContents') {return '@intecion/ipal-kit/client'} if (rel === 'forms/RenderForm' || rel === 'richtext/RenderRichText') {return '@intecion/ipal-kit/rsc'} if (rel === 'richtext/headings') {return isComponent ? '@intecion/ipal-kit/rsc' : ROOT} return ROOT } /** Rewrites relative imports to package entries and merges imports of the same entry. */ function rewrite(source, file) { const isComponent = file.endsWith('.tsx') const merged = new Map() const body = source.replace(/^import (type )?\{([^}]*)\} from '(\.[^']+)'\n/gm, (_, type, names, spec) => { const entry = target(resolve(dirname(file), spec), isComponent) const key = `${type ? 'type ' : ''}${entry}` const set = merged.get(key) ?? new Set() for (const n of names.split(',').map((s) => s.trim()).filter(Boolean)) {set.add(n)} merged.set(key, set) return '' }) const lines = [...merged].map(([key, set]) => { const [type, entry] = key.startsWith('type ') ? ['type ', key.slice(5)] : ['', key] return `import ${type}{ ${[...set].sort().join(', ')} } from '${entry}'` }) return body.replace(/^((?:import [^\n]*\n)*)/, (head) => `${head}${lines.join('\n')}\n`).replace(/\n{3,}/g, '\n\n') } const folder = slug[0].toUpperCase() + slug.slice(1) const outDir = join(dest, folder) const files = readdirSync(join(blocksRoot, slug)).filter((f) => /\.(ts|tsx)$/.test(f)) for (const name of files) { const from = join(blocksRoot, slug, name) const to = join(outDir, name) if (existsSync(to)) { console.error(`skip ${relative(process.cwd(), to)} (exists — delete it first to eject again)`) continue } const out = `// Ejected from @intecion/ipal-kit (block library: ${slug}). The project owns this file now.\n${rewrite(readFileSync(from, 'utf8'), from)}` console.log(`${dryRun ? 'would write' : 'write'} ${relative(process.cwd(), to)}`) if (!dryRun) { mkdirSync(outDir, { recursive: true }) writeFileSync(to, out) } } console.log(`\nNext: Pages.layout → the ejected build${folder}Block; registry → components: { ${slug}: ${folder}Block }; AGENTS.md → "Project decisions".`)