Skip to content

Commit

Permalink
🧹 chore(none): add simple git commit-msg hook (#2520)
Browse files Browse the repository at this point in the history
Add simple commit-msg & pre-commit hooks

## Type of change

**NONE: internal change**
  • Loading branch information
kellymears authored Dec 14, 2023
1 parent 292f455 commit 41c6f4d
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 8 deletions.
99 changes: 99 additions & 0 deletions .github/hooks/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env node

/**
* This script is used as a commit-msg Git hook to validate and format commit messages.
* It checks if the commit message follows a specific format and adds an emoji and additional information to the message.
* The commit message format should be: <type>:<severity> <description>
* Where <type> is one of: chore, feat, fix, test, deps
* And <severity> is one of: none, patch, minor, major
* Example: feat:minor add new feature
*
* @remarks
* The script exports an empty object to satisfy the requirements of the ES module system.
*/

import fs from 'node:fs/promises'

import chalk from '@roots/bud-support/chalk'

class emoji {
static chore = `🧹`
static deps = `📦`
static feat = `✨`
static fix = `🩹`
static test = `🧪`

static get(type) {
return emoji[type]
}
}

class code {
static invalid = 1
static write = 2
static read = 3

static get(key) {
return code[key]
}
}

const validator =
/^(chore|feat|fix|test|deps):(none|patch|minor|major)(.*)/



const withCode =
code =>
(...messages) => {
messages.map(message =>
process.stderr.write(`${message.message ?? message}\n`),
)
process.exit(code)
}

const [, , file] = process.argv

const original = await fs
.readFile(file, `utf8`)
.catch(withCode(code.get(`read`)))
.then(message =>
message
.split(`\n`)
.filter(ln => !ln.startsWith(`#`))
.map(ln => ln.trim())
.join(`\n`)
)

/**
* Allow for totally empty commit messages
*/
if (original === `\n`) {
process.exit(0)
}

if (!validator.test(original)) {
withCode(code.get(`invalid`))(
chalk.red(`Invalid commit message format\n`),
`Message should follow the format: <type>:<severity> <description>\n`,
`Where <type> is one of: chore, feat, fix, test, deps\n`,
`And <severity> is one of: none, patch, minor, major\n`,
`Example: feat:minor add new feature`,
)
}

await fs
.writeFile(
file,
original.replace(
validator,
(_, type, severity, description) =>
`${emoji.get(type)} ${type}(${severity}):${description}`,
),
`utf8`,
)
.catch(withCode(code.get(`write`)))

// module exports required for esm
export default {}

2 changes: 2 additions & 0 deletions .github/hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo "linting...\n"
yarn @bud lint --fix
2 changes: 1 addition & 1 deletion .github/workflows/release-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- run: git checkout -b v${{ github.event.inputs.version }}

- run: |
git commit -am "chore: Bump @roots/bud to v${{ github.event.inputs.version }}"
git commit -am "chore:none Bump @roots/bud to v${{ github.event.inputs.version }}"
git push -u origin v${{ github.event.inputs.version }}
- run: |
Expand Down
14 changes: 7 additions & 7 deletions sources/@repo/yarn-plugin-bud/bundles/@yarnpkg/plugin-bud.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export default async () => {
await execute(`yarn`, [`@bud`, `build`])
await execute(`yarn`, [`@bud`])
await execute(`yarn`, [`playwright`, `install`])
await execute(`git`, [`config`, `core.hooksPath`, `.github/hooks`])
}

0 comments on commit 41c6f4d

Please sign in to comment.