#!/usr/bin/env bash # Shared build flow. Sourced by clean_rebuild.sh and dev_rebuild.sh. # Expects: MODE (production|development), DO_CLEAN (1|0), PACKAGE_ONLY (1|0) set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$ROOT" # shellcheck source=scripts/config.sh source "$ROOT/scripts/config.sh" log() { printf '==> %s\n' "$*"; } fail() { printf 'ERROR: %s\n' "$*" >&2; exit 1; } detect_pm() { local dir="$1" if [ -f "$dir/pnpm-lock.yaml" ]; then echo pnpm elif [ -f "$dir/yarn.lock" ]; then echo yarn elif [ -f "$dir/package-lock.json" ]; then echo npm else fail "No lockfile in $dir. Commit a lockfile first (task T-006)." fi } require_apps() { for d in $APP_DIRS; do [ -f "$d/package.json" ] || fail "$d/package.json does not exist yet. Create the project first (task T-006)." done } install_deps() { local d="$1" pm pm="$(detect_pm "$d")" command -v "$pm" >/dev/null 2>&1 || fail "$pm not found." log "Installing dependencies in $d with $pm (frozen lockfile)" case "$pm" in npm) (cd "$d" && npm ci) ;; pnpm) (cd "$d" && pnpm install --frozen-lockfile) ;; yarn) (cd "$d" && yarn install --frozen-lockfile) ;; esac } run_script() { local d="$1" script="$2" pm pm="$(detect_pm "$d")" log "Running '$script' in $d" (cd "$d" && "$pm" run "$script") } run_flow() { require_apps if [ "$DO_CLEAN" = "1" ]; then log "Cleaning build output" rm -rf web/.next api/dist fi for d in $APP_DIRS; do if [ "$DO_CLEAN" = "1" ] || [ ! -d "$d/node_modules" ]; then install_deps "$d" fi run_script "$d" build done [ -d web/.next ] || fail "web/.next was not produced." [ -d api/dist ] || fail "api/dist was not produced." if [ "$PACKAGE_ONLY" = "1" ]; then log "Package-only mode: build verified, nothing started or deployed" log "SUCCESS (build and verify only)" return 0 fi if [ "$MODE" = "development" ]; then log "Build finished. Start the dev servers with: (cd web && npm run dev) and (cd api && npm run start:dev)" else log "Production build finished. Deploying is a separate step and only on the owner's request (task T-028)." fi log "SUCCESS" }