#!/usr/bin/env bash # Runs the project's validation scripts in order and stops at the first failure. # Usage: validate.sh [--lint-all] [--int | --int-all] [--build] # lint by default lints only the files changed in the branch (eslint on the whole project # is the slowest and most memory-hungry step); falls back to the lint script when the # project has no local eslint or the script is not a plain `eslint .` # --lint-all lints the whole project (release, QA, full review) # --int also runs test:int, but only the integration tests affected by the branch diff # (changed *.int.test.* files and the ones in the same module as a changed file); # integration tests hit a real (often remote) database and are slow, so the full # suite is reserved for --int-all # --int-all also runs the whole test:int suite (release, QA, full review) # --build also runs build set -uo pipefail RUN_INT=0 # 0 = no, 1 = affected only, 2 = all RUN_BUILD=0 LINT_ALL=0 for arg in "$@"; do case "$arg" in --lint-all) LINT_ALL=1 ;; --int) [ "$RUN_INT" = 2 ] || RUN_INT=1 ;; --int-all) RUN_INT=2 ;; --build) RUN_BUILD=1 ;; *) echo "unknown option: $arg" >&2; exit 2 ;; esac done if [ ! -f package.json ]; then echo "package.json not found in $(pwd)" >&2 exit 2 fi if [ -f pnpm-lock.yaml ]; then PM=pnpm elif [ -f yarn.lock ]; then PM=yarn elif [ -f bun.lockb ] || [ -f bun.lock ]; then PM=bun else PM=npm fi has_script() { node -e "const s = require('./package.json').scripts || {}; process.exit(s[process.argv[1]] ? 0 : 1)" "$1" } # Changed files of the branch: commits since the base branch (the local default branch first, the # remote one may be behind), plus staged, unstaged and untracked. changed_files() { local base="" for candidate in main master origin/main origin/master origin/HEAD; do if git rev-parse --verify --quiet "$candidate" >/dev/null; then base="$candidate"; break; fi done { [ -n "$base" ] && git diff --name-only "$(git merge-base HEAD "$base")" HEAD git diff --name-only HEAD git ls-files --others --exclude-standard } 2>/dev/null | sort -u } # Integration tests touched by the diff: changed ones, plus every one inside the module # (src/server/modules//, or scripts/) of a changed production file (test-only edits do not # widen the scope). affected_int_tests() { local all scopes all=$(git ls-files --cached --others --exclude-standard | grep -E '\.int\.test\.[cm]?[jt]sx?$' || true) [ -z "$all" ] && return scopes=$(changed_files | grep -vE '\.test\.[cm]?[jt]sx?$|\.contract\.[cm]?[jt]sx?$' | sed -nE 's#^(src/server/modules/[^/]+/).*#\1#p; s#^(scripts/).*#\1#p' | sort -u) { changed_files | grep -E '\.int\.test\.[cm]?[jt]sx?$' || true for scope in $scopes; do printf '%s\n' "$all" | grep -F "$scope" || true; done } | sort -u | while read -r file; do [ -f "$file" ] && echo "$file"; done } # Lint only the changed files when the lint script is a plain eslint run over the project. lint_scoped() { [ "$LINT_ALL" = 0 ] && [ -x node_modules/.bin/eslint ] && node -e "const s = (require('./package.json').scripts || {}).lint || ''; process.exit(/^(NODE_OPTIONS=\S+ )?eslint( \.)?$/.test(s) ? 0 : 1)" } STEPS=(typecheck lint test) INT_FILES=() if [ "$RUN_INT" = 1 ]; then while IFS= read -r file; do [ -n "$file" ] && INT_FILES+=("$file"); done < <(affected_int_tests) if [ "${#INT_FILES[@]}" -gt 0 ]; then STEPS+=(test:int) else RESULTS_PRE="SKIP test:int (no integration test affected by the diff; use --int-all for the whole suite)" fi elif [ "$RUN_INT" = 2 ]; then STEPS+=(test:int) fi [ "$RUN_BUILD" = 1 ] && STEPS+=(build) RESULTS=() [ -n "${RESULTS_PRE:-}" ] && RESULTS+=("$RESULTS_PRE") print_summary() { echo echo "== validation summary ==" printf '%s\n' "${RESULTS[@]}" } # CI=true keeps test runners (vitest, jest) out of watch mode. export CI=true for step in "${STEPS[@]}"; do if [ "$step" = lint ] && has_script lint && lint_scoped; then LINT_FILES=() while IFS= read -r file; do [ -f "$file" ] && LINT_FILES+=("$file"); done < <(changed_files | grep -E '\.[cm]?[jt]sx?$' || true) if [ "${#LINT_FILES[@]}" -eq 0 ]; then RESULTS+=("SKIP lint (no changed file to lint; use --lint-all for the whole project)") continue fi echo "lint scoped to the ${#LINT_FILES[@]} changed file(s) (use --lint-all for the whole project)" command=(node_modules/.bin/eslint --no-warn-ignored "${LINT_FILES[@]}") elif has_script "$step"; then command=("$PM" run "$step") if [ "$step" = test:int ] && [ "${#INT_FILES[@]}" -gt 0 ]; then [ "$PM" = npm ] && command+=(--) command+=("${INT_FILES[@]}") echo "integration tests affected by the diff: ${INT_FILES[*]}" fi elif [ "$step" = typecheck ] && [ -f tsconfig.json ]; then command=("$PM" exec tsc --noEmit) else RESULTS+=("SKIP $step (no script)") continue fi echo "> ${command[*]}" if "${command[@]}"; then RESULTS+=("PASS $step") else RESULTS+=("FAIL $step") print_summary exit 1 fi done print_summary