changelog.mjs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Fetch the latest release from GitHub and prepend it to the CHANGELOG.md
  3. * Nothing will happen if the latest release is already in the CHANGELOG.md
  4. */
  5. import { $ } from "execa";
  6. import { readFile, writeFile } from "node:fs/promises";
  7. import { fileURLToPath } from "node:url";
  8. import { dirname, join } from "node:path";
  9. import configGit from "./utils/configGit.mjs";
  10. const changelogFilename = "CHANGELOG.md";
  11. const changeLogFilePath = join(
  12. dirname(fileURLToPath(import.meta.url)),
  13. "..",
  14. changelogFilename
  15. );
  16. const currentChangeLog = await readFile(changeLogFilePath, "utf-8");
  17. const { stdout } =
  18. await $`gh release list --exclude-drafts --json=tagName,publishedAt,name,isLatest`;
  19. const release = JSON.parse(stdout).find((release) => release.isLatest);
  20. if (currentChangeLog.includes(`# ${release.tagName}`)) {
  21. process.exit(0);
  22. }
  23. await configGit();
  24. const normalizeReleaseNote = (note) => {
  25. const ignoreSections = [
  26. "## new contributors",
  27. "## all changes",
  28. "## other changes",
  29. ];
  30. ignoreSections.forEach((section) => {
  31. const index = note.toLowerCase().indexOf(section);
  32. if (index > -1) {
  33. note = note.slice(0, index).replace(/#\s*$/, "");
  34. }
  35. });
  36. return note
  37. .replace(/by @([-\w]+)/g, (_, username) => {
  38. return `by [@${username}](https://github.com/${username})`;
  39. })
  40. .trim();
  41. };
  42. const formatDate = (date) => {
  43. const str = date.toISOString();
  44. return str.substring(0, str.indexOf("T"));
  45. };
  46. const { body } = JSON.parse(
  47. (await $`gh release view ${release.tagName} --json=body`).stdout
  48. );
  49. 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`;
  50. await writeFile(changeLogFilePath, `${note}\n${currentChangeLog}`);
  51. await $`git add ${changelogFilename}`;
  52. const message = `Update ${changelogFilename}: ${release.tagName}`;
  53. await $`git commit -m ${message}`;
  54. await $`git push origin main`;