Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: prepare script instead of postinstall #890

Merged
merged 1 commit into from Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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