Skip to content

Commit 4a618e6

Browse files
authored
docs: improve local hook install explanation (#2457)
* docs(hook): husky v5 installation * docs(readme): update readme
1 parent 4dda1e7 commit 4a618e6

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

README.md

+16-15
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,28 @@ npm install --save-dev @commitlint/config-conventional @commitlint/cli
9393
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
9494
```
9595

96-
To lint commits before they are created you can use Husky's 'commit-msg' hook.
96+
To lint commits before they are created you can use Husky's `commit-msg` hook:
9797

98-
Install in your project `npm install husky --save-dev` or `yarn add -D husky`.
99-
100-
After that, you can create a `.huskyrc` file or add to your `package.json` the following code for
98+
```sh
99+
# Install Husky v5
100+
npm install husky --save-dev
101+
# or
102+
yarn add husky --dev
101103

102-
Husky V4:
104+
# Active hooks
105+
npx husky install
106+
# or
107+
yarn husky install
103108

104-
```json
105-
{
106-
"husky": {
107-
"hooks": {
108-
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
109-
}
110-
}
111-
}
109+
# Add hook
110+
npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"
111+
# or
112+
yarn husky add .husky/commit-msg "yarn commitlint --edit $1"
112113
```
113114

114-
Husky V5
115+
If the file `.husky/commit-msg` already exists, you can edit the file and put this:
115116

116-
```
117+
```sh
117118
# .husky/commit-msg
118119
# ...
119120
npx --no-install commitlint --edit $1

docs/guides-local-setup.md

+23-18
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ Install `commitlint` and a `commitlint-config-*` of your choice as devDependency
1010
configure `commitlint` to use it.
1111

1212
```bash
13-
# Create a package.json if needed
14-
npm init
15-
1613
# Install and configure if needed
1714
npm install --save-dev @commitlint/{cli,config-conventional}
1815
echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js
@@ -24,24 +21,32 @@ Alternatively the configuration can be defined in `.commitlintrc.js`, `.commitli
2421

2522
Install `husky` as devDependency, a handy git hook helper available on npm.
2623

27-
```bash
28-
npm install --save-dev husky
24+
```sh
25+
# Install Husky v5
26+
npm install husky --save-dev
27+
# or
28+
yarn add husky --dev
29+
30+
# Active hooks
31+
npx husky install
32+
# or
33+
yarn husky install
34+
35+
# Add hook
36+
npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"
37+
# or
38+
yarn husky add .husky/commit-msg "yarn commitlint --edit $1"
2939
```
3040

31-
This allows us to add [git hooks](https://git-scm.com/docs/githooks) directly into our `package.json` via the `husky.hooks` field.
32-
33-
```json
34-
// package.json
35-
{
36-
"husky": {
37-
"hooks": {
38-
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
39-
}
40-
}
41-
}
42-
```
41+
If the file `.husky/commit-msg` already exists, you can edit the file and put this:
4342

44-
Using `commit-msg` gives us exactly what we want: It is executed whenever a new commit is created. Passing husky's `HUSKY_GIT_PARAMS` to `commitlint` via the `-E|--env` flag directs it to the relevant edit file. `-e` would default to `.git/COMMIT_EDITMSG`.
43+
```sh
44+
# .husky/commit-msg
45+
# ...
46+
npx --no-install commitlint --edit $1
47+
# or
48+
yarn commitlint --edit $1
49+
```
4550

4651
## Test
4752

0 commit comments

Comments
 (0)