Skip to content

Commit

Permalink
Merge pull request #48 from moinulmoin/refactor/husky-to-lefthook
Browse files Browse the repository at this point in the history
Refactor/husky-to-lefthook
  • Loading branch information
moinulmoin authored Jul 29, 2023
2 parents e03adfa + 8a673ea commit aac2bd9
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 17 deletions.
46 changes: 40 additions & 6 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
#!/bin/sh

# Run lint and format before each commit
pnpm lint:fix && pnpm format
if [ "$LEFTHOOK" = "0" ]; then
exit 0
fi

# Add the changes to the staging area
git add .
call_lefthook()
{
dir="$(git rev-parse --show-toplevel)"
osArch=$(uname | tr '[:upper:]' '[:lower:]')
cpuArch=$(uname -m | sed 's/aarch64/arm64/')

if lefthook -h >/dev/null 2>&1
then
lefthook "$@"
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"
then
"$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
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 "$@"
else
echo "Can't find lefthook in PATH"
fi
}

call_lefthook run "pre-commit" "$@"
42 changes: 42 additions & 0 deletions .husky/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/sh

if [ "$LEFTHOOK" = "0" ]; then
exit 0
fi

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

if lefthook -h >/dev/null 2>&1
then
lefthook "$@"
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"
then
"$dir/node_modules/@evilmartians/lefthook/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
elif test -f "$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook"
then
"$dir/node_modules/@evilmartians/lefthook-installer/bin/lefthook_${osArch}_${cpuArch}/lefthook" "$@"
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 "$@"
else
echo "Can't find lefthook in PATH"
fi
}

call_lefthook run "prepare-commit-msg" "$@"
14 changes: 14 additions & 0 deletions lefthook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# EXAMPLE USAGE:
#
# Refer for explanation to following link:
# https://github.com/evilmartians/lefthook/blob/master/docs/configuration.md
#

pre-commit:
commands:
lint:
glob: "src/**/*.{ts,tsx,js,jsx}"
run: npx eslint {staged_files} --fix && git add {staged_files}
format:
glob: "src/**/*.{ts,tsx,js,jsx}"
run: npx prettier --write {staged_files} && git add {staged_files}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "next lint",
"format": "prettier --write ./src/**/*.{ts,tsx,js,jsx}",
"lint:fix": "eslint src/**/*.{js,jsx,ts,tsx} --fix",
"prepare": "husky install"
"postinstall": "lefthook install"
},
"dependencies": {
"@ducanh2912/next-pwa": "^9.2.1",
Expand Down Expand Up @@ -62,7 +62,7 @@
"@typescript-eslint/parser": "^5.59.7",
"eslint": "8.43.0",
"eslint-config-next": "13.4.7",
"husky": "^8.0.3",
"lefthook": "^1.4.7",
"prettier": "2.8.8",
"prettier-plugin-tailwindcss": "^0.3.0"
}
Expand Down
91 changes: 82 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit aac2bd9

@vercel
Copy link

@vercel vercel bot commented on aac2bd9 Jul 29, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.