Skip to content

Commit

Permalink
feat: add conventional changelog
Browse files Browse the repository at this point in the history
This update includes:
- full switch to comventional commits
- auto-generated conventional CHANGELOG.md
- custom changelog template
- helper function for changelog to transform data
- npm package configuration to ensure proper generation process
- custom script to:
  - autmatically bump package.json version
  - tag the latest commit with the bumped version
  - generate changelog
  - commit changes as chore(release)
  - push commit to the main branch along with the tag
  • Loading branch information
bandantonio committed Apr 25, 2022
1 parent 7e22233 commit 24665d4
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 2 deletions.
34 changes: 34 additions & 0 deletions .changelog/changelog-template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Changelog

This document outlines the summary of all notable changes to the project.

{{#each releases}}
{{#if href}}
## [{{title}}]({{href}}) - {{niceDate}}
{{else}}
## {{title}} - {{niceDate}}
{{/if}}

{{#commit-list commits heading='### Breaking Changes ⚠️' message='BREAKING CHANGE'}}
{{subject}} ([`{{shorthash}}`]({{href}}))
{{/commit-list}}

{{#commit-list commits heading='### New ✨' message='feat(\([\w-]*?\))?:'}}
- {{subject}} ([`{{shorthash}}`]({{href}}))
{{/commit-list}}

{{#commit-list commits heading='### Bug fixes 🐛' message='fix(\([\w-]*?\))?:'}}
- {{subject}} ([`{{shorthash}}`]({{href}}))
{{/commit-list}}

{{#commit-list commits heading='### Documentation 📚' message='docs(\([\w-]*?\))?:'}}
- {{subject}} ([`{{shorthash}}`]({{href}}))
{{/commit-list}}

{{#commit-list commits heading='### Improvements 📈' message='(build|chore|ci|perf|refactor|revert|style|test)(\([\w-]*?\))?:' exclude='chore\(release\): '}}
- {{#emoji subject }}{{/emoji}}{{#if href}} ([`{{shorthash}}`]({{href}})){{/if}}
{{/commit-list}}

----
{{/each}}
17 changes: 17 additions & 0 deletions .changelog/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = (Handlebars) => {
Handlebars.registerHelper('emoji', (message, options) => {
const emojies = {
build: '⚙️',
chore: 'ℹ️',
ci: '🚀',
perf: '🚄',
refactor: '♻️',
revert: '⏮️',
style: '🎨',
test: '✅'
}

let match = message.match(/(\w*)(\([\w-]*?\))?:/);
return match ? emojies[match[1]] + ' ' + message : message
});
}
3 changes: 3 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
git-tag-version=false
sign-git-commit=true
sign-git-tag=true
34 changes: 34 additions & 0 deletions bump-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { execSync } = require('child_process');

// Check the type of the latest commit to know what version should be bumped
const latestCommitMessage = execSync('git log -1 --format=%s').toString('utf-8');
const commitParts = latestCommitMessage.matchAll(/(?<type>build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(?<scope>\(\w*?\))?(?<breaking>!)?:/g);

let newBumbedVersion = '';

for (const commitPart of commitParts) {
if (commitPart.groups.breaking) {
newBumbedVersion = execSync('npm version major').toString('utf-8');
} else if (commitPart.groups.type === 'feat') {
newBumbedVersion = execSync('npm version minor').toString('utf-8');
} else if (commitPart.groups.type === 'fix') {
newBumbedVersion = execSync('npm version patch').toString('utf-8');
} else {
newBumbedVersion = execSync('npm version minor').toString('utf-8');
}

newBumbedVersion = newBumbedVersion.trim();

// Create a new tag and assign it to the latest commit
execSync(`git tag ${newBumbedVersion}`);

// Generate changelog for the latest release
execSync('npm run changelog');

// Commit bumped package.jspon and generated CHANGELOG as a chore(release)
execSync(`git add package.json package-lock.json CHANGELOG.md && git commit -q -m "chore(release): ${newBumbedVersion}"`);

// Push commit along with the tag to the main branch
execSync('git push --follow-tags origin main');
// execSync('git push origin --tags');
}
Loading

0 comments on commit 24665d4

Please sign in to comment.