Skip to content

Commit

Permalink
chore: add git-hook on working-dir change, run yalc:publish
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurin-W committed Oct 19, 2023
1 parent 9ada4a3 commit 835bf6e
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 56 deletions.
36 changes: 36 additions & 0 deletions .git-hooks/post-index-change
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

# Catch cases where the work dir changed after a git command.
# In that cases, run ./scripts/on-workdir-changed.sh


function in_rebase {
git_dir=$(git rev-parse --git-dir)
# Check if git_dir/rebase-merge or /rebase_apply exist.
# Then return the result
return $([ -d "$git_dir/rebase-merge" ] || [ -d "$git_dir/rebase-apply" ])
}

ROOT_DIR=$(git rev-parse --show-toplevel)
cd "$ROOT_DIR"

# If in rebase, this hook is called multiple times.
# Skip here and wait for post-rewrite to have been called.
if in_rebase; then
exit 0
fi


# Get first argument. If it is 1, this indicates, working dir files have changed.
if [ "$1" = 1 ] ; then
./scripts/on-workdir-changed.sh
fi









14 changes: 14 additions & 0 deletions .git-hooks/post-rewrite
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# Catch cases where the work dir changed after a git rebase.
# In that cases, run ./scripts/on-workdir-changed.sh


# If coming from a rewrite-rebase ($1 == rebase), run on-workdir-changed.
if [ "$1" = "rebase" ] ; then
# cd to root directory
ROOT_DIR=$(git rev-parse --show-toplevel)
cd "$ROOT_DIR"

./scripts/on-workdir-changed.sh
fi
69 changes: 13 additions & 56 deletions .git-hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,34 @@
# This script runs the linter and prettier for staged files
# in the middleware and frontend packages.


# Skip, if flag is set.
if [ "$SKIP_PRECOMMIT_CHECKS" = true ] || [ "$SKIP_PRECOMMIT_CHECKS" = 1 ] ; then
echo "=== Skipping pre-commit prettier run ==="
exit 0
fi

# cd to root directory.
ROOT_DIR=$(git rev-parse --show-toplevel)
cd "$ROOT_DIR"


# Select staged files.
FILES="$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g')"
FILES="$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g' | xargs -r realpath | sed 's/.*/"&"/')"
[ -z "$FILES" ] && echo "=== Nothing to do ===" && exit 0

## Run scripts

echo "======================"
echo "=== Running linter ==="
echo "======================"

MIDDLEWARE_FILES="$(echo "$FILES" | grep -E "src/middleware" | xargs -r realpath)"
FRONTEND_FILES="$(echo "$FILES" | grep -E "src/frontend" | xargs -r realpath)"

LINT_FILE_TYPES="js|jsx|ts|tsx"
LINT_MIDDLEWARE_FILES=$(echo "$MIDDLEWARE_FILES" | grep -E "\.($LINT_FILE_TYPES)$")
LINT_FRONTEND_FILES=$(echo "$FRONTEND_FILES" | grep -E "\.($LINT_FILE_TYPES)$")

# Function to evaluate the linter results
function evaluate_linter_results {
if [ $? == 1 ]; then
echo "================================================================="
echo "The linter found some errors. Please fix them before committing."
echo "You can skip pre-commit checks by setting \$SKIP_PRECOMMIT_CHECKS"
echo "================================================================="
exit 1
elif [ $? -ge 2 ]; then
echo "==============================================================================="
echo "Something went wrong running the linter. Have you bootstrapped the project yet?"
echo "You can skip pre-commit checks by setting \$SKIP_PRECOMMIT_CHECKS"
echo "==============================================================================="
exit 1
fi
}

cd "$ROOT_DIR"/src/middleware
yarn run lint-files $LINT_MIDDLEWARE_FILES
evaluate_linter_results

cd "$ROOT_DIR"/src/frontend
yarn run lint-files $LINT_FRONTEND_FILES
evaluate_linter_results

cd "$ROOT_DIR"


echo "========================"
echo "=== Running prettier ==="
echo "========================"

# Run prettier on all staged files.
cd "$ROOT_DIR"/src/middleware
echo "$MIDDLEWARE_FILES" | xargs npx prettier --write --ignore-unknown
cd "$ROOT_DIR"/src/frontend
echo "$FRONTEND_FILES" | xargs npx prettier --write --ignore-unknown
cd "$ROOT_DIR"
./scripts/run-linter.sh $FILES
if [ $? != 0 ]; then
exit $?
fi

./scripts/run-prettier.sh $FILES
if [ $? != 0 ]; then
echo "========================================================================"
echo "Something went wrong running prettier. Have you run \`npm install\` yet?"
echo "You can skip pre-commit checks by setting \$SKIP_PRECOMMIT_CHECKS"
echo "========================================================================"
exit 1
exit $?
fi

echo "=== Prettier done ==="

# Add back the modified/prettified files to staging
echo "$FILES" | xargs -r git add
Expand Down
9 changes: 9 additions & 0 deletions scripts/on-workdir-changed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# This script is called when a git command possibly modified files in the working dir.


ROOT_DIR=$(git rev-parse --show-toplevel)
cd "$ROOT_DIR"

./scripts/run-yalc-publish.sh
54 changes: 54 additions & 0 deletions scripts/run-linter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

# Script to run linter before commit.
# This is called by `.git-hooks/pre-commit`


echo "======================"
echo "=== Running linter ==="
echo "======================"

cd $(git rev-parse --show-toplevel)

FILES="$@"
ROOT_DIR=$(git rev-parse --show-toplevel)

MIDDLEWARE_FILES="$(echo "$FILES" | grep -E "src/middleware" | xargs -r realpath | sed 's/.*/"&"/')"
FRONTEND_FILES="$(echo "$FILES" | grep -E "src/frontend" | xargs -r realpath | sed 's/.*/"&"/')"

LINT_FILE_TYPES="js|jsx|ts|tsx"
LINT_MIDDLEWARE_FILES=$(echo "$MIDDLEWARE_FILES" | grep -E "\.($LINT_FILE_TYPES)$")
LINT_FRONTEND_FILES=$(echo "$FRONTEND_FILES" | grep -E "\.($LINT_FILE_TYPES)$")

# Function to evaluate the linter results
function evaluate_linter_results {
if [ $? == 1 ]; then
echo "================================================================="
echo "The linter found some errors. Please fix them before committing."
echo "You can skip pre-commit checks by setting \$SKIP_PRECOMMIT_CHECKS"
echo "================================================================="
exit 1
elif [ $? -ge 2 ]; then
echo "==============================================================================="
echo "Something went wrong running the linter. Have you bootstrapped the project yet?"
echo "You can skip pre-commit checks by setting \$SKIP_PRECOMMIT_CHECKS"
echo "==============================================================================="
exit 1
fi
}


# Run linter on middleware and frontend files

cd "$ROOT_DIR"/src/middleware
yarn run lint-files $LINT_MIDDLEWARE_FILES
evaluate_linter_results

cd "$ROOT_DIR"/src/frontend
yarn run lint-files $LINT_FRONTEND_FILES
evaluate_linter_results


echo "==== Linter done ====="

exit 0
36 changes: 36 additions & 0 deletions scripts/run-prettier.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

# Script to run prettier on staged files before commit.
# This is called by `.git-hooks/pre-commit`


echo "========================"
echo "=== Running prettier ==="
echo "========================"

FILES="$@"

ROOT_DIR=$(git rev-parse --show-toplevel)

MIDDLEWARE_FILES="$(echo "$FILES" | grep -E "src/middleware" | xargs -r realpath | sed 's/.*/"&"/')"
FRONTEND_FILES="$(echo "$FILES" | grep -E "src/frontend" | xargs -r realpath | sed 's/.*/"&"/')"


# Run prettier on all given files.
cd "$ROOT_DIR"/src/middleware
echo "$MIDDLEWARE_FILES" | xargs npx prettier --write --ignore-unknown
cd "$ROOT_DIR"/src/frontend
echo "$FRONTEND_FILES" | xargs npx prettier --write --ignore-unknown
cd "$ROOT_DIR"

if [ $? != 0 ]; then
echo "========================================================================"
echo "Something went wrong running prettier. Have you run \`npm install\` yet?"
echo "You can skip pre-commit checks by setting \$SKIP_PRECOMMIT_CHECKS"
echo "========================================================================"
exit 1
fi

echo "==== Prettier done ====="

exit 0
16 changes: 16 additions & 0 deletions scripts/run-yalc-publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# Script to run yalc:publish.
# Called by on-workdir-changed.sh


echo == Running yalc:publish.

ROOT_DIR=$(git rev-parse --show-toplevel)
cd "$ROOT_DIR"/src/frontend

# Run yalc:publish. If not available, just print an info message.
yarn yalc:publish &> /dev/null || \
echo "==== yarn or yalc is not installed. Skipping yalc:publish"

exit 0

0 comments on commit 835bf6e

Please sign in to comment.