Skip to content
/ bud Public

Commit 41c6f4d

Browse files
authored
🧹 chore(none): add simple git commit-msg hook (#2520)
Add simple commit-msg & pre-commit hooks ## Type of change **NONE: internal change**
1 parent 292f455 commit 41c6f4d

File tree

5 files changed

+110
-8
lines changed

5 files changed

+110
-8
lines changed

.github/hooks/commit-msg

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* This script is used as a commit-msg Git hook to validate and format commit messages.
5+
* It checks if the commit message follows a specific format and adds an emoji and additional information to the message.
6+
* The commit message format should be: <type>:<severity> <description>
7+
* Where <type> is one of: chore, feat, fix, test, deps
8+
* And <severity> is one of: none, patch, minor, major
9+
* Example: feat:minor add new feature
10+
*
11+
* @remarks
12+
* The script exports an empty object to satisfy the requirements of the ES module system.
13+
*/
14+
15+
import fs from 'node:fs/promises'
16+
17+
import chalk from '@roots/bud-support/chalk'
18+
19+
class emoji {
20+
static chore = `🧹`
21+
static deps = `📦`
22+
static feat = `✨`
23+
static fix = `🩹`
24+
static test = `🧪`
25+
26+
static get(type) {
27+
return emoji[type]
28+
}
29+
}
30+
31+
class code {
32+
static invalid = 1
33+
static write = 2
34+
static read = 3
35+
36+
static get(key) {
37+
return code[key]
38+
}
39+
}
40+
41+
const validator =
42+
/^(chore|feat|fix|test|deps):(none|patch|minor|major)(.*)/
43+
44+
45+
46+
const withCode =
47+
code =>
48+
(...messages) => {
49+
messages.map(message =>
50+
process.stderr.write(`${message.message ?? message}\n`),
51+
)
52+
process.exit(code)
53+
}
54+
55+
const [, , file] = process.argv
56+
57+
const original = await fs
58+
.readFile(file, `utf8`)
59+
.catch(withCode(code.get(`read`)))
60+
.then(message =>
61+
message
62+
.split(`\n`)
63+
.filter(ln => !ln.startsWith(`#`))
64+
.map(ln => ln.trim())
65+
.join(`\n`)
66+
)
67+
68+
/**
69+
* Allow for totally empty commit messages
70+
*/
71+
if (original === `\n`) {
72+
process.exit(0)
73+
}
74+
75+
if (!validator.test(original)) {
76+
withCode(code.get(`invalid`))(
77+
chalk.red(`Invalid commit message format\n`),
78+
`Message should follow the format: <type>:<severity> <description>\n`,
79+
`Where <type> is one of: chore, feat, fix, test, deps\n`,
80+
`And <severity> is one of: none, patch, minor, major\n`,
81+
`Example: feat:minor add new feature`,
82+
)
83+
}
84+
85+
await fs
86+
.writeFile(
87+
file,
88+
original.replace(
89+
validator,
90+
(_, type, severity, description) =>
91+
`${emoji.get(type)} ${type}(${severity}):${description}`,
92+
),
93+
`utf8`,
94+
)
95+
.catch(withCode(code.get(`write`)))
96+
97+
// module exports required for esm
98+
export default {}
99+

.github/hooks/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
echo "linting...\n"
2+
yarn @bud lint --fix

.github/workflows/release-main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
- run: git checkout -b v${{ github.event.inputs.version }}
3636

3737
- run: |
38-
git commit -am "chore: Bump @roots/bud to v${{ github.event.inputs.version }}"
38+
git commit -am "chore:none Bump @roots/bud to v${{ github.event.inputs.version }}"
3939
git push -u origin v${{ github.event.inputs.version }}
4040
4141
- run: |

sources/@repo/yarn-plugin-bud/bundles/@yarnpkg/plugin-bud.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sources/@repo/yarn-plugin-bud/sources/hooks/afterAllInstalled.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export default async () => {
88
await execute(`yarn`, [`@bud`, `build`])
99
await execute(`yarn`, [`@bud`])
1010
await execute(`yarn`, [`playwright`, `install`])
11+
await execute(`git`, [`config`, `core.hooksPath`, `.github/hooks`])
1112
}

0 commit comments

Comments
 (0)