feat(infra): scope pre-commit test hook to staged packages (GOO-228)

Replace blanket `npm test` with lint-staged for linting/formatting and
a turbo --filter script that runs tests only for workspace packages
that have staged .ts/.tsx files.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Ho Ngoc Hai
2026-04-25 18:34:38 +07:00
parent f70d7e3deb
commit 0aa4fec615
2 changed files with 37 additions and 1 deletions

View File

@@ -1 +1,2 @@
npm test npx lint-staged
bash scripts/pre-commit-tests.sh

35
scripts/pre-commit-tests.sh Executable file
View File

@@ -0,0 +1,35 @@
#!/usr/bin/env bash
set -euo pipefail
# Detect which workspace packages have staged .ts/.tsx files and run tests
# only for those packages via turbo --filter.
STAGED_TS_FILES=$(git diff --cached --name-only --diff-filter=ACMR -- '*.ts' '*.tsx')
if [ -z "$STAGED_TS_FILES" ]; then
exit 0
fi
FILTERS=""
SEEN=""
while IFS= read -r file; do
case "$file" in
apps/api/*) pkg="@goodgo/api" ;;
apps/web/*) pkg="@goodgo/web" ;;
libs/mcp-servers/*) pkg="@goodgo/mcp-servers" ;;
*) continue ;;
esac
if [[ ! " $SEEN " =~ " $pkg " ]]; then
SEEN="$SEEN $pkg"
FILTERS="$FILTERS --filter=$pkg"
fi
done <<< "$STAGED_TS_FILES"
if [ -z "$FILTERS" ]; then
exit 0
fi
echo "Running tests for affected packages:$SEEN"
exec npx turbo run test $FILTERS