Files
server-config-cli/scripts/install.sh
hanruo 1129f9f915
Some checks failed
release / release (push) Failing after 9m17s
feat: "工作流"
2026-05-23 21:55:11 +08:00

99 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# server-config-cli installer
#
# Usage:
# curl -fsSL <REPO>/releases/latest/download/install.sh | bash
#
# Environment overrides:
# VERSION release tag to install (default: latest)
# NODE_VERSION portable Node version (default: v20.18.1)
# NODE_MIRROR Node download base URL
# REPO_BASE Gitea repo URL
# SERVER_CONFIG_HOME install root (default: $HOME/.server-config)
# SERVER_CONFIG_BIN launcher dir (default: $HOME/.local/bin)
set -euo pipefail
REPO_BASE="${REPO_BASE:-https://gitea1.deeppolicy.cn:3002/hanruo/server-config-cli}"
VERSION="${VERSION:-latest}"
NODE_VERSION="${NODE_VERSION:-v20.18.1}"
NODE_MIRROR="${NODE_MIRROR:-https://registry.npmmirror.com/-/binary/node}"
INSTALL_DIR="${SERVER_CONFIG_HOME:-$HOME/.server-config}"
BIN_DIR="${SERVER_CONFIG_BIN:-$HOME/.local/bin}"
log() { printf '\033[1;34m==>\033[0m %s\n' "$*"; }
warn() { printf '\033[1;33m!!\033[0m %s\n' "$*" >&2; }
die() { printf '\033[1;31mxx\033[0m %s\n' "$*" >&2; exit 1; }
command -v curl >/dev/null 2>&1 || die "curl is required"
command -v tar >/dev/null 2>&1 || die "tar is required"
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$OS" in
linux) NODE_OS=linux ;;
darwin) NODE_OS=darwin ;;
*) die "unsupported OS: $OS" ;;
esac
ARCH=$(uname -m)
case "$ARCH" in
x86_64|amd64) NODE_ARCH=x64 ;;
aarch64|arm64) NODE_ARCH=arm64 ;;
*) die "unsupported arch: $ARCH" ;;
esac
NODE_PKG="node-${NODE_VERSION}-${NODE_OS}-${NODE_ARCH}.tar.gz"
NODE_URL="${NODE_MIRROR}/${NODE_VERSION}/${NODE_PKG}"
NODE_DIR="${INSTALL_DIR}/node-${NODE_VERSION}-${NODE_OS}-${NODE_ARCH}"
if [ "$VERSION" = "latest" ]; then
BUNDLE_URL="${REPO_BASE}/releases/latest/download/app.mjs"
else
BUNDLE_URL="${REPO_BASE}/releases/download/${VERSION}/app.mjs"
fi
mkdir -p "$INSTALL_DIR" "$BIN_DIR"
if [ ! -x "${NODE_DIR}/bin/node" ]; then
log "Downloading Node ${NODE_VERSION} (${NODE_OS}-${NODE_ARCH})"
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT
curl -fL --progress-bar "$NODE_URL" -o "${TMP}/node.tar.gz" \
|| die "failed to download $NODE_URL"
tar -xzf "${TMP}/node.tar.gz" -C "$INSTALL_DIR"
[ -x "${NODE_DIR}/bin/node" ] || die "Node extraction did not produce ${NODE_DIR}/bin/node"
else
log "Reusing existing Node at ${NODE_DIR}"
fi
log "Downloading server-config bundle (${VERSION})"
mkdir -p "${INSTALL_DIR}/app"
curl -fL --progress-bar "$BUNDLE_URL" -o "${INSTALL_DIR}/app/app.mjs" \
|| die "failed to download $BUNDLE_URL"
log "Writing launcher to ${BIN_DIR}/server-config"
cat > "${BIN_DIR}/server-config" <<EOF
#!/bin/sh
exec "${NODE_DIR}/bin/node" "${INSTALL_DIR}/app/app.mjs" "\$@"
EOF
chmod +x "${BIN_DIR}/server-config"
ln -sf "${BIN_DIR}/server-config" "${BIN_DIR}/scc"
echo
log "Installed:"
echo " ${BIN_DIR}/server-config"
echo " ${BIN_DIR}/scc -> server-config"
case ":$PATH:" in
*":${BIN_DIR}:"*) ;;
*)
echo
warn "${BIN_DIR} is not in your PATH."
echo " Add this to ~/.zshrc or ~/.bashrc:"
echo " export PATH=\"${BIN_DIR}:\$PATH\""
;;
esac
echo
echo "Run: server-config"