BlueGit

timfa questward

Files

questward / deploy / start-server.sh

start-server.sh

start-server.sh 2.2 KiB Download Raw
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).
5set -euo pipefail
6
7REPO="${QUESTWARD_REPO:-${TILE_MMO_REPO:-$(cd "$(dirname "$0")/.." && pwd)}}"
8BRANCH="${QUESTWARD_GIT_BRANCH:-${TILE_MMO_GIT_BRANCH:-master}}"
9cd "$REPO"
10
11echo "[deploy] Working tree: $REPO (branch $BRANCH)"
12git fetch origin "$BRANCH"
13git reset --hard "origin/$BRANCH"
14git clean -fd
15git pull --ff-only origin "$BRANCH" || true
16
17DEPLOY_DIR="$(cd "$(dirname "$0")" && pwd)"
18chmod +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.
22NPM_STAMP="node_modules/.questward-npm-stamp"
23npm_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
37WANT_HASH="$(npm_manifest_hash)"
38HAVE_HASH=""
39if [[ -f "$NPM_STAMP" ]]
40then
41 HAVE_HASH="$(tr -d '[:space:]' < "$NPM_STAMP" || true)"
42fi
43
44if [[ -d node_modules && -n "$HAVE_HASH" && "$HAVE_HASH" == "$WANT_HASH" ]]
45then
46 echo "[deploy] npm install skipped (package.json / package-lock.json unchanged)"
47else
48 echo "[deploy] npm install…"
49 npm install --omit=dev
50 mkdir -p node_modules
51 printf '%s\n' "$WANT_HASH" > "$NPM_STAMP"
52fi
53
54echo "[deploy] Submodules…"
55git submodule sync --recursive
56git submodule update --init --recursive
57
58echo "[deploy] Build client framework…"
59npm run build:client
60
61if [[ ! -f client/frameworkv3/js/framework.js ]]; then
62 echo "[deploy] ERROR: client/frameworkv3/js/framework.js missing after build." >&2
63 exit 1
64fi
65
66echo "[deploy] Starting server…"
67exec node --experimental-sqlite server/src/index.js