Skip to content

Commit 84be675

Browse files
docs: prepare script instead of postinstall (#890)
Co-authored-by: Nathan Smith <nathan@sonspring.com>
1 parent a7cc2ce commit 84be675

File tree

6 files changed

+12
-39
lines changed

6 files changed

+12
-39
lines changed

docs/README.md

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,14 @@ yarn husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
4949

5050
### Install
5151

52-
1. Install `husky` and [pinst](https://github.com/typicode/pinst) (optional)
52+
1. Install `husky`
5353

5454
```shell
5555
# npm
5656
npm install husky --save-dev
57-
npm install pinst --save-dev # if your package is not private
5857

5958
# yarn
6059
yarn add husky --dev
61-
yarn add pinst --dev # if your package is not private
6260
```
6361

6462
2. Enable Git hooks
@@ -76,23 +74,8 @@ yarn husky install
7674
```js
7775
// package.json
7876
{
79-
"private": true,
8077
"scripts": {
81-
"postinstall": "husky install"
82-
}
83-
}
84-
```
85-
86-
!> **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.
87-
88-
```js
89-
// package.json
90-
{
91-
"private": false,
92-
"scripts": {
93-
"postinstall": "husky install",
94-
"prepublishOnly": "pinst --disable",
95-
"postpublish": "pinst --enable"
78+
"prepare": "husky install"
9679
}
9780
}
9881
```
@@ -105,8 +88,6 @@ To add a hook, use `husky add <file> [cmd]` (don't forget to run `husky install`
10588
npx husky add .husky/pre-commit "npm test"
10689
```
10790

108-
_Requires npm v7.4+ on Windows_
109-
11091
Try to make a commit
11192

11293
```shell
@@ -143,20 +124,20 @@ If you want to install husky in another directory, for example `.config`, you ca
143124
// package.json
144125
{
145126
"scripts": {
146-
"postinstall": "husky install .config/husky"
127+
"prepare": "husky install .config/husky"
147128
}
148129
}
149130
```
150131

151132
Another case you may be in is if your `package.json` file and `.git` directory are not at the same level. For example, `project/.git` and `project/front/package.json`.
152133

153-
By design, `husky install` must be run in the same directory as `.git`, but you can change directory during `postinstall` script and pass a subdirectory:
134+
By design, `husky install` must be run in the same directory as `.git`, but you can change directory during `prepare` script and pass a subdirectory:
154135

155136
```js
156137
// package.json
157138
{
158139
"scripts": {
159-
"postinstall": "cd .. && husky install front/.husky"
140+
"prepare": "cd .. && husky install front/.husky"
160141
}
161142
}
162143
```
@@ -196,7 +177,7 @@ Alternatively, most Continuous Integration Servers set a `CI` environment variab
196177
[ -n "$CI" ] && exit 0
197178
```
198179

199-
You can also use [is-ci](https://github.com/watson/is-ci) in your `postinstall` script to conditionnally install husky
180+
You can also use [is-ci](https://github.com/watson/is-ci) in your `prepare` script to conditionnally install husky
200181

201182
```shell
202183
npm install is-ci --save-dev
@@ -206,7 +187,7 @@ npm install is-ci --save-dev
206187
// package.json
207188
{
208189
"scripts": {
209-
"postinstall": "is-ci || husky install"
190+
"prepare": "is-ci || husky install"
210191
}
211192
}
212193
```

package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"build": "tsc",
3737
"serve": "docsify serve docs",
3838
"lint": "eslint . --ext .js,.ts --ignore-path .gitignore",
39-
"postinstall": "npm run build && node lib/bin install",
39+
"prepare": "npm run build && node lib/bin install",
4040
"preuninstall": "node lib/bin uninstall",
4141
"prepack": "pinst --disable",
4242
"postpack": "pinst --enable",

src/commands/init.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function init(): void {
1212

1313
// Add postinstall script
1414
pkg.scripts ||= {}
15-
pkg.scripts.postinstall = 'husky install'
15+
pkg.scripts.prepare = 'husky install'
1616

1717
// Write package.json
1818
const indent = regex.exec(str)?.[0]
@@ -23,11 +23,4 @@ export function init(): void {
2323

2424
// Add pre-commit sample
2525
add('.husky/pre-commit', 'npm test')
26-
27-
// Add pinst
28-
if (pkg.private !== true) {
29-
console.log(`⚠ if you're publishing your package to npm, you need to disable postinstall script using pinst.
30-
see https://typicode.github.io/husky/#/?id=install
31-
`)
32-
}
3326
}

test/init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ npm set-script test "echo \"msg from pre-commit hook\" && exit 1"
1818
# cat .husky/*
1919

2020
# Test package.json scripts
21-
grep '"postinstall": "husky install"' package.json || echo -e "\e[0;32mOK\e[m"
21+
grep '"prepare": "husky install"' package.json || echo -e "\e[0;32mOK\e[m"
2222

2323
# Test core.hooksPath
2424
test_hooksPath ".husky"

test/sub-dir.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ cd $subDir
2121
cat > package.json << EOL
2222
{
2323
"scripts": {
24-
"postinstall": "cd .. && husky install sub/.husky"
24+
"prepare": "cd .. && husky install sub/.husky"
2525
}
2626
}
2727
EOL
2828

2929
# Install
30-
npm run postinstall
30+
npm run prepare
3131

3232
# Add hook
3333
npx --no-install husky add pre-commit "echo \"msg from pre-commit hook\" && exit 1"

0 commit comments

Comments
 (0)