Skip to content

Instantly share code, notes, and snippets.

@neeravp
Last active March 15, 2021 01:05
Show Gist options
  • Save neeravp/fb50e17649f3caca7968a5d01bc64f7d to your computer and use it in GitHub Desktop.
Save neeravp/fb50e17649f3caca7968a5d01bc64f7d to your computer and use it in GitHub Desktop.
Bash Script to configure a new Laravel application
#!/usr/bin/env bash
# Declare variables of type array to gather information regarding packages
name="$1"
with_jetstream=false
stack=''
teams=false
with_livewire=false
with_inertia=false
with_vue=false
composer_require=()
composer_require_dev=()
npm_dependencies=()
npm_dev_dependencies=()
PHP=`which php`
APPDIR=`pwd`
# Create a stubs directory to hold all stubs required
# and assign path to STUBSDIR variable
STUBSDIR="$HOME/Workspace/Stubs"
# Function to print brown colored line on stdout
question() {
# builtin echo "$1" | sed $'s,.*,\e[0;33m&\e[0m,'>&1
# builtin echo "$1" | sed -e $'s,.*,\033[1m&\033[1m,'>&1
# builtin printf "\e[1;36m$1\e[0m"
builtin echo -e "\e[3;33m $1 \e[0m"
}
info() { builtin echo; echo -e "\e[36m $1 \e[0m"; echo; }
error() { builtin echo; echo -e "\e[1;31m $1 \e[0m"; echo; }
# read command options
# -p prompt output the string PROMPT without a trailing newline before attempting to read
# -r do not allow backslashes to escape any characters
ask_question(){
# ask_question <question> <default>
local ANSWER
read -r -p "$1 ($2): " ANSWER
echo "${ANSWER:-$2}"
}
confirm() {
local answer
read -r -p "$(question "$1 ($2):")" answer
echo ${answer:-$2}
}
# String concatenate first_name & last_name | full_name="${first_name} ${last_name}"
# String (name) toLowercase | ${name,,}
# Array (names) push "John" | names=("${names[@]}", "John")
# Array (names) implode i.e join all elements separated by space in to a string | "${names[@]}"
execute() {
info "Run '$(question 'npm run dev')' to start development server"; exit 1;
PS3=$(question "Which auth package do you want to install?" )
select opt in "Breeze" "Fortify" "Jetstream" "None"
do
case "$REPLY" in
1|2) configure_without_jetstream "laravel/${opt,,}"; break; ;;
3) configure_with_jetstream; break; ;;
4) echo ""; break; ;;
*) error "$REPLY is not a valid option. Please select again from options shown above [1-4]"; ;;
esac
# break
done
configure
}
configure_with_jetstream() {
with_jetstream=true
info "Configuring with Jetstream ... & $with_jetstream"
composer_require=("${composer_require[@]}" "laravel/jetstream")
### Jetsetream stack choice ###
PS3=$(question "Which Jetstream stack do you prefer?")
select opt in "Livewire" "Inertia"
do
case "$REPLY" in
1|2) stack=${opt,,}; break; ;;
*) error "$REPLY is not valid option. Please select from options shown above [1-2]" ;;
esac
done
### Jetstream teams toggle ###
teams=$(confirm "Will your application use teams (Y/n)" "n")
### Add alpinejs to npm dependencies if Livewire stack is chosen ###
if [[ $stack =~ 'livewire' ]]; then
npm_dependencies=("${npm_dependencies[@]}" "alpinejs")
fi
setup_vue "$stack"
# setup_eslint
# setup_typescript
# setup_phplint
# setup_tailwindcss
}
configure_without_jetstream() {
echo "Configuring without Jetstream... & $with_jetstream & package: $1"
composer_require=("${composer_require[@]}" "$1")
setup_livewire
if [[ $with_livewire =~ false ]]; then
setup_inertia
setup_vue "$stack"
fi
# setup_eslint
# setup_typescript
# setup_phplint
# setup_tailwindcss
if [[ $with_jetstream =~ false ]] && [[ -n $1 ]]; then
setup_oauth
fi
}
configure() {
setup_eslint
setup_typescript
setup_phplint
setup_tailwindcss
setup_bundler
install_composer_dependencies
install_node_dependencies
### Install Jetstream ###
if [[ $with_jetstream =~ true ]] && [[ -e "artisan" ]]; then
if [[ $teams =~ ^[Yy]$ ]]; then
info "Installing Jetstream with teams and stack: $stack"
"$PHP" artisan jetstream:install "$stack" --teams
else
info "Installing Jetstream without teams and stack: $stack"
"$PHP" artisan jetstream:install "$stack"
fi
fi
info "Configuration setup & scaffolding complete."
info "Run '$(question 'npm run dev')' to start development server"
}
setup_oauth() {
PS3=$(question "Which oauth package do you want to install?")
select opt in "None" "Sanctum" "Passport"
do
case "$REPLY" in
1) break; ;;
2) composer_require=("${composer_require[@]}" "laravel/sanctum"); break; ;;
3) composer_require=("${composer_require[@]}" "laravel/passport"); break; ;;
*) error "$REPLY is not a valid option. Please select again from options shown above [1-3]"; ;;
esac
done
}
setup_livewire() {
response=$(confirm "Do you want to install Livewire and AlpineJS? (Y/n)" "n")
if [[ $response =~ ^[Yy]$ ]]; then
with_livewire=true
stack="livewire"
composer_require=("${composer_require[@]}" "livewire/livewire")
npm_dependencies=("${npm_dependencies[@]}" "alpinejs")
fi
copy "$STUBSDIR/livewire/app.js" "$APPDIR/resources/js/app.ts"
}
setup_inertia() {
local deps=("@inertiajs/inertia" "@inertiajs/inertia-vue3")
local reqs=("inertiajs/inertia-laravel" "tightenco/ziggy")
response=$(confirm "Do you want to install InertiaJS? (Y/n)" "n")
info "Setting up Inertia js..."
if [[ $response =~ ^[Yy]$ ]]; then
with_inertia=true
stack="inertia"
npm_dependencies=("${npm_dependencies[@]}" "${deps[@]}")
composer_require=("${composer_require[@]}" "${reqs[@]}")
fi
}
setup_vue() {
local install_vue
local install_router
local install_vuex
local devDeps=(
"@vue/compiler-sfc"
"eslint-plugin-vue"
"@vue/eslint-config-standard"
"@vue/eslint-config-typescript"
"vue-loader@next"
)
if [[ $stack != "livewire" ]]; then
echo "Install Vue? stack: $stack"
install_vue=$(confirm "Do you want to install Vue? (Y/n)" "n")
if [[ $install_vue =~ ^[Yy]$ ]]; then
npm_dependencies=("${npm_dependencies[@]}" "vue@next")
fi
if [[ -z $stack ]] || [[ $stack != 'inertia' ]]; then
install_router=$(confirm "Do you want to install Vue Router? (Y/n)" "n")
if [[ $install_router =~ ^[Yy]$ ]]; then
npm_dependencies=("${npm_dependencies[@]}" "vue-router@4")
fi
fi
install_vuex=$(confirm "Do you want to install Vuex? (Y/n)" "n")
if [[ $install_vuex =~ ^[Yy]$ ]]; then
npm_dependencies=("${npm_dependencies[@]}" "vuex@next")
fi
npm_dev_dependencies=("${npm_dev_dependencies[@]}" "${devDeps[@]}")
info "Setting up VueJS..."
# copy app.js stub to /resources/js/app.ts
copy "$STUBSDIR/app.js" "$APPDIR/resources/js/app.ts"
# mkdir -p /resources/js/components with 0755 permissions
make_directory "$APPDIR/resources/js/components"
# copy Example.vue to /resources/js/components/Example.vue
copy "$STUBSDIR/components/Example.vue" "$APPDIR/resources/js/components/Example.vue"
fi
}
setup_eslint() {
info "Setup Eslint..."
local devDeps=("eslint" "eslint-plugin-import" "eslint-plugin-vue" "eslint-plugin-node" "eslint-plugin-promise")
npm_dev_dependencies=("${npm_dev_dependencies[@]}" "${devDeps[@]}")
copy "$STUBSDIR/eslintrc" "$APPDIR/.eslintrc.js"
}
setup_typescript() {
info "Setting up Typescript support..."
confirmed=$(confirm "Do you want to add Typescript support? (Y/n)" "Y")
local devDeps=(
'@types/node'
'@types/webpack-env'
'@typescript-eslint/eslint-plugin'
'@typescript-eslint/parser'
'typescript'
'ts-loader'
)
npm_dev_dependencies=("${npm_dev_dependencies[@]}" "${devDeps[@]}")
copy "$STUBSDIR/tsconfig" "$APPDIR/tsconfig.json"
copy "$STUBSDIR/shims-vue.d.ts" "$APPDIR/resources/js/shims-vue.d.ts"
if [[ $stack != 'livewire' ]]; then
copy "$STUBSDIR/webpack.mix.js" "$APPDIR/webpack.mix.js"
else
copy "$STUBSDIR/livewire/webpack.mix.js" "$APPDIR/webpack.mix.js"
fi
}
setup_phplint() {
info "Setup PHP linting..."
copy "$STUBSDIR/php_cs" "$APPDIR/.php_cs"
copy "$STUBSDIR/phpstan.neon" "$APPDIR/phpstan.neon"
}
setup_tailwindcss() {
info "Setting up TailwindCSS..."
local deps=("tailwindcss" "@tailwindcss/forms" "@tailwindcss/typography")
local devDeps=("autoprefixer" "postcss-import")
if [[ $with_jetstream =~ false ]]; then
npm_dependencies=("${npm_dependencies[@]}" "${deps[@]}")
npm_dev_dependencies=("${npm_dev_dependencies[@]}" "${devDeps[@]}")
copy "$STUBSDIR/tailwind.config" "$APPDIR/tailwind.config.js"
copy "$STUBSDIR/app.css" "$APPDIR/resources/css/app.css"
fi
}
setup_bundler() {
local bundler=$(confirm "Would you like to use ViteJs for bundling instead of Laravel Mix? (Y/n)" "n")
local deps=("vite" "@vitejs/plugin-vue" "dotenv")
if [[ $bundler =~ ^[Yy]$ ]]; then
info "Setting up Vitejs as the bundler..."
# remove laravel-mix from package.json
#sed -i '' -e 's/"laravel-mix"\:[ ]\{1,2\}"^[0-9]\{1,2\}.[0-9]\{1,2\}.[0-9]\{1,2\}",//' package.json
# remove webpack.mix.js
rm -f webpack.mix.js
#replace package.json
copy "$STUBSDIR/vite/package.json" "$APPDIR/package.json"
# replace tsconfig
copy "$STUBSDIR/vite/tsconfig.json" "$APPDIR/tsconfig.json"
# cp vite.config.js
copy "$STUBSDIR/vite/vite.config.ts" "$APPDIR/vite.config.ts"
# cp postcss.config.js
copy "$STUBSDIR/vite/postcss.config.js" "$APPDIR/postcss.config.js"
# update node dependencies
npm_dev_dependencies=("${npm_dev_dependencies[@]}" "${deps[@]}")
# add ASSET_URL to .env
echo "ASSET_URL=http:localhost:3000" | tee -a .env > /dev/null
fi
}
install_composer_dependencies() {
if [[ -e "composer.json" ]]; then
composer install
if [[ -n $composer_require ]]; then
composer require "$composer_require"
fi
if [[ -n $composer_require_dev ]]; then
composer require "$composer_require_dev" --dev
fi
fi
}
install_node_dependencies() {
if [[ -e "package.json" ]]; then
info "Installing node dependencies..."
npm --silent install "${npm_dependencies[@]}"
npm --silent install "${npm_dev_dependencies[@]}" --save-dev
npm --silent install
# npm run dev
fi
}
copy() {
if [[ -e "artisan" ]]; then
cp "$1" "$2"
fi
}
make_directory() {
if [[ -e "artisan" ]]; then
mkdir -p -m 0755 "$1"
fi
}
execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment