#!/usr/bin/env bash # Stop hook: makes Claude fix TypeScript errors before it can finish a reply. # # Why: CLAUDE.md tells Claude to run `npm run typecheck` after editing a stack, # but an instruction can be forgotten. A hook can't: Claude Code runs it itself. # # When: "Stop" is the Claude Code event that fires every time Claude finishes a # reply. It has nothing to do with Gitea issues or tickets. # # What: for every stack (01-..05-) with uncommitted .ts changes, run # `npm run typecheck`. If one fails, exit 2 sends the errors back to Claude and # it has to keep working. If nothing changed, it does nothing. # # Not included: `pulumi preview`. It's slow and needs credentials, so it stays a # manual step (see "Verifying changes" in CLAUDE.md). # # Idea taken from the deverenz repo (.claude/hooks/check-on-stop.sh). input=$(cat) # Set when Claude is already continuing because of this hook; stopping here # prevents an endless loop if it can't fix the error. [[ "$(jq -r '.stop_hook_active // false' <<<"$input")" == "true" ]] && exit 0 cd "$CLAUDE_PROJECT_DIR" || exit 0 # -uall lists files inside new folders, not just the folder. stacks=$(git status --porcelain -uall | grep -oE '0[1-5]-[a-z0-9-]+/.*\.ts$' | cut -d/ -f1 | sort -u) failed=0 for stack in $stacks; do [[ -f "$stack/package.json" && -d "$stack/node_modules" ]] || continue if ! out=$(npm run --silent --prefix "$stack" typecheck 2>&1); then echo "typecheck failed in $stack, fix before finishing:" >&2 tail -n 30 <<<"$out" >&2 failed=1 fi done [[ $failed -eq 1 ]] && exit 2 exit 0