Skip to content

Commit

Permalink
docs: prepare script instead of postinstall (typicode#890)
Browse files Browse the repository at this point in the history
Co-authored-by: Nathan Smith <nathan@sonspring.com>
  • Loading branch information
2 people authored and nikoladavitkovski committed Sep 2, 2022
1 parent 66a9407 commit e418f2b
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 39 deletions.
33 changes: 7 additions & 26 deletions docs/README.md
Expand Up @@ -49,16 +49,14 @@ yarn husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'

### Install

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

```shell
# npm
npm install husky --save-dev
npm install pinst --save-dev # if your package is not private

# yarn
yarn add husky --dev
yarn add pinst --dev # if your package is not private
```

2. Enable Git hooks
Expand All @@ -76,23 +74,8 @@ yarn husky install
```js
// package.json
{
"private": true,
"scripts": {
"postinstall": "husky install"
}
}
```

!> **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.

```js
// package.json
{
"private": false,
"scripts": {
"postinstall": "husky install",
"prepublishOnly": "pinst --disable",
"postpublish": "pinst --enable"
"prepare": "husky install"
}
}
```
Expand All @@ -105,8 +88,6 @@ To add a hook, use `husky add <file> [cmd]` (don't forget to run `husky install`
npx husky add .husky/pre-commit "npm test"
```

_Requires npm v7.4+ on Windows_

Try to make a commit

```shell
Expand Down Expand Up @@ -143,20 +124,20 @@ If you want to install husky in another directory, for example `.config`, you ca
// package.json
{
"scripts": {
"postinstall": "husky install .config/husky"
"prepare": "husky install .config/husky"
}
}
```

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`.

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:
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:

```js
// package.json
{
"scripts": {
"postinstall": "cd .. && husky install front/.husky"
"prepare": "cd .. && husky install front/.husky"
}
}
```
Expand Down Expand Up @@ -196,7 +177,7 @@ Alternatively, most Continuous Integration Servers set a `CI` environment variab
[ -n "$CI" ] && exit 0
```

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

```shell
npm install is-ci --save-dev
Expand All @@ -206,7 +187,7 @@ npm install is-ci --save-dev
// package.json
{
"scripts": {
"postinstall": "is-ci || husky install"
"prepare": "is-ci || husky install"
}
}
```
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -36,7 +36,7 @@
"build": "tsc",
"serve": "docsify serve docs",
"lint": "eslint . --ext .js,.ts --ignore-path .gitignore",
"postinstall": "npm run build && node lib/bin install",
"prepare": "npm run build && node lib/bin install",
"preuninstall": "node lib/bin uninstall",
"prepack": "pinst --disable",
"postpack": "pinst --enable",
Expand Down
9 changes: 1 addition & 8 deletions src/commands/init.ts
Expand Up @@ -12,7 +12,7 @@ export function init(): void {

// Add postinstall script
pkg.scripts ||= {}
pkg.scripts.postinstall = 'husky install'
pkg.scripts.prepare = 'husky install'

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

// Add pre-commit sample
add('.husky/pre-commit', 'npm test')

// Add pinst
if (pkg.private !== true) {
console.log(`⚠ if you're publishing your package to npm, you need to disable postinstall script using pinst.
see https://typicode.github.io/husky/#/?id=install
`)
}
}
2 changes: 1 addition & 1 deletion test/init.sh
Expand Up @@ -18,7 +18,7 @@ npm set-script test "echo \"msg from pre-commit hook\" && exit 1"
# cat .husky/*

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

# Test core.hooksPath
test_hooksPath ".husky"
Expand Down
4 changes: 2 additions & 2 deletions test/sub-dir.sh
Expand Up @@ -21,13 +21,13 @@ cd $subDir
cat > package.json << EOL
{
"scripts": {
"postinstall": "cd .. && husky install sub/.husky"
"prepare": "cd .. && husky install sub/.husky"
}
}
EOL

# Install
npm run postinstall
npm run prepare

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

0 comments on commit e418f2b

Please sign in to comment.