|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Colors |
| 4 | +NOCOLOR='\033[0m' |
| 5 | +RED='\033[0;31m' |
| 6 | +YELLOW='\033[1;33m' |
| 7 | +GREEN='\033[32m' |
| 8 | +BOLD='\033[1m' |
| 9 | + |
| 10 | +# Configureation part of .env file and docker-compose file |
| 11 | +function proceed_with_WP_environment() { |
| 12 | + |
| 13 | + # user-input - WP Backend URL |
| 14 | + read -p "$(echo -e $BOLD"✍️ What is your WP backend URL? (defaults to http://localhost:8020): "$NOCOLOR)" WP_backend_url |
| 15 | + [ -z "$WP_backend_url" ] && echo -e $YELLOW"⚠️ Using default WP URL"$NOCOLOR && WP_backend_url="http://localhost:8020" |
| 16 | + check_url_input $WP_backend_url |
| 17 | + echo "NEXT_PUBLIC_WORDPRESS_SITE_URL=$WP_backend_url" > frontend/.env |
| 18 | + |
| 19 | + # get port from url and update it in docker-compose file |
| 20 | + WP_backend_url_port="$(echo $WP_backend_url | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')" |
| 21 | + |
| 22 | + # if no port in URL then use port 80 |
| 23 | + if [[ $WP_backend_url_port == "" ]] |
| 24 | + then |
| 25 | + sed -i -e "s/8020\:80/80:80/g" backend/docker-compose.yml |
| 26 | + else |
| 27 | + sed -i -e "s/8020\:80/$WP_backend_url_port\:80/g" backend/docker-compose.yml |
| 28 | + fi |
| 29 | + |
| 30 | + echo "" |
| 31 | + |
| 32 | + # user-input - NextJS Frontend URL |
| 33 | + read -p "$(echo -e $BOLD"✍️ What is your frontend next js URL? (defaults to http://localhost:3000): "$NOCOLOR)" NextJS_frontend_url |
| 34 | + [ -z $NextJS_frontend_url ] && echo -e $YELLOW"⚠️ Using default NextJS URL"$NOCOLOR && NextJS_frontend_url="http://localhost:3000" |
| 35 | + check_url_input $NextJS_frontend_url |
| 36 | + echo "NEXT_PUBLIC_NEXTJS_SITE_URL=$NextJS_frontend_url" >> frontend/.env |
| 37 | + |
| 38 | + echo "" |
| 39 | + |
| 40 | + # user-input - Disqus comment shortname |
| 41 | + read -p "$(echo -e $BOLD"✍️ What is your Disqus comments shortname? (leave blank if you are not using): "$NOCOLOR)" Disqus_comment_shortname |
| 42 | + ! [ -z "$Disqus_comment_shortname" ] && echo "NEXT_PUBLIC_DISQUS_SHORTNAME=$Disqus_comment_shortname" >> frontend/.env |
| 43 | + |
| 44 | + echo "" |
| 45 | + echo -e $GREEN$BOLD"✅ Successfully created frontend/.env file"$NOCOLOR |
| 46 | +} |
| 47 | + |
| 48 | +# Start docker containers |
| 49 | +function start_env_backend() { |
| 50 | + |
| 51 | + echo -e $BOLD"📦 Starting backend containers"$NOCOLOR |
| 52 | + docker-compose -f backend/docker-compose.yml up -d |
| 53 | + |
| 54 | + echo "" |
| 55 | +} |
| 56 | + |
| 57 | +# Start npm server |
| 58 | +function start_env_frontend() { |
| 59 | + |
| 60 | + echo -e $BOLD "💻 Starting frontend"$NOCOLOR |
| 61 | + npm i --prefix frontend/ |
| 62 | + npm run dev --prefix frontend/ |
| 63 | +} |
| 64 | + |
| 65 | +# Check URL |
| 66 | +function check_url_input() { |
| 67 | + regex='(http?)://[-A-Za-z0-9:0-9]' |
| 68 | + user_input=$1 |
| 69 | + |
| 70 | + if ! [[ $user_input =~ $regex ]] |
| 71 | + then |
| 72 | + echo -e $RED"🛑 URL is not valid."$NOCOLOR |
| 73 | + echo -e $RED"Example URL is: http://example.com:8000"$NOCOLOR |
| 74 | + exit |
| 75 | + fi |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +# Configure options |
| 80 | +function configure() { |
| 81 | + # Ask user for preffered WP environment |
| 82 | + read -p "$(echo -e $BOLD"Do you already have a WordPress setup that you want to continue with? [y/n]: "$NOCOLOR)" already_have_WP_environment |
| 83 | + |
| 84 | + case $already_have_WP_environment in |
| 85 | + [yY]* ) proceed_with_WP_environment;; |
| 86 | + [nN]* ) proceed_with_WP_environment;; |
| 87 | + * ) echo -e $RED"🛑 Please answer yes or no."$NOCOLOR && exit ;; |
| 88 | + esac |
| 89 | + if [[ "$already_have_WP_environment" == "y" ]] || [[ $already_have_WP_environment == "Y" ]] |
| 90 | + then |
| 91 | + start_env_frontend |
| 92 | + else |
| 93 | + start_env_backend && start_env_frontend |
| 94 | + fi |
| 95 | +} |
| 96 | + |
| 97 | +# Stops all the running docker containers and remove them |
| 98 | +function docker_container_stop() { |
| 99 | + |
| 100 | + echo -e $BOLD"📦 Stopping backend containers"$NOCOLOR |
| 101 | + docker-compose -f backend/docker-compose.yml down |
| 102 | +} |
| 103 | + |
| 104 | +# Help commands |
| 105 | +function command_help() { |
| 106 | + |
| 107 | + echo "Please pass an argument" |
| 108 | + echo "Arguments: [ configure | start-all | start-backend | start-frontend | stop ]" |
| 109 | + echo "" |
| 110 | + echo "configure Configures WP Url and .env file" |
| 111 | + echo "start-all Creates and starts docker environment for WP and runs Next JS server" |
| 112 | + echo "start-backend Creates and starts docker environment" |
| 113 | + echo "start-frontend Runs Next JS server" |
| 114 | + echo "stop Stops the WordPress docker containers" |
| 115 | +} |
| 116 | + |
| 117 | +# Main function. |
| 118 | +# Checks command line arguments. |
| 119 | +function main() { |
| 120 | + |
| 121 | + case $1 in |
| 122 | + "configure" ) configure ;; |
| 123 | + "start-backend" ) start_env_backend ;; |
| 124 | + "start-frontend" ) start_env_frontend ;; |
| 125 | + "start-all" ) start_env_backend && start_env_frontend ;; |
| 126 | + "stop" ) docker_container_stop ;; |
| 127 | + * ) command_help ;; |
| 128 | + esac |
| 129 | +} |
| 130 | + |
| 131 | +main $1 |
0 commit comments