Files
openclaw-setup-cn/install-macos.sh

143 lines
5.4 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
# ─────────────────────────────────────────────
# OpenClaw 一键部署安装脚本 (macOS)
# ─────────────────────────────────────────────
# 颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
NC='\033[0m' # 无颜色
info() { echo -e "${BLUE}[信息]${NC} $*"; }
success() { echo -e "${GREEN}[完成]${NC} $*"; }
warn() { echo -e "${YELLOW}[警告]${NC} $*"; }
error() { echo -e "${RED}[错误]${NC} $*"; exit 1; }
header() {
echo ""
echo -e "${BOLD}========================================${NC}"
echo -e "${BOLD} OpenClaw 一键部署安装器 (macOS)${NC}"
echo -e "${BOLD}========================================${NC}"
echo ""
}
header
echo -e "${BOLD}请选择操作:${NC}"
echo " 1) 安装 / 更新 OpenClaw"
echo " 2) 卸载 OpenClaw"
read -r -p "请输入选项 [1/2](默认 1: " ACTION
ACTION=${ACTION:-1}
if [[ "$ACTION" == "2" ]]; then
info "你选择了:卸载 OpenClaw"
if command -v openclaw &>/dev/null; then
openclaw uninstall --all --yes --non-interactive
else
warn "在 PATH 中未找到 openclaw尝试使用 npx 卸载..."
npx -y openclaw uninstall --all --yes --non-interactive
fi
success "OpenClaw 已卸载。"
exit 0
fi
# ─────────────────────────────────────────────
# 步骤 1: 检查 / 安装 Node 22+
# ─────────────────────────────────────────────
info "正在检查 Node.js 版本..."
install_node() {
# 检查是否安装了 Homebrew
if ! command -v brew &>/dev/null; then
info "未找到 Homebrew正在安装 Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 为 Apple Silicon 添加 Homebrew 到 PATH
if [[ -f /opt/homebrew/bin/brew ]]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [[ -f /usr/local/bin/brew ]]; then
eval "$(/usr/local/bin/brew shellenv)"
fi
success "Homebrew 安装完成。"
else
success "已找到 Homebrew。"
fi
info "正在通过 Homebrew 安装 Node.js 22..."
brew install node@22
brew link --overwrite node@22 2>/dev/null || true
success "Node.js 22 安装完成。"
}
if command -v node &>/dev/null; then
NODE_VERSION=$(node -v | sed 's/v//' | cut -d. -f1)
if [[ "$NODE_VERSION" -ge 22 ]]; then
success "已找到 Node.js v$(node -v | sed 's/v//') (>= 22)。"
else
warn "已找到 Node.js v$(node -v | sed 's/v//'),但需要 >= 22。"
install_node
fi
else
warn "未找到 Node.js。"
install_node
fi
# ─────────────────────────────────────────────
# 步骤 2: 安装 / 更新 OpenClaw
# 国内网络优先npmmirror → npmjs必要时
# ─────────────────────────────────────────────
info "正在安装 / 更新 OpenClaw..."
install_openclaw_with_registry() {
local registry="$1"
info "尝试通过 npm 安装 OpenClaw$registry..."
npm install -g openclaw@latest --registry="$registry"
}
if install_openclaw_with_registry "https://registry.npmmirror.com"; then
success "已通过 npmmirror 安装 / 更新 OpenClaw。"
elif install_openclaw_with_registry "https://registry.npmjs.org"; then
success "已通过 npm 官方源安装 / 更新 OpenClaw。"
else
error "OpenClaw 安装失败npmmirror 与 npm 官方源均不可用。"
fi
# 确保 openclaw 在 PATH 中
NPM_PREFIX=$(npm config get prefix 2>/dev/null || true)
if [[ -n "$NPM_PREFIX" ]]; then
export PATH="$NPM_PREFIX/bin:$HOME/.openclaw/bin:$PATH"
else
export PATH="$HOME/.openclaw/bin:$PATH"
fi
if ! command -v openclaw &>/dev/null; then
error "OpenClaw 安装失败 — 在 PATH 中找不到 'openclaw'。请尝试重启终端后再次运行此脚本。"
fi
# ─────────────────────────────────────────────
# 步骤 3: 启动交互式配置
# ─────────────────────────────────────────────
echo ""
echo -e "${GREEN}${BOLD}========================================${NC}"
echo -e "${GREEN}${BOLD} OpenClaw 安装完成!开始配置...${NC}"
echo -e "${GREEN}${BOLD}========================================${NC}"
echo ""
openclaw onboard --accept-risk --flow quickstart --install-daemon --gateway-bind loopback --node-manager npm --skip-search --skip-channels --skip-skills < /dev/tty
echo ""
echo -e "${GREEN}${BOLD}========================================${NC}"
echo -e "${GREEN}${BOLD} 配置完成!${NC}"
echo -e "${GREEN}${BOLD}========================================${NC}"
echo ""
echo -e " ${BOLD}接下来:${NC}"
echo -e " 1. 开始使用 OpenClaw${BLUE}openclaw${NC}"
echo -e " 2. 查看网关状态:${BLUE}openclaw gateway status${NC}"
echo -e " 3. 如需卸载:${BLUE}curl -fsSL https://github.carrydelahaye.work/openclaw/openclaw-setup-cn/raw/branch/main/uninstall-macos.sh | bash${NC}"
echo ""