forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraft-blog-post.js
More file actions
132 lines (113 loc) · 3.54 KB
/
draft-blog-post.js
File metadata and controls
132 lines (113 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";
import createEsmUtils from "esm-utils";
import fg from "fast-glob";
import semver from "semver";
import {
changelogUnreleasedDirPath,
changelogUnreleasedDirs,
getEntries,
printEntries,
replaceVersions,
} from "./utils/changelog.js";
const { __dirname, require } = createEsmUtils(import.meta);
const blogDir = path.join(__dirname, "../website/blog");
const introTemplateFile = path.join(
changelogUnreleasedDirPath,
"BLOG_POST_INTRO_TEMPLATE.md",
);
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`);
const postFile = path.join(
blogDir,
`${new Date().toISOString().replace(/T.+/u, "")}-${nextVersion}.md`,
);
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" },
{ dir: "graphql", title: "GraphQL" },
{ dir: "markdown", title: "Markdown" },
{ dir: "mdx", title: "MDX" },
{ dir: "yaml", title: "YAML" },
{ dir: "api", title: "API" },
{ dir: "cli", title: "CLI" },
{ dir: "misc", title: "Miscellaneous" },
];
const categoriesByDir = new Map(
categories.map((category) => [category.dir, category]),
);
for (const dir of changelogUnreleasedDirs) {
const dirPath = path.join(changelogUnreleasedDirPath, dir.name);
const category = categoriesByDir.get(dir.name);
if (!category) {
throw new Error("Unknown category: " + dir.name);
}
category.entries = getEntries(dirPath);
}
for (const filePath of fg.sync(postGlob)) {
fs.rmSync(filePath);
}
const introFileData = fs.readFileSync(introFile, "utf8").trim();
const TRUNCATE_COMMENT = "<!-- truncate -->";
const shouldPrintTruncate = !introFileData.includes(TRUNCATE_COMMENT);
fs.writeFileSync(
postFile,
replaceVersions(
[
introFileData,
shouldPrintTruncate ? TRUNCATE_COMMENT : "",
...printEntriesWithTitle({
title: "Highlights",
filter: (entry) => entry.section === "highlight",
}),
...printEntriesWithTitle({
title: "Breaking Changes",
filter: (entry) => entry.section === "breaking",
}),
...printEntriesWithTitle({
title: "Formatting Improvements",
filter: (entry) => entry.section === "improvement",
}),
...printEntriesWithTitle({
title: "Other Changes",
filter: (entry) => !entry.section,
}),
]
.filter(Boolean)
.join("\n\n") + "\n",
prevVersion,
nextVersion,
),
);
function printEntriesWithTitle({ title, filter }) {
const result = [];
for (const { entries = [], title } of categories) {
const filteredEntries = entries.filter(filter);
if (filteredEntries.length > 0) {
result.push("### " + title, ...printEntries(filteredEntries));
}
}
if (result.length > 0) {
result.unshift("## " + title);
}
return result;
}