Skip to content

Commit

Permalink
chore: update husky config
Browse files Browse the repository at this point in the history
  • Loading branch information
lem-onade committed Aug 8, 2023
1 parent ac1728d commit 536d8c6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 83 deletions.
75 changes: 2 additions & 73 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

. "$(dirname $0)/_/husky.sh"

Expand All @@ -22,75 +22,4 @@ if [[ -z "$STAGED" && -z "$MERGE" ]] ; then
fi

## Lint commit message
echo "[COMMIT-MSG Hook] Linting commit message" && ./node_modules/.bin/commitlint --edit $1

## Check for WIP mention
if (grep -qe "WIP" $1); then

## If WIP, skip tests
echo "[COMMIT-MSG Hook] Found WIP mention => Skipping tests"

else

## If no WIP, run tests
echo "[COMMIT-MSG Hook] No WIP mention found"

if [[ -n "$MERGE" ]] ; then
echo "[COMMIT-MSG Hook] Saved ongoing merge status"
fi

if [[ -n "$STAGED" ]] ; then
echo "[COMMIT-MSG Hook] Stashing working tree"
STASH="pre-commit hook test on $(date '+%F %T')" && git stash push -qukm "$STASH"
fi

echo "[COMMIT-MSG Hook] Running tests"

## Build list of previous WIP changes
LIST=""
NEW=$(git rev-parse HEAD)
while [[ -n "$NEW" ]] ; do
LIST=$(echo "$LIST" ; echo "$NEW")
NEW=$(echo "$NEW" | sed -e 's/^\(.*\)$/\1\^@/g' | xargs git rev-list --no-walk -E --grep="WIP")
done

RELEVANT="$(echo "$LIST" | xargs -L1 git diff-tree --no-commit-id --name-only -r | sort -u ; echo "$STAGED")"

## Install root dependencies if package.json changed
echo "$RELEVANT" | grep -q "^package.json$" && npm i

## Bootstrap packages with changed package.json
PACKAGES=$(echo "$RELEVANT" | grep "package.json$" | grep -v "^package.json$" | xargs cat | grep name
echo "$PACKAGES" | sed 's/.*"name": "\(.*\)".*/--scope "\1"/g') | xargs ./node_modules/.bin/lerna bootstrap --since HEAD

## Run tests related to staged files
echo "$RELEVANT" | xargs ./node_modules/.bin/jest --bail --coverage --silent --passWithNoTests --findRelatedTests

if [[ $? -ne 0 ]] ; then
echo "[COMMIT-MSG Hook] You tried to commit changes without a WIP mention,"
echo "[COMMIT-MSG Hook] but tests related to staged files or yet untested WIP commits are failing."
echo "[COMMIT-MSG Hook] Resolve the remaining issues or commit current changes as WIP."
fi

echo "[COMMIT-MSG Hook] Resetting the stage"

## Reset tracked files & clean untracked files
git reset --hard
git clean -df

if [[ -n "$STAGED" ]] ; then
echo "[COMMIT-MSG Hook] Restoring working tree"
git stash list | grep -q "$STASH" && git stash pop -q --index
fi

if [[ -n "$MERGE" ]] ; then
echo "[COMMIT-MSG Hook] Restoring ongoing merge status"
git update-ref MERGE_HEAD "$MERGE"
if [[ -n "$M_MSG" ]] ; then
echo "$M_MSG" > .git/MERGE_MSG
fi
fi

exit $TESTS

fi
echo "[COMMIT-MSG Hook] Linting commit message" && pnpm commitlint --edit $1
6 changes: 0 additions & 6 deletions .husky/pre-commit

This file was deleted.

8 changes: 4 additions & 4 deletions .husky/pre-push
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/sh
#!/bin/bash

. "$(dirname $0)/_/husky.sh"

REGEX="^(main|next|release(release|feat|fix|test|chore|build|docs)\/[a-zA-Z0-9\-]+)$"
REGEX="^((master|develop|release|(release|feat|fix|docs|test|chore|build|docs)\/|snyk-)[a-zA-Z0-9\.\-]+)$"

REMOTE="$1"
URL="$2"
Expand All @@ -12,7 +12,7 @@ do

BRANCH=$(git rev-parse --abbrev-ref "$L_REF")

if [[ "$R_SHA" = "$(printf "%040d")" ]] ; then
if [ "$R_SHA" = "$(printf "%040d")" ] ; then

# Check branch name convention
if ! (echo "$BRANCH" | grep -qE "$REGEX") ; then
Expand All @@ -24,4 +24,4 @@ do

fi

done
done

0 comments on commit 536d8c6

Please sign in to comment.