Skip to content

Commit 20f4cf4

Browse files
Add automated linting setup using Husky and lint-staged to guide
1 parent 39614ab commit 20f4cf4

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

guides/eslint.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,46 @@ Add the following scripts to your `package.json` file:
8282
}
8383
```
8484

85-
### 4 - Add ESLint to your development workflow
85+
### 4 - Add ESLint to your development workflow (optional)
8686

87-
You can now run ESLint manually by executing the following command in your terminal:
87+
#### A - Run it automatically before committing (recommended)
88+
89+
If you want to run ESLint automatically before committing any changes, you can use a tool like [Husky](https://typicode.github.io/husky/) to set up a pre-commit hook and [lint-staged](https://www.npmjs.com/package/lint-staged) to only lint staged files. This will help ensure that your code is always linted before it is committed.
90+
91+
To set this up, first install `Husky` and `lint-staged` as development dependencies:
92+
93+
```bash
94+
npm install --save-dev husky lint-staged
95+
```
96+
97+
Then, add the following configuration to your `package.json` file:
98+
99+
```json
100+
"lint-staged": {
101+
"*.{js,mjs}": "eslint --fix"
102+
}
103+
```
104+
105+
And also in the `package.json` file, add the following lines to the `scripts` section:
106+
107+
```json
108+
"scripts": {
109+
"prepare": "husky",
110+
}
111+
```
112+
113+
Create a pre-commit hook by running the following command in your terminal:
114+
115+
```bash
116+
npx husky init
117+
echo "npx lint-staged" > .husky/pre-commit
118+
```
119+
120+
This will set up `Husky` to run the `lint-staged` command before committing any changes. The `lint-staged` command will only lint the files that are staged for commit, which can save time and resources.
121+
122+
#### B - Run it manually before committing (optional)
123+
124+
You can run ESLint manually by executing the following command in your terminal:
88125

89126
```bash
90127
npm run lint

0 commit comments

Comments
 (0)