#!/bin/bash
# This script is used to build and prepare the frontend code during the Docker build process
# It is called from the Dockerfile
set -e # Exit on error
echo "Starting frontend build process..."
# Clear the static directory but create it if it doesn't exist
mkdir -p ./static
rm -rf ./static/*
if [ -d "frontend/podcraft" ]; then
echo "Frontend code found, attempting to build..."
# Navigate to the frontend directory
cd frontend/podcraft
# Check if package.json exists
if [ -f "package.json" ]; then
echo "Installing frontend dependencies..."
npm install --quiet || { echo "npm install failed"; exit 1; }
echo "Building frontend application..."
npm run build || { echo "npm build failed"; exit 1; }
# Check if the build directory exists
if [ -d "dist" ]; then
echo "Build successful, copying files to static directory..."
cp -r dist/* ../../static/
echo "Frontend build files copied to static directory"
# List the contents of the static directory to verify
echo "Static directory contents:"
ls -la ../../static/
else
echo "ERROR: Build directory 'dist' not found after build"
exit 1
fi
else
echo "ERROR: package.json not found in frontend/podcraft"
exit 1
fi
# Return to the original directory
cd ../..
else
echo "Frontend code not found, creating a fallback UI"
mkdir -p frontend/podcraft/public
mkdir -p frontend/podcraft/src
# Create a minimal package.json
cat > frontend/podcraft/package.json << EOF
{
"name": "podcraft",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.10.1",
"react-router-dom": "^6.15.0",
"reactflow": "^11.8.3"
},
"devDependencies": {
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@vitejs/plugin-react": "^4.0.3",
"eslint": "^8.0.0",
"eslint-plugin-react-hooks": "^4.0.0",
"eslint-plugin-react-refresh": "^0.4.0",
"vite": "^4.0.0"
}
}
EOF
# Create a minimal index.html
cat > frontend/podcraft/index.html << EOF
PodCraft - AI Podcast Generator
EOF
# Create a minimal vite.config.js
cat > frontend/podcraft/vite.config.js << EOF
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
server: {
proxy: {
'/api': 'http://localhost:8000',
'/login': 'http://localhost:8000',
'/signup': 'http://localhost:8000',
'/token': 'http://localhost:8000',
'/generate-podcast': 'http://localhost:8000'
}
}
})
EOF
# Create a minimal main.jsx
mkdir -p frontend/podcraft/src
cat > frontend/podcraft/src/main.jsx << EOF
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'
ReactDOM.createRoot(document.getElementById('root')).render(
,
)
EOF
# Create a minimal App.jsx with a more functional UI
cat > frontend/podcraft/src/App.jsx << EOF
import React, { useState, useEffect } from 'react'
import './App.css'
function App() {
const [apiStatus, setApiStatus] = useState('checking')
const [mongoStatus, setMongoStatus] = useState('unknown')
const [apiMessage, setApiMessage] = useState('')
useEffect(() => {
// Check the API status
fetch('/api/status')
.then(response => response.json())
.then(data => {
setApiStatus('online')
setApiMessage(data.message || 'API is running')
})
.catch(error => {
setApiStatus('offline')
console.error('API Error:', error)
})
}, [])
return (