Skip to content

Commit

Permalink
feat(main): #74 support trailers (#76)
Browse files Browse the repository at this point in the history
Added trailer as optional footer. Trailers can be configured per commit type.

Closes: #74
Changelog: feature
  • Loading branch information
Everduin94 authored Jan 25, 2024
1 parent c0645c9 commit 159a1a9
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 73 deletions.
63 changes: 8 additions & 55 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "better-commits",
"private": false,
"version": "1.8.1",
"description": "A CLI for creating better commits following the conventional commit guidelines",
"description": "A CLI for creating better commits following the conventional commits specification",
"author": "Erik Verduin (https://github.com/everduin94)",
"keywords": [
"typescript",
Expand Down
39 changes: 30 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ https://github.com/Everduin94/better-commits/assets/14320878/8fb15d46-17c4-4e5d-
- Configure globally or per repository
- Config validation and error messaging

As a side-effect of formatting messages
- Auto populate PR title / body
- Automate semantic releases
- Automate changelogs
- Automatically link & close related tickets / issues

## 📦 Installation

```sh
Expand Down Expand Up @@ -89,55 +95,64 @@ Better-commits (& better-branch) are highly flexible with sane defaults. These o
"value": "feat",
"label": "feat",
"hint": "A new feature",
"emoji": ""
"emoji": "",
"trailer": "Changelog: feature"
},
{
"value": "fix",
"label": "fix",
"hint": "A bug fix",
"emoji": "🐛"
"emoji": "🐛",
"trailer": "Changelog: fix"
},
{
"value": "docs",
"label": "docs",
"hint": "Documentation only changes",
"emoji": "📚"
"emoji": "📚",
"trailer": "Changelog: documentation"
},
{
"value": "refactor",
"label": "refactor",
"hint": "A code change that neither fixes a bug nor adds a feature",
"emoji": "🔨"
"emoji": "🔨",
"trailer": "Changelog: refactor"
},
{
"value": "perf",
"label": "perf",
"hint": "A code change that improves performance",
"emoji": "🚀"
"emoji": "🚀",
"trailer": "Changelog: performance"
},
{
"value": "test",
"label": "test",
"hint": "Adding missing tests or correcting existing tests",
"emoji": "🚨"
"emoji": "🚨",
"trailer": "Changelog: test"
},
{
"value": "build",
"label": "build",
"hint": "Changes that affect the build system or external dependencies",
"emoji": "🚧"
"emoji": "🚧",
"trailer": "Changelog: build"
},
{
"value": "ci",
"label": "ci",
"hint": "Changes to our CI configuration files and scripts",
"emoji": "🤖"
"emoji": "🤖",
"trailer": "Changelog: ci"
},
{
"value": "chore",
"label": "chore",
"hint": "Other changes that do not modify src or test files",
"emoji": "🧹"
"emoji": "🧹",
"trailer": "Changelog: chore"
},
{
"value": "",
Expand Down Expand Up @@ -191,6 +206,7 @@ Better-commits (& better-branch) are highly flexible with sane defaults. These o
"initial_value": [],
"options": [
"closes",
"trailer",
"breaking-change",
"deprecated",
"custom"
Expand Down Expand Up @@ -257,6 +273,7 @@ Better-commits (& better-branch) are highly flexible with sane defaults. These o
| `commit_type.options.label` | Commit type prompt label |
| `commit_type.options.hint` | Commit type inline hint (like this) |
| `commit_type.options.emoji` | Commit type emoji |
| `commit_type.options.trailer` | Commit type trailer |
| `commit_scope.enable` | If true include commit scope |
| `commit_scope.custom_scope` | If true allow custom scope at run-time |
| `commit_scope.initial_value` | Default commit scope selected |
Expand Down Expand Up @@ -349,6 +366,10 @@ If you use `better-commits` to create your *first* commit on a new branch

If you're using Github issues to track your work, and select the `closes` footer option when writing your commit. Github will **automatically link and close** that issue when your **pr is merged**

### Changelogs

`better-commits` can append a commit trailer per commit type. This allows you to [automate change logs](https://docs.gitlab.com/ee/user/project/changelogs.html) with tools like Gitlab.

### Fun Facts
[better-commits](https://packagephobia.com/result?p=better-commits) is much smaller than its alternative [commitizen](https://packagephobia.com/result?p=commitizen)

Expand Down
28 changes: 22 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export async function main(config: z.infer<typeof Config>) {
initial_value = type_from_branch
}
}
const value_to_emoji: Record<string,string> = config.commit_type.options.reduce(
(acc, curr) => ({ ...acc, [curr.value]: curr.emoji ?? '' }), {}
const value_to_data: Record<string, {emoji: string, trailer: string}> = config.commit_type.options.reduce(
(acc, curr) => ({ ...acc, [curr.value]: {emoji: curr.emoji ?? '', trailer: curr.trailer ?? '' }}), {}
)
const commit_type = await p.select(
{
Expand All @@ -61,8 +61,9 @@ export async function main(config: z.infer<typeof Config>) {
}
)
if (p.isCancel(commit_type)) process.exit(0)
commit_state.trailer = value_to_data[commit_type].trailer;
commit_state.type = config.commit_type.append_emoji_to_commit ?
`${value_to_emoji[commit_type]} ${commit_type}`.trim()
`${value_to_data[commit_type].emoji} ${commit_type}`.trim()
: commit_type;
}

Expand Down Expand Up @@ -201,11 +202,15 @@ export async function main(config: z.infer<typeof Config>) {
if (p.isCancel(custom_footer)) process.exit(0)
commit_state.custom_footer = custom_footer;
}

if (!commit_footer.includes('trailer')) {
commit_state.trailer = '';
}
}


let continue_commit = true;
p.note(build_commit_string(commit_state, config, true, false), 'Commit Preview')
p.note(build_commit_string(commit_state, config, true, false, true), 'Commit Preview')
if (config.confirm_commit) {
continue_commit = await p.confirm({message: 'Confirm Commit?'}) as boolean;
if (p.isCancel(continue_commit)) process.exit(0)
Expand All @@ -218,14 +223,20 @@ export async function main(config: z.infer<typeof Config>) {

try {
const options = config.overrides.shell ? { shell: config.overrides.shell } : {}
const output = execSync(`git commit -m "${build_commit_string(commit_state, config, false, true)}"`, options).toString().trim();
const trailer = commit_state.trailer ? `--trailer="${commit_state.trailer}"` : '';
const output = execSync(`git commit -m "${build_commit_string(commit_state, config, false, true, false)}" ${trailer}`, options).toString().trim();
if (config.print_commit_output) p.log.info(output)
} catch(err) {
p.log.error('Something went wrong when committing: ' + err)
}
}

function build_commit_string(commit_state: z.infer<typeof CommitState>, config: z.infer<typeof Config>, colorize: boolean = false, escape_quotes: boolean = false): string {
function build_commit_string(commit_state: z.infer<typeof CommitState>,
config: z.infer<typeof Config>,
colorize: boolean = false,
escape_quotes: boolean = false,
include_trailer: boolean = false
): string {
let commit_string = '';
if (commit_state.type) {
commit_string += colorize ? color.blue(commit_state.type) : commit_state.type
Expand Down Expand Up @@ -295,10 +306,15 @@ function build_commit_string(commit_state: z.infer<typeof CommitState>, config:
commit_string += colorize ? `\n\n${color.reset(commit_state.closes)} ${color.magenta(commit_state.ticket)}` : `\n\n${commit_state.closes} ${commit_state.ticket}`;
}

if (include_trailer && commit_state.trailer) {
commit_string += colorize ? `\n\n${color.dim(commit_state.trailer)}` : `\n\n${commit_state.trailer}`
}

if (escape_quotes) {
commit_string = commit_string.replaceAll('"', '\\"')
}


return commit_string;
}

Loading

0 comments on commit 159a1a9

Please sign in to comment.