# shellcheck shell=bash # The Church App's hooks for the crosscode script (docs/APP-1.0-PLAN.md ยง12), the same hooks as # products/website.sh. Every hook takes the product's checkout (APP_DIR, a checkout of # crosscode-platform) and the app user. Values come from church.env (CENV): APP_DIR, # APP_DOMAIN, APP_PM2 (" "), APP_PORTS (" "), # APP_DB, APP_USER. app_app() { printf '%s/apps/app' "$1"; } app_pm2_names() { pvar app PM2; } app_api_port() { local p; read -r p _ <<< "$(pvar app PORTS)"; printf '%s' "${p:-5400}"; } app_web_port() { local p; read -r _ p <<< "$(pvar app PORTS)"; printf '%s' "${p:-3400}"; } app_health_url() { printf 'http://127.0.0.1:%s/api/health' "$(app_api_port)"; } app_expected_version() { node -p 'require(process.argv[1]).version' "$(app_app "$1")/backend/package.json" } # The shared packages the App is built with (contracts, auth-client for signing in, ui and seo for its # pages, demo for the demo scenarios). app_shared_packages() { local p for p in packages/contracts packages/ui packages/auth-client packages/seo packages/demo; do [ -f "$1/$p/package.json" ] && printf '%s\n' "$p" done } # A database dump into backend/backups/ (the last 14 are kept), before every deploy and uninstall. app_backup() { local dir="$1" user="$2" backend backend="$(app_app "$dir")/backend" # shellcheck disable=SC2016 # the app user's shell expands these run_as "$user" "$backend" bash -c ' set -euo pipefail url="$(grep -E "^DATABASE_URL=" .env | head -1 | cut -d= -f2- | sed -e "s/^\"//" -e "s/\"$//")" mkdir -p backups file="backups/app-$(date +%Y%m%d-%H%M%S).sql.gz" pg_dump --no-owner --no-privileges "$url" | gzip > "$file" chmod 600 "$file" ls -1t backups/app-*.sql.gz | tail -n +15 | xargs -r rm -f echo " $file"' } app_install_deps() { local -a ws=() local p while IFS= read -r p; do ws+=(-w "$p"); done < <(app_shared_packages "$1") run_as "$2" "$1" npm ci --include=dev --no-audit --no-fund --include-workspace-root "${ws[@]}" -w apps/app/backend -w apps/app/frontend } app_migrate() { run_as "$2" "$(app_app "$1")/backend" npm run prisma:deploy } app_build() { local p for p in packages/contracts packages/auth-client packages/seo packages/demo; do [ ! -f "$1/$p/package.json" ] || run_as "$2" "$1/$p" npm run build || return 1 done run_as "$2" "$(app_app "$1")/backend" npm run build && run_as "$2" "$(app_app "$1")/frontend" npm run build } app_rollback_build() { app_install_deps "$1" "$2" && app_build "$1" "$2" } # The install's own PM2 config (not in git), made from the example the first time. app_ensure_pm2_config() { local dir="$1" user="$2" app conf backend frontend app="$(app_app "$dir")" conf="$app/ecosystem.config.cjs" [ -f "$conf" ] && return 0 read -r backend frontend <<< "$(app_pm2_names)" sed -e "s/'app-backend'/'${backend}'/" \ -e "s/'app-frontend'/'${frontend}'/" \ -e "s/-p 3400/-p $(app_web_port)/" \ "$app/ecosystem.config.example.cjs" > "$conf" chown "$user:$user" "$conf" 2>/dev/null || true info "Created $conf from the example." } app_reload() { app_ensure_pm2_config "$1" "$2" run_as "$2" "$(app_app "$1")" pm2 reload ecosystem.config.cjs --update-env } # export.json (crosscode.church-export/1 with its `app` part: members and roles, the prayer wall, # requests, announcements, devotionals, events, uploads; no secrets) into . The uploads are copied by app_copy_uploads. app_export() { local app app="$(app_app "$1")" run_as "$2" "$app/backend" npx tsx scripts/export-church.ts --out-dir "$3" } # The church's uploads (announcement, campaign and event images) into , as Events' copy_uploads. app_copy_uploads() { local uploads uploads="$(app_app "$1")/backend/uploads" mkdir -p "$2" [ ! -d "$uploads" ] || rsync -a "$uploads/" "$2/" } # What uninstall keeps for 30 days: the backups and the .env (its APP_SECRET decrypts what's stored). app_keep() { local app dest="$2" app="$(app_app "$1")" mkdir -p "$dest" # -H: the uploads snapshots share unchanged files as hard links; keep them shared. [ ! -d "$app/backend/backups" ] || rsync -aH "$app/backend/backups/" "$dest/backups/" cp -p "$app/backend/.env" "$dest/backend.env" 2>/dev/null || true chmod -R go-rwx "$dest" } app_env_vars() { printf '%s\n' --var "domain=$(pvar app DOMAIN)" --var "apiPort=$(app_api_port)" --var "webPort=$(app_web_port)" --var "church=$(cenv CHURCH)" } # As events_resolve_failed_migrations: a migration that failed in this release and isn't in the # previous one is marked as rolled back (the backup taken first has the database as it was). app_resolve_failed_migrations() { local dir="$1" user="$2" prev="$3" backend name backend="$(app_app "$dir")/backend" local -a failed # shellcheck disable=SC2016 # SQL through psql, run as the app user mapfile -t failed < <(run_as "$user" "$backend" bash -c ' url="$(grep -E "^DATABASE_URL=" .env | head -1 | cut -d= -f2- | sed -e "s/^\"//" -e "s/\"$//")" psql "$url" -At -c "select migration_name from _prisma_migrations where finished_at is null and rolled_back_at is null"') for name in "${failed[@]}"; do [ -n "$name" ] || continue if git_in "$dir" cat-file -e "$prev:apps/app/backend/prisma/migrations/$name/migration.sql" 2>/dev/null; then warn "app: $name failed but $prev has it too; left for a look (npx prisma migrate status)." continue fi run_as "$user" "$backend" npx prisma migrate resolve --rolled-back "$name" >/dev/null info "app: marked the failed migration $name as rolled back." report_result "resolvedMigration" "$name" done }