master
noah Publish 1.0.0 a09b9ee 2mo ago
#!/usr/bin/env bash
# wire-harness-designer — launch the Wire Harness Designer webview app.
# Starts the static server (if needed) and opens/refreshes its Hydrogen webview tab.
set -euo pipefail

VERSION="1.0.0"
PORT="${PORT:-8830}"
NAME="Wire Harness Designer"

# resolve the app root (this script lives at <root>/bin/wire-harness-designer)
SELF="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
ROOT="$(dirname "$SELF")"
SERVER="$ROOT/server.js"

say()  { printf 'OK: %s\n' "$1"; }
err()  { printf 'ERROR: %s\n' "$1" >&2; }
hint() { printf 'Hint: %s\n' "$1" >&2; }

server_up() { curl -s -o /dev/null --max-time 2 "http://localhost:$PORT/" 2>/dev/null; }

start_server() {
  if server_up; then return 0; fi
  if [ ! -f "$SERVER" ]; then
    err "server.js not found at $SERVER"; hint "reinstall with: adom-wiki pkg install adom/wire-harness-designer"; return 1
  fi
  PORT="$PORT" nohup node "$SERVER" >"/tmp/wire-harness-designer-$PORT.log" 2>&1 &
  for _ in 1 2 3 4 5 6 7 8 9 10; do sleep 0.3; server_up && break; done
  if ! server_up; then
    err "server failed to start on port $PORT"; hint "check the log: /tmp/wire-harness-designer-$PORT.log"; return 1
  fi
}

proxy_url() {
  # Build the public proxy URL from VSCODE_PROXY_URI; fall back to localhost.
  if [ -n "${VSCODE_PROXY_URI:-}" ]; then
    printf '%s' "${VSCODE_PROXY_URI//\{\{port\}\}/$PORT}"
  else
    printf 'http://localhost:%s/' "$PORT"
  fi
}

open_webview() {
  start_server || return 1
  local url; url="$(proxy_url)"
  if ! command -v adom-cli >/dev/null 2>&1; then
    say "server running at $url"; hint "adom-cli not found; open $url in a webview manually (load the 'webview' skill)"; return 0
  fi
  local panel; panel="$(adom-cli hydrogen workspace get 2>/dev/null | node -e 'let d="";process.stdin.on("data",c=>d+=c).on("end",()=>{try{console.log(JSON.parse(d).focusedPanelId||"")}catch(e){console.log("")}})' 2>/dev/null || true)"
  local args=(hydrogen webview open-or-refresh --name "$NAME" --url "$url")
  [ -n "$panel" ] && args+=(--panel-id "$panel")
  if adom-cli "${args[@]}" >/dev/null 2>&1; then
    say "opened '$NAME' in a webview ($url)"
    hint "generate a PDF or Save JSON from the app header; both download to your machine"
  else
    say "server running at $url"
    hint "open it yourself: adom-cli hydrogen webview open-or-refresh --name \"$NAME\" --url \"$url\" --panel-id <id from 'adom-cli hydrogen workspace get'>"
  fi
}

case "${1:-open}" in
  open|show|"")   open_webview ;;
  serve)          exec env PORT="$PORT" node "$SERVER" ;;
  health)
    if server_up; then say "server healthy on port $PORT ($(proxy_url))"; else err "server not running on port $PORT"; hint "start it: wire-harness-designer open"; exit 1; fi ;;
  install)        say "already installed at $ROOT"; hint "run 'wire-harness-designer open' to launch" ;;
  --version|-v|version) echo "$VERSION" ;;
  --help|-h|help)
    cat <<EOF
$NAME v$VERSION — build wire harnesses, export a professional PDF, save/load JSON.

Usage: wire-harness-designer [command]
  open      start the server (if needed) and open the webview   [default]
  serve     run the static server in the foreground (PORT=$PORT)
  health    check whether the server is running
  --version print the version

Env: PORT (default 8830)
EOF
    ;;
  *) err "unknown command: $1"; hint "run 'wire-harness-designer --help'"; exit 1 ;;
esac