Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Husky with commitlint and lint-staged
Edson Junior de Andrade
Edson Junior de Andrade

Posted on • Edited on

     

Husky with commitlint and lint-staged

In this post, I'll show you how to improve your husky workflow, using pre-commit to trigger error checking on your code before uploading it to the repository.

Start husky with the following command:

npx husky-init
Enter fullscreen modeExit fullscreen mode

Verify that the husky information has been entered into your package.json:

{"scripts":{..."prepare":"husky install"},"devDependencies":{..."husky":"^6.0.0"}}
Enter fullscreen modeExit fullscreen mode

Install husky in your project:

#yarnyarn; npx husky add .husky/commit-msg'npx --no-install commitlint --edit ""'#npmnpminstall; npx husky add .husky/commit-msg'npx --no-install commitlint --edit ""'
Enter fullscreen modeExit fullscreen mode

Checking.husky/commit-msg file you might find the following bash script:

#!/bin/sh."$(dirname"$0")/_/husky.sh"npx--no-install commitlint--edit""
Enter fullscreen modeExit fullscreen mode

Let's add the commit-lint package to the lint confirmation messages:

#yarnyarn add @commitlint/config-conventional @commitlint/cli--dev#npmnpminstall @commitlint/config-conventional @commitlint/cli--dev
Enter fullscreen modeExit fullscreen mode

Create thecommitlint.config.js file in the root of your directory and insert the following contents:

module.exports={extends:['@commitlint/config-conventional']};
Enter fullscreen modeExit fullscreen mode

Now we will install lint-staged:

#yarnyarn add lint-staged--dev#npmnpminstalllint-staged--save-dev
Enter fullscreen modeExit fullscreen mode

In package.json insert the following script for running lint-staged into our project:

{"scripts":{..."pre-commit":"lint-staged","prepare":"husky install"}}
Enter fullscreen modeExit fullscreen mode

We will create the.lintstagedrc file to run eslint and prettier at commit time:

{"src/**/*.+(js|json|ts|tsx)":["eslint"],"src/**/*.{js,jsx,ts,tsx,json,css,scss,md}":["eslint --fix","prettier --write"]}
Enter fullscreen modeExit fullscreen mode

Inside.husky/pre-commit insert the following script:

#!/bin/sh."$(dirname"$0")/_/husky.sh"yarn run pre-commit
Enter fullscreen modeExit fullscreen mode

Finally, run the command that created the 'prepare-commit-msg' file:

#yarnnpx husky add .husky/prepare-commit-msg'exec < /dev/tty && node_modules/.bin/cz --hook || true'; yarn#npmnpx husky add .husky/prepare-commit-msg'exec < /dev/tty && node_modules/.bin/cz --hook || true'; npminstall
Enter fullscreen modeExit fullscreen mode

That's it! Now test everything we have installed.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Front-end developer, Web e Mobile
  • Location
    Umuarama-PR, Brasil
  • Education
    Graduated and postgraduate in web and mobile development
  • Work
    Mobile Developer at Superlógica Tecnologias
  • Joined

More fromEdson Junior de Andrade

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp