# shellcheck shell=bash # The Church Website's hooks for the crosscode script (docs/WEBSITE-1.0-PLAN.md ยง11), the same # hooks as products/accounts.sh. Every hook takes the product's checkout (WEBSITE_DIR, a checkout of # crosscode-platform) and the app user. Values come from church.env (CENV): WEBSITE_DIR, # WEBSITE_DOMAIN, WEBSITE_PM2 (" "), WEBSITE_PORTS (" "), # WEBSITE_DB, WEBSITE_USER. website_app() { printf '%s/apps/website' "$1"; } website_pm2_names() { pvar website PM2; } website_api_port() { local p; read -r p _ <<< "$(pvar website PORTS)"; printf '%s' "${p:-5300}"; } website_web_port() { local p; read -r _ p <<< "$(pvar website PORTS)"; printf '%s' "${p:-3300}"; } website_health_url() { printf 'http://127.0.0.1:%s/api/health' "$(website_api_port)"; } website_expected_version() { node -p 'require(process.argv[1]).version' "$(website_app "$1")/backend/package.json" } # The shared packages the Website is built with (contracts, auth-client for signing in, ui and seo for its pages). website_shared_packages() { local p for p in packages/contracts packages/ui packages/auth-client packages/seo; 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. website_backup() { local dir="$1" user="$2" backend backend="$(website_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/website-$(date +%Y%m%d-%H%M%S).sql.gz" pg_dump --no-owner --no-privileges "$url" | gzip > "$file" chmod 600 "$file" ls -1t backups/website-*.sql.gz | tail -n +15 | xargs -r rm -f echo " $file"' } website_install_deps() { local -a ws=() local p while IFS= read -r p; do ws+=(-w "$p"); done < <(website_shared_packages "$1") run_as "$2" "$1" npm ci --include=dev --no-audit --no-fund --include-workspace-root "${ws[@]}" -w apps/website/backend -w apps/website/frontend } website_migrate() { run_as "$2" "$(website_app "$1")/backend" npm run prisma:deploy } website_build() { local p for p in packages/contracts packages/auth-client packages/seo; do [ ! -f "$1/$p/package.json" ] || run_as "$2" "$1/$p" npm run build || return 1 done run_as "$2" "$(website_app "$1")/backend" npm run build && run_as "$2" "$(website_app "$1")/frontend" npm run build } website_rollback_build() { website_install_deps "$1" "$2" && website_build "$1" "$2" } # The install's own PM2 config (not in git), made from the example the first time. website_ensure_pm2_config() { local dir="$1" user="$2" app conf backend frontend app="$(website_app "$dir")" conf="$app/ecosystem.config.cjs" [ -f "$conf" ] && return 0 read -r backend frontend <<< "$(website_pm2_names)" sed -e "s/'website-backend'/'${backend}'/" \ -e "s/'website-frontend'/'${frontend}'/" \ -e "s/-p 3300/-p $(website_web_port)/" \ "$app/ecosystem.config.example.cjs" > "$conf" chown "$user:$user" "$conf" 2>/dev/null || true info "Created $conf from the example." } website_reload() { website_ensure_pm2_config "$1" "$2" run_as "$2" "$(website_app "$1")" pm2 reload ecosystem.config.cjs --update-env } # export.json (crosscode.church-export/1 with its `website` part: pages, messages, events, media, # the editors; no secrets) into . The uploads are copied by website_copy_uploads. website_export() { local app app="$(website_app "$1")" run_as "$2" "$app/backend" npx tsx scripts/export-church.ts --out-dir "$3" } # The church's uploads (photos, documents) into , as Events' copy_uploads. website_copy_uploads() { local uploads uploads="$(website_app "$1")/backend/uploads" mkdir -p "$2" # .thumbnails is the messages' YouTube thumbnails kept by the site (fetched again), not the church's. [ ! -d "$uploads" ] || rsync -a --exclude .thumbnails "$uploads/" "$2/" } # What uninstall keeps for 30 days: the backups and the .env (its WEBSITE_SECRET decrypts what's stored). website_keep() { local app dest="$2" app="$(website_app "$1")" mkdir -p "$dest" # -H: the uploads snapshots (Website 1.5.0) 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" } website_env_vars() { printf '%s\n' --var "domain=$(pvar website DOMAIN)" --var "apiPort=$(website_api_port)" --var "webPort=$(website_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). website_resolve_failed_migrations() { local dir="$1" user="$2" prev="$3" backend name backend="$(website_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/website/backend/prisma/migrations/$name/migration.sql" 2>/dev/null; then warn "website: $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 "website: marked the failed migration $name as rolled back." report_result "resolvedMigration" "$name" done }