Skip to content

Instantly share code, notes, and snippets.

@shubhamp-sf
Last active May 21, 2024 07:29
Show Gist options
  • Save shubhamp-sf/47bddd8d64d7b784a1e923419e04bdd8 to your computer and use it in GitHub Desktop.
Save shubhamp-sf/47bddd8d64d7b784a1e923419e04bdd8 to your computer and use it in GitHub Desktop.
  1. install commitlint cli
npm install --save-dev @commitlint/config-conventional @commitlint/cli
  1. Add convention configuration to project
echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js
  1. Install Husky
npm install husky --save-dev
  1. Install Hook
npx husky install
  1. Add Hook
cat <<EEE > .husky/commit-msg
#!/bin/sh
. "\$(dirname "\$0")/_/husky.sh"

npx --no -- commitlint --edit "\${1}"
EEE
  1. Make Hook Executable
chmod a+x .husky/commit-msg
@shubhamp-sf
Copy link
Author

fixed pre-commit issue by running prettier:fix npm script manually for loopback app

@shubhamp-sf
Copy link
Author

for hint: The '.husky/pre-commit' hook was ignored because it's not set as executable error on mac or linux
run chmod ug+x .husky/*

@effemmeffe
Copy link

Hi. I'm trying to follow this instructions to setup commitlint on windows.
I have one doubt: what does exactly means "Add Hook" and the following lines?
Care to expand the concept, please?
TIA

@shubhamp-sf
Copy link
Author

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
#!/bin/sh
. "\$(dirname "\$0")/_/husky.sh"

npx --no -- commitlint --edit "\${1}"
EEE
  1. cat <<EEE > .husky/commit-msg: This command creates a new file named commit-msg inside the .husky directory. The cat command is used to write the content between the EEE delimiters to the file.

  2. #!/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).

  3. . "\$(dirname "\$0")/_/husky.sh": This line sources the Husky environment, which provides access to various Husky utilities and functions.

  4. npx --no -- commitlint --edit "\${1}": This line runs the commitlint command using npx (an npm package runner) with the --edit flag. The --edit flag 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 commitlint command with the --edit flag, passing the commit message file as an argument. If the commit message doesn't comply with the configured rules (defined in the commitlint.config.js file), commitlint will open the default text editor, allowing the user to modify the commit message before proceeding with the commit.

@effemmeffe
Copy link

Thanks, it's really clear now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment