Skip to content

Commit

Permalink
Merge branch 'githooks' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
codejedi365 committed Oct 6, 2021
2 parents 287b85c + 043ad03 commit 45f2290
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .husky/hook-utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
[ -z "$LOG_PREFIX" ] && LOG_PREFIX="[.husky/???]"

log() {
echo "${LOG_PREFIX} $1"
}

error() {
echo >&2 "${LOG_PREFIX} $1"
}
37 changes: 37 additions & 0 deletions .husky/post-checkout
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

LOG_PREFIX="[.husky/post-checkout]"

. "$(dirname "$0")/hook-utils.sh"

HEAD_PREV="$1"
HEAD_NEW="$2"
CKOUT_TYPE_FLAG="$3" # 0 = retrieve file from index, 1 = changing branches

# check if this is a post-checkout after a `git clone`
IS_CLONING=$( [ -z "$HEAD_PREV" ] && echo true || echo false )

update_npm_dependencies() {
# Only run if chaning branches and not performing the initial `git clone`
if [ $CKOUT_TYPE_FLAG == 1 ] && [ $IS_CLONING == false ]; then
local changed_files=""
# derived from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224
changed_files="$(git diff-tree -r --name-only --no-commit-id $HEAD_PREV $HEAD_NEW)"

if echo "$changed_files" | grep --quiet "package-lock.json"; then
log "CHANGE DETECTED: 'package-lock.json'"
log "Dependency requirements changed! This will take a few seconds..."

local cmd="npm install --prefer-offline"
if ! command -v npm &>/dev/null; then
log "NPM not found on \$PATH, however '$cmd' is desired. Please accomplish manually."
return
fi
log "$> $cmd"
eval "$cmd"
fi
fi
}

update_npm_dependencies
60 changes: 60 additions & 0 deletions .husky/post-merge
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

[ -z "$LOG_PREFIX" ] && LOG_PREFIX="[.husky/post-merge]"

. "$(dirname "$0")/hook-utils.sh"

update_npm_dependencies() {
# derived from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224
local changed_files=""
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"

local NEEDS_INSTALL=true

if echo -e "$changed_files" | grep --quiet "package-lock.json"; then
log "CHANGE DETECTED: 'package-lock.json'"
log "Lock files rarely merge properly. Deleting and re-generating it..."

local output="$changed_files"
output="$(echo -e "$output" | grep "package-lock.json")"
local lockfiles=("$output") # make array of filepaths
rm ${lockfiles[@]}

elif echo -e "$changed_files" | grep --quiet "package.json"; then
log "CHANGE DETECTED: 'package.json'"
log "Dependencies might of changed, lets make sure we are g2g!"
else
NEEDS_INSTALL=false
fi

if [ "$NEEDS_INSTALL" == true ]; then
cmd="npm install"
if ! command -v npm &>/dev/null; then
log "NPM not found on \$PATH, however '$cmd' is desired. Please accomplish manually."
return
fi
log "$> $cmd"
eval "$cmd"

changed_files="$(git diff --name-only --diff-filter=d)"
if echo -e "$changed_files" | grep --quiet "package-lock.json"; then
log "ALL FIXED! package-lock.json needed a refresh."

local lockfiles=($(echo -e "$changed_files" | grep "package-lock.json"))
git add -- "${lockfiles[@]}"

for lockfile in "${lockfiles[@]}"; do
log "STAGED: $lockfile"
done

local filenoun="file"
if [ "${#lockfiles[@]}" -gt 1 ]; then
filenoun="${filenoun}s"
fi
log "Please commit the newly generated $filenoun for the team!"
fi
fi
}

update_npm_dependencies
13 changes: 13 additions & 0 deletions .husky/post-rewrite
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

CALLER_CMD="$1" # rebase || amend
export LOG_PREFIX="[.husky/post-rewrite]"

. "$(dirname "$0")/hook-utils.sh"

# derived from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224
if [ "$CALLER_CMD" == "rebase" ]; then
log "DETECTED: git-rebase.";
$(dirname "$0")/post-merge
fi
20 changes: 20 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

export NODE_ENV=production

LOG_PREFIX="[.husky/pre-commit]"

. "$(dirname "$0")/hook-utils.sh"

if command -v npm &>/dev/null; then
log "$> npm run lint"
if ! npm run lint; then
error "ERROR: Dirty code detected!"
error "Fix the lint errors before committing to the repository."
exit -1
fi
else
error "NPM not found on \$PATH, unable to run pre-commit lint hook."
exit -1
fi
23 changes: 23 additions & 0 deletions package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"format": "npm run lint -- --fix",
"lint": "eslint . --ext ts,js,md",
"test": "jest",
"generate-readme-table": "ts-node build/generate-readme-table.ts"
"generate-readme-table": "ts-node build/generate-readme-table.ts",
"prepare": "is-ci || husky install"
},
"devDependencies": {
"@types/eslint": "^7.28.0",
Expand All @@ -56,6 +57,8 @@
"eslint-plugin-jest": "^24.5.0",
"eslint-plugin-mdx": "^1.15.1",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^7.0.2",
"is-ci": "^3.0.0",
"jest": "^27.2.4",
"jest-extended": "^0.11.5",
"prettier": "^2.4.1",
Expand Down

0 comments on commit 45f2290

Please sign in to comment.