2d3b3bb665
The Windows launcher already avoids the word "server" in what users read, and the macOS health probe now uses curl --noproxy so starting offline cannot stall on a system proxy. The app windows also ignore proxies, which only ever point at 127.0.0.1.
253 lines
8.1 KiB
Bash
253 lines
8.1 KiB
Bash
#!/bin/bash
|
|
# Luminary.app launcher.
|
|
#
|
|
# Called by the native shim in Contents/MacOS/Luminary. Keeps the same rules as
|
|
# the Windows launcher: pick a loopback port, start the bundled node exactly like
|
|
# `node server/index.mjs`, wait for /api/health, then open the browser. The
|
|
# server is detached and keeps running after the app exits, so launching again
|
|
# reuses it instead of starting a second copy.
|
|
#
|
|
# launcher.sh start (or reuse) and open the window
|
|
# launcher.sh --stop stop the server recorded in runtime.json
|
|
# launcher.sh --status print pid, port, health and data directory
|
|
# launcher.sh --verify check that this is a complete bundle (exit 3 if not)
|
|
# launcher.sh --browser open a normal browser tab instead of an app window
|
|
set -u
|
|
|
|
HERE="$(cd "$(dirname "$0")" && pwd)"
|
|
APP="$HERE/app"
|
|
NODE="$HERE/runtime/node"
|
|
DATA="${LUMINARY_DATA:-$HOME/Library/Application Support/Luminary/data}"
|
|
STATE="$DATA/runtime.json"
|
|
LOGS="$DATA/logs"
|
|
WINDOW="${LUMINARY_WINDOW:-app}"
|
|
|
|
say() { printf 'Luminary: %s\n' "$*"; }
|
|
err() { printf 'Luminary: %s\n' "$*" >&2; }
|
|
die() { err "$*"; exit 1; }
|
|
|
|
# A blocking dialog for launches from Finder, where stderr goes nowhere.
|
|
dialog() {
|
|
local message
|
|
message=$(printf '%s' "$1" | tr '"' "'" | tr '\n' ' ')
|
|
if command -v osascript >/dev/null 2>&1; then
|
|
osascript -e "display dialog \"$message\" with title \"Luminary\" buttons {\"OK\"} default button 1 with icon caution" >/dev/null 2>&1 || true
|
|
fi
|
|
}
|
|
|
|
version_line() {
|
|
if [ -f "$HERE/version.txt" ]; then head -1 "$HERE/version.txt"; else echo "Luminary (version unknown)"; fi
|
|
}
|
|
|
|
state_port() {
|
|
[ -f "$STATE" ] || return 0
|
|
sed -n 's/.*"port"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p' "$STATE" | head -1
|
|
}
|
|
|
|
state_pid() {
|
|
[ -f "$STATE" ] || return 0
|
|
sed -n 's/.*"pid"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p' "$STATE" | head -1
|
|
}
|
|
|
|
# --noproxy forces a direct connection: offline, curl would otherwise resolve
|
|
# the system proxy/PAC first and every health probe would fail.
|
|
healthy() {
|
|
curl -fs -m 2 --noproxy '*' "http://127.0.0.1:$1/api/health" >/dev/null 2>&1
|
|
}
|
|
|
|
# Ask node rather than nc: the bundle always has one, and nc's flags differ
|
|
# between the BSD tool macOS ships and builds found elsewhere.
|
|
port_in_use() {
|
|
"$NODE" -e 'const net=require("net");const s=net.connect(+process.argv[1],"127.0.0.1");s.on("connect",()=>{s.destroy();process.exit(0)});s.on("error",()=>process.exit(1))' "$1" >/dev/null 2>&1
|
|
}
|
|
|
|
tail_logs() {
|
|
[ -f "$LOGS/server.err.log" ] || return 0
|
|
err "last lines from $LOGS/server.err.log:"
|
|
tail -n 20 "$LOGS/server.err.log" >&2 || true
|
|
}
|
|
|
|
open_window() {
|
|
local url="$1"
|
|
if [ "$WINDOW" = "browser" ]; then
|
|
open "$url"
|
|
return
|
|
fi
|
|
if [ -d "/Applications/Google Chrome.app" ] || [ -d "$HOME/Applications/Google Chrome.app" ]; then
|
|
open -na "Google Chrome" --args \
|
|
--app="$url" \
|
|
--user-data-dir="$DATA/window-profile-chrome" \
|
|
--window-size=1500,950 --no-first-run --no-default-browser-check \
|
|
--no-proxy-server
|
|
elif [ -d "/Applications/Microsoft Edge.app" ] || [ -d "$HOME/Applications/Microsoft Edge.app" ]; then
|
|
open -na "Microsoft Edge" --args \
|
|
--app="$url" \
|
|
--user-data-dir="$DATA/window-profile-edge" \
|
|
--window-size=1500,950 --no-first-run --no-default-browser-check \
|
|
--no-proxy-server
|
|
else
|
|
open "$url"
|
|
fi
|
|
}
|
|
|
|
verify() {
|
|
local status=0 file size
|
|
for file in "$NODE" "$APP/server/index.mjs" "$APP/dist/index.html"; do
|
|
if [ ! -f "$file" ]; then
|
|
err "missing: ${file#"$HERE"/}"
|
|
status=3
|
|
elif [ ! -s "$file" ]; then
|
|
err "empty: ${file#"$HERE"/}"
|
|
status=3
|
|
else
|
|
size=$(wc -c <"$file" | tr -d ' ')
|
|
say "ok: ${file#"$HERE"/} ($size bytes)"
|
|
fi
|
|
done
|
|
say "bundle: $HERE"
|
|
say "version: $(version_line)"
|
|
return $status
|
|
}
|
|
|
|
start() {
|
|
[ -x "$NODE" ] || chmod +x "$NODE" 2>/dev/null || true
|
|
|
|
if [ -f "$STATE" ]; then
|
|
local existing
|
|
existing=$(state_port)
|
|
if [ -n "$existing" ] && healthy "$existing"; then
|
|
say "already running on port $existing"
|
|
open_window "http://127.0.0.1:$existing"
|
|
exit 0
|
|
fi
|
|
rm -f "$STATE"
|
|
fi
|
|
|
|
if ! verify >/dev/null 2>&1; then
|
|
verify >&2 || true
|
|
dialog "This copy of Luminary is incomplete. Copy Luminary.app out of the disk image or zip and try again. If it still fails, download the app again."
|
|
exit 3
|
|
fi
|
|
|
|
mkdir -p "$DATA" "$LOGS" 2>/dev/null || {
|
|
dialog "Luminary cannot write to $DATA. Set LUMINARY_DATA to a folder you can write to and try again."
|
|
exit 1
|
|
}
|
|
|
|
local port=""
|
|
local candidate
|
|
# Brace expansion instead of seq: macOS does not ship seq.
|
|
for candidate in {8787..8887}; do
|
|
if ! port_in_use "$candidate"; then
|
|
port="$candidate"
|
|
break
|
|
fi
|
|
done
|
|
[ -n "$port" ] || { dialog "No free port between 8787 and 8887. Quit the app using one and try again."; exit 1; }
|
|
|
|
LUMINARY_DATA="$DATA" \
|
|
LUMINARY_LOCAL=1 \
|
|
HOST=127.0.0.1 \
|
|
PORT="$port" \
|
|
NODE_ENV=production \
|
|
nohup "$NODE" --disable-warning=ExperimentalWarning "$APP/server/index.mjs" \
|
|
>>"$LOGS/server.out.log" 2>>"$LOGS/server.err.log" &
|
|
local pid=$!
|
|
|
|
printf '{\n "pid": %s,\n "port": %s,\n "url": "http://127.0.0.1:%s",\n "data": "%s",\n "started": "%s"\n}\n' \
|
|
"$pid" "$port" "$port" "$DATA" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" >"$STATE"
|
|
|
|
local waited=0
|
|
while [ "$waited" -lt 240 ]; do
|
|
if healthy "$port"; then
|
|
open_window "http://127.0.0.1:$port"
|
|
exit 0
|
|
fi
|
|
if ! kill -0 "$pid" 2>/dev/null; then
|
|
rm -f "$STATE"
|
|
tail_logs
|
|
dialog "Luminary's local engine stopped while starting. Log: $LOGS/server.err.log"
|
|
exit 1
|
|
fi
|
|
sleep 0.5
|
|
waited=$((waited + 1))
|
|
done
|
|
|
|
kill "$pid" 2>/dev/null || true
|
|
rm -f "$STATE"
|
|
tail_logs
|
|
dialog "Luminary did not start within 120 seconds. Log: $LOGS/server.err.log"
|
|
exit 1
|
|
}
|
|
|
|
stop() {
|
|
if [ ! -f "$STATE" ]; then
|
|
say "not running"
|
|
return 0
|
|
fi
|
|
local pid
|
|
pid=$(state_pid)
|
|
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
|
|
kill -TERM "$pid" 2>/dev/null || true
|
|
local tick
|
|
for tick in {1..20}; do
|
|
kill -0 "$pid" 2>/dev/null || break
|
|
sleep 0.25
|
|
done
|
|
kill -0 "$pid" 2>/dev/null && kill -KILL "$pid" 2>/dev/null || true
|
|
say "stopped (pid $pid)"
|
|
else
|
|
say "not running (removed a stale runtime.json)"
|
|
fi
|
|
rm -f "$STATE"
|
|
}
|
|
|
|
status() {
|
|
local port pid
|
|
port=$(state_port)
|
|
pid=$(state_pid)
|
|
say "version: $(version_line)"
|
|
say "bundle: $HERE"
|
|
say "data: $DATA"
|
|
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
|
|
say "pid: $pid"
|
|
elif [ -n "$pid" ]; then
|
|
say "pid: $pid (not running)"
|
|
else
|
|
say "pid: -"
|
|
fi
|
|
if [ -n "$port" ] && healthy "$port"; then
|
|
say "health: ok on http://127.0.0.1:$port"
|
|
elif [ -n "$port" ]; then
|
|
say "health: no response on port $port"
|
|
else
|
|
say "health: not started"
|
|
fi
|
|
}
|
|
|
|
case "${1:-}" in
|
|
--stop) stop ;;
|
|
--status) status ;;
|
|
--verify) verify; exit $? ;;
|
|
--browser) WINDOW=browser; start ;;
|
|
--help | -h)
|
|
cat <<'USAGE'
|
|
Luminary.app launcher
|
|
|
|
No argument starts the app (or reuses a running copy) and opens the window.
|
|
--browser open a normal browser tab instead of a Chrome/Edge app window
|
|
--stop stop the local engine this bundle started
|
|
--status print version, pid, port, health and the data directory
|
|
--verify check that the bundle is complete
|
|
|
|
Data: ~/Library/Application Support/Luminary/data (override with LUMINARY_DATA)
|
|
Logs: <data>/logs/server.out.log and server.err.log
|
|
USAGE
|
|
;;
|
|
"") start ;;
|
|
*)
|
|
err "unknown argument: $1 (try --help)"
|
|
exit 2
|
|
;;
|
|
esac
|