UNPKG

3.38 kBMarkdownView Raw
1# Documentation
2
3## Supported hooks
4
5`husky` supports all Git hooks defined [here](https://git-scm.com/docs/githooks).
6
7Server-side hooks (`pre-receive`, `update` and `post-receive`) aren't supported.
8
9## Access Git params and stdin
10
11Git hooks can get parameters via command-line arguments and stdin. `husky` makes them accessible via `HUSKY_GIT_PARAMS` and `HUSKY_GIT_STDIN` environment variables.
12
13```
14{
15 "husky": {
16 "hooks": {
17 "commit-msg": "echo $HUSKY_GIT_PARAMS"
18 }
19 }
20}
21```
22
23## Disable auto-install
24
25If you don't want `husky` to automatically install Git hooks, simply set `HUSKY_SKIP_INSTALL` environment variable to `1`.
26
27```sh
28HUSKY_SKIP_INSTALL=1 npm install
29```
30
31## Skip all hooks
32
33During a rebase you may want to skip all hooks, you can set `HUSKY_SKIP_HOOKS` environment variable to `1`.
34
35```sh
36HUSKY_SKIP_HOOKS=1 git rebase ...
37```
38
39## Multi-package repository (monorepo)
40
41If you have a multi-package repository, it's __recommended__ to use tools like [lerna](https://github.com/lerna/lerna) and have `husky` installed ONLY in the root `package.json` to act as the source of truth.
42
43Generally speaking, you should AVOID defining `husky` in multiple `package.json`, as each package would overwrite previous `husky` installations.
44
45```sh
46.
47└── root
48 ├── .git
49 ├── package.json 🐶 # Add husky here
50 └── packages
51 ├── A
52 │ └── package.json
53 ├── B
54 │ └── package.json
55 └── C
56 └── package.json
57```
58
59```js
60// root/package.json
61{
62 "private": true,
63 "devDependencies": {
64 "husky": "..."
65 },
66 "husky": {
67 "hooks": {
68 "pre-commit": "lerna run test"
69 }
70 }
71}
72```
73
74## Node version management
75
76If you're on Windows, husky will simply use the version installed globally on your system.
77
78For macOS and Linux users:
79- if you're running `git` commands in the terminal, `husky` will use the version defined in your shell `PATH`. In other words, if you're a `nvm` user, husky will use the version that you've set with `nvm`.
80- if you're using a GUI client and `nvm`, it may have a different `PATH` and not load `nvm`, in this case the highest `node` version installed by `nvm` will usually be picked. You can also check `~/.node_path` to see which version is used by GUIs and edit if you want to use something else.
81
82## ~/.huskyrc
83
84`husky` will source `~/.huskyrc` file if it exists before running hook scripts.
85You can use it, for example, to load a node version manager or run some `shell` commands before hooks.
86
87```sh
88# ~/.huskyrc
89export NVM_DIR="$HOME/.nvm"
90[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
91```
92
93_This feature is experimental 🧪. Feedback is welcome._
94
95## Debug
96
97It's basic for the moment, but you can use `HUSKY_DEBUG=1` to log debug messages.
98
99## Multiple commands
100
101By design, `husky` will run hook scripts as a single command. Just like `scripts` defined in `package.json` are run.
102
103```json
104{
105 "pre-commit": "cmd && cmd && cmd"
106}
107```
108
109That said, for readability, you may want to use an array. In this case, the recommended way is to define them in a `.huskyrc.js`
110
111```js
112const tasks = arr => arr.join(' && ')
113
114module.exports = {
115 'hooks': {
116 'pre-commit': tasks([
117 'cmd',
118 'cmd',
119 'cmd'
120 ])
121 }
122}
123```
124
125Tools like [npm-run-all](https://github.com/mysticatea/npm-run-all) can help too.