Skip to content

Instantly share code, notes, and snippets.

@uluumbch
Created February 9, 2025 02:53
Show Gist options
  • Save uluumbch/edba8c3748f62ba08731ae24d56a2cd1 to your computer and use it in GitHub Desktop.
Save uluumbch/edba8c3748f62ba08731ae24d56a2cd1 to your computer and use it in GitHub Desktop.
Zip laravel project for production ready

don't forget to run chmod +x zip-project.sh first

Usage

✅ Exclude dev dependencies (default)

./zip-project.sh 8.1

(Runs composer install --no-dev by default)

✅ Include dev dependencies

./zip-project.sh 8.1 --dev

(Runs composer install without --no-dev)

✅ Use default PHP version (8.3)

./zip-project.sh --dev

(Uses PHP 8.3 and installs dev dependencies)

#!/bin/bash
# Define colors
GREEN="\e[32m"
YELLOW="\e[33m"
CYAN="\e[36m"
RED="\e[31m"
RESET="\e[0m"
# Define default PHP version
PHP_VERSION=83
NODE_VERSION="lts"
OUTPUT_ZIP="project-installed.zip"
INSTALL_DEV=false
# Check if PHP version is provided as the first argument
if [[ $1 =~ ^[0-9]+\.[0-9]+$ ]]; then
PHP_VERSION="${1//.}"
shift
fi
# Check for --dev flag
if [[ "$1" == "--dev" ]]; then
INSTALL_DEV=true
shift
fi
# Create a temporary directory
TMP_DIR=$(mktemp -d)
echo -e "${CYAN}Creating temporary directory: $TMP_DIR${RESET}"
# Create an rsync exclude pattern from .gitignore
EXCLUDES="--exclude=.git --exclude=$OUTPUT_ZIP"
if [ -f .gitignore ]; then
while IFS= read -r line; do
# Ignore comments and empty lines
if [[ -n "$line" && "$line" != \#* ]]; then
EXCLUDES+=" --exclude=$line"
fi
done < .gitignore
fi
# Copy the project to the temporary directory, excluding ignored files
echo -e "${YELLOW}Copying project files...${RESET}"
rsync -av $EXCLUDES ./ "$TMP_DIR"
# Determine composer install command
COMPOSER_FLAGS="--ignore-platform-reqs"
if [ "$INSTALL_DEV" = false ]; then
COMPOSER_FLAGS+=" --no-dev"
echo -e "${CYAN}Running Composer install without dev dependencies...${RESET}"
else
echo -e "${CYAN}Running Composer install with dev dependencies...${RESET}"
fi
# Run composer install inside the temporary directory
docker run --rm \
-u "$(id -u):$(id -g)" \
-v "$TMP_DIR:/var/www/html" \
-w /var/www/html \
laravelsail/php${PHP_VERSION}-composer:latest \
composer install $COMPOSER_FLAGS
# Check if package.json exists (to determine if npm is needed)
if [ -f "$TMP_DIR/package.json" ]; then
echo -e "${GREEN}Running npm install and npm run build...${RESET}"
# Run npm install (with dev dependencies)
docker run --rm \
-u "$(id -u):$(id -g)" \
-v "$TMP_DIR:/app" \
-w /app \
node:${NODE_VERSION} \
npm install
# Run npm run build
docker run --rm \
-u "$(id -u):$(id -g)" \
-v "$TMP_DIR:/app" \
-w /app \
node:${NODE_VERSION} \
npm run build
# Remove node_modules to reduce zip size
echo -e "${YELLOW}Removing node_modules folder...${RESET}"
rm -rf "$TMP_DIR/node_modules"
else
echo -e "${RED}Skipping npm install and build (package.json not found).${RESET}"
fi
# Zip the entire installed project (quiet mode)
echo -e "${CYAN}Creating zip file: $OUTPUT_ZIP${RESET}"
(cd "$TMP_DIR" && zip -rq "$OUTPUT_ZIP" .)
# Move the zip file back to the project directory
mv "$TMP_DIR/$OUTPUT_ZIP" "$(pwd)/$OUTPUT_ZIP"
# Clean up temporary directory
rm -rf "$TMP_DIR"
echo -e "${GREEN}Done. Output file: $(pwd)/$OUTPUT_ZIP${RESET}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment