|
1
|
import { mkdir, readdir, writeFile } from "fs/promises" |
|
2
|
import path from "path" |
|
3
|
|
|
4
|
const CHANGELOG_DIR = path.join(process.cwd(), "generated", "changelog") |
|
5
|
const MANIFEST_NAME = "index.mjs" |
|
6
|
const ENTRY_FILE_REGEX = /^(\d{4}-\d{2}-\d{2})\.mjs$/ |
|
7
|
|
|
8
|
/** |
|
9
|
* Generates `generated/changelog/index.mjs` from the entry files in |
|
10
|
* `generated/changelog`. The manifest only holds each entry's date and a lazy |
|
11
|
* `import()` of its file, so consumers (the changelog page and the public |
|
12
|
* `/api/changelog` endpoint) load the entries of the requested page only, |
|
13
|
* rather than the whole changelog. Bundlers code-split each `import()` into its |
|
14
|
* own chunk, so the manifest's size stays constant as entries are added. |
|
15
|
*/ |
|
16
|
export async function generateChangelogManifest() { |
|
17
|
await mkdir(CHANGELOG_DIR, { recursive: true }) |
|
18
|
|
|
19
|
const files = await readdir(CHANGELOG_DIR) |
|
20
|
|
|
21
|
// File names are `YYYY-MM-DD.mjs`, so a reverse lexicographic sort puts the |
|
22
|
// newest entry first. |
|
23
|
const dates = files |
|
24
|
.map((file) => ENTRY_FILE_REGEX.exec(file)?.[1]) |
|
25
|
.filter(Boolean) |
|
26
|
.sort() |
|
27
|
.reverse() |
|
28
|
|
|
29
|
const content = `// This file is generated by \`scripts/generate-changelog-manifest.mjs\`. |
|
30
|
// Don't edit it manually. Add a \`YYYY-MM-DD.mjs\` entry file to this directory |
|
31
|
// instead, then run \`yarn prep\` in \`www/apps/cloud\`. |
|
32
|
|
|
33
|
/** @type {{ date: string, load: () => Promise<{ default: import("../../utils/changelog").ChangelogEntry }> }[]} */ |
|
34
|
export const changelogEntries = [ |
|
35
|
${dates.map((date) => ` { date: "${date}", load: () => import("./${date}.mjs") },`).join("\n")} |
|
36
|
] |
|
37
|
` |
|
38
|
|
|
39
|
await writeFile(path.join(CHANGELOG_DIR, MANIFEST_NAME), content, "utf-8") |
|
40
|
|
|
41
|
return dates.length |
|
42
|
} |