158 lines
7.1 KiB
Bash
158 lines
7.1 KiB
Bash
#!/usr/bin/env bash
|
|
# ipal-audit — deterministic checks of an ipal-kit project against AGENT.md.
|
|
# Run from the project root:
|
|
# bash .agents/skills/ipal-audit/scripts/audit.sh # all checks + tsc + build
|
|
# bash .agents/skills/ipal-audit/scripts/audit.sh --quick # static checks only
|
|
# bash .agents/skills/ipal-audit/scripts/audit.sh --help
|
|
# Exit code: 0 = no errors (warnings allowed), 1 = at least one error.
|
|
|
|
set -u
|
|
|
|
QUICK=0
|
|
for arg in "$@"; do
|
|
case "$arg" in
|
|
--quick) QUICK=1 ;;
|
|
--help|-h)
|
|
sed -n '2,7p' "$0" | sed 's/^# \{0,1\}//'
|
|
exit 0
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [ ! -d src ]; then
|
|
echo "ipal-audit: run from the project root (no ./src found)." >&2
|
|
exit 2
|
|
fi
|
|
|
|
ERRORS=0
|
|
WARNINGS=0
|
|
|
|
# report LEVEL "title" "rule" <<< "$matches"
|
|
report() {
|
|
local level="$1" title="$2" rule="$3" matches="$4"
|
|
[ -z "$matches" ] && return 0
|
|
local count
|
|
count=$(printf '%s\n' "$matches" | grep -c .)
|
|
if [ "$level" = ERROR ]; then ERRORS=$((ERRORS + count)); else WARNINGS=$((WARNINGS + count)); fi
|
|
printf '\n[%s] %s (%s) — %s\n' "$level" "$title" "$count" "$rule"
|
|
printf '%s\n' "$matches" | head -20 | sed 's/^/ /'
|
|
[ "$count" -gt 20 ] && printf ' … and %s more\n' "$((count - 20))"
|
|
}
|
|
|
|
g() { grep -rnE "$@" 2>/dev/null || true; }
|
|
# drop matches that sit inside a // or /* comment (keeps http:// URLs)
|
|
nc() { grep -vE "(^|[^:])//.*$1|/\*.*$1" || true; }
|
|
dirs() { for d in "$@"; do [ -d "$d" ] && printf '%s ' "$d"; done; }
|
|
|
|
BLOCKS=$(dirs src/blocks)
|
|
UI=$(dirs src/blocks src/components)
|
|
|
|
echo "ipal-audit — $(pwd)"
|
|
|
|
# --- Architecture (AGENT.md §4) ---
|
|
report ERROR "middleware.ts exists" "§4.1 Next 16 uses proxy.ts only" \
|
|
"$(ls src/middleware.ts middleware.ts 2>/dev/null)"
|
|
|
|
report ERROR "<head> in app" "§4.2 no explicit <head> — metadata ends up in <body>" \
|
|
"$(g '<head[ >]' src/app --include=*.tsx | nc '<head')"
|
|
|
|
PAGES=$(find src/app -path '*\[\[...slug\]\]*' -name 'page.tsx' 2>/dev/null)
|
|
for p in $PAGES; do
|
|
grep -q 'revalidate' "$p" || report ERROR "no ISR" "§4.3 export const revalidate" "$p"
|
|
grep -q 'generateStaticParams' "$p" || report ERROR "no generateStaticParams" "§4.4 without it the route is dynamic and metadata lands in <body>" "$p"
|
|
report WARNING "searchParams in catch-all page" "§4.6 deoptimizes SSG/ISR — only for paginated archives" \
|
|
"$(grep -n 'searchParams' "$p" | nc 'searchParams' | sed "s|^|$p:|")"
|
|
done
|
|
|
|
for r in src/app/sitemap.ts src/app/manifest.ts src/app/llms.txt/route.ts src/app/sitemap.xml/route.ts src/app/api/health/route.ts; do
|
|
[ -f "$r" ] && ! grep -q "dynamic = 'force-dynamic'" "$r" && \
|
|
report ERROR "DB route without force-dynamic" "§4.7 build without DB will crash" "$r"
|
|
done
|
|
|
|
report ERROR "headers()/cookies() in blocks/components" "§4.6 forces dynamic rendering — pass locale via enhanceProps" \
|
|
"$( [ -n "$UI" ] && g "from 'next/headers'" $UI)"
|
|
|
|
report ERROR "headers()/cookies() in static page tree" "§4.6 SSG/ISR → 'Page changed from static to dynamic' — not-found: LocaleView (pages.md)" \
|
|
"$(g "from 'next/headers'" src/app --include=page.tsx --include=layout.tsx --include=not-found.tsx --include=template.tsx | grep -v '/api/\|(payload)')"
|
|
|
|
# --- Hardcoding & types (§2, §8) ---
|
|
report ERROR "hardcoded locale map / any / ts-ignore" "§2, §8" \
|
|
"$(g 'localizedRoutes|as any\b|@ts-ignore|: any\b' src --include=*.ts --include=*.tsx | grep -v '/payload-types.ts:')"
|
|
|
|
report ERROR "hardcoded locale list" "§2 languages come from i18n.config.ts" \
|
|
"$(g "\[ *'(pl|en|de|fr|es|it)' *, *'(pl|en|de|fr|es|it)'" src --include=*.ts --include=*.tsx | grep -v 'i18n.config.ts')"
|
|
|
|
# --- Sections (§6) ---
|
|
if [ -n "$BLOCKS" ]; then
|
|
report ERROR "forbidden field names" "§6.5 use links/link/media (linksField, linkField, upload)" \
|
|
"$(g "name: *'[a-zA-Z]*([cC]ta|[bB]utton|Url|Href|[pP]df|[iI]mage)[a-zA-Z]*'" $BLOCKS --include=config.ts)"
|
|
|
|
report ERROR "hardcoded href/src/file paths in blocks" "§2, §6 use CMSLink / Media" \
|
|
"$(g 'href=\{?["`]/|<img |/files/|/images/|\.pdf' $BLOCKS --include=*.tsx | nc '(href|<img|/files/|/images/|\.pdf)')"
|
|
|
|
report ERROR "raw <a>/<Link> in blocks" "§6.3 links from the panel render via CMSLink" \
|
|
"$(g '<(a|Link)[ >]' $BLOCKS --include=*.tsx)"
|
|
|
|
report WARNING "inline <svg> in blocks" "§6.10 icons via media upload or Icon map" \
|
|
"$(g '<svg[ >]' $BLOCKS --include=*.tsx)"
|
|
|
|
report WARNING "large section component (>120 lines)" "§6.4 split into subcomponents" \
|
|
"$(find $BLOCKS -name '*.tsx' -exec wc -l {} + 2>/dev/null | awk '$2 != "total" && $1 > 120 {print $2": "$1" lines"}')"
|
|
|
|
report ERROR "Component imported into block config" "§6.12 payload.config must not pull React components" \
|
|
"$(g "from '\./Component'|/Component'" $BLOCKS --include=config.ts)"
|
|
fi
|
|
|
|
if [ -n "$UI" ]; then
|
|
report ERROR "hex colors in classes" "§6.11 use design tokens (bg-primary…)" \
|
|
"$(g '(bg|text|border|from|to|via|fill|stroke)-\[#' $UI --include=*.tsx)"
|
|
|
|
report ERROR "barrel index.ts" "§6.12 import files directly" \
|
|
"$(find $UI -name 'index.ts' 2>/dev/null)"
|
|
|
|
CLIENT=$(grep -rlE "^['\"]use client['\"]" $UI 2>/dev/null)
|
|
for c in $CLIENT; do
|
|
report ERROR "server import in client component" "§6.9 no payload / lib/content / server-only in 'use client'" \
|
|
"$(grep -nE "from '(payload|server-only|@/lib/content|@/payload\.config)'" "$c" | grep -v 'import type' | sed "s|^|$c:|")"
|
|
done
|
|
fi
|
|
|
|
# --- Collections (§5.3) ---
|
|
if [ -d src/collections ]; then
|
|
report ERROR "forbidden collection" "§5.3 users are built in; settings/nav/footer are globals; FAQ/hero are blocks" \
|
|
"$(ls src/collections | grep -iE '^(users|user|settings|navigation|nav|footer|header|faq|hero|company|danefirmy)\.ts$')"
|
|
report WARNING "collections present — confirm each passed the §5.3 test" "minimum: Pages, Media" \
|
|
"$(ls src/collections | grep -viE '^(pages|media|users|user|settings|navigation|nav|footer|header|faq|hero|company|danefirmy)\.ts$')"
|
|
fi
|
|
|
|
# --- Plugin duplicates (§1, §7) ---
|
|
report ERROR "own block renderer switch" "§7 use RenderBlocks + registry" \
|
|
"$(g 'switch *\(.*blockType' src --include=*.tsx --include=*.ts)"
|
|
report WARNING "files that usually duplicate the plugin" "§3 plugin has resolveRoute / getConfiguredLocales" \
|
|
"$(ls src/lib/pages.ts src/lib/locales.ts 2>/dev/null)"
|
|
report ERROR "manual Lexical rendering" "§7 use RenderRichText" \
|
|
"$(g 'node\.type *===|dangerouslySetInnerHTML' src --include=*.tsx)"
|
|
|
|
# --- Compile & build ---
|
|
if [ "$QUICK" -eq 0 ]; then
|
|
echo
|
|
echo "== pnpm generate:types && tsc --noEmit =="
|
|
if ! (pnpm -s generate:types >/dev/null && pnpm -s exec tsc --noEmit); then
|
|
ERRORS=$((ERRORS + 1)); echo "[ERROR] type check failed"
|
|
fi
|
|
echo
|
|
echo "== pnpm build =="
|
|
BUILD_LOG=$(mktemp)
|
|
if pnpm -s build >"$BUILD_LOG" 2>&1; then
|
|
DYN=$(grep -E '^[│├└ ]*ƒ .*\[locale\]' "$BUILD_LOG" || true)
|
|
report WARNING "page routes rendered dynamically (ƒ)" "§4 expected ● SSG — does the build see the DB?" "$DYN"
|
|
else
|
|
ERRORS=$((ERRORS + 1)); echo "[ERROR] build failed — last lines:"; tail -30 "$BUILD_LOG" | sed 's/^/ /'
|
|
fi
|
|
rm -f "$BUILD_LOG"
|
|
fi
|
|
|
|
echo
|
|
echo "ipal-audit: $ERRORS error(s), $WARNINGS warning(s)$( [ "$QUICK" -eq 1 ] && echo ' (quick: no tsc/build)')"
|
|
[ "$ERRORS" -eq 0 ]
|