BASH

Automating Git Repository Setup and Dependency Installation

Streamline your development workflow by automating Git repository cloning, navigating into the project, and installing dependencies like npm packages with a single bash script.

#!/bin/bash

# Usage: ./setup_project.sh <repository_url>

REPO_URL="$1"

if [ -z "$REPO_URL" ]; then
  echo "Usage: $0 <repository_url>"
  exit 1
fi

# Extract repository name from URL (assumes .git suffix for simplicity)
REPO_NAME=$(basename "$REPO_URL" .git)

echo "Cloning repository: $REPO_URL"
git clone "$REPO_URL"

if [ $? -ne 0 ]; then
  echo "Failed to clone repository." >&2
  exit 1
fi

cd "$REPO_NAME" || { echo "Failed to change directory to $REPO_NAME"; exit 1; }

# Check for common package managers and install dependencies
if [ -f "package.json" ]; then
  echo "Found package.json. Installing npm dependencies..."
  npm install
  if [ $? -ne 0 ]; then echo "npm install failed."; exit 1; fi
elif [ -f "requirements.txt" ]; then
  echo "Found requirements.txt. Installing pip dependencies..."
  pip install -r requirements.txt
  if [ $? -ne 0 ]; then echo "pip install failed."; exit 1; fi
else
  echo "No common dependency file (package.json, requirements.txt) found. Skipping dependency installation."
fi

echo "Project $REPO_NAME setup complete. You are now in the project directory."
# Optionally, add a command to start a development server, e.g., npm start
# npm start
How it works: This bash script automates the initial setup for a new project. It takes a Git repository URL as an argument, clones the repository, and then navigates into the new project directory. It intelligently checks for common dependency files like `package.json` (for Node.js projects) or `requirements.txt` (for Python projects) and attempts to install the necessary dependencies using `npm install` or `pip install -r`, respectively. Error handling is included to ensure the script exits if any step fails, providing clear feedback to the user.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs