# shellcheck shell=bash # Cross Events' hooks for the crosscode script (from hope-events' scripts/deploy.sh 2.1.x). # Every hook takes the product's checkout (EVENTS_DIR, a checkout of crosscode-platform) and the # app user, and runs the app's commands as that user. Values come from church.env (CENV). # # Adding a product means adding a file like this one with the same hooks, and its name in # SUPPORTED_PRODUCTS (lib/common.sh). events_app() { printf '%s/apps/events' "$1"; } events_pm2_names() { pvar events PM2; } events_pm2_config() { printf '%s/ecosystem.config.js' "$(events_app "$1")"; } # EVENTS_PORTS=" " events_api_port() { local p; read -r p _ <<< "$(pvar events PORTS)"; printf '%s' "${p:-5000}"; } events_web_port() { local p; read -r _ p <<< "$(pvar events PORTS)"; printf '%s' "${p:-3000}"; } events_health_url() { printf 'http://127.0.0.1:%s/health' "$(events_api_port)"; } # The version /health reports once this checkout runs (the backend's package.json). events_expected_version() { node -p 'require(process.argv[1]).version' "$(events_app "$1")/backend/package.json" } events_backup() { local dir="$1" user="$2" # shellcheck disable=SC2016 run_as "$user" "$(events_app "$dir")/backend" node -e ' require("dotenv").config(); require("./src/utils/backupUtils").runBackup() .then(r => console.log(" " + r.filename)) .catch(e => { console.error(" Backup failed: " + e.message); process.exit(1); });' } # Only this app's workspaces, never the other products'. --include=dev: the TypeScript and Next # builds need type and build packages even where NODE_ENV=production. events_install_deps() { run_as "$2" "$1" npm ci --include=dev --no-audit --no-fund -w apps/events/backend -w apps/events/frontend } events_migrate() { run_as "$2" "$(events_app "$1")/backend" npm run prisma:deploy } events_pending_migrations() { # prisma migrate status exits non-zero when migrations are pending (or the DB can't be read). ! run_as "$2" "$(events_app "$1")/backend" npx prisma migrate status >/dev/null 2>&1 } events_build() { run_as "$2" "$(events_app "$1")/backend" npm run build && run_as "$2" "$(events_app "$1")/frontend" npm run build } # After going back to the previous tag: install and build it again. Migrations aren't undone # (the backup taken first has the database as it was). events_rollback_build() { events_install_deps "$1" "$2" && events_build "$1" "$2" } # The install's own PM2 config (not in git). Created from the example the first time, with this # church's PM2 names and web port. events_ensure_pm2_config() { local dir="$1" user="$2" app conf backend frontend app="$(events_app "$dir")" conf="$app/ecosystem.config.js" [ -f "$conf" ] && return 0 read -r backend frontend <<< "$(events_pm2_names)" sed -e "s/'hope-events-backend'/'${backend}'/" \ -e "s/'hope-events-frontend'/'${frontend}'/" \ -e "s/-p 3000/-p $(events_web_port)/" \ "$app/ecosystem.config.example.js" > "$conf" chown "$user:$user" "$conf" 2>/dev/null || true info "Created $conf from the example." } events_reload() { events_ensure_pm2_config "$1" "$2" run_as "$2" "$(events_app "$1")" pm2 reload ecosystem.config.js --update-env }