Files

115 lines
3.4 KiB
Bash

#!/usr/bin/env bash
# safe/install-bun.sh
# Replaces: curl -fsSL https://bun.sh/install | bash
#
# What this does:
# 1. Detects platform: linux-aarch64 for Termux
# 2. Downloads bun-linux-aarch64.zip from oven-sh/bun releases
# 3. Extracts to $BUN_INSTALL/bin (default: ~/.bun/bin)
# 4. Sets executable permission
#
# No curl|bash. No pipe. No blind trust.
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
BUN_VERSION="${1:-}" # Optional: specific version/tag
BUN_INSTALL="${BUN_INSTALL:-$HOME/.bun}"
BIN_DIR="$BUN_INSTALL/bin"
echo ""
echo -e "${BOLD}=== Safe Bun install ===${NC}"
echo " This script downloads Bun directly from GitHub releases."
echo ""
# Detect platform
platform=$(uname -ms)
case "$platform" in
'Linux aarch64'|'Linux arm64') target="linux-aarch64" ;;
'Linux x86_64'|'Linux'*) target="linux-x64" ;;
'Darwin arm64') target="darwin-aarch64" ;;
'Darwin x86_64') target="darwin-x64" ;;
*) target="linux-aarch64" ;;
esac
# Check for AVX2 on x86_64 (bun uses -baseline if not available)
if [ "$target" = "linux-x64" ]; then
if [ -z "$(cat /proc/cpuinfo | grep avx2)" ]; then
target="linux-x64-baseline"
echo -e "${CYAN}[INFO]${NC} AVX2 not detected, using baseline build"
fi
fi
echo -e "${CYAN}[INFO]${NC} Platform: $target"
# Determine URL
github_repo="https://github.com/oven-sh/bun"
if [ -n "$BUN_VERSION" ]; then
# Specific version/tag provided
zip_url="${github_repo}/releases/download/${BUN_VERSION}/bun-${target}.zip"
echo -e "${CYAN}[INFO]${NC} Version: $BUN_VERSION (user-specified)"
else
# Latest
echo -e "${CYAN}[INFO]${NC} Fetching latest version from GitHub..."
latest_url="${github_repo}/releases/latest"
redirect_url=$(curl -fsSL -o /dev/null -w "%{url_effective}" "$latest_url")
BUN_VERSION=$(echo "$redirect_url" | sed 's/.*\/tag\/\([^}]*\).*/\1/')
zip_url="${github_repo}/releases/download/${BUN_VERSION}/bun-${target}.zip"
echo -e "${GREEN}[OK]${NC} Latest version: $BUN_VERSION"
fi
# Check if already installed
if [ -x "$BIN_DIR/bun" ]; then
CURRENT=$("$BIN_DIR/bun" --version 2>/dev/null || echo "unknown")
echo -e "${CYAN}[INFO]${NC} Already installed: bun $CURRENT"
echo -e "${CYAN}[INFO]${NC} To upgrade: remove $BIN_DIR/bun and run this script again"
echo -e "${CYAN}[INFO]${NC} Skipping."
exit 0
fi
# Download
TMP_ZIP="/tmp/bun-${target}.zip"
echo ""
echo -e "${CYAN}[DOWNLOAD]${NC} ${zip_url}"
echo " -> $TMP_ZIP"
curl --fail --location --progress-bar -o "$TMP_ZIP" "$zip_url"
# Extract
echo ""
echo -e "${CYAN}[EXTRACT]${NC} Extracting to $BIN_DIR..."
mkdir -p "$BIN_DIR"
unzip -oqd "$BIN_DIR" "$TMP_ZIP"
# The zip contains bun-${target}/bun — move it up
if [ -d "$BIN_DIR/bun-${target}" ]; then
mv "$BIN_DIR/bun-${target}/bun" "$BIN_DIR/bun"
rm -rf "$BIN_DIR/bun-${target}"
fi
chmod +x "$BIN_DIR/bun"
# Cleanup
rm -f "$TMP_ZIP"
# Verify
if [ -x "$BIN_DIR/bun" ]; then
echo ""
echo -e "${GREEN}[OK]${NC} Bun installed: $("$BIN_DIR/bun" --version)"
echo " Binary: $BIN_DIR/bun"
else
echo -e "${RED}[FAIL]${NC} Binary not found at expected path"
exit 1
fi
echo ""
echo -e "${GREEN}Done.${NC} Run 'bun --help' to get started."
echo ""
echo "Add to your PATH:"
echo " export BUN_INSTALL=\"$BUN_INSTALL\""
echo " export PATH=\"\$BUN_INSTALL/bin:\$PATH\""