From 4c52a12568ed02afeed8bb86bf1292c6afa692eb Mon Sep 17 00:00:00 2001 From: typicode Date: Tue, 2 Mar 2021 02:40:58 +0100 Subject: [PATCH] fix(init): use postinstall for yarn v2 --- package.json | 2 +- src/bin.ts | 5 +++- src/commands/init.ts | 20 ++++++++++++---- test/_functions.sh | 13 ++++++----- test/config-dir.sh | 5 +--- test/default.sh | 7 ++---- test/{init.sh => init-npm.sh} | 12 ++++------ test/init-yarn-1.sh | 43 +++++++++++++++++++++++++++++++++++ test/init-yarn-2.sh | 40 ++++++++++++++++++++++++++++++++ test/not-git-dir.sh | 2 +- test/sub-dir.sh | 5 +--- 11 files changed, 120 insertions(+), 34 deletions(-) rename test/{init.sh => init-npm.sh} (63%) create mode 100644 test/init-yarn-1.sh create mode 100644 test/init-yarn-2.sh diff --git a/package.json b/package.json index 4add5e20a..c2c02526c 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "prepublishOnly": "pinst --disable", "postpublish": "pinst --enable", "pretest": "npm run build --silent && npm pack --silent", - "test": "sh ./test/init.sh && sh ./test/default.sh && sh ./test/sub-dir.sh && sh ./test/config-dir.sh && sh ./test/not-git-dir.sh", + "test": "sh ./test/init-npm.sh && sh ./test/init-yarn-2.sh && sh ./test/default.sh && sh ./test/sub-dir.sh && sh ./test/config-dir.sh && sh ./test/not-git-dir.sh", "posttest": "rm husky-*.tgz", "commit": "commit" }, diff --git a/src/bin.ts b/src/bin.ts index fe9dc1c3a..97f19541a 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -43,7 +43,10 @@ Examples switch (cmd) { case 'init': { - init() + const isYarn2 = String(process.env.npm_config_user_agent).startsWith( + 'yarn/2' + ) + init(isYarn2) break } case 'install': { diff --git a/src/commands/init.ts b/src/commands/init.ts index 11ef1fc9e..2889dde87 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -5,18 +5,30 @@ import { install } from './install' const regex = /^[ ]+|\t+/m -export function init(): void { +export function init(isYarn2: boolean): void { // Read package.json const str = fs.readFileSync('package.json', 'utf-8') const pkg = JSON.parse(str) as PackageJson - // Add postinstall script - pkg.scripts ||= {} - pkg.scripts.prepare = 'husky install' + // Update package.json fields + if (isYarn2) { + pkg.scripts ||= {} + pkg.scripts.postinstall = 'husky install' + if (pkg.private !== true) { + pkg.scripts.prepublishOnly = 'pinst --disable' + pkg.scripts.postpublish = 'pinst --enable' + pkg.devDependencies ||= {} + pkg.devDependencies.pinst = '^2.0.0' + } + } else { + pkg.scripts ||= {} + pkg.scripts.prepare = 'husky install' + } // Write package.json const indent = regex.exec(str)?.[0] fs.writeFileSync('package.json', `${JSON.stringify(pkg, null, indent)}\n`) + console.log('husky - updated package.json') // Install husky install() diff --git a/test/_functions.sh b/test/_functions.sh index 3f90fe033..3648eebbe 100644 --- a/test/_functions.sh +++ b/test/_functions.sh @@ -11,15 +11,12 @@ function title { # Create $1 and install tgz function cd_and_install_tgz { - # generated by pretest script - tgz="./husky-*.tgz" - # Create directory mkdir -p $1 - # Install - cp $tgz $1 - cd $1 && npm init -y && npm install $tgz + # Install tarball generated by pretest script + cp ./husky-*.tgz $1/husky.tgz + cd $1 && npm init -y && npm install husky.tgz } function init_git { @@ -35,3 +32,7 @@ function test_hooksPath { exit 1 fi } + +function ok { + echo -e "\e[0;32mOK\e[m" +} diff --git a/test/config-dir.sh b/test/config-dir.sh index a040983bb..79ed6cbcf 100644 --- a/test/config-dir.sh +++ b/test/config-dir.sh @@ -17,12 +17,9 @@ init_git npx --no-install husky install .config/husky npx --no-install husky add .config/husky/pre-commit "echo \"msg from pre-commit hook\" && exit 1" -# Debug -# cat .husky/* - # Test core.hooksPath test_hooksPath ".config/husky" # Test pre-commit git add package.json -git commit -m "should fail" || echo -e "\e[0;32mOK\e[m" +git commit -m "should fail" || ok diff --git a/test/default.sh b/test/default.sh index 882aad6f9..c8c97a44f 100644 --- a/test/default.sh +++ b/test/default.sh @@ -13,16 +13,13 @@ init_git npx --no-install husky install npx --no-install husky add .husky/pre-commit "echo \"msg from pre-commit hook\" && exit 1" -# Debug -# cat .husky/* - # Test core.hooksPath test_hooksPath ".husky" # Test pre-commit git add package.json -git commit -m "should fail" || echo -e "\e[0;32mOK\e[m" +git commit -m "should fail" || ok # Uninstall npx --no-install husky uninstall -git config core.hooksPath || echo -e "\e[0;32mOK\e[m" +git config core.hooksPath || ok diff --git a/test/init.sh b/test/init-npm.sh similarity index 63% rename from test/init.sh rename to test/init-npm.sh index e0d1d01bd..cae2f386a 100644 --- a/test/init.sh +++ b/test/init-npm.sh @@ -4,7 +4,7 @@ . "$(dirname "$0")/_functions.sh" title "init" -tempDir="/tmp/husky-default-test" +tempDir="/tmp/husky-init-npm-test" rm -rf $tempDir cd_and_install_tgz $tempDir @@ -13,20 +13,16 @@ init_git npx --no-install husky init npm set-script test "echo \"msg from pre-commit hook\" && exit 1" - -# Debug -# cat .husky/* - # Test package.json scripts -grep '"prepare": "husky install"' package.json || echo -e "\e[0;32mOK\e[m" +grep '"prepare": "husky install"' package.json || ok # Test core.hooksPath test_hooksPath ".husky" # Test pre-commit git add package.json -git commit -m "should fail" || echo -e "\e[0;32mOK\e[m" +git commit -m "should fail" || ok # Uninstall npx --no-install husky uninstall -git config core.hooksPath || echo -e "\e[0;32mOK\e[m" +git config core.hooksPath || ok diff --git a/test/init-yarn-1.sh b/test/init-yarn-1.sh new file mode 100644 index 000000000..e4c5f74f9 --- /dev/null +++ b/test/init-yarn-1.sh @@ -0,0 +1,43 @@ +# shellcheck shell=bash + +# shellcheck source=./_functions.sh +. "$(dirname "$0")/_functions.sh" + +title "init" +tempDir="/tmp/husky-yarn-1-test" + +rm -rf $tempDir + +# generated by pretest script +tgz="./husky-*.tgz" + +# Create directory +mkdir -p $tempDir + +# Install +cp $tgz $tempDir/husky.tgz +yarn set version berry +cd $tempDir && yarn init -y && yarn add ./husky.tgz + +init_git +yarn husky init +npm set-script test "echo \"msg from pre-commit hook\" && exit 1" + +# Debug +# cat .husky/* + +# Test package.json scripts +grep '"postinstall": "husky install"' package.json || ok +grep '"prepublishOnly": "pinst --disable"' package.json || ok +grep '"postpublish": "pinst --enable"' package.json || ok + +# Test core.hooksPath +test_hooksPath ".husky" + +# Test pre-commit +git add package.json +git commit -m "should fail" || ok + +# Uninstall +yarn remove husky +git config core.hooksPath || ok diff --git a/test/init-yarn-2.sh b/test/init-yarn-2.sh new file mode 100644 index 000000000..09811245f --- /dev/null +++ b/test/init-yarn-2.sh @@ -0,0 +1,40 @@ +# shellcheck shell=bash + +# shellcheck source=./_functions.sh +. "$(dirname "$0")/_functions.sh" + +title "init" +tempDir="/tmp/husky-yarn-2-test" + +rm -rf $tempDir + +# generated by pretest script +tgz="./husky-*.tgz" + +# Create directory +mkdir -p $tempDir + +# Install +cp $tgz $tempDir/husky.tgz +yarn set version berry +cd $tempDir && yarn init -y && yarn add ./husky.tgz + +init_git +yarn husky init +npm set-script test "echo \"msg from pre-commit hook\" && exit 1" + +# Test package.json scripts +grep '"postinstall": "husky install"' package.json || ok +grep '"prepublishOnly": "pinst --disable"' package.json || ok +grep '"postpublish": "pinst --enable"' package.json || ok + +# Test core.hooksPath +test_hooksPath ".husky" + +# Test pre-commit +git add package.json +git commit -m "should fail" || ok + +# Uninstall +yarn remove husky +git config core.hooksPath || ok diff --git a/test/not-git-dir.sh b/test/not-git-dir.sh index fb4a7e365..60ecb1f42 100644 --- a/test/not-git-dir.sh +++ b/test/not-git-dir.sh @@ -10,4 +10,4 @@ rm -rf $tempDir cd_and_install_tgz $tempDir # Should not fail -npx --no-install husky install && echo -e "\e[0;32mOK\e[m" +npx --no-install husky install && ok diff --git a/test/sub-dir.sh b/test/sub-dir.sh index 851771a29..8b130b29f 100644 --- a/test/sub-dir.sh +++ b/test/sub-dir.sh @@ -32,12 +32,9 @@ npm run prepare # Add hook npx --no-install husky add pre-commit "echo \"msg from pre-commit hook\" && exit 1" -# Debug -# cat .husky/* - # Test core.hooksPath test_hooksPath "sub/.husky" # Test pre-commit git add package.json -git commit -m "should fail" || echo -e "\e[0;32mOK\e[m" +git commit -m "should fail" || ok