2020-03-21 22:24:23 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
2021-05-15 20:09:52 +08:00
|
|
|
import fs from "node:fs";
|
|
|
|
|
import path from "node:path";
|
|
|
|
|
import createEsmUtils from "esm-utils";
|
2022-10-23 22:23:30 +09:00
|
|
|
import fg from "fast-glob";
|
2023-04-01 13:21:24 +09:00
|
|
|
import semver from "semver";
|
2021-06-26 16:29:30 +09:00
|
|
|
import {
|
2025-03-03 09:07:40 +08:00
|
|
|
categories,
|
2021-06-26 16:29:30 +09:00
|
|
|
changelogUnreleasedDirPath,
|
|
|
|
|
changelogUnreleasedDirs,
|
2023-12-17 19:34:12 +00:00
|
|
|
getEntries,
|
2021-06-26 16:29:30 +09:00
|
|
|
printEntries,
|
2023-12-17 19:34:12 +00:00
|
|
|
replaceVersions,
|
2023-03-28 16:18:53 +08:00
|
|
|
} from "./utils/changelog.js";
|
2020-03-21 22:24:23 +02:00
|
|
|
|
2021-05-15 20:09:52 +08:00
|
|
|
const { __dirname, require } = createEsmUtils(import.meta);
|
2020-03-21 22:24:23 +02:00
|
|
|
const blogDir = path.join(__dirname, "../website/blog");
|
2020-11-18 13:45:44 +09:00
|
|
|
const introTemplateFile = path.join(
|
2021-06-26 16:29:30 +09:00
|
|
|
changelogUnreleasedDirPath,
|
2020-11-18 13:45:44 +09:00
|
|
|
"BLOG_POST_INTRO_TEMPLATE.md",
|
|
|
|
|
);
|
2021-06-26 16:29:30 +09:00
|
|
|
const introFile = path.join(changelogUnreleasedDirPath, "blog-post-intro.md");
|
2020-11-18 13:45:44 +09:00
|
|
|
if (!fs.existsSync(introFile)) {
|
|
|
|
|
fs.copyFileSync(introTemplateFile, introFile);
|
|
|
|
|
}
|
2023-04-01 13:21:24 +09:00
|
|
|
|
|
|
|
|
const prevVersion = require("../node_modules/prettier/package.json").version;
|
|
|
|
|
const { version } = require("../package.json");
|
|
|
|
|
const nextVersion = `${semver.major(version)}.${semver.minor(
|
|
|
|
|
version,
|
|
|
|
|
)}.${semver.patch(version)}`;
|
|
|
|
|
|
|
|
|
|
const postGlob = path.join(blogDir, `????-??-??-${nextVersion}.md`);
|
2020-03-21 22:24:23 +02:00
|
|
|
const postFile = path.join(
|
|
|
|
|
blogDir,
|
2024-06-18 06:52:54 +08:00
|
|
|
`${new Date().toISOString().replace(/T.+/u, "")}-${nextVersion}.md`,
|
2020-03-21 22:24:23 +02:00
|
|
|
);
|
|
|
|
|
|
2021-05-24 06:26:00 +08:00
|
|
|
const categoriesByDir = new Map(
|
|
|
|
|
categories.map((category) => [category.dir, category]),
|
|
|
|
|
);
|
2020-03-21 22:24:23 +02:00
|
|
|
|
2021-06-26 16:29:30 +09:00
|
|
|
for (const dir of changelogUnreleasedDirs) {
|
|
|
|
|
const dirPath = path.join(changelogUnreleasedDirPath, dir.name);
|
2021-05-24 06:26:00 +08:00
|
|
|
const category = categoriesByDir.get(dir.name);
|
2020-03-21 22:24:23 +02:00
|
|
|
|
|
|
|
|
if (!category) {
|
|
|
|
|
throw new Error("Unknown category: " + dir.name);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-02 23:00:59 +08:00
|
|
|
category.entries = getEntries(dirPath, { useFriendlyHeadingId: true });
|
2020-03-21 22:24:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-23 22:23:30 +09:00
|
|
|
for (const filePath of fg.sync(postGlob)) {
|
|
|
|
|
fs.rmSync(filePath);
|
|
|
|
|
}
|
2020-03-21 22:24:23 +02:00
|
|
|
|
2022-03-16 13:25:48 +09:00
|
|
|
const introFileData = fs.readFileSync(introFile, "utf8").trim();
|
|
|
|
|
|
2023-12-01 13:29:30 +09:00
|
|
|
const TRUNCATE_COMMENT = "<!-- truncate -->";
|
2022-09-07 18:11:10 +09:00
|
|
|
const shouldPrintTruncate = !introFileData.includes(TRUNCATE_COMMENT);
|
2022-03-16 13:25:48 +09:00
|
|
|
|
2020-03-21 22:24:23 +02:00
|
|
|
fs.writeFileSync(
|
|
|
|
|
postFile,
|
2020-08-26 12:19:10 +09:00
|
|
|
replaceVersions(
|
|
|
|
|
[
|
2022-03-16 13:25:48 +09:00
|
|
|
introFileData,
|
|
|
|
|
shouldPrintTruncate ? TRUNCATE_COMMENT : "",
|
2021-06-26 16:29:30 +09:00
|
|
|
...printEntriesWithTitle({
|
2020-08-26 12:19:10 +09:00
|
|
|
title: "Highlights",
|
2021-05-10 00:15:27 +03:00
|
|
|
filter: (entry) => entry.section === "highlight",
|
2020-08-26 12:19:10 +09:00
|
|
|
}),
|
2021-06-26 16:29:30 +09:00
|
|
|
...printEntriesWithTitle({
|
2021-05-10 00:15:27 +03:00
|
|
|
title: "Breaking Changes",
|
|
|
|
|
filter: (entry) => entry.section === "breaking",
|
2020-08-26 12:19:10 +09:00
|
|
|
}),
|
2021-06-26 16:29:30 +09:00
|
|
|
...printEntriesWithTitle({
|
2021-05-10 00:15:27 +03:00
|
|
|
title: "Formatting Improvements",
|
|
|
|
|
filter: (entry) => entry.section === "improvement",
|
|
|
|
|
}),
|
2021-06-26 16:29:30 +09:00
|
|
|
...printEntriesWithTitle({
|
2021-05-10 00:15:27 +03:00
|
|
|
title: "Other Changes",
|
|
|
|
|
filter: (entry) => !entry.section,
|
2020-08-26 12:19:10 +09:00
|
|
|
}),
|
2022-03-16 13:25:48 +09:00
|
|
|
]
|
|
|
|
|
.filter(Boolean)
|
|
|
|
|
.join("\n\n") + "\n",
|
2023-04-01 13:21:24 +09:00
|
|
|
prevVersion,
|
|
|
|
|
nextVersion,
|
2020-08-26 12:19:10 +09:00
|
|
|
),
|
2020-03-21 22:24:23 +02:00
|
|
|
);
|
|
|
|
|
|
2021-06-26 16:29:30 +09:00
|
|
|
function printEntriesWithTitle({ title, filter }) {
|
2020-03-21 22:24:23 +02:00
|
|
|
const result = [];
|
|
|
|
|
|
|
|
|
|
for (const { entries = [], title } of categories) {
|
|
|
|
|
const filteredEntries = entries.filter(filter);
|
2021-01-04 10:11:57 +08:00
|
|
|
if (filteredEntries.length > 0) {
|
2021-09-05 22:51:23 +09:00
|
|
|
result.push("### " + title, ...printEntries(filteredEntries));
|
2020-03-21 22:24:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 10:11:57 +08:00
|
|
|
if (result.length > 0) {
|
2020-03-21 22:24:23 +02:00
|
|
|
result.unshift("## " + title);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|