forked from slab/quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changelog.mjs
68 lines (54 loc) · 1.97 KB
/
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
/**
* Fetch the latest release from GitHub and prepend it to the CHANGELOG.md
* Nothing will happen if the latest release is already in the CHANGELOG.md
*/
import { $ } from "execa";
import { readFile, writeFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import configGit from "./utils/configGit.mjs";
const changelogFilename = "CHANGELOG.md";
const changeLogFilePath = join(
dirname(fileURLToPath(import.meta.url)),
"..",
changelogFilename
);
const currentChangeLog = await readFile(changeLogFilePath, "utf-8");
const { stdout } =
await $`gh release list --exclude-drafts --json=tagName,publishedAt,name,isLatest`;
const release = JSON.parse(stdout).find((release) => release.isLatest);
if (currentChangeLog.includes(`# ${release.tagName}`)) {
process.exit(0);
}
await configGit();
const normalizeReleaseNote = (note) => {
const ignoreSections = [
"## new contributors",
"## all changes",
"## other changes",
];
ignoreSections.forEach((section) => {
const index = note.toLowerCase().indexOf(section);
if (index > -1) {
note = note.slice(0, index).replace(/#\s*$/, "");
}
});
return note
.replace(/by @([-\w]+)/g, (_, username) => {
return `by [@${username}](https://github.com/${username})`;
})
.trim();
};
const formatDate = (date) => {
const str = date.toISOString();
return str.substring(0, str.indexOf("T"));
};
const { body } = JSON.parse(
(await $`gh release view ${release.tagName} --json=body`).stdout
);
const note = `# ${release.tagName} (${formatDate(new Date(release.publishedAt))})\n\n${normalizeReleaseNote(body)}\n\n[All changes](https://github.com/slab/quill/releases/tag/${release.tagName})\n`;
await writeFile(changeLogFilePath, `${note}\n${currentChangeLog}`);
await $`git add ${changelogFilename}`;
const message = `Update ${changelogFilename}: ${release.tagName}`;
await $`git commit -m ${message}`;
await $`git push origin main`;