Skip to content

Commit

Permalink
Move the extension logic to Go
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed May 15, 2021
1 parent 505f9bb commit 33280fb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
13 changes: 12 additions & 1 deletion cmd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"embed"
"text/template"
"runtime"

"github.com/spf13/afero"
)
Expand All @@ -14,6 +15,7 @@ var templatesFS embed.FS
type hookTmplData struct {
AutoInstall string
HookName string
Extension string
}

func hookTemplate(hookName string, fs afero.Fs) []byte {
Expand All @@ -22,6 +24,7 @@ func hookTemplate(hookName string, fs afero.Fs) []byte {
err := t.Execute(buf, hookTmplData{
AutoInstall: autoInstall(hookName, fs),
HookName: hookName,
Extension: getExtension(),
})
check(err)

Expand All @@ -40,5 +43,13 @@ func autoInstall(hookName string, fs afero.Fs) string {
return ""
}

return "# lefthook_version: " + configChecksum(fs) + "\n\ncall_lefthook \"lefthook\" \"install\""
return "# lefthook_version: " + configChecksum(fs) + "\n\ncall_lefthook \"install\""
}

func getExtension() string {
if (runtime.GOOS == "windows") {
return ".exe"
} else {
return ""
}
}
19 changes: 7 additions & 12 deletions cmd/templates/hook.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,28 @@ fi

dir="$(cd "$(dirname "$(dirname "$(dirname "$0")")")" >/dev/null 2>&1 || exit ; pwd -P)"

extension=""
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
extension=".exe"
fi

call_lefthook()
{
if lefthook -h >/dev/null 2>&1
then
eval "$1 $2"
elif test -f "$dir/node_modules/@arkweid/lefthook/bin/$1$extension"
eval lefthook $1
elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook{{.Extension}}"
then
eval "$dir/node_modules/@arkweid/lefthook/bin/$1$extension $2"
eval "$dir/node_modules/@arkweid/lefthook/bin/lefthook{{.Extension}} $2"
elif bundle exec lefthook -h >/dev/null 2>&1
then
bundle exec "$1 $2"
bundle exec "lefthook $1"
elif npx lefthook -h >/dev/null 2>&1
then
npx "$1 $2"
npx "lefthook $1"
elif yarn lefthook -h >/dev/null 2>&1
then
yarn "$1 $2"
yarn "lefthook $1"
else
echo "Can't find lefthook in PATH"
fi
}

{{.AutoInstall}}

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

0 comments on commit 33280fb

Please sign in to comment.