Skip to content

Commit

Permalink
automatically rm deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Jul 18, 2024
1 parent 71e5dcb commit b9f5889
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 137 deletions.
266 changes: 134 additions & 132 deletions docs/sponsorkit/sponsors.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import c from 'child_process'
import f, { writeFileSync as w } from 'fs'
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' ]
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

export default (d = '.husky') => {
if (process.env.HUSKY === '0') return 'HUSKY=0 skip install'
Expand All @@ -14,10 +15,19 @@ export default (d = '.husky') => {
if (s == null) return 'git command not found'
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. "\${0%/*}/h"`, { mode: 0o755 }))
w(_('husky.sh'), 'echo "husky - `#!/usr/bin/env sh` and `. "$(dirname -- "$0")/_/husky.sh"` lines in hooks are DEPRECATED and won\'t be supported in v10. You can remove these two lines for even simpler scripts"')
return ''
}
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +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
Empty file modified test/10_init.sh
100644 → 100755
Empty file.
21 changes: 21 additions & 0 deletions test/12_rm_deprecated.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh
. test/functions.sh
setup
install

npx --no-install husky

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"
2 changes: 1 addition & 1 deletion test/1_default.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ expect_hooksPath_to_be ".husky/_"

# Test pre-commit
git add package.json
echo "echo \"pre-commit\" && exit 1" >.husky/pre-commit
echo "echo \"pre-commit\" && exit 1" > .husky/pre-commit
expect 1 "git commit -m foo"
2 changes: 1 addition & 1 deletion test/6_command_not_found.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ expect_hooksPath_to_be ".husky/_"

# Test pre-commit with 127 exit code
git add package.json
echo "exit 127" >.husky/pre-commit
echo "exit 127" > .husky/pre-commit
expect 1 "git commit -m foo"
Empty file modified test/8_set_u.sh
100644 → 100755
Empty file.
Empty file modified test/9_husky_0.sh
100644 → 100755
Empty file.

0 comments on commit b9f5889

Please sign in to comment.