#!/bin/sh script_location=$(dirname -- "$(readlink "$0")") script_name="ghost-dev" tmp_folder_location="/tmp/ghostfolio-dev" usage() { cat < SUBCOMMANDS: init Installs project dependencies, builds from source starts redis and psql and sets up the database. migrate Syncs the database schema. start Starts the server and client in the background. stop Stops all ghostfolio services with a SIGTERM. follow Follows the log files generated by the client and server. status Checks if the client and server are active and since when. storybook Runs the storybook server in the current session. debug Runs the Visual Studio server debug in the current session. help Print this help text. EOF exit } ghostfolio_dev_init () { cd "$script_location" || { echo "Could not cd into $script_location. Aborting."; exit; } echo "(1/4) Executing: yarn install..." yarn install echo "(2/4) Executing: yarn build:dev..." yarn build:dev echo "(3/4) Executing: docker-compose --env-file $script_location/.env -f $script_location/docker/docker-compose.dev.yml up -d..." docker-compose --env-file ./.env -f docker/docker-compose.dev.yml up -d echo "(4/4) Executing: yarn database:setup..." yarn database:setup echo "Finished initiallizing the ghostfolio development environment!" } migrate_dev_database () { cd "$script_location" || { echo "Could not cd into $script_location. Aborting."; exit; } echo "Executing: yarn databse:push..." yarn database:push echo "The Ghostfolio database is now in sync!" } create_tmp_folder () { mkdir -p "$tmp_folder_location" touch "$tmp_folder_location/server.log" touch "$tmp_folder_location/client.log" } start_ghostfolio_dev_server () { create_tmp_folder cd "$script_location" || { echo "Could not cd into $script_location. Aborting."; exit; } echo "Executing: yarn start:server" nohup yarn start:server > /tmp/ghostfolio-dev/server.log 2>&1 & echo "Executing: yarn start:client" nohup yarn start:client > /tmp/ghostfolio-dev/client.log 2>&1 & echo "Follow the logs by running the script with '$script_name follow'". } start_storybook () { cd "$script_location" || { echo "Could not cd into $script_location. Aborting."; exit; } echo "Executing: yarn start:storybook" yarn start:storybook } get_process_pid () { search_term="$1" ps -e -o %p, -o %a | grep "$search_term" | grep -v "grep" | head -n 1 | cut -d',' -f1 | sed --posix "s/ //g" } terminate_process () { search_term_pid=$(get_process_pid "$1") if [ -z "$search_term_pid" ]; then echo "Process '$1' is already dead, skipping..." else echo "Sending SIGTERM to: $1 (PID: $search_term_pid)" kill -15 "$search_term_pid" fi } get_process_status () { search_term_pid=$(get_process_pid "$1") if [ -z "$search_term_pid" ]; then echo "Process '$1' is not active." else pid_start_time=$(ps -p "$search_term_pid" -o lstart=) echo "Process '$1' (PID: $search_term_pid) has been active since $pid_start_time." fi } finish_ghostfolio_dev_server () { echo "Terminating Server:" terminate_process "yarn start:server" terminate_process "nx run api:serve" echo "Terminating Client:" terminate_process "yarn start:client" terminate_process "nx run client:serve" echo "Terminating Server Debug:" terminate_process "yarn watch:server" terminate_process "nx run api:build" echo "Terminating Storybook:" terminate_process "yarn start:storybook" terminate_process "nx run ui:storybook" } open_dev_loggers () { create_tmp_folder tail -f "$tmp_folder_location/server.log" "$tmp_folder_location/client.log" } dev_server_status_overview () { echo "Server Status:" get_process_status "yarn start:server" get_process_status "nx run api:serve" echo "\nClient Status:" get_process_status "yarn start:client" get_process_status "nx run client:serve" echo "\nServer Debug Status:" get_process_status "yarn watch:server" get_process_status "nx run api:build" echo "\nTerminating Storybook:" get_process_status "yarn start:storybook" get_process_status "nx run ui:storybook" } start_vs_code_debug () { cd "$script_location" || { echo "Could not cd into $script_location. Aborting."; exit; } echo "Executing: yarn watch:server" yarn watch:server } case "$1" in init) ghostfolio_dev_init ;; migrate) migrate_dev_database ;; start) start_ghostfolio_dev_server ;; stop) finish_ghostfolio_dev_server ;; status) dev_server_status_overview ;; follow) open_dev_loggers ;; storybook) start_storybook ;; debug) start_vs_code_debug ;; help) usage ;; *) echo "Invalid command. Execute '$script_name help' for all possibilities." esac