claude-code-termux/bin/claude-update

69 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# claude-update: Safe updater for Claude Code on Termux
# Respects version ceiling (v2.1.112 = last pure Node.js release)
#
set -e
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
NC="\033[0m"
MAX_VERSION="2.1.112"
CURRENT=$(claude --version 2>/dev/null | grep -oE "[0-9]+\.[0-9]+\.[0-9]+" | head -1)
echo "Current: v${CURRENT:-not installed}"
# Check what latest available is
LATEST=$(npm view @anthropic-ai/claude-code version 2>/dev/null)
echo "Latest available: v${LATEST:-unknown}"
echo "Termux ceiling: v$MAX_VERSION"
echo ""
# Compare versions (is latest > ceiling?)
version_gt() {
test "$(printf '%s\n' "$1" "$2" | sort -V | tail -1)" != "$2"
}
if [[ "$CURRENT" == "$MAX_VERSION" ]]; then
echo -e "${GREEN}Already at ceiling version v$MAX_VERSION${NC}"
echo "Versions above this use native binaries (incompatible with Termux/Android)"
exit 0
fi
if version_gt "$LATEST" "$MAX_VERSION"; then
echo -e "${YELLOW}Latest (v$LATEST) exceeds ceiling.${NC}"
echo "Installing ceiling version v$MAX_VERSION instead..."
TARGET="$MAX_VERSION"
else
TARGET="$LATEST"
fi
# Verify target is pure Node.js (has cli.js, not cli-wrapper.cjs)
echo "Verifying v$TARGET is Node.js compatible..."
FILES=$(npm pack "@anthropic-ai/claude-code@$TARGET" --dry-run 2>&1 || true)
if echo "$FILES" | grep -q "cli-wrapper.cjs"; then
echo -e "${RED}ERROR: v$TARGET uses native binaries (cli-wrapper.cjs)${NC}"
echo "This version is NOT compatible with Termux/Android."
exit 1
fi
# Unprotect if previously locked
CLAUDE_DIR="$PREFIX/lib/node_modules/@anthropic-ai/claude-code"
if [[ -d "$CLAUDE_DIR" ]]; then
chmod -R u+w "$CLAUDE_DIR" 2>/dev/null || true
fi
echo "Installing v$TARGET..."
npm install -g "@anthropic-ai/claude-code@$TARGET"
# Re-protect against auto-updater
chmod -R a-w "$CLAUDE_DIR" 2>/dev/null || true
NEW=$(claude --version 2>/dev/null | grep -oE "[0-9]+\.[0-9]+\.[0-9]+" | head -1)
echo ""
echo -e "${GREEN}Updated: v$CURRENT -> v$NEW${NC}"
echo "Package protected against auto-updater (chmod a-w)"