From 20603175c10e35e58d1eadd20d08a491cfcc5b6a Mon Sep 17 00:00:00 2001 From: typicode Date: Thu, 25 Jul 2024 14:53:25 +0200 Subject: [PATCH] show message instead of automatically remove deprecated code --- husky | 19 +++++++------------ index.js | 12 ++---------- test.sh | 2 +- .../{12_rm_deprecated.sh => 12_deprecated.sh} | 11 ++--------- test/7_node_modules_path.sh | 3 ++- test/8_set_u.sh | 2 +- 6 files changed, 15 insertions(+), 34 deletions(-) rename test/{12_rm_deprecated.sh => 12_deprecated.sh} (57%) diff --git a/husky b/husky index 8edf346db..facf66f95 100644 --- a/husky +++ b/husky @@ -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 - [ $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" \ No newline at end of file +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 \ No newline at end of file diff --git a/index.js b/index.js index 29966479f..9c92b60a6 100644 --- a/index.js +++ b/index.js @@ -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 '' } \ No newline at end of file diff --git a/test.sh b/test.sh index 3ca0a5eaf..75e90bf3e 100755 --- a/test.sh +++ b/test.sh @@ -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 \ No newline at end of file +sh test/12_deprecated.sh \ No newline at end of file diff --git a/test/12_rm_deprecated.sh b/test/12_deprecated.sh similarity index 57% rename from test/12_rm_deprecated.sh rename to test/12_deprecated.sh index 7f6755cdb..ee5474d1b 100755 --- a/test/12_rm_deprecated.sh +++ b/test/12_deprecated.sh @@ -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" \ No newline at end of file +expect 0 "git commit -m foo" \ No newline at end of file diff --git a/test/7_node_modules_path.sh b/test/7_node_modules_path.sh index ec72811e6..d177a35e7 100755 --- a/test/7_node_modules_path.sh +++ b/test/7_node_modules_path.sh @@ -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" diff --git a/test/8_set_u.sh b/test/8_set_u.sh index 6767e7a1a..f7c631453 100755 --- a/test/8_set_u.sh +++ b/test/8_set_u.sh @@ -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