changelog-writer.mjs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import createConventionalCommitsPreset from "conventional-changelog-conventionalcommits";
  2. export const RELEASE_CHANGELOG_TYPES = [
  3. { type: "feat", section: "Features" },
  4. { type: "fix", section: "Bug Fixes" },
  5. { type: "chore", section: "Chores" },
  6. { type: "docs", section: "Documentation" },
  7. { type: "refactor", section: "Refactorings" },
  8. { type: "", section: "Other Changes" },
  9. ];
  10. export const RELEASE_CHANGELOG_PARSER_OPTS = {
  11. headerPattern: /^(?:(\w+)(?:\(([^)]*)\))?:\s+)?(.+)$/,
  12. breakingHeaderPattern: /^(\w+)(?:\(([^)]*)\))?!:\s+(.+)$/,
  13. headerCorrespondence: ["type", "scope", "subject"],
  14. };
  15. export function extractCommitBodyBullets(body) {
  16. if (typeof body !== "string" || body.trim() === "") {
  17. return [];
  18. }
  19. return body
  20. .split(/\r?\n/)
  21. .map((line) => line.match(/^\s*[-*+]\s+(.+?)\s*$/)?.[1]?.trim())
  22. .filter((item) => item && !/^BREAKING[ -]CHANGE:/i.test(item));
  23. }
  24. export function createReleaseChangelogWriterOpts() {
  25. const preset = createConventionalCommitsPreset({
  26. types: RELEASE_CHANGELOG_TYPES,
  27. });
  28. const baseWriter = preset.writer;
  29. const baseTransform = baseWriter.transform;
  30. return {
  31. ...baseWriter,
  32. commitPartial: `*{{#if scope}} **{{scope}}:**{{/if}} {{#if subject}}{{subject}}{{else}}{{header}}{{/if}}{{#if hash}} {{#if @root.linkReferences}}([{{shortHash}}]({{@root.host}}/{{@root.owner}}/{{@root.repository}}/commit/{{hash}})){{else}}{{shortHash}}{{/if}}{{/if}}{{#if references}}, closes{{#each references}} {{#if @root.linkReferences}}[{{#if this.owner}}{{this.owner}}/{{/if}}{{this.repository}}{{this.prefix}}{{this.issue}}]({{issueUrlFormat}}){{else}}{{#if this.owner}}{{this.owner}}/{{/if}}{{this.repository}}{{this.prefix}}{{this.issue}}{{/if}}{{/each}}{{/if}}
  33. {{#if bodyBullets}}{{#each bodyBullets}} * {{this}}
  34. {{/each}}{{/if}}
  35. `,
  36. transform(commit, context) {
  37. const transformed = baseTransform(commit, context);
  38. if (!transformed) {
  39. return transformed;
  40. }
  41. const bodyBullets = extractCommitBodyBullets(commit.body);
  42. if (bodyBullets.length === 0) {
  43. return transformed;
  44. }
  45. return {
  46. ...transformed,
  47. bodyBullets,
  48. };
  49. },
  50. };
  51. }