#!/usr/bin/env bash # Deploys Pilot for the Owner. Run it with: just deploy # It builds the App as an ad hoc signed .app and installs it in /Applications, installs or upgrades the Daemon's launchd agent, # and restarts the Daemon. launchd starts the Daemon at login and again after a crash. # Running it again upgrades both. The Daemon's files stay: its address file (port and token) and its SQLite file. # It always deploys the Owner's Pilot, never the development one: it ignores PILOT_PROFILE. # PILOT_APPLICATIONS changes where the App goes (the tests use it). set -euo pipefail cd "$(dirname "$0")/.." unset PILOT_PROFILE label=com.babariviere.pilot.daemon # The Daemon's folder (daemon/src/address.ts) holds its address file and SQLite file, and the Daemon's program. folder="$HOME/Library/Application Support/com.babariviere.pilot" # The Daemon's code and packages. A deploy replaces this folder, never the files next to it. program="$folder/program" staged_program="$program.next" log="$HOME/Library/Logs/com.babariviere.pilot/daemon.log" agent="$HOME/Library/LaunchAgents/$label.plist" apps=${PILOT_APPLICATIONS:-/Applications} app="$apps/Pilot.app" app_binary="$app/Contents/MacOS/Pilot" domain="gui/$(id -u)" fail() { echo "deploy: $*" >&2 exit 1 } # Escapes a value for the launchd agent's XML. xml() { printf '%s' "$1" | sed -e 's/&/\&/g' -e 's//\>/g' } # Whether a Daemon answers on its port. It answers plain HTTP with 426, because it speaks WebSocket only. answers() { [ "$(curl -s -o /dev/null -w '%{http_code}' --max-time 1 "http://127.0.0.1:$port/" || true)" = 426 ] } # The Daemon runs TypeScript as it is, which needs Node 22.19 or newer. node=$(command -v node) || fail "Pilot's Daemon needs Node 22.19 or newer, and no node is on the PATH." version=$("$node" -p process.versions.node) IFS=. read -r major minor _ <<<"$version" if [ "$major" -lt 22 ] || { [ "$major" -eq 22 ] && [ "$minor" -lt 19 ]; }; then fail "Pilot's Daemon needs Node 22.19 or newer. This node is $version ($node)." fi command -v nsh >/dev/null || fail "nsh is not on the PATH. The App needs it to build." # 1. Build the App and stage the Daemon's new program. Nothing installed changes yet, so a failure here leaves the old Pilot running. app_build=$(mktemp -d) # Its real path: npm ci --prefix through macOS's /var link finds the lock file out of sync. app_source=$(cd "$(mktemp -d)" && pwd -P) trap 'rm -rf "$app_build" "$app_source" "$staged_program"' EXIT echo "Building the App..." # The App builds from a copy with packages of its own (its fonts), so a deploy leaves the working tree's node_modules as they are. # The copy skips what tools write in app/: node_modules and NSH's .nsh (tens of MB). mkdir "$app_source/app" cp -R ui protocol "$app_source/" for file in app/* app/.npmrc; do [ "$file" = app/node_modules ] || cp -R "$file" "$app_source/app/" done npm --prefix "$app_source/app" ci --no-audit --no-fund --prefer-offline nsh package "$app_source/app" --target macos -o "$app_build" echo "Installing the Daemon's packages..." rm -rf "$staged_program" mkdir -p "$staged_program/daemon" "$(dirname "$log")" # .npmrc: the Daemon's packages install without the peers they ask for (daemon/.npmrc says why). cp -R daemon/src daemon/package.json daemon/package-lock.json daemon/.npmrc "$staged_program/daemon/" cp -R protocol "$staged_program/" npm --prefix "$staged_program/daemon" ci --omit=dev --no-audit --no-fund --prefer-offline # 2. The Daemon's token and address file: created on the first deploy, kept on an upgrade. port=$("$node" --input-type=module -e "import { readOrCreateAddress } from './daemon/src/address.ts'; console.log((await readOrCreateAddress('owner')).port)") # 3. Stop the Daemon, replace its program, and start it again under launchd. echo "Restarting the Daemon..." launchctl bootout "$domain/$label" 2>/dev/null || true # With the Daemon stopped, nothing may answer on its port: the new Daemon could not listen, and the check below would ask the wrong program. ! answers || fail "Another program listens on 127.0.0.1:$port. Stop it, then deploy again." rm -rf "$program" mv "$staged_program" "$program" mkdir -p "$(dirname "$agent")" # PATH: the Daemon runs gh, git and jj, and launchd gives it almost no PATH. It gets the PATH of this deploy. cat >"$agent" < Label $label ProgramArguments $(xml "$node") $(xml "$program/daemon/src/main.ts") WorkingDirectory $(xml "$program/daemon") EnvironmentVariables PATH $(xml "$PATH") RunAtLoad KeepAlive StandardOutPath $(xml "$log") StandardErrorPath $(xml "$log") PLIST # launchd remembers that a label was disabled, and then refuses to start it. An older app used this label. enable clears that. launchctl enable "$domain/$label" # Right after a bootout, launchd can refuse a bootstrap for a moment: try a few times. for try in 1 2 3 4 5; do if error=$(launchctl bootstrap "$domain" "$agent" 2>&1); then break; fi [ "$try" -lt 5 ] || fail "launchd did not start Pilot's Daemon: $error" sleep 1 done # 4. Install the App. A running App is closed first, and opened again at the end. echo "Installing the App in $apps..." running=false if pgrep -f "$app_binary" >/dev/null; then running=true pkill -f "$app_binary" while pgrep -f "$app_binary" >/dev/null; do sleep 0.2; done fi rm -rf "$app" cp -R "$app_build/Pilot.app" "$apps/" # 5. Check that the Daemon answers. for _ in $(seq 50); do answers && break sleep 0.2 done answers || fail "Pilot's Daemon did not answer on 127.0.0.1:$port. Its log is $log." if $running; then open "$app"; fi echo "Pilot is deployed. Its Daemon listens on 127.0.0.1:$port, and its log is $log." echo "Open Pilot from Applications."