Skip to content

Instantly share code, notes, and snippets.

@ngfw
Created August 25, 2024 15:14
Show Gist options
  • Save ngfw/ad8c879604fc3cd5a894377e3836670f to your computer and use it in GitHub Desktop.
Save ngfw/ad8c879604fc3cd5a894377e3836670f to your computer and use it in GitHub Desktop.
This Bash script automates the creation and setup of a new Laravel project, streamlining the initial configuration process. With this script, you can quickly set up a Laravel project with essential packages like Livewire, Filament, Jetstream, and Tailwind CSS. It also handles MySQL database creation and configuration, making it easier to get sta…
#!/bin/bash
# Check if project name is provided as an argument
if [ -z "$1" ]; then
echo "Error: No project name provided."
echo "Usage: ./setup_new_tallstack_project.sh project_name"
exit 1
fi
# Variables
PROJECT_NAME=$1
# Step 1: Create a new Laravel project
composer create-project --prefer-dist laravel/laravel $PROJECT_NAME
# Navigate into the project directory
cd $PROJECT_NAME
# Step 2: Set APP_NAME to the project name
sed -i "" "s/^APP_NAME=.*/APP_NAME='$PROJECT_NAME'/" .env
# Step 3: Ensure DB_CONNECTION is set to mysql and uncomment the relevant lines
sed -i "" "s/^DB_CONNECTION=.*/DB_CONNECTION=mysql/" .env
sed -i "" "s/^# DB_HOST=.*/DB_HOST=127.0.0.1/" .env
sed -i "" "s/^# DB_PORT=.*/DB_PORT=3306/" .env
sed -i "" "s/^# DB_DATABASE=.*/DB_DATABASE=$PROJECT_NAME/" .env
sed -i "" "s/^# DB_USERNAME=.*/DB_USERNAME=root/" .env
sed -i "" "s/^# DB_PASSWORD=.*/DB_PASSWORD=/" .env
# Step 4: Collect MySQL database details interactively
read -p "Enter your MySQL database name [$PROJECT_NAME]: " DB_NAME
DB_NAME=${DB_NAME:-$PROJECT_NAME}
read -p "Enter your MySQL username [root]: " DB_USERNAME
DB_USERNAME=${DB_USERNAME:-root}
read -sp "Enter your MySQL password: " DB_PASSWORD
echo
read -p "Enter your MySQL host [127.0.0.1]: " DB_HOST
DB_HOST=${DB_HOST:-127.0.0.1}
# Step 5: Attempt to connect to MySQL and create the database
echo "Connecting to MySQL..."
mysql --user=$DB_USERNAME --password=$DB_PASSWORD --host=$DB_HOST -e "CREATE DATABASE \`$DB_NAME\`;" 2>error.log
# Check if there was an error during database creation
if grep -q "ERROR" error.log; then
if grep -q "database exists" error.log; then
echo "Error: Database '$DB_NAME' already exists."
else
echo "An error occurred while creating the database. See error.log for details."
exit 1
fi
else
echo "Database '$DB_NAME' created successfully."
fi
# Step 6: Update .env file with MySQL credentials
sed -i "" "s/^DB_DATABASE=.*/DB_DATABASE=${DB_NAME//./\\.}/" .env
sed -i "" "s/^DB_USERNAME=.*/DB_USERNAME=$DB_USERNAME/" .env
sed -i "" "s/^DB_PASSWORD=.*/DB_PASSWORD=$DB_PASSWORD/" .env
sed -i "" "s/^DB_HOST=.*/DB_HOST=$DB_HOST/" .env
# Step 7: Install Livewire, Filament, and other required packages
composer require livewire/livewire filament/filament spatie/laravel-permission
php artisan filament:install --panels
# Step 8: Install and configure Jetstream with Livewire and dark mode
composer require laravel/jetstream
php artisan jetstream:install livewire --dark
# Step 9: Install Tailwind CSS
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init
# Step 10: Configure Tailwind CSS
cat <<EOT >> tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
darkMode: 'class',
content: [
'./resources/**/*.blade.php',
'./resources/**/*.js',
'./resources/**/*.vue',
'./vendor/filament/**/*.blade.php',
],
theme: {
extend: {},
},
plugins: [],
}
EOT
# Add Tailwind directives to resources/css/app.css
cat <<EOT > resources/css/app.css
@tailwind base;
@tailwind components;
@tailwind utilities;
EOT
# Step 11: Run migrations to create necessary database tables
php artisan migrate
# Step 12: Install npm dependencies and build assets
npm install
npm run build
# Step 13: Additional Laravel configurations (if any)
php artisan vendor:publish --tag=filament-config
# Step 14: Create Admin User
php artisan make:filament-user
php artisan filament:optimize
php artisan optimize:clear
# End script
echo "Setup is complete. Your Laravel project '$PROJECT_NAME' with Jetstream, Livewire, and Filament is ready."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment