diff --git a/docs/README.md b/docs/README.md index 00f35f8b7..c2fc32ada 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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 @@ -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" } } ``` @@ -105,8 +88,6 @@ To add a hook, use `husky add [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 @@ -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" } } ``` @@ -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 @@ -206,7 +187,7 @@ npm install is-ci --save-dev // package.json { "scripts": { - "postinstall": "is-ci || husky install" + "prepare": "is-ci || husky install" } } ``` diff --git a/package-lock.json b/package-lock.json index 1a59a8a5c..d8391bd84 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,6 @@ "url": "https://opencollective.com/husky" } ], - "hasInstallScript": true, "license": "Parity-7.0.0 AND MIT WITH Patron-1.0.0", "bin": { "husky": "lib/bin.js" diff --git a/package.json b/package.json index 772d6bb94..e0a6ea46a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/commands/init.ts b/src/commands/init.ts index 6305c50e7..11ef1fc9e 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -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] @@ -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 -`) - } } diff --git a/test/init.sh b/test/init.sh index d5e0536be..e0d1d01bd 100644 --- a/test/init.sh +++ b/test/init.sh @@ -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" diff --git a/test/sub-dir.sh b/test/sub-dir.sh index b4e72b619..851771a29 100644 --- a/test/sub-dir.sh +++ b/test/sub-dir.sh @@ -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"