Skip to content

Commit 4c52a12

Browse files
committed
fix(init): use postinstall for yarn v2
1 parent 67f1c26 commit 4c52a12

File tree

11 files changed

+120
-34
lines changed

11 files changed

+120
-34
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"prepublishOnly": "pinst --disable",
4646
"postpublish": "pinst --enable",
4747
"pretest": "npm run build --silent && npm pack --silent",
48-
"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",
48+
"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",
4949
"posttest": "rm husky-*.tgz",
5050
"commit": "commit"
5151
},

src/bin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ Examples
4343

4444
switch (cmd) {
4545
case 'init': {
46-
init()
46+
const isYarn2 = String(process.env.npm_config_user_agent).startsWith(
47+
'yarn/2'
48+
)
49+
init(isYarn2)
4750
break
4851
}
4952
case 'install': {

src/commands/init.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,30 @@ import { install } from './install'
55

66
const regex = /^[ ]+|\t+/m
77

8-
export function init(): void {
8+
export function init(isYarn2: boolean): void {
99
// Read package.json
1010
const str = fs.readFileSync('package.json', 'utf-8')
1111
const pkg = JSON.parse(str) as PackageJson
1212

13-
// Add postinstall script
14-
pkg.scripts ||= {}
15-
pkg.scripts.prepare = 'husky install'
13+
// Update package.json fields
14+
if (isYarn2) {
15+
pkg.scripts ||= {}
16+
pkg.scripts.postinstall = 'husky install'
17+
if (pkg.private !== true) {
18+
pkg.scripts.prepublishOnly = 'pinst --disable'
19+
pkg.scripts.postpublish = 'pinst --enable'
20+
pkg.devDependencies ||= {}
21+
pkg.devDependencies.pinst = '^2.0.0'
22+
}
23+
} else {
24+
pkg.scripts ||= {}
25+
pkg.scripts.prepare = 'husky install'
26+
}
1627

1728
// Write package.json
1829
const indent = regex.exec(str)?.[0]
1930
fs.writeFileSync('package.json', `${JSON.stringify(pkg, null, indent)}\n`)
31+
console.log('husky - updated package.json')
2032

2133
// Install husky
2234
install()

test/_functions.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@ function title {
1111

1212
# Create $1 and install tgz
1313
function cd_and_install_tgz {
14-
# generated by pretest script
15-
tgz="./husky-*.tgz"
16-
1714
# Create directory
1815
mkdir -p $1
1916

20-
# Install
21-
cp $tgz $1
22-
cd $1 && npm init -y && npm install $tgz
17+
# Install tarball generated by pretest script
18+
cp ./husky-*.tgz $1/husky.tgz
19+
cd $1 && npm init -y && npm install husky.tgz
2320
}
2421

2522
function init_git {
@@ -35,3 +32,7 @@ function test_hooksPath {
3532
exit 1
3633
fi
3734
}
35+
36+
function ok {
37+
echo -e "\e[0;32mOK\e[m"
38+
}

test/config-dir.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ init_git
1717
npx --no-install husky install .config/husky
1818
npx --no-install husky add .config/husky/pre-commit "echo \"msg from pre-commit hook\" && exit 1"
1919

20-
# Debug
21-
# cat .husky/*
22-
2320
# Test core.hooksPath
2421
test_hooksPath ".config/husky"
2522

2623
# Test pre-commit
2724
git add package.json
28-
git commit -m "should fail" || echo -e "\e[0;32mOK\e[m"
25+
git commit -m "should fail" || ok

test/default.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@ init_git
1313
npx --no-install husky install
1414
npx --no-install husky add .husky/pre-commit "echo \"msg from pre-commit hook\" && exit 1"
1515

16-
# Debug
17-
# cat .husky/*
18-
1916
# Test core.hooksPath
2017
test_hooksPath ".husky"
2118

2219
# Test pre-commit
2320
git add package.json
24-
git commit -m "should fail" || echo -e "\e[0;32mOK\e[m"
21+
git commit -m "should fail" || ok
2522

2623
# Uninstall
2724
npx --no-install husky uninstall
28-
git config core.hooksPath || echo -e "\e[0;32mOK\e[m"
25+
git config core.hooksPath || ok

test/init.sh renamed to test/init-npm.sh

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
. "$(dirname "$0")/_functions.sh"
55

66
title "init"
7-
tempDir="/tmp/husky-default-test"
7+
tempDir="/tmp/husky-init-npm-test"
88

99
rm -rf $tempDir
1010
cd_and_install_tgz $tempDir
@@ -13,20 +13,16 @@ init_git
1313
npx --no-install husky init
1414
npm set-script test "echo \"msg from pre-commit hook\" && exit 1"
1515

16-
17-
# Debug
18-
# cat .husky/*
19-
2016
# Test package.json scripts
21-
grep '"prepare": "husky install"' package.json || echo -e "\e[0;32mOK\e[m"
17+
grep '"prepare": "husky install"' package.json || ok
2218

2319
# Test core.hooksPath
2420
test_hooksPath ".husky"
2521

2622
# Test pre-commit
2723
git add package.json
28-
git commit -m "should fail" || echo -e "\e[0;32mOK\e[m"
24+
git commit -m "should fail" || ok
2925

3026
# Uninstall
3127
npx --no-install husky uninstall
32-
git config core.hooksPath || echo -e "\e[0;32mOK\e[m"
28+
git config core.hooksPath || ok

test/init-yarn-1.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# shellcheck shell=bash
2+
3+
# shellcheck source=./_functions.sh
4+
. "$(dirname "$0")/_functions.sh"
5+
6+
title "init"
7+
tempDir="/tmp/husky-yarn-1-test"
8+
9+
rm -rf $tempDir
10+
11+
# generated by pretest script
12+
tgz="./husky-*.tgz"
13+
14+
# Create directory
15+
mkdir -p $tempDir
16+
17+
# Install
18+
cp $tgz $tempDir/husky.tgz
19+
yarn set version berry
20+
cd $tempDir && yarn init -y && yarn add ./husky.tgz
21+
22+
init_git
23+
yarn husky init
24+
npm set-script test "echo \"msg from pre-commit hook\" && exit 1"
25+
26+
# Debug
27+
# cat .husky/*
28+
29+
# Test package.json scripts
30+
grep '"postinstall": "husky install"' package.json || ok
31+
grep '"prepublishOnly": "pinst --disable"' package.json || ok
32+
grep '"postpublish": "pinst --enable"' package.json || ok
33+
34+
# Test core.hooksPath
35+
test_hooksPath ".husky"
36+
37+
# Test pre-commit
38+
git add package.json
39+
git commit -m "should fail" || ok
40+
41+
# Uninstall
42+
yarn remove husky
43+
git config core.hooksPath || ok

test/init-yarn-2.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# shellcheck shell=bash
2+
3+
# shellcheck source=./_functions.sh
4+
. "$(dirname "$0")/_functions.sh"
5+
6+
title "init"
7+
tempDir="/tmp/husky-yarn-2-test"
8+
9+
rm -rf $tempDir
10+
11+
# generated by pretest script
12+
tgz="./husky-*.tgz"
13+
14+
# Create directory
15+
mkdir -p $tempDir
16+
17+
# Install
18+
cp $tgz $tempDir/husky.tgz
19+
yarn set version berry
20+
cd $tempDir && yarn init -y && yarn add ./husky.tgz
21+
22+
init_git
23+
yarn husky init
24+
npm set-script test "echo \"msg from pre-commit hook\" && exit 1"
25+
26+
# Test package.json scripts
27+
grep '"postinstall": "husky install"' package.json || ok
28+
grep '"prepublishOnly": "pinst --disable"' package.json || ok
29+
grep '"postpublish": "pinst --enable"' package.json || ok
30+
31+
# Test core.hooksPath
32+
test_hooksPath ".husky"
33+
34+
# Test pre-commit
35+
git add package.json
36+
git commit -m "should fail" || ok
37+
38+
# Uninstall
39+
yarn remove husky
40+
git config core.hooksPath || ok

test/not-git-dir.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ rm -rf $tempDir
1010
cd_and_install_tgz $tempDir
1111

1212
# Should not fail
13-
npx --no-install husky install && echo -e "\e[0;32mOK\e[m"
13+
npx --no-install husky install && ok

test/sub-dir.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ npm run prepare
3232
# Add hook
3333
npx --no-install husky add pre-commit "echo \"msg from pre-commit hook\" && exit 1"
3434

35-
# Debug
36-
# cat .husky/*
37-
3835
# Test core.hooksPath
3936
test_hooksPath "sub/.husky"
4037

4138
# Test pre-commit
4239
git add package.json
43-
git commit -m "should fail" || echo -e "\e[0;32mOK\e[m"
40+
git commit -m "should fail" || ok

0 commit comments

Comments
 (0)