#!/bin/bash # experiment.sh # Author: Matthew Raymer # Description: Build script for TimeSafari Electron application # This script handles the complete build process for the TimeSafari Electron app, # including web asset compilation, TypeScript compilation, and AppImage packaging. # It ensures all dependencies are available and provides detailed build feedback. # # Build Process: # 1. Environment setup and dependency checks # 2. Web asset compilation (Vite) # 3. TypeScript compilation # 4. Electron main process build # 5. AppImage packaging # # Dependencies: # - Node.js and npm # - TypeScript # - Vite # - electron-builder # # Usage: ./experiment.sh # # Exit Codes: # 1 - Required command not found # 2 - TypeScript installation failed # 3 - TypeScript compilation failed # 4 - Build process failed # 5 - AppImage build failed # Exit on any error set -e # ANSI color codes for better output formatting readonly RED='\033[0;31m' readonly GREEN='\033[0;32m' readonly YELLOW='\033[1;33m' readonly BLUE='\033[0;34m' readonly NC='\033[0m' # No Color # Logging functions log_info() { echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')] [INFO]${NC} $1" } log_success() { echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] [SUCCESS]${NC} $1" } log_warn() { echo -e "${YELLOW}[$(date '+%Y-%m-%d %H:%M:%S')] [WARN]${NC} $1" } log_error() { echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] [ERROR]${NC} $1" } # Function to check if a command exists check_command() { if ! command -v "$1" &> /dev/null; then log_error "$1 is required but not installed." exit 1 fi log_info "Found $1: $(command -v "$1")" } # Function to measure and log execution time measure_time() { local start_time=$(date +%s) "$@" local end_time=$(date +%s) local duration=$((end_time - start_time)) log_success "Completed in ${duration} seconds" } # Function to find the AppImage find_appimage() { local appimage_path appimage_path=$(find dist-electron-packages -name "*.AppImage" -type f -print -quit) if [ -n "$appimage_path" ]; then echo "$appimage_path" else log_warn "AppImage not found in expected location" echo "dist-electron-packages/*.AppImage" fi } # Print build header echo -e "\n${BLUE}=== TimeSafari Electron Build Process ===${NC}\n" log_info "Starting build process at $(date)" # Check required commands log_info "Checking required dependencies..." check_command node check_command npm # Create application data directory log_info "Setting up application directories..." mkdir -p ~/.local/share/TimeSafari/timesafari # Clean up previous builds log_info "Cleaning previous builds..." rm -rf dist* || log_warn "No previous builds to clean" # Set environment variables for the build log_info "Configuring build environment..." export VITE_PLATFORM=electron export VITE_PWA_ENABLED=false export VITE_DISABLE_PWA=true # Ensure TypeScript is installed log_info "Verifying TypeScript installation..." if [ ! -f "./node_modules/.bin/tsc" ]; then log_info "Installing TypeScript..." if ! npm install --save-dev typescript@~5.2.2; then log_error "TypeScript installation failed!" exit 2 fi # Verify installation if [ ! -f "./node_modules/.bin/tsc" ]; then log_error "TypeScript installation verification failed!" exit 2 fi log_success "TypeScript installed successfully" else log_info "TypeScript already installed" fi # Get git hash for versioning GIT_HASH=$(git log -1 --pretty=format:%h) log_info "Using git hash: ${GIT_HASH}" # Build web assets log_info "Building web assets with Vite..." if ! measure_time env VITE_GIT_HASH="$GIT_HASH" npx vite build --config vite.config.app.electron.mts --mode electron; then log_error "Web asset build failed!" exit 4 fi # TypeScript compilation log_info "Compiling TypeScript..." if ! measure_time ./node_modules/.bin/tsc -p tsconfig.electron.json; then log_error "TypeScript compilation failed!" exit 3 fi # Build electron main process log_info "Building electron main process..." if ! measure_time env VITE_GIT_HASH="$GIT_HASH" npx vite build --config vite.config.electron.mts --mode electron; then log_error "Electron main process build failed!" exit 4 fi # Organize files log_info "Organizing build artifacts..." mkdir -p dist-electron/www cp -r dist/* dist-electron/www/ || log_error "Failed to copy web assets" mkdir -p dist-electron/resources cp src/electron/preload.js dist-electron/resources/preload.js || log_error "Failed to copy preload script" # Build the AppImage log_info "Building AppImage package..." if ! measure_time npx electron-builder --linux AppImage; then log_error "AppImage build failed!" exit 5 fi # Print build summary echo -e "\n${GREEN}=== Build Summary ===${NC}" log_success "Build completed successfully!" log_info "Build artifacts location: $(pwd)/dist-electron" log_info "AppImage location: $(find_appimage)" # Check for build warnings if grep -q "default Electron icon is used" dist-electron-packages/builder-effective-config.yaml; then log_warn "Using default Electron icon - consider adding a custom icon" fi if grep -q "chunks are larger than 1000 kB" dist-electron-packages/builder-effective-config.yaml; then log_warn "Large chunks detected - consider implementing code splitting" fi echo -e "\n${GREEN}=== End of Build Process ===${NC}\n" # Exit with success exit 0