Lint JS and CSS files staged by git
Linting makes more sense when running before commiting you code into repository. In this case you can ensure no 💩 is going put into it and enforce styles. This repsitory contains 2 shell scrtips that run ESLint and Stylelint against only currently staged files.
npm install --save-dev lint-staged
Setup ESLint and Stylelint just like you would do normally. Add appropriate .eslintrc
and .stylelintrc
configs (see ESLint and Stylelint docs if you need help here).
To start linting, you have to install a pre-commit hook:
npm install --save-dev pre-commit
- Add
"eslint-staged": "eslint-staged"
to scripts section of package.json (if you want to lint your JS) - Add
"stylelint-staged": "stylelint-staged"
to scripts section of package.json (if you want to lint your CSS) - Add
pre-commit": [ "eslint-staged", "stylelint-staged" ]
to package.json
Example package.json
{
"name": "My project",
"version": "0.0.1",
"scripts": {
"eslint-staged": "eslint-staged",
"stylelint-staged": "stylelint-staged"
},
"pre-commit": [ "eslint-staged", "stylelint-staged" ]
"devDependencies: {}
}