show message instead of automatically remove deprecated code

This commit is contained in:
typicode 2024-07-25 14:53:25 +02:00
parent f6cc410883
commit 20603175c1
6 changed files with 15 additions and 34 deletions

15
husky
View File

@ -1,5 +1,4 @@
#!/usr/bin/env sh
# shellcheck disable=SC1090
[ "$HUSKY" = "2" ] && set -x
n=$(basename "$0")
s=$(dirname "$(dirname "$0")")/$n
@ -14,14 +13,10 @@ i="${XDG_CONFIG_HOME:-$HOME/.config}/husky/init.sh"
[ "${HUSKY-}" = "0" ] && exit 0
c=0
h() {
[ $c = 0 ] && return
export PATH=node_modules/.bin:$PATH
sh -e "$s" "$@"
c=$?
[ $c != 0 ] && echo "husky - $n script failed (code $c)"
[ $c = 127 ] && echo "husky - command not found in PATH=$PATH"
exit 1
}
trap 'c=$?; h' EXIT
set -e
PATH=node_modules/.bin:$PATH
. "$s"
exit $c

View File

@ -3,7 +3,7 @@ import f, { readdir, writeFileSync as w } from 'fs'
import p from 'path'
let l = [ 'pre-commit', 'prepare-commit-msg', 'commit-msg', 'post-commit', 'applypatch-msg', 'pre-applypatch', 'post-applypatch', 'pre-rebase', 'post-rewrite', 'post-checkout', 'post-merge', 'pre-push', 'pre-auto-gc' ],
re = /^(#!\/usr\/bin\/env sh|\. "\$\(dirname -- "\$0"\)\/_\/husky\.sh")\r?\n/gm
msg = `echo "husky - DEPRECATED\n\nPlease remove the following lines from your hook scripts:\n\n#!/usr/bin/env sh\n. \\"\\$(dirname -- \\"\\$0\\")/_/husky.sh\\"\n\nThey WILL FAIL in v10.0.0\n"`
export default (d = '.husky') => {
if (process.env.HUSKY === '0') return 'HUSKY=0 skip install'
@ -16,18 +16,10 @@ export default (d = '.husky') => {
if (s) return '' + e
f.rmSync(_('husky.sh'), { force: true })
l.forEach(h => {
let hp = p.join(d, h)
if (!f.existsSync(hp)) return
let prev = f.readFileSync(hp, 'utf8')
let next = prev.replace(re, '')
if (prev !== next) console.log(`husky - removed deprecated code from ${hp}`)
f.writeFileSync(hp, next)
})
f.mkdirSync(_(), { recursive: true })
w(_('.gitignore'), '*')
f.copyFileSync(new URL('husky', import.meta.url), _('h'))
l.forEach(h => w(_(h), `#!/usr/bin/env sh\n. "\$(dirname "\$0")/h"`, { mode: 0o755 }))
w(_('husky.sh'), msg)
return ''
}

View File

@ -13,4 +13,4 @@ sh test/8_set_u.sh
sh test/9_husky_0.sh
sh test/10_init.sh
sh test/11_time.sh
sh test/12_rm_deprecated.sh
sh test/12_deprecated.sh

View File

@ -5,17 +5,10 @@ install
npx --no-install husky
git add package.json
cat > .husky/pre-commit <<'EOL'
# foo
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
# bar
EOL
cat > expected <<'EOL'
# foo
# bar
EOL
npx --no-install husky
expect 0 "diff .husky/pre-commit expected"
expect 0 "git commit -m foo"

View File

@ -7,5 +7,6 @@ npx --no-install husky
# Test pre-commit
git add package.json
echo 'echo "$PATH" | grep -q "node_modules/.bin"' > .husky/pre-commit
# Should not fail when running hook
echo 'echo "$PATH" | grep "node_modules/.bin"' > .husky/pre-commit
expect 0 "git commit -m foo"