|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +R='\033[0;31m' G='\033[0;32m' Y='\033[1;33m' B='\033[0;34m' NC='\033[0m' |
| 5 | + |
| 6 | +error_exit() { |
| 7 | + echo -e "\n${R}✗ $1${NC}" >&2 |
| 8 | + [ -n "$2" ] && echo -e "${Y}→ $2${NC}" >&2 |
| 9 | + exit 1 |
| 10 | +} |
| 11 | + |
| 12 | +# Detect operating system |
| 13 | +detect_os() { |
| 14 | + case "$(uname -s)" in |
| 15 | + Darwin*) |
| 16 | + echo "macos" |
| 17 | + ;; |
| 18 | + Linux*) |
| 19 | + if grep -qi microsoft /proc/version 2>/dev/null; then |
| 20 | + echo "wsl" |
| 21 | + else |
| 22 | + echo "linux" |
| 23 | + fi |
| 24 | + ;; |
| 25 | + MINGW*|MSYS*|CYGWIN*) |
| 26 | + echo "windows" |
| 27 | + ;; |
| 28 | + *) |
| 29 | + echo "unknown" |
| 30 | + ;; |
| 31 | + esac |
| 32 | +} |
| 33 | + |
| 34 | +OS_TYPE=$(detect_os) |
| 35 | + |
| 36 | +echo -e "\n🌐 Configure npmx.test domain for local development" |
| 37 | + |
| 38 | +# Check for unsupported platforms |
| 39 | +if [ "$OS_TYPE" = "windows" ]; then |
| 40 | + error_exit "Windows is not supported by Localias" \ |
| 41 | + "Please use WSL2 instead: https://learn.microsoft.com/windows/wsl/install" |
| 42 | +fi |
| 43 | + |
| 44 | +if [ "$OS_TYPE" = "unknown" ]; then |
| 45 | + error_exit "Unsupported operating system" "Localias supports macOS, Linux, and WSL2" |
| 46 | +fi |
| 47 | + |
| 48 | +# Check prerequisites with platform-specific instructions |
| 49 | +if ! command -v localias &>/dev/null; then |
| 50 | + case "$OS_TYPE" in |
| 51 | + macos) |
| 52 | + error_exit "Localias not installed" "brew install peterldowns/tap/localias" |
| 53 | + ;; |
| 54 | + linux|wsl) |
| 55 | + error_exit "Localias not installed" "go install github.com/peterldowns/localias/cmd/localias@latest" |
| 56 | + ;; |
| 57 | + esac |
| 58 | +fi |
| 59 | + |
| 60 | +[ -f ".localias.yml" ] || error_exit ".localias.yml not found" "Run from project root" |
| 61 | + |
| 62 | +# Linux/WSL: Check and configure privileged port capability |
| 63 | +if [ "$OS_TYPE" = "linux" ] || [ "$OS_TYPE" = "wsl" ]; then |
| 64 | + LOCALIAS_PATH=$(which localias) |
| 65 | + if ! getcap "$LOCALIAS_PATH" 2>/dev/null | grep -q "cap_net_bind_service"; then |
| 66 | + echo -e "${Y}⚙ Configuring privileged port access for Localias${NC}" |
| 67 | + echo "This allows Localias to bind to ports 80 and 443 without sudo" |
| 68 | + sudo setcap CAP_NET_BIND_SERVICE=+eip "$LOCALIAS_PATH" || \ |
| 69 | + error_exit "Failed to set capabilities" "Run: sudo setcap CAP_NET_BIND_SERVICE=+eip \$(which localias)" |
| 70 | + echo -e "${G}✓ Capabilities configured${NC}" |
| 71 | + fi |
| 72 | +fi |
| 73 | + |
| 74 | +# Check if already configured |
| 75 | +if localias list 2>/dev/null | grep -q "npmx.test"; then |
| 76 | + echo -e "${Y}⚠ npmx.test already configured${NC}" |
| 77 | + if localias status &>/dev/null; then |
| 78 | + echo -e "${G}✓ Daemon running${NC} - visit ${B}https://npmx.test${NC}" |
| 79 | + exit 0 |
| 80 | + fi |
| 81 | + echo "Restarting daemon..." |
| 82 | +else |
| 83 | + echo "Configuring npmx.test → localhost:3000 (requires sudo)" |
| 84 | + read -p "Press Enter to continue or Ctrl+C to cancel... " |
| 85 | + localias set npmx.test 3000 || error_exit "Failed to configure alias" "Check sudo access" |
| 86 | +fi |
| 87 | + |
| 88 | +# Start daemon |
| 89 | +echo "Starting localias daemon (requires sudo)..." |
| 90 | +sudo localias stop 2>/dev/null || true |
| 91 | +if ! sudo localias start 2>&1 | grep -q "server running"; then |
| 92 | + error_exit "Failed to start daemon" "Check ports 80/443 or run: sudo localias start" |
| 93 | +fi |
| 94 | + |
| 95 | +# Verify |
| 96 | +sleep 1 |
| 97 | +if ! localias status &>/dev/null; then |
| 98 | + error_exit "Daemon not running" "Run manually: sudo localias start" |
| 99 | +fi |
| 100 | + |
| 101 | +echo -e "\n${G}✓ Setup complete${NC}" |
| 102 | +echo -e " Start dev server: ${B}pnpm dev${NC}" |
| 103 | +echo -e " Visit: ${B}https://npmx.test${NC}" |
| 104 | +echo -e " Remove: ${B}pnpm setup:local:uninstall${NC}" |
| 105 | + |
| 106 | +# Platform-specific notes |
| 107 | +if [ "$OS_TYPE" = "wsl" ]; then |
| 108 | + echo -e "\n${Y}ℹ WSL2 Note:${NC}" |
| 109 | + echo -e " - SSL certificates work in WSL browsers" |
| 110 | + echo -e " - For Windows browsers, manually install cert from:" |
| 111 | + echo -e " ~/.localias/certs/npmx.test.crt" |
| 112 | +fi |
| 113 | + |
| 114 | +# AtProto OAuth caveat |
| 115 | +echo -e "\n${Y}⚠ AtProto OAuth Note:${NC}" |
| 116 | +echo -e " When using Bluesky login, the OAuth callback will redirect to" |
| 117 | +echo -e " ${B}http://127.0.0.1:3000${NC} instead of ${B}npmx.test${NC}" |
| 118 | +echo -e " This is required per AtProto spec for localhost development." |
| 119 | +echo -e " Your session will work correctly after the redirect." |
| 120 | +echo -e " See: ${B}https://atproto.com/specs/oauth#localhost-client-development${NC}" |
| 121 | + |
| 122 | +echo "" |
0 commit comments