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

19
husky
View File

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

View File

@ -3,7 +3,7 @@ import f, { readdir, writeFileSync as w } from 'fs'
import p from 'path' 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' ], 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') => { export default (d = '.husky') => {
if (process.env.HUSKY === '0') return 'HUSKY=0 skip install' if (process.env.HUSKY === '0') return 'HUSKY=0 skip install'
@ -16,18 +16,10 @@ export default (d = '.husky') => {
if (s) return '' + e if (s) return '' + e
f.rmSync(_('husky.sh'), { force: true }) 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 }) f.mkdirSync(_(), { recursive: true })
w(_('.gitignore'), '*') w(_('.gitignore'), '*')
f.copyFileSync(new URL('husky', import.meta.url), _('h')) f.copyFileSync(new URL('husky', import.meta.url), _('h'))
l.forEach(h => w(_(h), `#!/usr/bin/env sh\n. "\$(dirname "\$0")/h"`, { mode: 0o755 })) l.forEach(h => w(_(h), `#!/usr/bin/env sh\n. "\$(dirname "\$0")/h"`, { mode: 0o755 }))
w(_('husky.sh'), msg)
return '' return ''
} }

View File

@ -13,4 +13,4 @@ sh test/8_set_u.sh
sh test/9_husky_0.sh sh test/9_husky_0.sh
sh test/10_init.sh sh test/10_init.sh
sh test/11_time.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 npx --no-install husky
git add package.json
cat > .husky/pre-commit <<'EOL' cat > .husky/pre-commit <<'EOL'
# foo
#!/usr/bin/env sh #!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh" . "$(dirname -- "$0")/_/husky.sh"
# bar
EOL EOL
cat > expected <<'EOL' expect 0 "git commit -m foo"
# foo
# bar
EOL
npx --no-install husky
expect 0 "diff .husky/pre-commit expected"

View File

@ -7,5 +7,6 @@ npx --no-install husky
# Test pre-commit # Test pre-commit
git add package.json 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" expect 0 "git commit -m foo"

View File

@ -7,7 +7,7 @@ npx --no-install husky
expect_hooksPath_to_be ".husky/_" expect_hooksPath_to_be ".husky/_"
git add package.json git add package.json
echo "echo \"pre-commit\"" >.husky/pre-commit echo "echo \"pre-commit\"" > .husky/pre-commit
# Should not fail if set -u is used # Should not fail if set -u is used
mkdir -p config/husky mkdir -p config/husky