Bash Alias for Creating a Laravel Project

Create a Laravel Project from Anywhere and Open in VS Code with One Command

Image By EJ Yao on Unsplash

Posted on

Whenever I create a new Laravel project, there are a number of things that I do in the terminal before I can start working on code:

  1. Navigate to my ~/Sites/ directory
  2. Create the Laravel App
  3. Install NPM dependencies
  4. Initialize git and make the first commit
  5. Open in VS Code

I wanted to streamline that into one command. So I created a Bash alias/function to do everything for me: nlp (New Laravel Project). It will check if that project exists, automatically navigate to the ~/Sites directory, create the project, install the dependencies (Composer and NPM), and open the project in VS Code all with one command. Below is the function in my .bash_profile file. Of course, edit the code to fit your needs.

function nlp() {
    DIR="$HOME/Sites"

    if [ -d "$DIR/$1" ]; then
        echo "Directory $DIR/$1 exists already."

        return 1
    fi

    cd $DIR
    composer create-project laravel/laravel --prefer-dist $1
    cd $1
    npm install
    git init
    git add -A
    git commit -m "Initial commit"
    code .
}

Usage: nlp my-project-name.

You can call the command from anywhere on your system and the project will be created in the ~/Sites directory.

P.S. Don't forget to source ~/.bash_profile to load the new command into your current shell.

Newsletter


Get notified when I post something new.

Latest Quick Tips


Bash Alias for Creating a Laravel Project
  • A Quick Tip posted on 2018-07-31
Carbon Helper
  • A Quick Tip posted on 2017-10-05

Latest Blog Posts


How to Persist Databases Across Homestead Instances
  • A Blog Post posted on 2018-02-16
How to Set Up Xdebug on Laravel Homestead and VSCode
  • A Blog Post posted on 2017-10-23
Compiling LESS Using Blade Templates
  • A Blog Post posted on 2016-07-21
A Simple Look at the Laravel Service Container
  • A Blog Post posted on 2015-10-12