- install commitlint cli
npm install --save-dev @commitlint/config-conventional @commitlint/cli- Add convention configuration to project
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js- Install Husky
npm install husky --save-dev- Install Hook
npx husky install- Add Hook
cat <<EEE > .husky/commit-msg
#!/bin/sh
. "\$(dirname "\$0")/_/husky.sh"
npx --no -- commitlint --edit "\${1}"
EEE- Make Hook Executable
chmod a+x .husky/commit-msg
Hi @effemmeffe, The "Add Hook" step in the provided sequence sets up a Git hook that will run the commitlint tool to validate commit messages before they are committed. Here's a breakdown of what's happening:
cat <<EEE > .husky/commit-msg: This command creates a new file namedcommit-msginside the.huskydirectory. Thecatcommand is used to write the content between theEEEdelimiters to the file.#!/bin/sh: This is a shebang line that specifies the shell interpreter to be used for executing the script. In this case, it's the default Bourne shell (sh).. "\$(dirname "\$0")/_/husky.sh": This line sources the Husky environment, which provides access to various Husky utilities and functions.npx --no -- commitlint --edit "\${1}": This line runs thecommitlintcommand usingnpx(an npm package runner) with the--editflag. The--editflag tells commitlint to validate the commit message and open the default text editor if the message is invalid, allowing the user to edit and fix the commit message before committing.--no --is used to prevent any potential npm hooks or scripts from being executed."\${1}"is a shell parameter that represents the first argument passed to the script, which in this case is the commit message file.So in a nutshell that hook script runs the
commitlintcommand with the--editflag, passing the commit message file as an argument. If the commit message doesn't comply with the configured rules (defined in thecommitlint.config.jsfile), commitlint will open the default text editor, allowing the user to modify the commit message before proceeding with the commit.