1 | # Documentation
|
2 |
|
3 | ## Supported hooks
|
4 |
|
5 | `husky` supports all Git hooks defined [here](https://git-scm.com/docs/githooks).
|
6 |
|
7 | Server-side hooks (`pre-receive`, `update` and `post-receive`) aren't supported.
|
8 |
|
9 | ## Access Git params and stdin
|
10 |
|
11 | Git 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 |
|
25 | If you don't want `husky` to automatically install Git hooks, simply set `HUSKY_SKIP_INSTALL` environment variable to `1`.
|
26 |
|
27 | ```sh
|
28 | HUSKY_SKIP_INSTALL=1 npm install
|
29 | ```
|
30 |
|
31 | ## Multi-package repository (monorepo)
|
32 |
|
33 | If 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.
|
34 |
|
35 | Generally speaking, you should AVOID defining `husky` in multiple `package.json`, as each package would overwrite previous `husky` installations.
|
36 |
|
37 | ```sh
|
38 | .
|
39 | └── root
|
40 | ├── .git
|
41 | ├── package.json 🐶 # Add husky here
|
42 | └── packages
|
43 | ├── A
|
44 | │ └── package.json
|
45 | ├── B
|
46 | │ └── package.json
|
47 | └── C
|
48 | └── package.json
|
49 | ```
|
50 |
|
51 | ```js
|
52 | // root/package.json
|
53 | {
|
54 | "private": true,
|
55 | "devDependencies": {
|
56 | "husky": "..."
|
57 | },
|
58 | "husky": {
|
59 | "hooks": {
|
60 | "pre-commit": "lerna run test"
|
61 | }
|
62 | }
|
63 | }
|
64 | ```
|
65 |
|
66 | ## Node version management
|
67 |
|
68 | If you're on Windows, husky will simply use the version installed globally on your system.
|
69 |
|
70 | For macOS and Linux users:
|
71 | - 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`.
|
72 | - 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.
|
73 |
|
74 | ## ~/.huskyrc
|
75 |
|
76 | `husky` will source `~/.huskyrc` file if it exists before running hook scripts.
|
77 | You can use it, for example, to load a node version manager or run some `shell` commands before hooks.
|
78 |
|
79 | ```sh
|
80 | # ~/.huskyrc
|
81 | export NVM_DIR="$HOME/.nvm"
|
82 | [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
|
83 | ```
|
84 |
|
85 | _This feature is experimental 🧪. Feedbacks are welcome._
|
86 |
|
87 | ## Debug
|
88 |
|
89 | It's basic for the moment, but you can use `HUSKY_DEBUG=1` to log debug messages.
|
90 |
|
91 | ## Multiple commands
|
92 |
|
93 | By design, `husky` will run hook scripts as a single command. Just like `scripts` defined in `package.json` are run.
|
94 |
|
95 | ```json
|
96 | {
|
97 | "pre-commit": "cmd && cmd && cmd"
|
98 | }
|
99 | ```
|
100 |
|
101 | That said, for readability, you may want to use an array. In this case, the recommended way is to define them in a `.huskyrc.js`
|
102 |
|
103 | ```js
|
104 | const tasks = arr => arr.join(' && ')
|
105 |
|
106 | module.exports = {
|
107 | 'hooks': {
|
108 | 'pre-commit': tasks([
|
109 | 'cmd',
|
110 | 'cmd',
|
111 | 'cmd'
|
112 | ])
|
113 | }
|
114 | }
|
115 | ```
|
116 |
|
117 | Tools like [npm-run-all](https://github.com/mysticatea/npm-run-all) can help too.
|