#!/bin/bash # Configuration PROJECT_NAME="" DEFAULT_TEMPLATES_DIR="$HOME/.bash_templates/project_scaffold" # Custom templates directory # Function to display script usage usage() { echo "Usage: $0 [-t ] [-h]" echo " The name of the new project directory." echo " -t Optional: Specify a custom directory for template files." echo " Default: $DEFAULT_TEMPLATES_DIR" echo " -h Display this help message." exit 0 } # Function to create basic project structure create_structure() { local name="$1" local templates_path="$2" if [ -d "$name" ]; then echo "Error: Directory '$name' already exists. Please choose a different name or remove it." >&2 exit 1 fi echo "Creating project directory: $name" mkdir "$name" cd "$name" || exit 1 # Exit if cannot change directory echo "Creating core directories..." mkdir -p src public assets config tests docs scripts echo "Creating basic files..." touch README.md echo "# $name" > README.md echo "## Project Setup" >> README.md echo "```bash" >> README.md echo "npm install" >> README.md echo "npm run dev" >> README.md echo "```" >> README.md touch .env.example echo "APP_NAME=$name" > .env.example echo "APP_ENV=development" >> .env.example echo "DB_HOST=localhost" >> .env.example touch .gitignore echo "node_modules/" >> .gitignore echo "dist/" >> .gitignore echo "build/" >> .gitignore echo ".env" >> .gitignore echo "*.log" >> .gitignore echo "*.DS_Store" >> .gitignore # Copy custom templates if directory exists if [ -d "$templates_path" ]; then echo "Copying custom templates from '$templates_path'..." # Using cp -r to copy directories and files recursively # rsync can also be used for more control cp -r "$templates_path"/* . 2>/dev/null || : # Ignore errors if dir is empty echo "Custom templates copied." else echo "No custom template directory found at '$templates_path'. Skipping custom templates." echo "You can create one (e.g., mkdir -p $templates_path) and add your common files there." fi echo "Project '$name' created successfully at $(pwd)." echo "Navigate into it with: cd $name" } # --- Main Script Logic --- # Parse command-line arguments # ':' for option with argument, 'h' for help while getopts "ht:" opt; do case "${opt}" in h) usage ;; t) DEFAULT_TEMPLATES_DIR="${OPTARG}" ;; :) echo "Error: Option -${OPTARG} requires an argument." >&2 usage ;; ?) echo "Error: Invalid option -${OPTARG}." >&2 usage ;; esac done # Shift off the options and their arguments shift $((OPTIND -1)) # Check for project name argument if [ -z "$1" ]; then echo "Error: Project name is required." >&2 usage fi PROJECT_NAME="$1" create_structure "$PROJECT_NAME" "$DEFAULT_TEMPLATES_DIR"