Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve script performance and reliability #552

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 29 additions & 33 deletions internal/templates/hook.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,33 @@ fi
[ -f {{.Rc}} ] && . {{.Rc}}
{{- end}}

call_lefthook()
{
dir="$(git rev-parse --show-toplevel)"
call_lefthook() {
osArch=$(uname | tr '[:upper:]' '[:lower:]')
cpuArch=$(uname -m | sed 's/aarch64/arm64/')

if lefthook{{.Extension}} -h >/dev/null 2>&1
then
lefthook{{.Extension}} "$@"
if command -v lefthook{{.Extension}} >/dev/null 2>&1; then
exec lefthook{{.Extension}} "$@"
{{if .Extension -}}
{{/* Check if lefthook.bat exists. Ruby bundler creates such a wrapper */ -}}
elif lefthook.bat -h >/dev/null 2>&1
then
lefthook.bat "$@"
elif command -v lefthook.bat >/dev/null 2>&1; then
exec lefthook.bat "$@"
{{end -}}
elif test -f "$dir/node_modules/lefthook/bin/index.js"
then
"$dir/node_modules/lefthook/bin/index.js" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}"
then
"$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}"
then
"$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}" "$@"
elif bundle exec lefthook -h >/dev/null 2>&1
then
bundle exec lefthook "$@"
elif yarn lefthook -h >/dev/null 2>&1
then
yarn lefthook "$@"
elif pnpm lefthook -h >/dev/null 2>&1
then
pnpm lefthook "$@"
elif command -v npx >/dev/null 2>&1
then
npx @evilmartians/lefthook "$@"
elif swift package plugin lefthook >/dev/null 2>&1
then
swift package --disable-sandbox plugin lefthook "$@"
fi

try_exec "./node_modules/lefthook/bin/index.js" "$@"
try_exec "./node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}" "$@"
try_exec "./node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook{{.Extension}}" "$@"
Comment on lines +25 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was $dir variable assigned before. Since we don't check upper directories, please, add this variable


if bundle exec lefthook -h >/dev/null 2>&1; then
exec bundle exec lefthook "$@"
elif yarn lefthook -h >/dev/null 2>&1; then
exec yarn lefthook "$@"
elif pnpm lefthook -h >/dev/null 2>&1; then
exec pnpm lefthook "$@"
elif command -v npx >/dev/null 2>&1; then
exec npx @evilmartians/lefthook "$@"
elif swift package plugin lefthook >/dev/null 2>&1; then
exec swift package --disable-sandbox plugin lefthook "$@"
else
echo "Can't find lefthook in PATH"
{{- if .AssertLefthookInstalled}}
Expand All @@ -59,4 +47,12 @@ call_lefthook()
fi
}

try_exec() {
_path=$1; shift

if [ -x "$_path" ]; then
exec "$_path" "$@"
fi
}

call_lefthook run "{{.HookName}}" "$@"
Loading