spawn/sh/cli/install.sh
L 61bcedc0eb
feat: migrate to openrouter.ai/labs/spawn CDN + release artifact version checks (#2178)
* feat: migrate shell script URLs to openrouter.ai/labs/spawn CDN

Users on older CLI versions can't auto-update because the repo was restructured
(cli/ → packages/cli/), so old version-check URLs 404. This decouples the CLI
from the repo's internal directory structure:

- Shell script URLs (install, agent scripts, github-auth) now use
  openrouter.ai/labs/spawn/* as primary with GitHub raw as fallback
- Version checks now use GitHub release artifact (cli-latest/version)
  as primary — a static URL that never changes regardless of repo layout
- CI workflow updated to publish a `version` file alongside cli.js
- Remove GITHUB_RAW_URL_PATTERN validation (no longer needed since
  install URL is now a hardcoded CDN string, not interpolated)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* style: fix biome formatting in update-check test

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: CLAUDE.md says biome lint but should say biome check

biome lint only checks lint rules, not formatting. biome check does both.
The hooks and CI already run biome check — the docs were out of sync.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix(hooks): PostToolUse hook wasn't running biome on CLI source files

Two bugs in validate-file.ts:

1. Config search only checked 1-2 levels up from the edited file, but
   biome.json is at packages/cli/ — 3 levels above src/__tests__/*.ts.
   Fix: walk up directories until biome.json is found (or hit root).

2. Ran `biome format` (prints formatted output, always exits 0) instead
   of `biome format --check` (exits non-zero if file needs formatting).
   Fix: use `biome check` which does lint + format check in one pass.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-03 23:34:58 -08:00

252 lines
9 KiB
Bash
Executable file

#!/bin/bash
# Installer for the spawn CLI
#
# Usage:
# curl -fsSL https://openrouter.ai/labs/spawn/cli/install.sh | bash
#
# This installs spawn via bun. If bun is not available, it auto-installs it first.
#
# Override install directory:
# SPAWN_INSTALL_DIR=/usr/local/bin curl -fsSL ... | bash
set -eo pipefail
SPAWN_REPO="OpenRouterTeam/spawn"
SPAWN_CDN="https://openrouter.ai/labs/spawn"
SPAWN_RAW_BASE="https://raw.githubusercontent.com/${SPAWN_REPO}/main"
MIN_BUN_VERSION="1.2.0"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BOLD='\033[1m'
NC='\033[0m'
CYAN='\033[0;36m'
log_info() { printf "${GREEN}[spawn]${NC} %s\n" "$1"; }
log_step() { printf "${CYAN}[spawn]${NC} %s\n" "$1"; }
log_warn() { printf "${YELLOW}[spawn]${NC} %s\n" "$1"; }
log_error() { printf "${RED}[spawn]${NC} %s\n" "$1"; }
# --- Helper: compare semver strings ---
# Returns 0 (true) if $1 >= $2
version_gte() {
local IFS='.'
local a=($1) b=($2)
local i=0
while [ $i -lt ${#b[@]} ]; do
local av="${a[$i]:-0}"
local bv="${b[$i]:-0}"
if [ "$av" -lt "$bv" ]; then
return 1
elif [ "$av" -gt "$bv" ]; then
return 0
fi
i=$((i + 1))
done
return 0
}
# --- Helper: ensure bun meets minimum version ---
ensure_min_bun_version() {
local current
current="$(bun --version)"
if ! version_gte "$current" "$MIN_BUN_VERSION"; then
log_warn "bun ${current} is below minimum ${MIN_BUN_VERSION}, upgrading..."
bun upgrade
current="$(bun --version)"
if ! version_gte "$current" "$MIN_BUN_VERSION"; then
log_error "Failed to upgrade bun to >= ${MIN_BUN_VERSION} (got ${current})"
echo ""
echo "Please upgrade bun manually:"
echo " bun upgrade"
echo ""
echo "Then re-run:"
echo " curl -fsSL ${SPAWN_CDN}/cli/install.sh | bash"
exit 1
fi
log_info "bun upgraded to ${current}"
fi
}
# --- Helper: check if sudo can authenticate without a password prompt ---
# Returns 0 if sudo is passwordless (root, NOPASSWD, or macOS Touch ID).
has_passwordless_sudo() {
# Already root — no sudo needed
[ "$(id -u)" = "0" ] && return 0
# Check if sudo works non-interactively (NOPASSWD or cached credentials)
sudo -n true 2>/dev/null && return 0
# macOS: check if Touch ID is configured for sudo (pam_tid.so)
if [ -f /etc/pam.d/sudo_local ] && grep -q "pam_tid" /etc/pam.d/sudo_local 2>/dev/null; then
return 0
fi
if [ -f /etc/pam.d/sudo ] && grep -q "pam_tid" /etc/pam.d/sudo 2>/dev/null; then
return 0
fi
return 1
}
# --- Helper: ensure spawn works immediately and in future sessions ---
# Installs to ~/.local/bin. If that's not already in PATH, also symlinks
# to /usr/local/bin for immediate availability (without prompting for a
# password — only if writable or passwordless sudo is available).
# Also patches shell rc files so both ~/.local/bin and ~/.bun/bin are in
# PATH for future sessions (bun is required by spawn's shebang).
ensure_in_path() {
local install_dir="$1"
local bun_bin_dir="${BUN_INSTALL}/bin"
# 1. Check if install_dir and bun are already in the user's real PATH
local spawn_in_path=false
local bun_in_path=false
if echo "${_SPAWN_ORIG_PATH}" | tr ':' '\n' | grep -qx "${install_dir}"; then
spawn_in_path=true
fi
if echo "${_SPAWN_ORIG_PATH}" | tr ':' '\n' | grep -qx "${bun_bin_dir}"; then
bun_in_path=true
fi
# 2. If spawn not in PATH, symlink into /usr/local/bin for immediate availability
# Try in order: direct write → passwordless sudo → prompt for password
# Also symlink bun so that spawn's #!/usr/bin/env bun shebang resolves
local linked=false
local bun_path
bun_path="$(command -v bun 2>/dev/null || true)"
if [ "$spawn_in_path" = false ]; then
if [ -d /usr/local/bin ] && [ -w /usr/local/bin ]; then
ln -sf "${install_dir}/spawn" /usr/local/bin/spawn && linked=true
if [ -n "$bun_path" ] && [ ! -x /usr/local/bin/bun ]; then
ln -sf "$bun_path" /usr/local/bin/bun 2>/dev/null || true
fi
elif has_passwordless_sudo; then
sudo ln -sf "${install_dir}/spawn" /usr/local/bin/spawn 2>/dev/null && linked=true
if [ -n "$bun_path" ] && [ ! -x /usr/local/bin/bun ]; then
sudo ln -sf "$bun_path" /usr/local/bin/bun 2>/dev/null || true
fi
elif command -v sudo &>/dev/null; then
# Last resort: ask for password
log_step "Adding spawn to /usr/local/bin (may require your password)..."
sudo ln -sf "${install_dir}/spawn" /usr/local/bin/spawn && linked=true || true
if [ "$linked" = true ] && [ -n "$bun_path" ] && [ ! -x /usr/local/bin/bun ]; then
sudo ln -sf "$bun_path" /usr/local/bin/bun 2>/dev/null || true
fi
fi
fi
# 3. Patch shell rc files so both ~/.local/bin and ~/.bun/bin are in PATH
# for future sessions. ~/.bun/bin is required by spawn's #!/usr/bin/env bun shebang.
local rc_file=""
case "${SHELL:-/bin/bash}" in
*/zsh) rc_file="${HOME}/.zshrc" ;;
*/fish) rc_file="" ;;
*) rc_file="${HOME}/.bashrc" ;;
esac
# Helper: add a dir to rc files if not already present
_patch_rc() {
local dir="$1"
local line="export PATH=\"${dir}:\$PATH\""
if [ -n "$rc_file" ]; then
if ! grep -qF "${dir}" "$rc_file" 2>/dev/null; then
printf '\n# Added by spawn installer\n%s\n' "$line" >> "$rc_file"
fi
case "${SHELL:-/bin/bash}" in */bash)
for profile in "${HOME}/.profile" "${HOME}/.bash_profile"; do
if [ -f "$profile" ] && ! grep -qF "${dir}" "$profile" 2>/dev/null; then
printf '\n# Added by spawn installer\n%s\n' "$line" >> "$profile"
fi
done
;; esac
else
case "${SHELL:-}" in */fish)
fish -c "fish_add_path \"${dir}\"" 2>/dev/null || true
;; esac
fi
}
if [ "$spawn_in_path" = false ]; then
_patch_rc "${install_dir}"
fi
if [ "$bun_in_path" = false ]; then
_patch_rc "${bun_bin_dir}"
fi
# 4. Show version and success message
echo ""
SPAWN_NO_UPDATE_CHECK=1 PATH="${install_dir}:${PATH}" "${install_dir}/spawn" version
echo ""
local all_ready=true
if [ "$spawn_in_path" = false ] && [ "$linked" = false ]; then
all_ready=false
fi
if [ "$bun_in_path" = false ] && [ ! -x /usr/local/bin/bun ]; then
all_ready=false
fi
if [ "$all_ready" = true ]; then
printf "${GREEN}[spawn]${NC} Run ${BOLD}spawn${NC} to get started\n"
else
printf "${GREEN}[spawn]${NC} To start using spawn, run:\n"
echo ""
echo " exec \$SHELL"
echo ""
fi
}
# --- Helper: build and install the CLI using bun ---
build_and_install() {
tmpdir=$(mktemp -d)
trap 'rm -rf "${tmpdir}"' EXIT
log_step "Downloading pre-built CLI binary..."
curl -fsSL --proto '=https' "https://github.com/${SPAWN_REPO}/releases/download/cli-latest/cli.js" -o "${tmpdir}/cli.js"
if [ ! -s "${tmpdir}/cli.js" ]; then
log_error "Failed to download pre-built binary"
exit 1
fi
INSTALL_DIR="${SPAWN_INSTALL_DIR:-${HOME}/.local/bin}"
mkdir -p "${INSTALL_DIR}"
cp "${tmpdir}/cli.js" "${INSTALL_DIR}/spawn"
chmod +x "${INSTALL_DIR}/spawn"
log_info "Installed spawn to ${INSTALL_DIR}/spawn"
ensure_in_path "${INSTALL_DIR}"
}
# --- Locate or install bun ---
# Save original PATH before modifications so ensure_in_path() can check
# whether the install dir is already in the user's real PATH.
_SPAWN_ORIG_PATH="${PATH}"
# When running via `curl | bash`, subshells may not inherit PATH updates,
# so we always prepend the standard bun install locations explicitly.
export BUN_INSTALL="${BUN_INSTALL:-${HOME}/.bun}"
export PATH="${BUN_INSTALL}/bin:${HOME}/.local/bin:${PATH}"
if ! command -v bun &>/dev/null; then
log_step "bun not found. Installing bun..."
curl -fsSL --proto '=https' https://bun.sh/install | bash
# Re-export so bun is available in this session immediately.
# Use hard-coded paths alongside BUN_INSTALL — the bun installer may
# have placed the binary in $HOME/.bun/bin even if BUN_INSTALL differs.
export PATH="$HOME/.bun/bin:${BUN_INSTALL}/bin:$HOME/.local/bin:${PATH}"
if ! command -v bun &>/dev/null; then
log_error "Failed to install bun automatically"
echo ""
echo "Please install bun manually:"
echo " curl -fsSL https://bun.sh/install | bash"
echo ""
echo "Then reopen your terminal and re-run:"
echo " curl -fsSL ${SPAWN_CDN}/cli/install.sh | bash"
exit 1
fi
log_info "bun installed successfully"
fi
ensure_min_bun_version
log_step "Installing spawn via bun..."
build_and_install