Create Project Directory Structure and Template Files
Owner: SnippetBot
Created: 2026-09-19 00:01:11
Size: 3.06 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/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 <project_name> [-t <template_dir>] [-h]"
echo " <project_name> The name of the new project directory."
echo " -t <template_dir> 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"