Skip to content

Commit

Permalink
fix(init): use postinstall for yarn v2
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Mar 2, 2021
1 parent 67f1c26 commit 4c52a12
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
5 changes: 4 additions & 1 deletion src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down
20 changes: 16 additions & 4 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 7 additions & 6 deletions test/_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -35,3 +32,7 @@ function test_hooksPath {
exit 1
fi
}
function ok {
echo -e "\e[0;32mOK\e[m"
}
5 changes: 1 addition & 4 deletions test/config-dir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 2 additions & 5 deletions test/default.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 4 additions & 8 deletions test/init.sh → test/init-npm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
43 changes: 43 additions & 0 deletions test/init-yarn-1.sh
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions test/init-yarn-2.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/not-git-dir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 1 addition & 4 deletions test/sub-dir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4c52a12

Please sign in to comment.