How to Prevent Bad Git Commits with Lint-staged
Git hooks to prevent bad commits.
Lint-staged can be found at https://github.com/okonet/lint-staged
Lint-staged is a tool that prevents bad code from leaving a developer's computer. It allows us to run commands on staged files when a developer runs git commit
. If any of the commands fail then the commit also fails—preventing bad commits from being pushed to GitHub. Lint-staged is a great way of ensuring linting, testing, and other operations are performed before contributions get pushed to GitHub.
Lint-staged uses Husky and Git hooks behind the scenes.
Run npx [email protected] lint-staged
at the root of Scroller. Since we need to use the [email protected]
script, which is a remote script, we need npx
. Enter y
to proceed if the following is prompted:
Need to install the following packages:
mrm@2
Ok to proceed? (y)
This adds lint-staged
and husky
as a devDependency, and a .husky/
directory which can be ignored. Lint-staged also adds a "lint-staged"
property into our package.json
where we'll define what scripts we want to run before commits. Lint-staged defaults with "*.js": "eslint --cache --fix"
which will run ESLint on changed JavaScript files; we'll update this later to lint TypeScript files and run tests.
"lint-staged": {
"*.ts": [
"eslint --cache --fix",
"jest --passWithNoTests"
]
}
}
Loading...