OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git / lib / routes / bootstrap.js

98 lines · javascript ref:
/**
 * Bootstrap Route — serves install script for new users
 *
 * GET /bootstrap.sh
 *   Returns a shell script that downloads, installs, and configures pear-git.
 *   Default install dir: ~/.local/share/pear-git
 *   Override: PEAR_GIT_DIR=/custom/path curl -L .../bootstrap.sh | sh
 */

import { text } from '../response.js'

export function registerBootstrapRoutes (router) {
  router.get('/bootstrap.sh', handleBootstrap)
}

function handleBootstrap (req, res, server) {
  const { pathPrefix } = server.config
  // Build the server URL from the request
  const proto = req.headers['x-forwarded-proto'] || 'http'
  const host = req.headers['x-forwarded-host'] || req.headers.host
  const prefix = pathPrefix || ''
  const serverUrl = `${proto}://${host}${prefix}`

  const script = `#!/bin/sh
set -e

# Pear-Git installer
# Usage:
#   curl -L ${serverUrl}/bootstrap.sh | sh
#   PEAR_GIT_DIR=/opt/pear-git curl -L ${serverUrl}/bootstrap.sh | sh

INSTALL_DIR="\${PEAR_GIT_DIR:-\$HOME/.local/share/pear-git}"
SERVER="${serverUrl}"

echo "Installing pear-git to \$INSTALL_DIR..."

# Check dependencies
MISSING=""
for cmd in bare git npm curl tar; do
  if ! command -v "\$cmd" >/dev/null 2>&1; then
    MISSING="\$MISSING \$cmd"
  fi
done

if [ -n "\$MISSING" ]; then
  echo ""
  echo "Missing dependencies:\$MISSING"
  echo ""
  echo "Install them:"
  echo "  npm i -g bare-runtime      # for bare"
  echo "  sudo apt install git curl   # for git, curl (tar is usually pre-installed)"
  echo ""
  exit 1
fi

# Download and extract
mkdir -p "\$INSTALL_DIR"
echo "Downloading from \$SERVER..."
curl -fSL "\$SERVER/tabertron/pear-git/archive/release.tar.gz" | \\
  tar xz --strip-components=1 -C "\$INSTALL_DIR"

# Install npm dependencies (must happen before bare can load the server)
echo "Installing dependencies..."
cd "\$INSTALL_DIR" && npm install --production 2>&1 | tail -1 && cd - > /dev/null

# Install CLI tools
bare "\$INSTALL_DIR/pear-git-server.js" install

# Configure server
export PATH="\$HOME/.local/bin:\$PATH"
pgh config set server "\$SERVER" 2>/dev/null || true

echo ""
echo "Done! pear-git installed to \$INSTALL_DIR"
echo ""

# Check PATH
case ":\$PATH:" in
  *":\$HOME/.local/bin:"*) ;;
  *)
    echo "Add to your shell profile:"
    echo "  export PATH=\\"\\\$HOME/.local/bin:\\\$PATH\\""
    echo ""
    ;;
esac

echo "Get started:"
echo "  pgh repo list                  Browse repos"
echo "  pgh app install --app NAME     Install an app"
echo "  git clone punch://key/repo     Clone via P2P"
`

  res.statusCode = 200
  res.setHeader('Content-Type', 'text/plain')
  res.setHeader('Content-Disposition', 'attachment; filename="bootstrap.sh"')
  res.end(script)
}