Husky is used to make our commits more cool, and powerful to help development easier. In this guide, husky will be used to format both the files (source code) and commits themselves before they are executed (thus "pre" in the pre-commit hooks).
# install huskyyarn add -D husky
# initializenpx husky-init
If we would like to have our commits automatically format our code on every commit - to ensure that the codebase follows a specified standard, defined in the prettierrc
file, then this is a handy tool to have.
Add Prettier and eslint to your project:
# prettiernpm install -D prettier
# eslintnpm install -D eslint
Add a prettier config file to the repository - named .prettierrc.json
(or following the specified format for configuration files:
{ "trailingComma": "all", "tabWidth": 4, "semi": false, "singleQuote": true //more rules below}
Initialize EsLint:
npx eslint --init
Set up ESLint to work with prettier
Add prettier plugin to the eslint configuration file:
// .eslintrc.json{ "extends": [ // other extensions, "prettier" ]}
Now, you can specify prettier rules to work with your linter and not have both ESLint and Prettier enforcing different styles
npm install -D pretty-quick
npx husky set ./husky/pre-commit "npx pretty-quick --staged"
There are more ways to configure your prettier pre-commit hooks found here.
Commitlint is a tool that lints commits - and make sure they are up to standard. We will also add a husky pre-commit hook that lints our commit messages
npm install -D @commitlint/config-conventional @commitlint/cli
# Configure commitlint to use conventional configecho "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
. Add the commitlint hooks:
# Add hooknpx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
Add a husky pre-commit hook config to the package.json
"husky": { "hooks": { "prepare-commit-msg": "exec < /dev/tty && git cz --hook || true" }}
Commitizen is a command line interface tool that can be helpful in making commits a pretty-forward process following your linting rules.
Install the tool
npm install -D commitizen
Add a script to the package.json to easily run commitizen:
{ "scripts": { "commit": "cz" }}
An Example use:
This will assist in generating changelogs automatically from commits:
npx commitizen init cz-conventional-changelog -D --save-exac
We can use Standard Version to automatically generate versions for out projects.
Install Standard Version
npm install -D standard-version
Add scripts to easily run releases and generate changelogs automaticallly:
{ "scripts": { "release": "standard-version" }}
Another option to Standard version is semantic-release
You can now run your first release by:
npm run release
Plug: Here is an example of a changelog for my website.
If you create a release - then you can push that release by running:
git push --follow-tags
I ended up writing a simple npm package to automate setting this whole process up for new projects You can find the package here.
Last modified on: Sat Feb 26 2022