🍴 Setup code formatter as a pre-commit Git hooks task only for changes
- 🐶 Use husky to set
pre-commit
hooks - 🧱 Use lint-staged to process only changes in files (not whole project)
- 🧬 Use prettier to have the same syntax in the whole project
-
Create
package.json
:{ "name": "boilerplate-husky-lint-staged-prettier" }
-
Install deps:
npm install -D prettier lint-staged husky
-
Insert in
package.json
:... "scripts": { ... "prepare": "husky install", "pre-commit": "lint-staged" }, "lint-staged": { "*.md": "prettier --write" }, "prettier": { "trailingComma": "all", "tabWidth": 2, "semi": true, "singleQuote": true, "arrowParens": "always", "proseWrap": "never" } ...
Alternate option will be create files for each configuration:
-
Husky
Husky was evolved and introduce new way to setup Git hooks.
- In this article, you can read about dropping
husky.config.js
file: - There is a project to reduce overhead to setup Husky
npx husky-init && npm install npx husky add .husky/pre-commit "npm run pre-commit"
- In this article, you can read about dropping
-
Make a commit (using Git of course) and see how changed files look like 🎊