/
generate-changelog.mjs
193 lines (174 loc) · 4.96 KB
/
generate-changelog.mjs
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env node
/**
* When you run the script, enter the number and category of the Pull Request at the prompt.
* Get the PR title and author name via the GitHub API and create a file in ./changelog_unreleased
*
* $ node ./scripts/generate-changelog.mjs
* ✔ Input your Pull Request number: · 10961
* ✔ Input category of your Pull Request: · typescript
*/
import fs from "node:fs/promises";
import path from "node:path";
import fetch from "node-fetch";
import createEsmUtils from "esm-utils";
import enquirer from "enquirer";
import { CHANGELOG_CATEGORIES } from "./utils/changelog-categories.mjs";
const { __dirname } = createEsmUtils(import.meta);
(async () => {
const prNumberPrompt = new enquirer.NumberPrompt({
message: "Input your Pull Request number:",
});
const prNumber = await prNumberPrompt.run();
const categoryPrompt = new enquirer.AutoComplete({
message: "Input category of your Pull Request:",
limit: CHANGELOG_CATEGORIES.length,
// The array passed to `choices` will be broken, so copy it.
choices: [...CHANGELOG_CATEGORIES],
});
const category = (await categoryPrompt.run()).trim();
if (!prNumber || !category) {
throw new Error("Two args are required.");
}
assertCategory(category);
const { title, user } = await getPr(prNumber);
const newChangelog = await createChangelog(title, user, prNumber, category);
const changelogPath = await addNewChangelog(prNumber, category, newChangelog);
const relativePath = path.relative(path.join(__dirname, ".."), changelogPath);
console.log("Generated changelog file: " + relativePath);
})();
/**
* @param {number} prNumber
* @returns {Promise<{ title: string; user: string }>}
*/
async function getPr(prNumber) {
// https://docs.github.com/en/rest/reference/pulls#get-a-pull-request
const url = `https://api.github.com/repos/prettier/prettier/pulls/${prNumber}`;
const response = await fetch(url, {
Headers: {
"Content-Type": "application/json",
Accept: "application/vnd.github.v3+json",
},
});
if (!response.ok) {
if (response.status === 404) {
throw new Error(`Pull Request #${prNumber} not found.`);
}
throw new Error(response);
}
const { title, user } = await response.json();
return {
title,
user: user.login,
};
}
/**
* @param {number} prNumber
* @param {string} category
* @param {string} newChangelog
* @returns {Promise<string>}
*/
async function addNewChangelog(prNumber, category, newChangelog) {
const newChangelogPath = path.resolve(
__dirname,
`../changelog_unreleased/${category}/${prNumber}.md`
);
await fs.writeFile(newChangelogPath, newChangelog);
return newChangelogPath;
}
/**
* @param {string} title
* @param {string} user
* @param {number} prNumber
* @param {string} string
* @returns {string}
*/
async function createChangelog(title, user, prNumber, category) {
const changelogTemplatePath = path.resolve(
__dirname,
"../changelog_unreleased/TEMPLATE.md"
);
const changelogTemplate = await fs.readFile(changelogTemplatePath, "utf8");
const titlePart = "Title";
const prNumberPart = "#XXXX";
const userPart = "@user";
const codeBlockPart = "```jsx\n";
const inputCommentPart = "// Input\n";
const stableCommentPart = "// Prettier stable\n";
const mainCommentPart = "// Prettier main\n";
const syntax = getSyntaxFromCategory(category);
return changelogTemplate
.replace(titlePart, title)
.replace(prNumberPart, `#${prNumber}`)
.replace(userPart, `@${user}`)
.replace(codeBlockPart, `\`\`\`${syntax}\n`)
.replace(inputCommentPart, getCommentForSyntax(syntax, "Input") + "\n")
.replace(
stableCommentPart,
getCommentForSyntax(syntax, "Prettier stable") + "\n"
)
.replace(
mainCommentPart,
getCommentForSyntax(syntax, "Prettier main") + "\n"
);
}
/**
* @param {string} category
* @returns {string}
*/
function getSyntaxFromCategory(category) {
switch (category) {
case "angular":
case "html":
case "lwc":
return "html";
case "cli":
return "sh";
case "graphql":
return "gql";
case "handlebars":
return "hbs";
case "json":
return "jsonc";
case "markdown":
return "md";
case "mdx":
return "mdx";
case "flow":
case "javascript":
case "api":
return "jsx";
case "typescript":
return "tsx";
default:
return category;
}
}
/**
* @param {string} syntax
* @param {string} comment
* @returns {string}
*/
function getCommentForSyntax(syntax, comment) {
switch (syntax) {
case "md":
case "mdx":
case "html":
return `<!-- ${comment} -->`;
case "sh":
case "gql":
return `# ${comment}`;
case "hbs":
return `{{! ${comment} }}`;
default:
return `// ${comment}`;
}
}
/**
* @param {unknown}
* @returns {void}
*/
function assertCategory(category) {
if (!CHANGELOG_CATEGORIES.includes(category)) {
throw new Error(`${category} is invalid category`);
}
}