From 0aa4fec615a9dc449d9bb9661993fbcff562436e Mon Sep 17 00:00:00 2001 From: Ho Ngoc Hai Date: Sat, 25 Apr 2026 18:34:38 +0700 Subject: [PATCH] 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 --- .husky/pre-commit | 3 ++- scripts/pre-commit-tests.sh | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100755 scripts/pre-commit-tests.sh diff --git a/.husky/pre-commit b/.husky/pre-commit index 72c4429..88744bf 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1 +1,2 @@ -npm test +npx lint-staged +bash scripts/pre-commit-tests.sh diff --git a/scripts/pre-commit-tests.sh b/scripts/pre-commit-tests.sh new file mode 100755 index 0000000..4c74316 --- /dev/null +++ b/scripts/pre-commit-tests.sh @@ -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