Skip to content

Commit

Permalink
show message instead of automatically remove deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Jul 25, 2024
1 parent f6cc410 commit 2060317
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 34 deletions.
19 changes: 7 additions & 12 deletions husky
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env sh
# shellcheck disable=SC1090
[ "$HUSKY" = "2" ] && set -x
n=$(basename "$0")
s=$(dirname "$(dirname "$0")")/$n
Expand All @@ -14,14 +13,10 @@ i="${XDG_CONFIG_HOME:-$HOME/.config}/husky/init.sh"

[ "${HUSKY-}" = "0" ] && exit 0

c=0
h() {
[ $c = 0 ] && return
[ $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"
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 $c
12 changes: 2 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 ''
}
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 2 additions & 9 deletions test/12_rm_deprecated.sh → test/12_deprecated.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion test/7_node_modules_path.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion test/8_set_u.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ npx --no-install husky
expect_hooksPath_to_be ".husky/_"

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
mkdir -p config/husky
Expand Down

0 comments on commit 2060317

Please sign in to comment.