Skip to content

Instantly share code, notes, and snippets.

@ahmadrosid
Last active September 18, 2025 19:05
Show Gist options
  • Save ahmadrosid/85166af57da431f815807dcbf576ba62 to your computer and use it in GitHub Desktop.
Save ahmadrosid/85166af57da431f815807dcbf576ba62 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Migration script to move project files under src/ directory
# This script will move app/, components/, lib/, and styles/ directories into src/
set -e
echo "πŸš€ Starting migration to src/ directory structure..."
# Create src directory if it doesn't exist
if [ ! -d "src" ]; then
echo "πŸ“ Creating src/ directory..."
mkdir src
fi
# Move directories to src/
directories_to_move=("app" "components" "lib" "styles")
for dir in "${directories_to_move[@]}"; do
if [ -d "$dir" ]; then
echo "πŸ“¦ Moving $dir/ to src/$dir/..."
# Check if destination already exists
if [ -d "src/$dir" ]; then
echo "⚠️ src/$dir/ already exists, merging contents..."
# Move contents instead of the directory itself
cp -r "$dir"/* "src/$dir/" 2>/dev/null || true
rm -rf "$dir"
else
mv "$dir" "src/"
fi
else
echo "⚠️ Directory $dir/ not found, skipping..."
fi
done
echo "βœ… Directory migration completed!"
# Update configuration files
echo "πŸ”§ Updating configuration files..."
# Update next.config.mjs if it exists
if [ -f "next.config.mjs" ]; then
echo "πŸ“ Updating next.config.mjs..."
# Add srcDir configuration
if ! grep -q "srcDir" next.config.mjs; then
sed -i.bak 's/const nextConfig = {/const nextConfig = {\n srcDir: ".\/src",/' next.config.mjs
echo " βœ“ Added srcDir configuration to next.config.mjs"
fi
fi
# Update tsconfig.json
if [ -f "tsconfig.json" ]; then
echo "πŸ“ Updating tsconfig.json paths..."
# Backup original file
cp tsconfig.json tsconfig.json.bak
# Update paths in tsconfig.json
cat tsconfig.json | jq '.compilerOptions.paths = {
"@/*": ["./src/*"],
"@/components/*": ["./src/components/*"],
"@/lib/*": ["./src/lib/*"],
"@/app/*": ["./src/app/*"],
"@/styles/*": ["./src/styles/*"]
}' > tsconfig.json.tmp && mv tsconfig.json.tmp tsconfig.json
echo " βœ“ Updated tsconfig.json with new path mappings"
fi
# Update tailwind.config.js
if [ -f "tailwind.config.js" ]; then
echo "πŸ“ Updating tailwind.config.js..."
# Backup original file
cp tailwind.config.js tailwind.config.js.bak
# Update content paths
sed -i.tmp 's|"./app/|"./src/app/|g; s|"./components/|"./src/components/|g; s|"./lib/|"./src/lib/|g' tailwind.config.js
rm tailwind.config.js.tmp
echo " βœ“ Updated tailwind.config.js content paths"
fi
# Update package.json scripts if needed
if [ -f "package.json" ]; then
echo "πŸ“ Checking package.json..."
# Most Next.js scripts should work without changes due to srcDir config
echo " βœ“ package.json should work with new structure"
fi
echo ""
echo "πŸŽ‰ Migration completed successfully!"
echo ""
echo "πŸ“‹ Summary of changes:"
echo " β€’ Moved app/, components/, lib/, styles/ to src/"
echo " β€’ Updated next.config.mjs with srcDir configuration"
echo " β€’ Updated tsconfig.json with new path mappings"
echo " β€’ Updated tailwind.config.js content paths"
echo ""
echo "πŸ” Next steps:"
echo " 1. Test your application: npm run dev"
echo " 2. Update any remaining import paths if needed"
echo " 3. Remove .bak files if everything works correctly"
echo ""
echo "πŸ“ New project structure:"
echo " src/"
echo " β”œβ”€β”€ app/"
echo " β”œβ”€β”€ components/"
echo " β”œβ”€β”€ lib/"
echo " └── styles/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment