1 #!/usr/bin/env node
2 import { readFile, writeFile } from "fs/promises"
3 import path from "path"
4 import { parseArgs } from "util"
5
6 /**
7 * Writes a banner URL into a changelog entry file's `image` property.
8 *
9 * The changelog pipeline renders and uploads the banner after Claude has
10 * written the entry, so the URL has to be patched into a file that already
11 * exists. The edit is textual rather than a rewrite of the whole module, so the
12 * entry's Markdown content keeps the formatting it was written with.
13 *
14 * @example
15 * node scripts/set-changelog-image.mjs --date 2026-08-17 --url https://res.cloudinary.com/...
16 */
17
18 const IMAGE_LINE_REGEX = /^([ \t]*)image:.*$/m
19 const CONTENT_LINE_REGEX = /^([ \t]*)content:/m
20
21 const { values } = parseArgs({
22 options: {
23 date: { type: "string" },
24 file: { type: "string" },
25 url: { type: "string" },
26 },
27 })
28
29 if (!values.url) {
30 throw new Error("--url is required")
31 }
32
33 if (!values.date && !values.file) {
34 throw new Error("one of --date or --file is required")
35 }
36
37 const file =
38 values.file ??
39 path.join(process.cwd(), "generated", "changelog", `${values.date}.mjs`)
40
41 const original = await readFile(file, "utf-8")
42 const line = `image: ${JSON.stringify(values.url)},`
43
44 const contentMatch = CONTENT_LINE_REGEX.exec(original)
45
46 if (!contentMatch) {
47 throw new Error(
48 `${file} has no \`content\` property to place the image before. ` +
49 `Check that the entry follows the shape described in ` +
50 `generated/changelog/README.md.`
51 )
52 }
53
54 // `content` is a Markdown template literal that can hold anything, so only the
55 // properties declared before it are searched for an existing `image`.
56 const header = original.slice(0, contentMatch.index)
57 const rest = original.slice(contentMatch.index)
58 const indent = contentMatch[1]
59
60 // Re-running the pipeline for the same date overwrites the image in place, so
61 // the entry keeps one `image` rather than gaining another.
62 const updated = IMAGE_LINE_REGEX.test(header)
63 ? header.replace(
64 IMAGE_LINE_REGEX,
65 (_match, existingIndent) => `${existingIndent}${line}`
66 ) + rest
67 : `${header}${indent}${line}\n${rest}`
68
69 await writeFile(file, updated, "utf-8")
70
71 process.stdout.write(
72 `Set the image of ${path.basename(file)} to ${values.url}\n`
73 )