|
| 1 | +#!/usr/bin/env -S npx --yes tsx |
| 2 | +/** |
| 3 | + * blog-post — write to the plain-HTML blog without getting a convention wrong. |
| 4 | + * |
| 5 | + * The blog has no build step: writing a file is publishing. Nothing else |
| 6 | + * catches a post with no date (the feed generator skips it in silence) or one |
| 7 | + * dated in the future (it pins above every real post, and readers that filter |
| 8 | + * future items drop it, so the feed looks dead while the files look fine). |
| 9 | + */ |
| 10 | + |
| 11 | +import { existsSync } from 'node:fs'; |
| 12 | +import { readFile } from 'node:fs/promises'; |
| 13 | +import { spawnSync } from 'node:child_process'; |
| 14 | +import { join } from 'node:path'; |
| 15 | + |
| 16 | +import { parseArgs, UsageError } from '../src/args.ts'; |
| 17 | +import { isMain } from '../src/is-main.ts'; |
| 18 | +import { |
| 19 | + blogDir, |
| 20 | + createPost, |
| 21 | + DEFAULT_DIR, |
| 22 | + isoSeconds, |
| 23 | + lint, |
| 24 | + readPosts, |
| 25 | +} from '../src/blog.ts'; |
| 26 | + |
| 27 | +const USAGE = `Usage: |
| 28 | + blog-post new <title> --description <text> [--body file.html] [--date ISO] |
| 29 | + blog-post check |
| 30 | + blog-post list |
| 31 | + blog-post feed |
| 32 | +
|
| 33 | +Commands: |
| 34 | + new Write the next post, list it in index.html, rebuild the feed |
| 35 | + check Report posts that will break the feed (non-zero exit if any) |
| 36 | + list Every post with its date |
| 37 | + feed Regenerate feed.xml |
| 38 | +
|
| 39 | +Options: |
| 40 | + --description TEXT Feed summary. Required by \`new\`. |
| 41 | + --body FILE HTML fragment for the body (default: a stub) |
| 42 | + --date ISO Publish date (default: now). Refuses the future. |
| 43 | + --dir PATH Blog directory (default: $BLOG_DIR, else |
| 44 | + ${DEFAULT_DIR}) |
| 45 | + --allow-future Permit a future date. You almost never want this. |
| 46 | + -h, --help show this help |
| 47 | +`; |
| 48 | + |
| 49 | +const SPEC = { |
| 50 | + boolean: ['--allow-future', '-h', '--help'], |
| 51 | + string: ['--description', '--body', '--date', '--dir'], |
| 52 | +} as const; |
| 53 | + |
| 54 | +/** |
| 55 | + * Regenerate feed.xml by running the blog's own generator. |
| 56 | + * |
| 57 | + * Shelling out rather than reimplementing: build-feed.mjs lives beside the |
| 58 | + * posts and is the single source of truth for the feed's shape. |
| 59 | + */ |
| 60 | +function rebuildFeed(dir: string): number { |
| 61 | + const script = join(dir, 'build-feed.mjs'); |
| 62 | + if (!existsSync(script)) { |
| 63 | + process.stderr.write(`no build-feed.mjs in ${dir} — feed not regenerated\n`); |
| 64 | + return 1; |
| 65 | + } |
| 66 | + return spawnSync(process.execPath, [script], { cwd: dir, stdio: 'inherit' }).status ?? 1; |
| 67 | +} |
| 68 | + |
| 69 | +export async function run(argv: readonly string[]): Promise<number> { |
| 70 | + let parsed; |
| 71 | + try { |
| 72 | + parsed = parseArgs(argv, SPEC); |
| 73 | + } catch (error) { |
| 74 | + if (error instanceof UsageError) { |
| 75 | + process.stderr.write(`${error.message}\n\n${USAGE}`); |
| 76 | + return 2; |
| 77 | + } |
| 78 | + throw error; |
| 79 | + } |
| 80 | + |
| 81 | + const { flags, values, positional } = parsed; |
| 82 | + |
| 83 | + if (flags.has('-h') || flags.has('--help') || positional.length === 0) { |
| 84 | + process.stdout.write(USAGE); |
| 85 | + return positional.length === 0 && !flags.has('-h') && !flags.has('--help') ? 1 : 0; |
| 86 | + } |
| 87 | + |
| 88 | + const [command, ...rest] = positional; |
| 89 | + const dir = blogDir(values.get('--dir')); |
| 90 | + |
| 91 | + if (!existsSync(dir)) { |
| 92 | + process.stderr.write(`blog directory not found: ${dir}\n`); |
| 93 | + return 1; |
| 94 | + } |
| 95 | + |
| 96 | + switch (command) { |
| 97 | + case 'new': { |
| 98 | + const title = rest.join(' ').trim(); |
| 99 | + if (!title) { |
| 100 | + process.stderr.write('new: give a title\n'); |
| 101 | + return 1; |
| 102 | + } |
| 103 | + |
| 104 | + const description = (values.get('--description') ?? '').trim(); |
| 105 | + if (!description) { |
| 106 | + process.stderr.write('new: --description is required (it becomes the feed summary)\n'); |
| 107 | + return 1; |
| 108 | + } |
| 109 | + |
| 110 | + const raw = values.get('--date'); |
| 111 | + const when = raw ? new Date(raw) : new Date(); |
| 112 | + if (Number.isNaN(when.getTime())) { |
| 113 | + process.stderr.write(`new: unparseable --date ${JSON.stringify(raw)}\n`); |
| 114 | + return 1; |
| 115 | + } |
| 116 | + if (when.getTime() > Date.now() && !flags.has('--allow-future')) { |
| 117 | + process.stderr.write( |
| 118 | + `new: ${isoSeconds(when)} is in the future.\n` + |
| 119 | + ' A future-dated post sits above every real post, and readers that hide\n' + |
| 120 | + ' future items drop it, so the feed looks dead. Pass --allow-future only\n' + |
| 121 | + ' if you genuinely mean to schedule it.\n', |
| 122 | + ); |
| 123 | + return 1; |
| 124 | + } |
| 125 | + |
| 126 | + const bodyFile = values.get('--body'); |
| 127 | + const body = bodyFile ? await readFile(bodyFile, 'utf8') : ''; |
| 128 | + |
| 129 | + const { file, path } = await createPost(dir, { |
| 130 | + title, |
| 131 | + description, |
| 132 | + date: isoSeconds(when), |
| 133 | + body, |
| 134 | + }); |
| 135 | + |
| 136 | + process.stdout.write(`created ${file}\n ${path}\n listed in index.html\n`); |
| 137 | + return rebuildFeed(dir); |
| 138 | + } |
| 139 | + |
| 140 | + case 'check': { |
| 141 | + const problems = lint(await readPosts(dir)); |
| 142 | + if (problems.length === 0) { |
| 143 | + process.stdout.write('all posts look publishable\n'); |
| 144 | + return 0; |
| 145 | + } |
| 146 | + for (const problem of problems) { |
| 147 | + process.stderr.write(`${problem.file}: ${problem.problem}\n`); |
| 148 | + } |
| 149 | + return 1; |
| 150 | + } |
| 151 | + |
| 152 | + case 'list': { |
| 153 | + for (const post of await readPosts(dir)) { |
| 154 | + const title = (post.title ?? '(no h1)').replace(/—/g, '—').slice(0, 52); |
| 155 | + process.stdout.write(`${post.file} ${(post.date ?? 'NO-DATE').padEnd(22)} ${title}\n`); |
| 156 | + } |
| 157 | + return 0; |
| 158 | + } |
| 159 | + |
| 160 | + case 'feed': |
| 161 | + return rebuildFeed(dir); |
| 162 | + |
| 163 | + default: |
| 164 | + process.stderr.write(`unknown command: ${command}\n\n${USAGE}`); |
| 165 | + return 1; |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +if (isMain(import.meta.url)) { |
| 170 | + process.exitCode = await run(process.argv.slice(2)); |
| 171 | +} |
0 commit comments