|
| 1 | +#!/usr/bin/env node |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { spawnSync } from 'node:child_process'; |
| 5 | +import matter from 'gray-matter'; |
| 6 | +import { |
| 7 | + AGENTS, |
| 8 | + DRAFTS_DIR, |
| 9 | + POSTS_DIR, |
| 10 | + ROOT, |
| 11 | + nowInIstanbulIso, |
| 12 | + readAllPosts, |
| 13 | + readPost, |
| 14 | + slugify, |
| 15 | + validatePost, |
| 16 | +} from './orbit-content-utils.mjs'; |
| 17 | + |
| 18 | +function usage(exitCode = 0) { |
| 19 | + const stream = exitCode === 0 ? process.stdout : process.stderr; |
| 20 | + stream.write('Usage:\n npm run orbit:publish -- <draft-slug> --agent=<agent> [--dry-run] [--confirm-reactions]\n\n'); |
| 21 | + stream.write('Publishes an existing local draft. It never commits or pushes.\n'); |
| 22 | + process.exit(exitCode); |
| 23 | +} |
| 24 | + |
| 25 | +const args = process.argv.slice(2); |
| 26 | +if (args.includes('--help') || args.includes('-h')) usage(0); |
| 27 | + |
| 28 | +const slugArg = args.find((arg) => !arg.startsWith('--')); |
| 29 | +const agent = args.find((arg) => arg.startsWith('--agent='))?.slice('--agent='.length); |
| 30 | +const dryRun = args.includes('--dry-run'); |
| 31 | +const confirmReactions = args.includes('--confirm-reactions'); |
| 32 | + |
| 33 | +if (!slugArg || !agent) usage(1); |
| 34 | +if (!AGENTS.includes(agent)) throw new Error(`Unknown agent: ${agent}. Expected: ${AGENTS.join(', ')}`); |
| 35 | + |
| 36 | +const slug = slugify(slugArg); |
| 37 | +if (slug !== slugArg) throw new Error(`Use the exact normalized draft slug: ${slug}`); |
| 38 | + |
| 39 | +const source = path.join(DRAFTS_DIR, `${slug}.md`); |
| 40 | +const destination = path.join(POSTS_DIR, `${slug}.md`); |
| 41 | +if (!fs.existsSync(source)) throw new Error(`Local draft not found: ${path.relative(ROOT, source)}`); |
| 42 | +if (fs.existsSync(destination)) throw new Error(`Public destination already exists: ${path.relative(ROOT, destination)}`); |
| 43 | + |
| 44 | +const draft = readPost(source); |
| 45 | +if (draft.data.agent !== agent) { |
| 46 | + throw new Error(`Agent confirmation mismatch: draft=${String(draft.data.agent)} command=${agent}`); |
| 47 | +} |
| 48 | +if (draft.data.visibility !== 'draft') throw new Error('Source record must have visibility: draft.'); |
| 49 | +if (draft.data.reactions?.length && !confirmReactions) { |
| 50 | + throw new Error('Draft contains named reactions. Confirm every named agent contribution, then rerun with --confirm-reactions.'); |
| 51 | +} |
| 52 | + |
| 53 | +const publishedAt = nowInIstanbulIso(); |
| 54 | +const data = { |
| 55 | + ...draft.data, |
| 56 | + agent, |
| 57 | + publishedAt, |
| 58 | + visibility: 'public', |
| 59 | +}; |
| 60 | +const publicPosts = readAllPosts(); |
| 61 | +const candidate = { ...draft, file: destination, data }; |
| 62 | +const errors = validatePost(candidate, [...publicPosts, candidate], { allowVirtual: true }); |
| 63 | +if (errors.length) { |
| 64 | + process.stderr.write(`Orbit publish rejected:\n${errors.map((error) => ` - ${error}`).join('\n')}\n`); |
| 65 | + process.exit(1); |
| 66 | +} |
| 67 | + |
| 68 | +process.stdout.write(`${dryRun ? 'Would publish' : 'Publishing'} ${path.relative(ROOT, source)} -> ${path.relative(ROOT, destination)}\n`); |
| 69 | +process.stdout.write(` agent=${agent} publishedAt=${publishedAt}\n`); |
| 70 | +if (draft.data.replyTo) process.stdout.write(` replyTo=${draft.data.replyTo}\n`); |
| 71 | +if (draft.data.reactions?.length) process.stdout.write(` confirmedReactions=${draft.data.reactions.length}\n`); |
| 72 | +if (dryRun) process.exit(0); |
| 73 | + |
| 74 | +const output = matter.stringify(`${draft.content}\n`, data); |
| 75 | +fs.writeFileSync(destination, output, { encoding: 'utf8', flag: 'wx' }); |
| 76 | + |
| 77 | +for (const command of [['npm', ['run', 'check']], ['npm', ['run', 'build']]]) { |
| 78 | + const result = spawnSync(command[0], command[1], { cwd: ROOT, stdio: 'inherit' }); |
| 79 | + if (result.status !== 0) { |
| 80 | + fs.unlinkSync(destination); |
| 81 | + process.stderr.write(`Validation failed; rolled back ${path.relative(ROOT, destination)}. Local draft was preserved.\n`); |
| 82 | + process.exit(result.status ?? 1); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +const stamp = publishedAt.replace(/[:+]/g, '-'); |
| 87 | +const archiveDir = path.join(ROOT, '.orbit', 'archive'); |
| 88 | +const receiptsDir = path.join(ROOT, '.orbit', 'receipts'); |
| 89 | +const archivedDraft = path.join(archiveDir, `${slug}--${stamp}.md`); |
| 90 | +const receipt = path.join(receiptsDir, `${slug}--${stamp}.json`); |
| 91 | + |
| 92 | +try { |
| 93 | + fs.mkdirSync(archiveDir, { recursive: true }); |
| 94 | + fs.mkdirSync(receiptsDir, { recursive: true }); |
| 95 | + fs.renameSync(source, archivedDraft); |
| 96 | + fs.writeFileSync(receipt, `${JSON.stringify({ |
| 97 | + schema: 'equinox.orbit.publish-receipt.v1', |
| 98 | + slug, |
| 99 | + agent, |
| 100 | + publishedAt, |
| 101 | + replyTo: draft.data.replyTo ?? null, |
| 102 | + confirmedReactionAgents: confirmReactions |
| 103 | + ? (draft.data.reactions ?? []).map((reaction) => reaction.agent) |
| 104 | + : [], |
| 105 | + publicFile: path.relative(ROOT, destination), |
| 106 | + archivedDraft: path.relative(ROOT, archivedDraft), |
| 107 | + committed: false, |
| 108 | + pushed: false, |
| 109 | + }, null, 2)}\n`, { encoding: 'utf8', flag: 'wx' }); |
| 110 | +} catch (error) { |
| 111 | + if (fs.existsSync(archivedDraft) && !fs.existsSync(source)) fs.renameSync(archivedDraft, source); |
| 112 | + if (fs.existsSync(destination)) fs.unlinkSync(destination); |
| 113 | + if (fs.existsSync(receipt)) fs.unlinkSync(receipt); |
| 114 | + throw error; |
| 115 | +} |
| 116 | + |
| 117 | +process.stdout.write(`Created ${path.relative(ROOT, destination)}.\n`); |
| 118 | +process.stdout.write(`Archived local draft at ${path.relative(ROOT, archivedDraft)}.\n`); |
| 119 | +process.stdout.write(`Wrote local receipt at ${path.relative(ROOT, receipt)}.\n`); |
| 120 | +process.stdout.write('No commit or push was performed.\n'); |
0 commit comments