Skip to content

Commit 4ff36dc

Browse files
committed
docs: add yarn v2 install
1 parent 77a7f03 commit 4ff36dc

File tree

1 file changed

+48
-23
lines changed

1 file changed

+48
-23
lines changed

docs/README.md

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,17 @@ Already using husky? See [Migrate from 4 to 5](https://typicode.github.io/husky/
2626
## Automatic (recommended)
2727

2828
```shell
29-
# npm
3029
npm install husky --save-dev && npx husky init
31-
32-
# yarn
33-
yarn add husky --dev && yarn husky init
3430
```
3531

36-
The command above will setup husky and create a sample `pre-commit` hook that you can edit. By default, it will run `npm test` when you commit.
32+
The command above will setup husky, modify `package.json` and create a sample `pre-commit` hook that you can edit. By default, it will run `npm test` when you commit.
33+
34+
To add another hook use `husky add`.
3735

38-
To add another hook use `husky add`. For example:
36+
For example:
3937

4038
```shell
41-
# npm
4239
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
43-
44-
# yarn
45-
yarn husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
4640
```
4741

4842
## Manual
@@ -52,21 +46,13 @@ yarn husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
5246
1. Install `husky`
5347

5448
```shell
55-
# npm
5649
npm install husky --save-dev
57-
58-
# yarn
59-
yarn add husky --dev
6050
```
6151

6252
2. Enable Git hooks
6353

6454
```shell
65-
# npm
6655
npx husky install
67-
68-
# yarn
69-
yarn husky install
7056
```
7157

7258
3. To automatically have Git hooks enabled after install, edit `package.json`
@@ -80,6 +66,8 @@ yarn husky install
8066
}
8167
```
8268

69+
!> **Yarn v2 doesn't support `prepare` lifecycle script, so husky needs to be installed differently (this doesn't apply to yarn v1 though). See [Yarn v2 install](/?id=yarn-v2).**
70+
8371
### Add a hook
8472

8573
To add a hook, use `husky add <file> [cmd]` (don't forget to run `husky install` before).
@@ -98,17 +86,54 @@ If `npm test` command fails, your commit will be automatically aborted.
9886

9987
!> **Using Yarn to run commands? There's an issue on Windows with Git Bash, see [Yarn on Windows](#/?id=yarn-on-windows).**
10088

101-
## Uninstall
89+
### Uninstall
10290

10391
```shell
104-
# npm
10592
npm uninstall husky
93+
```
94+
95+
## Yarn v2
96+
97+
### Install
98+
99+
1. Install `husky`
100+
101+
```shell
102+
yarn install husky --save-dev
103+
yarn add pinst --dev # if your package is not private
104+
```
105+
106+
2. Enable Git hooks
107+
108+
```shell
109+
yarn husky install
110+
```
111+
112+
3. To automatically have Git hooks enabled after install, edit `package.json`
106113

107-
# yarn
108-
yarn remove husky && git config --unset core.hooksPath
114+
```js
115+
// package.json
116+
{
117+
"private": true,
118+
"scripts": {
119+
"postinstall": "husky install"
120+
}
121+
}
109122
```
110123

111-
**Note:** When uninstalling with npm, `git config --unset core.hooksPath` will be automatically run for you.
124+
!> **if your package is not private and you're publishing it on a registry like [npmjs.com](https://npmjs.com), you need to disable `postinstall` script using [pinst](https://github.com/typicode/pinst)**. Otherwise, `postinstall` will run when someone installs your package and result in an error.
125+
126+
```js
127+
// package.json
128+
{
129+
"private": false,
130+
"scripts": {
131+
"postinstall": "husky install",
132+
"prepublishOnly": "pinst --disable",
133+
"postpublish": "pinst --enable"
134+
}
135+
}
136+
```
112137

113138
# Recipes
114139

0 commit comments

Comments
 (0)