Skip to content

Commit

Permalink
refactor: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Sep 28, 2023
1 parent e419556 commit 0856924
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 66 deletions.
34 changes: 11 additions & 23 deletions husky.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/usr/bin/env sh
debug() {
[ "$HUSKY_DEBUG" = "1" ] && echo "husky (debug) - $1"
}

log() {
echo "husky - $1"
if [ "$HUSKY_DEBUG" = "1" ]; then
echo "husky (debug) - $1"
fi
}
log() { echo "husky - $1"; }

exit_hook() {
[ "$1" != 0 ] && log "$2 hook exited with code $1 (error)"
Expand All @@ -17,30 +16,19 @@ name="${0##*/}"
script="${0%/*/*}/$name"

debug "starting $name..."
[ "$HUSKY" = "0" ] && debug "HUSKY env variable is set to 0, skipping hook" && exit 0
[ ! -f "$script" ] && debug "$script does not exist, skipping hook" && exit 0

if [ "$HUSKY" = "0" ] || [ ! -f "$script" ]; then
if [ "$HUSKY" = "0" ]; then
debug "HUSKY env variable is set to 0, skipping hook"
else
debug "$script does not exist, skipping hook"
fi
exit 0
fi

for file in "${XDG_CONFIG_HOME:-$HOME/.config}/husky/init.sh" "$HOME/.huskyrc.sh";
do if [ -f "$file" ]; then
for file in "${XDG_CONFIG_HOME:-$HOME/.config}/husky/init.sh" "$HOME/.huskyrc.sh"; do
if [ -f "$file" ]; then
debug "sourcing $file"
. "$file"
break
fi
done

if [ "${SHELL##*/}" = "zsh" ]; then
debug "running $script with $SHELL"
"$SHELL" -e "$script" "$@"
else
debug "running $script with sh"
sh -e "$script" "$@"
fi
[ "${SHELL##*/}" = "zsh" ] && shell="$SHELL" || shell="sh"
debug "running $script with $shell"
$shell -e "$script" "$@"

exit_hook "$?" "$name"
6 changes: 3 additions & 3 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import u from 'util'
import i from './index.js'

const { values } = u.parseArgs({
const { values: v } = u.parseArgs({
args: process.argv.slice(2),
options: {
directory: {
dir: {
type: 'string',
short: 'd',
},
Expand All @@ -14,7 +14,7 @@ const { values } = u.parseArgs({
})

try {
i(values.directory)
i(v.dir)
} catch (e) {
console.error(e instanceof Error ? `husky - ${e.message}` : e)
process.exit(1)
Expand Down
59 changes: 19 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,13 @@ import p from 'path'
const l = (msg: string): void => console.log(`husky - ${msg}`)

// https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
const hooks = [
const h = [
// Committing-Workflow Hooks
'pre-commit',
'prepare-commit-msg',
'commit-msg',
'post-commit',
'pre-commit', 'prepare-commit-msg', 'commit-msg', 'post-commit',
// Email Workflow Hooks
'applypatch-msg',
'pre-applypatch',
'post-applypatch',
'applypatch-msg', 'pre-applypatch', 'post-applypatch',
// Other Client Hooks
'pre-rebase',
'post-rewrite',
'post-checkout',
'post-merge',
'pre-push',
'pre-auto-gc',
'pre-rebase', 'post-rewrite', 'post-checkout', 'post-merge', 'pre-push', 'pre-auto-gc',
]

// Execute Git command
Expand All @@ -39,45 +29,34 @@ export default function(dir = '.husky'): void {
// Ensure that we're inside a Git repository
// If git command is not found, status is null and we should return
// That's why status value needs to be checked explicitly
if (git(['rev-parse']).status !== 0) {
l(`git command not found, skipping install`)
return
}
if (git(['rev-parse']).status !== 0)
return l(`git command not found, skipping install`)

// Custom dir help
const url = 'https://typicode.github.io/husky/guide.html#custom-directory'

// Ensure that we're not trying to install outside of cwd
if (!p.resolve(process.cwd(), dir).startsWith(process.cwd())) {
if (!p.resolve(process.cwd(), dir).startsWith(process.cwd()))
throw new Error(`.. not allowed (see ${url})`)
}

// Ensure that cwd is git top level
if (!fs.existsSync('.git')) {
if (!fs.existsSync('.git'))
throw new Error(`.git can't be found (see ${url})`)
}

try {
const h = p.join(dir, '_')
// Create hooks dir
fs.mkdirSync(h, { recursive: true })

// Create .gitignore in hooks dir
fs.writeFileSync(p.join(h, '.gitignore'), '*')
// Create hooks dir and change working dir to make file creation simpler
const d = p.join(dir, '_')
fs.mkdirSync(d, { recursive: true })
process.chdir(d)

// Copy husky.sh to hooks dir
fs.copyFileSync(new URL('../husky.sh', import.meta.url), p.join(h, 'husky.sh'))
// Create the different files in dir/_/
fs.writeFileSync('.gitignore', '*') // .gitignore
fs.copyFileSync(new URL('../husky.sh', import.meta.url), 'husky.sh') // husky.sh
h.forEach(f => fs.writeFileSync(f, `#!/usr/bin/env sh\n. "\${0%/*}/husky.sh"`, { mode: 0o755 })) // hooks

// Prepare hooks
const data = `#!/usr/bin/env sh\n. "\${0%/*}/husky.sh"`
for (const hook of hooks) {
fs.writeFileSync(p.join(h, hook), data, { mode: 0o755 })
}
// Configure repo
const { error } = git(['config', 'core.hooksPath', h])
if (error) {
throw error
}
// Configure Git
const { error: e } = git(['config', 'core.hooksPath', d])
if (e) throw e
} catch (e) {
l('Git hooks failed to install')
throw e
Expand Down

0 comments on commit 0856924

Please sign in to comment.