From 713947d804db455baa4c5d4c5e934cce10ba27d4 Mon Sep 17 00:00:00 2001 From: Jose Browne Date: Fri, 29 Mar 2024 13:56:59 -0400 Subject: [PATCH] Improve changelog rendering template --- .eslintignore | 1 + .gitignore | 3 +++ .prettierignore | 2 ++ changelog-setup.js | 59 ++++++++++++++++++++++++++++++++++++++++++ changelog-template.hbs | 32 +++++++++++++++++++++++ package.json | 4 ++- 6 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 changelog-setup.js create mode 100644 changelog-template.hbs diff --git a/.eslintignore b/.eslintignore index 73a6cd5..81194e8 100644 --- a/.eslintignore +++ b/.eslintignore @@ -2,3 +2,4 @@ /extension.js tailwind.config.js webpack.config.js +changelog-setup.js diff --git a/.gitignore b/.gitignore index b3869df..f1b916e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ /extension.js.map /extension.js.LICENSE.txt +# Change log debug data +changelog-data.json + # Ignore: Just adding so that we get tailwindcss completions in vscode tailwind.config.js diff --git a/.prettierignore b/.prettierignore index e7b307d..d1f0582 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1 +1,3 @@ /extension.js + +/changelog-template.hbs diff --git a/changelog-setup.js b/changelog-setup.js new file mode 100644 index 0000000..796004f --- /dev/null +++ b/changelog-setup.js @@ -0,0 +1,59 @@ +module.exports = function (Handlebars) { + Handlebars.registerHelper('full-commit-list', (commits, merges, fixes, options) => { + commits = commits + .concat(merges.map((merge) => merge.commit)) + .concat(fixes.map((fix) => fix.commit)); + + if (!commits || commits.length === 0) { + return ''; + } + + const { exclude, message, subject, heading } = options.hash; + + const list = commits + .filter((item) => { + const commit = item.commit || item; + + // Exclude commits where subject includes with "Release v" + if (commit.subject.includes('Release v')) { + return false; + } + + if (exclude) { + const excludeList = exclude.split('|'); + for (const e of excludeList) { + const pattern = new RegExp(e, 'm'); + if (pattern.test(commit.message)) { + return false; + } + } + } + if (message) { + const pattern = new RegExp(message, 'm'); + return pattern.test(commit.message); + } + if (subject) { + const pattern = new RegExp(subject); + const match = pattern.test(commit.subject); + if (match) { + // remove the message from the commit subject and message and trim + commit.subject = commit.subject.replace(pattern, '').trim(); + } + return match; + } + return true; + }) + .map((item) => options.fn(item)) + .join(''); + + if (!list) { + return ''; + } + + if (!heading) { + return list; + } + + return `${heading}\n\n${list}`; + }); +}; diff --git a/changelog-template.hbs b/changelog-template.hbs new file mode 100644 index 0000000..15481a1 --- /dev/null +++ b/changelog-template.hbs @@ -0,0 +1,32 @@ +### Changelog + +#### All notable changes to this project will be documented in this file. + +Generated by the awesome [`auto-changelog`](https://github.com/CookPete/auto-changelog) package 👏🏽. + +{{#each releases}} + {{#if href}} + ## [{{title}}]({{href}}){{#if tag}} - {{niceDate}}{{/if}} + {{else}} + ## {{title}}{{#if tag}} - {{isoDate}}{{/if}} + {{/if}} + {{#if summary}} + {{summary}} + {{/if}} + + {{#full-commit-list commits merges fixes heading='### Breaking Changes' subject='Breaking change: '}} + - {{subject}} [`{{shorthash}}`]({{href}}) + {{/full-commit-list}} + + {{#full-commit-list commits merges fixes heading='### New Features' subject='Feat:' exclude='Breaking change: '}} + - {{subject}} [`{{shorthash}}`]({{href}}) + {{/full-commit-list}} + + {{#full-commit-list commits merges fixes heading='### Bug Fixes' subject='Bug Fix:' exclude='Breaking change: '}} + - {{subject}} [`{{shorthash}}`]({{href}}) + {{/full-commit-list}} + + {{#full-commit-list commits merges fixes heading='### Other Changes' exclude='Feat:|Bug Fix:|Breaking change:'}} + - {{subject}} [`{{shorthash}}`]({{href}}) + {{/full-commit-list}} +{{/each}} diff --git a/package.json b/package.json index 9795dcd..47a01e2 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,9 @@ "lint": "eslint '**/*.{ts,tsx,js,jsx}'", "typecheck": "tsc", "test": "jest", - "changelog": "auto-changelog --package --commit-limit false && git add CHANGELOG.md" + "changelog-generate": "auto-changelog --starting-version v16 --handlebars-setup changelog-setup.js --template changelog-template.hbs --commit-limit false", + "changelog-data": "npm run changelog-generate -- --template json --output changelog-data.json", + "changelog": "npm run changelog-data && npm run changelog-generate && git add CHANGELOG.md" }, "jest": { "testEnvironment": "jsdom",