#!/bin/bash # Interactive Documentation Setup Script # Inspired by shadcn/ui approach set -e # Default values DEFAULT_DOCS_DIR="docs" DEFAULT_TARGET_DIR="docs-app" # Ensure we're in a valid working directory if ! pwd &> /dev/null; then cd /tmp 2>/dev/null || cd ~ 2>/dev/null || { echo "❌ Error: Cannot establish a valid working directory" exit 1 } fi echo "🚀 Interactive Documentation Setup" echo "==================================" echo "" # Show usage if help is requested if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then echo "Usage:" echo " ./setup.sh [docs_dir] [target_dir]" echo "" echo "Arguments:" echo " docs_dir Source directory containing your documentation (default: $DEFAULT_DOCS_DIR)" echo " target_dir Target directory for the documentation app (default: $DEFAULT_TARGET_DIR)" echo "" echo "Examples:" echo " ./setup.sh # Interactive mode with prompts" echo " ./setup.sh docs apps/docs # Use specific directories" echo "" echo "Remote usage:" echo " curl -s URL/setup.sh | bash -s -- docs apps/docs" echo "" exit 0 fi # Check if curl is available if ! command -v curl &> /dev/null; then echo "❌ Error: curl is required but not installed." exit 1 fi # Check if tar is available if ! command -v tar &> /dev/null; then echo "❌ Error: tar is required but not installed." exit 1 fi # Check Node.js version if ! command -v node &> /dev/null; then echo "❌ Error: Node.js is required but not installed." echo "Please install Node.js 18.0.0 or higher from https://nodejs.org/" exit 1 fi NODE_VERSION=$(node --version | sed 's/v//') echo "✅ Node.js version $NODE_VERSION detected" # Simple version check - extract major version number NODE_MAJOR=$(echo $NODE_VERSION | cut -d. -f1) if [ "$NODE_MAJOR" -lt 18 ]; then echo "❌ Error: Node.js $NODE_VERSION is too old. Version 18.0.0 or higher is required." echo "Please update Node.js from https://nodejs.org/" exit 1 fi # Check npm version if ! command -v npm &> /dev/null; then echo "❌ Error: npm is required but not installed." exit 1 fi # Ensure we're in a valid directory before running npm if ! pwd &> /dev/null; then cd /tmp || cd ~ || exit 1 fi NPM_VERSION=$(npm --version) echo "✅ npm version $NPM_VERSION detected" REPO_URL="https://github.com/fea-lib/mdxpress/archive/refs/heads/main.tar.gz" # Check if we're running locally (for development) # When run via curl | bash, BASH_SOURCE[0] will be empty or not a real file path SCRIPT_SOURCE="${BASH_SOURCE[0]}" if [ -z "$SCRIPT_SOURCE" ] || [[ "$SCRIPT_SOURCE" == "bash" ]] || [[ "$SCRIPT_SOURCE" == "-bash" ]] || [[ "$SCRIPT_SOURCE" == "/dev/fd/"* ]]; then # Script is being piped (curl | bash), force remote mode LOCAL_MODE=false echo "🌐 Running in remote mode" else # Script is running from a file, check if it's local development SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_SOURCE")" 2>/dev/null && pwd)" || SCRIPT_DIR="" if [ -n "$SCRIPT_DIR" ] && [ -f "$SCRIPT_DIR/../app-template/package.json" ]; then LOCAL_MODE=true TEMPLATE_PATH="$(cd "$SCRIPT_DIR/../app-template" && pwd)" echo "🔧 Running in local development mode" else LOCAL_MODE=false echo "🌐 Running in remote mode" fi fi echo "This script will set up an interactive documentation app in your project." echo "Working directory: $(pwd)" echo "" # Check if arguments were provided if [ $# -ge 2 ]; then # Use command-line arguments DOCS_DIR="$1" TARGET_DIR="$2" echo "📚 Using docs directory: $DOCS_DIR" echo "📁 Using target directory: $TARGET_DIR" else # Prompt for input interactively read -p "📚 Enter your docs source directory [$DEFAULT_DOCS_DIR]: " DOCS_DIR DOCS_DIR=${DOCS_DIR:-$DEFAULT_DOCS_DIR} read -p "📁 Enter the target directory [$DEFAULT_TARGET_DIR]: " TARGET_DIR TARGET_DIR=${TARGET_DIR:-$DEFAULT_TARGET_DIR} fi # Ensure docs directory exists if [ ! -d "$DOCS_DIR" ]; then echo "📁 Creating docs directory: $DOCS_DIR" mkdir -p "$DOCS_DIR" echo "✅ Created docs directory" else echo "✅ Docs directory already exists: $DOCS_DIR" fi # Create parent directories for target directory if needed mkdir -p "$(dirname "$TARGET_DIR")" # Create the target directory itself if it doesn't exist mkdir -p "$TARGET_DIR" # The template is already prepared in TEMP_DIR/app-template (remote) or TEMPLATE_PATH (local), copy contents to target if [ "$LOCAL_MODE" = true ]; then if command -v rsync &> /dev/null; then echo "🔄 Copying app-template with rsync and exclusions..." rsync -aL --quiet --exclude='node_modules' --exclude='.env' --exclude='dist' --exclude='.git' --exclude='.astro' --exclude='package-lock.json' --exclude='yarn.lock' "$TEMPLATE_PATH/" "$TARGET_DIR/" else echo " rsync not available, using cp and manual cleanup..." cp -rL "$TEMPLATE_PATH"/* "$TARGET_DIR/" 2>/dev/null || { echo " Falling back to regular copy..." cp -r "$TEMPLATE_PATH"/* "$TARGET_DIR/" || { echo "❌ Error copying app-template to target directory." exit 1 } } fi else # Remote mode: download and extract template echo "📦 Downloading app-template..." TEMP_DIR=$(mktemp -d) cd "$TEMP_DIR" curl -L "$REPO_URL" -o app-template.tar.gz || { echo "❌ Error downloading app-template." exit 1 } tar -xzf app-template.tar.gz --strip-components=1 || { echo "❌ Error extracting app-template." exit 1 } cd - > /dev/null # Copy app-template to target directory echo "🔄 Copying app-template to $TARGET_DIR..." if [ -d "$TEMP_DIR/app-template" ]; then cp -r "$TEMP_DIR/app-template"/* "$TARGET_DIR/" || { echo "❌ Error copying app-template to target directory." exit 1 } else echo "❌ Error: Template directory not found in download." exit 1 fi # Cleanup rm -rf "$TEMP_DIR" fi # Always write src/environment.ts with correct values ENV_FILE="$TARGET_DIR/src/environment.ts" mkdir -p "$(dirname "$ENV_FILE")" # Calculate PATH_APP_TO_REPO_ROOT (relative path from app dir to repo root) APP_DEPTH=$(echo "$TARGET_DIR" | tr '/' '\n' | grep -v '^$' | wc -l) if [ "$APP_DEPTH" -eq 1 ]; then REPO_ROOT_PATH=".." else REPO_ROOT_PATH=".." for ((i=2; i<=APP_DEPTH; i++)); do REPO_ROOT_PATH="../$REPO_ROOT_PATH" done fi cat > "$ENV_FILE" << EOF // This file is auto-generated by setup.sh export const PATH_APP_TO_REPO_ROOT = "$REPO_ROOT_PATH"; export const DOCS_DIR = "$DOCS_DIR"; EOF echo "" echo "🎉 Setup complete!" echo "" echo "📁 Your interactive documentation app is ready in: $TARGET_DIR" echo " Absolute path: $(pwd)/$TARGET_DIR" echo "📚 Your docs will be loaded from: $DOCS_DIR" echo " Absolute path: $(pwd)/$DOCS_DIR" echo "" echo "🚀 Next steps:" echo "" echo "1. Navigate to your app directory:" echo " cd $TARGET_DIR" echo "" echo "2. Install dependencies:" echo " npm install" echo "" echo " 💡 If you encounter installation issues, try:" echo " npm cache clean --force && rm -rf node_modules package-lock.json && npm install" echo "" echo "3. Start the development server:" echo " npm run dev" echo "" echo "4. Start writing your documentation in the '$DOCS_DIR' directory!" echo "" echo "💡 Tips:" echo " - Edit files in src/ to customize the app" echo " - Add .mdx files to $DOCS_DIR for new documentation pages" echo " - Use Sandpack components for interactive code examples" echo " - Changes to docs will auto-reload thanks to the symlink!" echo "" echo "🔧 Troubleshooting:" echo " - Ensure Node.js >=18.0.0 is installed: node --version" echo " - Ensure npm >=9.0.0 is installed: npm --version" echo " - If Vite fails to start, delete node_modules and reinstall" echo "" echo "Happy documenting! 📝"