|
| 1 | +#!/usr/bin/env node |
| 2 | +import { spawnSync } from 'node:child_process'; |
| 3 | +import { existsSync } from 'node:fs'; |
| 4 | +import { join } from 'node:path'; |
| 5 | + |
| 6 | +import { blogDir, createPost, lint, readPosts, DEFAULT_DIR } from '../src/blog.mjs'; |
| 7 | + |
| 8 | +const HELP = `blog-post — write to the plain-HTML blog without getting a convention wrong |
| 9 | +
|
| 10 | +Usage |
| 11 | + blog-post new <title> -d <description> [--body file.html] [--date ISO] |
| 12 | + blog-post check Report posts that will break the feed |
| 13 | + blog-post list Every post with its date |
| 14 | + blog-post feed Regenerate feed.xml |
| 15 | +
|
| 16 | +Options |
| 17 | + -d, --description <text> Feed summary. Required for a new post. |
| 18 | + --body <file> HTML body to drop in (default: a stub) |
| 19 | + --date <iso> Publish date (default: now). Refuses the future. |
| 20 | + --dir <path> Blog directory (default: $BLOG_DIR or |
| 21 | + ${DEFAULT_DIR}) |
| 22 | + --allow-future Permit a future date. You almost never want this. |
| 23 | + -h, --help |
| 24 | +
|
| 25 | +Why this exists |
| 26 | + The blog has no build step, so nothing catches a mistake. A post with no |
| 27 | + <meta name="date"> is skipped by the feed generator entirely; one dated in |
| 28 | + the future pins itself above everything and is hidden outright by readers |
| 29 | + that filter future items — the feed looks dead while the files look fine. |
| 30 | + \`blog-post check\` finds both. |
| 31 | +`; |
| 32 | + |
| 33 | +/** |
| 34 | + * @param {string[]} argv |
| 35 | + * @returns {{ command: string, args: string[], flags: Record<string, string|boolean> }} |
| 36 | + */ |
| 37 | +export function parseArgs(argv) { |
| 38 | + const flags = {}; |
| 39 | + const args = []; |
| 40 | + let command = ''; |
| 41 | + |
| 42 | + const alias = { d: 'description', h: 'help' }; |
| 43 | + |
| 44 | + for (let i = 0; i < argv.length; i += 1) { |
| 45 | + const token = argv[i]; |
| 46 | + |
| 47 | + if (token === '-h' || token === '--help') { |
| 48 | + flags.help = true; |
| 49 | + } else if (token.startsWith('--') || (token.startsWith('-') && token.length === 2)) { |
| 50 | + const key = alias[token.replace(/^-+/, '')] ?? token.replace(/^--/, ''); |
| 51 | + const next = argv[i + 1]; |
| 52 | + if (next === undefined || next.startsWith('--')) { |
| 53 | + flags[key] = true; |
| 54 | + } else { |
| 55 | + flags[key] = next; |
| 56 | + i += 1; |
| 57 | + } |
| 58 | + } else if (!command) { |
| 59 | + command = token; |
| 60 | + } else { |
| 61 | + args.push(token); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return { command, args, flags }; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Regenerate feed.xml by running the blog's own generator. |
| 70 | + * |
| 71 | + * Shelling out rather than reimplementing: build-feed.mjs lives with the blog |
| 72 | + * and is the single source of truth for what the feed looks like. |
| 73 | + * |
| 74 | + * @param {string} dir |
| 75 | + * @returns {number} exit code |
| 76 | + */ |
| 77 | +function rebuildFeed(dir) { |
| 78 | + const script = join(dir, 'build-feed.mjs'); |
| 79 | + if (!existsSync(script)) { |
| 80 | + console.error(`no build-feed.mjs in ${dir} — feed not regenerated`); |
| 81 | + return 1; |
| 82 | + } |
| 83 | + const res = spawnSync(process.execPath, [script], { cwd: dir, stdio: 'inherit' }); |
| 84 | + return res.status ?? 1; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * @param {string[]} argv |
| 89 | + * @returns {Promise<number>} |
| 90 | + */ |
| 91 | +export async function run(argv) { |
| 92 | + const { command, args, flags } = parseArgs(argv); |
| 93 | + |
| 94 | + if (flags.help || !command) { |
| 95 | + console.log(HELP); |
| 96 | + return command ? 0 : 1; |
| 97 | + } |
| 98 | + |
| 99 | + const dir = blogDir(typeof flags.dir === 'string' ? flags.dir : undefined); |
| 100 | + |
| 101 | + if (!existsSync(dir)) { |
| 102 | + console.error(`blog directory not found: ${dir}`); |
| 103 | + return 1; |
| 104 | + } |
| 105 | + |
| 106 | + switch (command) { |
| 107 | + case 'new': { |
| 108 | + const title = args.join(' ').trim(); |
| 109 | + if (!title) { |
| 110 | + console.error('new: give a title'); |
| 111 | + return 1; |
| 112 | + } |
| 113 | + |
| 114 | + const description = typeof flags.description === 'string' ? flags.description.trim() : ''; |
| 115 | + if (!description) { |
| 116 | + console.error('new: -d/--description is required (it becomes the feed summary)'); |
| 117 | + return 1; |
| 118 | + } |
| 119 | + |
| 120 | + const date = |
| 121 | + typeof flags.date === 'string' ? flags.date : new Date().toISOString().replace(/\.\d+Z$/, 'Z'); |
| 122 | + |
| 123 | + const when = new Date(date); |
| 124 | + if (Number.isNaN(when.getTime())) { |
| 125 | + console.error(`new: unparseable --date "${date}"`); |
| 126 | + return 1; |
| 127 | + } |
| 128 | + if (when.getTime() > Date.now() && !flags['allow-future']) { |
| 129 | + console.error( |
| 130 | + `new: ${date} is in the future. A future-dated post sits above every real post and\n` + |
| 131 | + ' readers that hide future items drop it, so the feed looks dead. Pass\n' + |
| 132 | + ' --allow-future only if you genuinely mean to schedule it.', |
| 133 | + ); |
| 134 | + return 1; |
| 135 | + } |
| 136 | + |
| 137 | + let body = ''; |
| 138 | + if (typeof flags.body === 'string') { |
| 139 | + body = await (await import('node:fs/promises')).readFile(flags.body, 'utf8'); |
| 140 | + } |
| 141 | + |
| 142 | + const { file, path } = await createPost(dir, { |
| 143 | + title, |
| 144 | + description, |
| 145 | + date: when.toISOString().replace(/\.\d+Z$/, 'Z'), |
| 146 | + body, |
| 147 | + }); |
| 148 | + |
| 149 | + console.log(`created ${file}`); |
| 150 | + console.log(` ${path}`); |
| 151 | + console.log(' listed in index.html'); |
| 152 | + return rebuildFeed(dir); |
| 153 | + } |
| 154 | + |
| 155 | + case 'check': { |
| 156 | + const problems = lint(await readPosts(dir)); |
| 157 | + if (problems.length === 0) { |
| 158 | + console.log('all posts look publishable'); |
| 159 | + return 0; |
| 160 | + } |
| 161 | + for (const p of problems) console.error(`${p.file}: ${p.problem}`); |
| 162 | + return 1; |
| 163 | + } |
| 164 | + |
| 165 | + case 'list': { |
| 166 | + for (const p of await readPosts(dir)) { |
| 167 | + const title = (p.title ?? '(no h1)').replace(/—/g, '—').slice(0, 52); |
| 168 | + console.log(`${p.file} ${(p.date ?? 'NO-DATE').padEnd(22)} ${title}`); |
| 169 | + } |
| 170 | + return 0; |
| 171 | + } |
| 172 | + |
| 173 | + case 'feed': |
| 174 | + return rebuildFeed(dir); |
| 175 | + |
| 176 | + default: |
| 177 | + console.error(`unknown command: ${command}\n`); |
| 178 | + console.error(HELP); |
| 179 | + return 1; |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +process.exitCode = await run(process.argv.slice(2)); |
0 commit comments