Files
qurani/scripts/check-invariants.sh
2026-11-11 20:25:21 +01:00

92 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Project invariants. Run by every make target before anything else.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"
source "$ROOT/scripts/config.sh"
FAILS=0
ok() { printf 'ok %s\n' "$*"; }
bad() { printf 'FAIL %s\n' "$*" >&2; FAILS=$((FAILS + 1)); }
IN_GIT=0
git rev-parse --git-dir >/dev/null 2>&1 && IN_GIT=1
# 1. Shell scripts are executable and strict.
for f in clean_rebuild.sh dev_rebuild.sh scripts/*.sh; do
[ -x "$f" ] || bad "$f is not executable"
head -1 "$f" | grep -q '^#!' || bad "$f has no shebang"
grep -q 'set -euo pipefail' "$f" || bad "$f lacks 'set -euo pipefail'"
done
ok "scripts checked"
# 2. Lockfiles exist for every application that exists.
for d in $APP_DIRS; do
if [ -f "$d/package.json" ]; then
if [ -f "$d/package-lock.json" ] || [ -f "$d/pnpm-lock.yaml" ] || [ -f "$d/yarn.lock" ]; then
ok "$d lockfile present"
else
bad "$d has package.json but no lockfile"
fi
fi
done
# 3. Build output and secrets are git-ignored.
if [ -f .gitignore ]; then
for pat in node_modules .next dist .env; do
grep -qE "^/?(\*\*/)?${pat}/?$" .gitignore || bad ".gitignore does not ignore $pat"
done
ok ".gitignore checked"
else
bad ".gitignore is missing"
fi
if [ "$IN_GIT" = "1" ]; then
# 4. No .env file is tracked.
if git ls-files | grep -E '(^|/)\.env($|\.)' | grep -vE '\.env\.example$' | grep -q .; then
bad "a .env file is tracked by git"
else
ok "no .env tracked"
fi
# 5. No secret values in tracked files.
if git ls-files -z | xargs -0 grep -InE '(QF_CLIENT_SECRET|DATABASE_URL|POSTGRES_PASSWORD|JWT_SECRET)=[^[:space:]]+' 2>/dev/null \
| grep -vE '\.env\.example|docs/|AGENTS.md|scripts/check-invariants.sh' | grep -q .; then
bad "a secret-looking value is in a tracked file"
else
ok "no secret values in tracked files"
fi
fi
# 4b. Tracking scripts and third-party CDNs in the website source.
if [ -d web/src ] || [ -d web/app ]; then
if grep -rIlE 'googletagmanager|google-analytics|gtag\(|fbq\(|hotjar|plausible\.io|cdn\.jsdelivr|fonts\.googleapis' web/src web/app web/public 2>/dev/null | grep -q .; then
bad "tracking script or third-party CDN found in web sources"
else
ok "no tracking scripts or third-party CDNs"
fi
fi
# 5. Data-zone boundary: the public API must not touch qf_* data or the QF module.
if [ -d api/src/public ]; then
if grep -rInE "qf_|/qf/|from ['\"].*qf" api/src/public 2>/dev/null | grep -q .; then
bad "api/src/public references Quran Foundation data (qf_*)"
else
ok "public API does not touch qf_* data"
fi
fi
# 6. Compose files never publish ports directly (Traefik only).
if [ -d deploy ]; then
if grep -rnE '^\s*ports:' deploy/*.yml deploy/*.yaml 2>/dev/null | grep -q .; then
bad "a compose file publishes ports (use Traefik labels only)"
else
ok "compose files publish no ports"
fi
fi
if [ "$FAILS" -gt 0 ]; then
printf '\n%s invariant(s) failed.\n' "$FAILS" >&2
exit 1
fi
printf '\nAll invariants passed.\n'