| 1 | #!/usr/bin/env bash |
| 2 | # Production process entrypoint for systemd. |
| 3 | # Always updates the tree before starting Node so crash/power-loss recovery |
| 4 | # can pick up a fix from origin (intentional — do not skip pull on crash). |
| 5 | set -euo pipefail |
| 6 | |
| 7 | REPO="${QUESTWARD_REPO:-${TILE_MMO_REPO:-$(cd "$(dirname "$0")/.." && pwd)}}" |
| 8 | BRANCH="${QUESTWARD_GIT_BRANCH:-${TILE_MMO_GIT_BRANCH:-master}}" |
| 9 | cd "$REPO" |
| 10 | |
| 11 | echo "[deploy] Working tree: $REPO (branch $BRANCH)" |
| 12 | git fetch origin "$BRANCH" |
| 13 | git reset --hard "origin/$BRANCH" |
| 14 | git clean -fd |
| 15 | git pull --ff-only origin "$BRANCH" || true |
| 16 | |
| 17 | DEPLOY_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 18 | chmod +x "$DEPLOY_DIR/start-server.sh" "$DEPLOY_DIR/install-service.sh" "$DEPLOY_DIR/update-if-requested.mjs" 2>/dev/null || true |
| 19 | |
| 20 | # Skip npm install when package manifests are unchanged since the last successful install. |
| 21 | # Stamp lives under node_modules/ (gitignored) so it survives hard-reset/pull. |
| 22 | NPM_STAMP="node_modules/.questward-npm-stamp" |
| 23 | npm_manifest_hash () |
| 24 | { |
| 25 | # Prefer sha256sum; fall back to shasum (macOS) / openssl. |
| 26 | if command -v sha256sum >/dev/null 2>&1 |
| 27 | then |
| 28 | cat package.json package-lock.json | sha256sum | awk '{ print $1 }' |
| 29 | elif command -v shasum >/dev/null 2>&1 |
| 30 | then |
| 31 | cat package.json package-lock.json | shasum -a 256 | awk '{ print $1 }' |
| 32 | else |
| 33 | cat package.json package-lock.json | openssl dgst -sha256 | awk '{ print $NF }' |
| 34 | fi |
| 35 | } |
| 36 | |
| 37 | WANT_HASH="$(npm_manifest_hash)" |
| 38 | HAVE_HASH="" |
| 39 | if [[ -f "$NPM_STAMP" ]] |
| 40 | then |
| 41 | HAVE_HASH="$(tr -d '[:space:]' < "$NPM_STAMP" || true)" |
| 42 | fi |
| 43 | |
| 44 | if [[ -d node_modules && -n "$HAVE_HASH" && "$HAVE_HASH" == "$WANT_HASH" ]] |
| 45 | then |
| 46 | echo "[deploy] npm install skipped (package.json / package-lock.json unchanged)" |
| 47 | else |
| 48 | echo "[deploy] npm install…" |
| 49 | npm install --omit=dev |
| 50 | mkdir -p node_modules |
| 51 | printf '%s\n' "$WANT_HASH" > "$NPM_STAMP" |
| 52 | fi |
| 53 | |
| 54 | echo "[deploy] Submodules…" |
| 55 | git submodule sync --recursive |
| 56 | git submodule update --init --recursive |
| 57 | |
| 58 | echo "[deploy] Build client framework…" |
| 59 | npm run build:client |
| 60 | |
| 61 | if [[ ! -f client/frameworkv3/js/framework.js ]]; then |
| 62 | echo "[deploy] ERROR: client/frameworkv3/js/framework.js missing after build." >&2 |
| 63 | exit 1 |
| 64 | fi |
| 65 | |
| 66 | echo "[deploy] Starting server…" |
| 67 | exec node --experimental-sqlite server/src/index.js |