Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 41472b7

Browse files
committedDec 5, 2020
provide workaround for npm7
1 parent 6dc9a51 commit 41472b7

File tree

3 files changed

+43
-38
lines changed

3 files changed

+43
-38
lines changed
 

‎README.md

+35-35
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
77
Husky can prevent bad `git commit`, `git push` and more 🐶 _woof!_
88

9-
__Announcement: Husky v5 (alpha) has been published, to view v5 docs click [here](https://typicode.github.io/husky).__
9+
**Announcement: Husky v5 has been published, to view v5 docs click [here](https://typicode.github.io/husky).**
10+
11+
**Note for npm 7 users** Currently `INIT_CWD` environment variable is missing in npm v7 and is required for Husky v4 to auto-install (https://github.com/npm/cli/issues/2033). To manually install husky v4, run `npx --no-install husky install .` or upgrade to husky v5.
1012

1113
## Install
1214

@@ -49,7 +51,7 @@ _Git hooks installed by husky will be removed._
4951

5052
## Sponsors
5153

52-
### Companies ($100+/month)
54+
### Companies (\$100+/month)
5355

5456
<!-- for (let i = 0; i < 40; i++) console.log(`[![Husky Sponsor](https://opencollective.com/husky/backer/${i}/avatar)](https://opencollective.com/husky/backer/${i}/website)`) -->
5557

@@ -115,32 +117,32 @@ GitHub sponsors can be viewed on my [profile](https://github.com/typicode). All
115117

116118
## Used by
117119

118-
* [webpack](https://github.com/webpack/webpack)
119-
* [babel](https://github.com/babel/babel)
120-
* [create-react-app](https://github.com/facebook/create-react-app)
121-
* ... and many other awesome projects
120+
- [webpack](https://github.com/webpack/webpack)
121+
- [babel](https://github.com/babel/babel)
122+
- [create-react-app](https://github.com/facebook/create-react-app)
123+
- ... and many other awesome projects
122124

123125
## Guides
124126

125127
<!-- toc -->
126128

127-
* [Upgrading from 0.14](#upgrading-from-014)
128-
* [Supported hooks](#supported-hooks)
129-
* [Access Git params and stdin](#access-git-params-and-stdin)
130-
* [Skip all hooks (rebase)](#skip-all-hooks-rebase)
131-
* [Disable auto-install](#disable-auto-install)
132-
* [CI servers](#ci-servers)
133-
* [Monorepos](#monorepos)
134-
* [Node version managers](#node-version-managers)
135-
* [Local commands (~/.huskyrc)](#local-commands-huskyrc)
136-
* [Multiple commands](#multiple-commands)
137-
* [Troubleshoot](#troubleshoot)
138-
+ [Debug messages](#debug-messages)
139-
+ [Hooks aren't running](#hooks-arent-running)
140-
+ [Commits aren't blocked](#commits-arent-blocked)
141-
+ [Commits are slow](#commits-are-slow)
142-
+ [Testing husky in a new repo](#testing-husky-in-a-new-repo)
143-
+ [ENOENT error 'node_modules/husky/.git/hooks'](#enoent-error-node_moduleshuskygithooks)
129+
- [Upgrading from 0.14](#upgrading-from-014)
130+
- [Supported hooks](#supported-hooks)
131+
- [Access Git params and stdin](#access-git-params-and-stdin)
132+
- [Skip all hooks (rebase)](#skip-all-hooks-rebase)
133+
- [Disable auto-install](#disable-auto-install)
134+
- [CI servers](#ci-servers)
135+
- [Monorepos](#monorepos)
136+
- [Node version managers](#node-version-managers)
137+
- [Local commands (~/.huskyrc)](#local-commands-huskyrc)
138+
- [Multiple commands](#multiple-commands)
139+
- [Troubleshoot](#troubleshoot)
140+
- [Debug messages](#debug-messages)
141+
- [Hooks aren't running](#hooks-arent-running)
142+
- [Commits aren't blocked](#commits-arent-blocked)
143+
- [Commits are slow](#commits-are-slow)
144+
- [Testing husky in a new repo](#testing-husky-in-a-new-repo)
145+
- [ENOENT error 'node_modules/husky/.git/hooks'](#enoent-error-node_moduleshuskygithooks)
144146

145147
<!-- tocstop -->
146148

@@ -214,7 +216,7 @@ By default, Husky won't install on CI servers.
214216

215217
### Monorepos
216218

217-
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.
219+
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.
218220

219221
Generally speaking, you should AVOID defining husky in multiple `package.json`, as each package would overwrite previous husky installation.
220222

@@ -252,6 +254,7 @@ Generally speaking, you should AVOID defining husky in multiple `package.json`,
252254
If you're on Windows, husky will simply use the version installed globally on your system.
253255

254256
For macOS and Linux users:
257+
255258
- 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`.
256259
- 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.
257260

@@ -268,7 +271,7 @@ export NVM_DIR="$HOME/.nvm"
268271

269272
### Multiple commands
270273

271-
By design and just like `scripts` defined in `package.json`, husky will run hook scripts as a single command.
274+
By design and just like `scripts` defined in `package.json`, husky will run hook scripts as a single command.
272275

273276
```json
274277
"pre-commit": "cmd && cmd"
@@ -277,15 +280,12 @@ By design and just like `scripts` defined in `package.json`, husky will run hook
277280
That said, if you prefer to use an array, the recommended approach is to define them in `.huskyrc.js` or `husky.config.js`.
278281

279282
```js
280-
const tasks = arr => arr.join(' && ')
283+
const tasks = (arr) => arr.join(' && ')
281284

282285
module.exports = {
283-
'hooks': {
284-
'pre-commit': tasks([
285-
'cmd',
286-
'cmd'
287-
])
288-
}
286+
hooks: {
287+
'pre-commit': tasks(['cmd', 'cmd']),
288+
},
289289
}
290290
```
291291

@@ -347,9 +347,9 @@ Verify that your version of Git is `>=2.13.0`.
347347

348348
## See also
349349

350-
* [pkg-ok](https://github.com/typicode/pkg-ok) - Prevents publishing a module with bad paths or incorrect line endings
351-
* [please-upgrade-node](https://github.com/typicode/please-upgrade-node) - Show a message to upgrade Node instead of a stacktrace in your CLIs
352-
* [pinst](https://github.com/typicode/pinst) - dev only postinstall hook
350+
- [pkg-ok](https://github.com/typicode/pkg-ok) - Prevents publishing a module with bad paths or incorrect line endings
351+
- [please-upgrade-node](https://github.com/typicode/please-upgrade-node) - Show a message to upgrade Node instead of a stacktrace in your CLIs
352+
- [pinst](https://github.com/typicode/pinst) - dev only postinstall hook
353353

354354
## Patreon
355355

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "4.3.1",
44
"description": "Prevents bad commit or push (git hooks, pre-commit/precommit, pre-push/prepush, post-merge/postmerge and all that stuff...)",
55
"bin": {
6+
"husky": "./husky.js",
67
"husky-run": "./bin/run.js",
78
"husky-upgrade": "./lib/upgrader/bin.js"
89
},

‎src/installer/bin.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function getInitCwdEnv(): string {
4343
if (INIT_CWD === undefined) {
4444
const { name, version } = whichPMRuns()
4545
throw new Error(
46-
`INIT_CWD is not set, please upgrade your package manager (${name} ${version})`
46+
`INIT_CWD is not set, please check that your package manager supports it (${name} ${version})`
4747
)
4848
}
4949

@@ -84,8 +84,12 @@ function run(): void {
8484
checkGitVersion()
8585
}
8686

87-
const INIT_CWD = getInitCwdEnv()
88-
const userPkgDir = getUserPkgDir(INIT_CWD)
87+
let cwd = process.argv[3]
88+
if (cwd === undefined) {
89+
cwd = getInitCwdEnv()
90+
}
91+
92+
const userPkgDir = getUserPkgDir(cwd)
8993
checkGitDirEnv()
9094
const { absoluteGitCommonDir, relativeUserPkgDir } = getDirs(userPkgDir)
9195

0 commit comments

Comments
 (0)
Please sign in to comment.