-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧹 chore(none): add simple git commit-msg hook (#2520)
Add simple commit-msg & pre-commit hooks ## Type of change **NONE: internal change**
- Loading branch information
1 parent
292f455
commit 41c6f4d
Showing
5 changed files
with
110 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
echo "linting...\n" | ||
yarn @bud lint --fix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 7 additions & 7 deletions
14
sources/@repo/yarn-plugin-bud/bundles/@yarnpkg/plugin-bud.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters