Skip to content

Commit eb54845

Browse files
committed
automatically rm deprecated code
1 parent e8badd7 commit eb54845

File tree

9 files changed

+171
-136
lines changed

9 files changed

+171
-136
lines changed

docs/sponsorkit/sponsors.svg

Lines changed: 134 additions & 132 deletions
Loading

index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import c from 'child_process'
2-
import f, { writeFileSync as w } from 'fs'
2+
import f, { readdir, writeFileSync as w } from 'fs'
33
import p from 'path'
44

5-
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' ]
5+
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' ],
6+
re = /^(#!\/usr\/bin\/env sh|\. "\$\(dirname -- "\$0"\)\/_\/husky\.sh")\r?\n/gm
67

78
export default (d = '.husky') => {
89
if (process.env.HUSKY === '0') return 'HUSKY=0 skip install'
@@ -14,6 +15,16 @@ export default (d = '.husky') => {
1415
if (s == null) return 'git command not found'
1516
if (s) return '' + e
1617

18+
f.rmSync(_('husky.sh'), { force: true })
19+
l.forEach(h => {
20+
let hp = p.join(d, h)
21+
if (!f.existsSync(hp)) return
22+
let prev = f.readFileSync(hp, 'utf8')
23+
let next = prev.replace(re, '')
24+
if (prev !== next) console.log(`husky - removed deprecated code from ${hp}`)
25+
f.writeFileSync(hp, next)
26+
})
27+
1728
f.mkdirSync(_(), { recursive: true })
1829
w(_('.gitignore'), '*')
1930
f.copyFileSync(new URL('husky', import.meta.url), _('h'))

test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ sh test/8_set_u.sh
1313
sh test/9_husky_0.sh
1414
sh test/10_init.sh
1515
sh test/11_time.sh
16+
sh test/12_rm_deprecated.sh

test/10_init.sh

100644100755
File mode changed.

test/12_rm_deprecated.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
. test/functions.sh
3+
setup
4+
install
5+
6+
npx --no-install husky
7+
8+
cat > .husky/pre-commit <<'EOL'
9+
# foo
10+
#!/usr/bin/env sh
11+
. "$(dirname -- "$0")/_/husky.sh"
12+
# bar
13+
EOL
14+
15+
cat > expected <<'EOL'
16+
# foo
17+
# bar
18+
EOL
19+
20+
npx --no-install husky
21+
expect 0 "diff .husky/pre-commit expected"

test/1_default.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ expect_hooksPath_to_be ".husky/_"
1010

1111
# Test pre-commit
1212
git add package.json
13-
echo "echo \"pre-commit\" && exit 1" >.husky/pre-commit
13+
echo "echo \"pre-commit\" && exit 1" > .husky/pre-commit
1414
expect 1 "git commit -m foo"

test/6_command_not_found.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ expect_hooksPath_to_be ".husky/_"
1010

1111
# Test pre-commit with 127 exit code
1212
git add package.json
13-
echo "exit 127" >.husky/pre-commit
13+
echo "exit 127" > .husky/pre-commit
1414
expect 1 "git commit -m foo"

test/8_set_u.sh

100644100755
File mode changed.

test/9_husky_0.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)