#!/usr/bin/env bash # Checks the mandatory folder structure of the `dev` skill (rule A0): # src/{shared, server/{modules//{domain, application/{usecases, dto}, infra/{db, drivers}}, kernel}, client} + drizzle/ # Subject first, layers inside: every layer lives in a module (or in the kernel, which has the same layers), # never loose in src/server, and never below another layer (no usecases//dto, no domain//ports). # # Usage (from the repo root): # structure.sh whole src/ tree, plus missing folders # structure.sh --diff [base] only files added or renamed against base (default: merge-base with main/master), # plus uncommitted and untracked files # structure.sh --stdin only the paths read from stdin (e.g. `gh pr diff 12 --name-only | structure.sh --stdin`) # Exit code: 0 = structure OK, 1 = violations found, 2 = usage error. set -uo pipefail mode="${1:-all}" [ -d src ] || { echo "structure: run from the repo root (no src/ here)"; exit 2; } list_files() { case "$mode" in all) find src -type f ! -path '*/node_modules/*' ! -name '.DS_Store' ;; --diff) local base="${2:-}" if [ -z "$base" ]; then for candidate in main master; do git rev-parse --verify -q "$candidate" >/dev/null && { base="$candidate"; break; } done fi [ -n "$base" ] || { echo "structure: no base branch found; pass one" >&2; exit 2; } { git diff --name-only --diff-filter=AR "$(git merge-base HEAD "$base")" HEAD git diff --name-only --diff-filter=AR HEAD git ls-files --others --exclude-standard } | sort -u ;; --stdin) cat ;; *) echo "usage: structure.sh [--diff [base] | --stdin]" >&2 exit 2 ;; esac } violations=$( { if [ "$mode" = all ]; then for dir in src/shared src/server src/server/modules src/client drizzle; do [ -d "$dir" ] || echo "missing $dir" done fi list_files "$@" | grep -E '^src/' | grep -v '/\.DS_Store$' | awk -F/ ' BEGIN { split("domain application infra infrastructure presentation modules features kernel usecases use-cases dto dtos db drivers ports adapters repositories services entities", s, " ") for (i in s) serverLayer[s[i]] = 1 split("domain application infra infrastructure presentation modules features usecases use-cases", c, " ") for (i in c) clientLayer[c[i]] = 1 split("container.ts container.test.ts router.ts router.test.ts main.ts", r, " ") for (i in r) serverRootFile[r[i]] = 1 } function prefix(last, out, i) { out = $1; for (i = 2; i <= last; i++) out = out "/" $i; return out } function nested(from, names, i) { for (i = from; i < NF; i++) if ($i in names) { print "layer folder below a layer: " prefix(i); return } } # L is the index of the layer folder inside a module (or the kernel). function unit(L) { if (NF == L) { print "loose file " $0 " (move it into domain/, application/ or infra/)"; return } if ($L == "domain") { nested(L + 1, serverLayer); return } if ($L != "application" && $L != "infra") { print "unexpected " prefix(L) " (a module holds only domain/, application/ and infra/)"; nested(L + 1, serverLayer); return } if (NF == L + 1) { print "loose file " $0 " (move it into a sub-layer)"; return } if ($L == "application" && $(L + 1) != "usecases" && $(L + 1) != "dto") { print "unexpected " prefix(L + 1) " (application holds only usecases/ and dto/)"; nested(L + 2, serverLayer); return } if ($L == "infra" && $(L + 1) != "db" && $(L + 1) != "drivers") { print "unexpected " prefix(L + 1) " (infra holds only db/ and drivers/)"; nested(L + 2, serverLayer); return } nested(L + 2, serverLayer) } NF <= 2 { next } # files at the root of src/ (architecture tests) are fine $2 != "shared" && $2 != "server" && $2 != "client" { print "unexpected " prefix(2); next } $2 == "shared" || $2 == "client" { nested(3, clientLayer); next } NF == 3 { if (!($3 in serverRootFile)) print "loose file " $0 " (src/server holds only container.ts, router.ts and main.ts)"; next } $3 == "modules" { if (NF == 4) print "loose file " $0 " (move it into a module)"; else unit(5); next } $3 == "kernel" { unit(4); next } { print "unexpected " prefix(3) " (server holds only modules/ and kernel/; layers live inside a module)" } ' } | sort -u ) if [ -z "$violations" ]; then echo "structure OK" exit 0 fi printf '%s\n' "$violations" printf '%s violation(s) of rule A0 (see ~/.claude/skills/dev/references/architecture.md)\n' "$(printf '%s\n' "$violations" | wc -l | tr -d ' ')" exit 1