entrypoint.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env bash
  2. # Startup sequence for the bind-mounted repo:
  3. # 1. install dependencies only when package-lock.json actually changed;
  4. # 2. regenerate the Prisma client only when the schema actually changed;
  5. # 3. apply pending migrations;
  6. # 4. start in dev (watch) or prod (compiled) mode.
  7. #
  8. # The stamp files live under node_modules/, so a `git pull` that touches only
  9. # application code goes straight to step 4.
  10. set -euo pipefail
  11. cd /app
  12. mkdir -p data
  13. STAMP_DIR="node_modules/.eks-stamps"
  14. hash_of() { sha256sum "$1" 2>/dev/null | awk '{print $1}'; }
  15. needs_refresh() {
  16. local file="$1" stamp="$STAMP_DIR/$2"
  17. [ -f "$stamp" ] || return 0
  18. [ "$(cat "$stamp")" = "$(hash_of "$file")" ] && return 1 || return 0
  19. }
  20. record() { mkdir -p "$STAMP_DIR"; hash_of "$1" > "$STAMP_DIR/$2"; }
  21. # --include=dev is required even in prod mode: NODE_ENV=production would
  22. # otherwise omit typescript/tsx, and both modes compile or watch from source.
  23. if [ ! -d node_modules/typescript ] || needs_refresh package-lock.json lock; then
  24. echo "[entrypoint] installing dependencies"
  25. npm ci --include=dev --no-audit --no-fund
  26. record package-lock.json lock
  27. else
  28. echo "[entrypoint] dependencies up to date"
  29. fi
  30. if [ ! -d node_modules/.prisma/client ] || needs_refresh prisma/schema.prisma schema; then
  31. echo "[entrypoint] generating prisma client"
  32. npx prisma generate
  33. record prisma/schema.prisma schema
  34. fi
  35. echo "[entrypoint] applying database migrations"
  36. npx prisma migrate deploy
  37. if [ "${RELAY_MODE:-prod}" = "dev" ]; then
  38. echo "[entrypoint] starting in dev mode (tsx watch — no rebuild needed)"
  39. exec npx tsx watch src/index.ts
  40. fi
  41. echo "[entrypoint] building and starting in prod mode"
  42. npm run build
  43. exec node dist/index.js