Files
test-repo/scripts/draft-blog-post.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

132 lines
3.5 KiB
JavaScript
Raw Normal View History

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";
2021-05-15 20:09:52 +08:00
import createEsmUtils from "esm-utils";
2022-10-23 22:23:30 +09:00
import fg from "fast-glob";
import semver from "semver";
2021-06-26 16:29:30 +09:00
import {
changelogUnreleasedDirPath,
changelogUnreleasedDirs,
getEntries,
2021-06-26 16:29:30 +09:00
printEntries,
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");
const introTemplateFile = path.join(
2021-06-26 16:29:30 +09:00
changelogUnreleasedDirPath,
"BLOG_POST_INTRO_TEMPLATE.md",
);
2021-06-26 16:29:30 +09:00
const introFile = path.join(changelogUnreleasedDirPath, "blog-post-intro.md");
if (!fs.existsSync(introFile)) {
fs.copyFileSync(introTemplateFile, introFile);
}
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,
`${new Date().toISOString().replace(/T.+/u, "")}-${nextVersion}.md`,
2020-03-21 22:24:23 +02:00
);
const categories = [
{ dir: "javascript", title: "JavaScript" },
{ dir: "typescript", title: "TypeScript" },
{ dir: "flow", title: "Flow" },
{ dir: "json", title: "JSON" },
{ dir: "css", title: "CSS" },
{ dir: "scss", title: "SCSS" },
{ dir: "less", title: "Less" },
{ dir: "html", title: "HTML" },
{ dir: "vue", title: "Vue" },
{ dir: "angular", title: "Angular" },
{ dir: "lwc", title: "LWC" },
{ dir: "handlebars", title: "Ember / Handlebars" },
2020-03-21 22:24:23 +02:00
{ dir: "graphql", title: "GraphQL" },
{ dir: "markdown", title: "Markdown" },
{ dir: "mdx", title: "MDX" },
{ dir: "yaml", title: "YAML" },
{ dir: "api", title: "API" },
{ dir: "cli", title: "CLI" },
2022-03-16 13:25:48 +09:00
{ dir: "misc", title: "Miscellaneous" },
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);
}
2021-06-26 16:29:30 +09:00
category.entries = getEntries(dirPath);
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();
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,
replaceVersions(
[
2022-03-16 13:25:48 +09:00
introFileData,
shouldPrintTruncate ? TRUNCATE_COMMENT : "",
2021-06-26 16:29:30 +09:00
...printEntriesWithTitle({
title: "Highlights",
2021-05-10 00:15:27 +03:00
filter: (entry) => entry.section === "highlight",
}),
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",
}),
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,
}),
2022-03-16 13:25:48 +09:00
]
.filter(Boolean)
.join("\n\n") + "\n",
prevVersion,
nextVersion,
),
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);
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
}
}
if (result.length > 0) {
2020-03-21 22:24:23 +02:00
result.unshift("## " + title);
}
return result;
}