How to Open a Husky Truck Tool Box
Tired of run ESLint every time before commiting code to a remote repo? In this article, I'm going to demonstrate how to automatically lint code convention and prettify it before pushing code to a remote repo, with:
- ESLint: linter to lint JavaScript code
- Husky: Pre-commit management tool
- Lint Staged: specify which file will be lint against. We don't want to run
Install Dependencies
Install dependencies with npm:
npm install prettier husky lint-staged es-lint
or install with yarn:
yarn add prettier husky lint-staged es-lint
Setup ESLint rules
Next step would be setup ESLint file. Here is example project demonstrates ESLint. In my .eslintrc file which has been set up to prevent using of the double quote.
Setup Pre-commit Hook with Husky
You can view the list of support hook by git in https://git-scm.com/docs/githooks. Here is some fundamental hook:
- post-commit/pre-commit: run after/before git commit
- post/pre-push: run after/before git push
- post/pre-checkout: run after/before checkout
- post/pre-rebase: run after/before rebase
To add Githook you add your hook in your package.json file under hooks field of husky. I'm going to not allow to commit when ESLint error happened using a pre-commit hook.
Then create JavaScript file in your project and then create variable use double quote which yields ESLint single quote
var a = "b" // this will yield eslint error
Fortunately, most ESLint error can be fixed automatically using fix flag in ESLint so I'm going to add npm fix command which runs ESLint with fix command to automatically fix ESLint error before lint
And result in ESLint error being automatically fixed.
Use Lint Staged to specify which file should be linted
You fix a CSS selected in your selector, add image and that still trigger ESLint ? Running a lint process on a whole project is slow and linting results can be irrelevant. Ultimately you only want to lint files that will be committed.
With linted-stage. You can specify what type of STAGED FILE should be lint. Just place field under lint-staged field:
- key: regex that matches staged file
- value: action should be executed against matched file
After this, you can't fix eslint bugs, commit then try to add non-javascript file and then commit again. You will see it won't trigger eslint to run since staged not contain JavaScript file.
Format code before making a commit with Prettier (Optional)
Prettier is an opinionated configurable code formatting CLI that support format: HTML, CSS, JavaScript and more… Check https://prettier.io/docs/en/why-prettier.html for more detail
Install it with:
yarn add prettier
Try add a JavaScript file with bad format
var a = 1; // lot of space
Then run prettier against the file with command:
prettier <javascript file> --write
Or run prettier against staged files with this configuration:
Try commit and you will see content of bad format javascript will be formatted into:
var a = 1; // lot of space
From now the file will be automatically lint and prettify. For more information about configuring prettier check: https://prettier.io/docs/en/options.html
Conclusion
You guys can view full setup code repository at https://github.com/PhmNgocNghia/example-husky-linted-stage-eslint-setup/tree/master/src
With the help of husky, lint-staged. You don't have to lint JavaScript file your self anymore. What do you think about this setup? Share your thought in the comment section below.
How to Open a Husky Truck Tool Box
Source: https://medium.com/dwarves-foundation/automatically-lint-prettify-your-javascript-project-using-husky-lint-staged-cae8e685bb06