diff --git a/package.json b/package.json index 03661cb3..608253eb 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,14 @@ "typecheck": "tsc", "typecheck:watch": "tsc -w", "validate": "run-p \"test -- --run\" lint typecheck test:e2e:run", + "fly:status:prod": "npm run fly:status -- --env=production", + "fly:status:staging": "npm run fly:status -- --env=staging", "fly:status": "./scripts/fly.io/app-status.sh", + "fly:console:prisma:studio:prod": "npm run fly:console:prisma:studio -- --env=production", + "fly:console:prisma:studio:staging": "npm run fly:console:prisma:studio -- --env=staging", "fly:console:prisma:studio": "./scripts/fly.io/app-console-prisma-studio.sh", + "fly:console:prisma:studio:proxy:prod": "npm run fly:console:prisma:studio:proxy -- --env=production", + "fly:console:prisma:studio:proxy:staging": "npm run fly:console:prisma:studio:proxy -- --env=staging", "fly:console:prisma:studio:proxy": "./scripts/fly.io/app-console-prisma-studio-proxy.sh", "prepare": "husky install" }, diff --git a/scripts/fly.io/app-console-prisma-studio-proxy.sh b/scripts/fly.io/app-console-prisma-studio-proxy.sh index 1a1104f3..07507eb9 100755 --- a/scripts/fly.io/app-console-prisma-studio-proxy.sh +++ b/scripts/fly.io/app-console-prisma-studio-proxy.sh @@ -9,6 +9,50 @@ # run proxy to prisma studio on fly app to local port (separate terminal) # npm run fly:app:console:prisma:studio:proxy +# Check if the script is running in production environment +if [ "$NODE_ENV" = "production" ]; then + echo "This script should not run in production." + exit 1 +fi + +# Default environment +ENV="development" + +# Parse arguments +for ARG in "$@" +do + case $ARG in + --env=*) + ENV="${ARG#*=}" + shift + ;; + esac +done + +# Source environment variables source .env +# Check if the environment variable is set +if [ -z "$FLY_APP_NAME" ]; then + echo "Error: FLY_APP_NAME environment variable is not set." + exit 1 +fi + +# Modify the app name for staging environment +if [ "$ENV" = "staging" ]; then + FLY_APP_NAME="${FLY_APP_NAME}-staging" +elif [ "$ENV" != "production" ]; then + echo "Unknown environment: $ENV" + exit 1 +fi + +# Run the Fly.io proxy command fly proxy 5556:5555 --app $FLY_APP_NAME + +# FYI closing the proxy will display a warning: +# "Error: ssh shell: session forcibly closed; the remote process may still be running" +# don't worry :D +# https://community.fly.io/t/does-fly-know-when-my-app-decides-to-gracefully-shutdown/19132 +# the comments from this post seem to suggest the healthcheck fail will close the connection +# https://fly.io/docs/reference/configuration/#services-tcp_checks +# here is the doc to confirm :D \ No newline at end of file diff --git a/scripts/fly.io/app-console-prisma-studio.sh b/scripts/fly.io/app-console-prisma-studio.sh index 98e42409..776da176 100755 --- a/scripts/fly.io/app-console-prisma-studio.sh +++ b/scripts/fly.io/app-console-prisma-studio.sh @@ -9,6 +9,42 @@ # run proxy to prisma studio on fly app to local port (separate terminal) # npm run fly:app:console:prisma:studio:proxy +# Check if the script is running in production environment +if [ "$NODE_ENV" = "production" ]; then + echo "This script should not run in production." + exit 1 +fi + +# Default environment +ENV="development" + +# Parse arguments +for ARG in "$@" +do + case $ARG in + --env=*) + ENV="${ARG#*=}" + shift + ;; + esac +done + +# Source environment variables source .env +# Check if the environment variable is set +if [ -z "$FLY_APP_NAME" ]; then + echo "Error: FLY_APP_NAME environment variable is not set." + exit 1 +fi + +# Modify the app name for staging environment +if [ "$ENV" = "staging" ]; then + FLY_APP_NAME="${FLY_APP_NAME}-staging" +elif [ "$ENV" != "production" ]; then + echo "Unknown environment: $ENV" + exit 1 +fi + +# Run the Fly.io ssh console command fly ssh console -C "npm run prisma:studio" --app $FLY_APP_NAME diff --git a/scripts/fly.io/app-status.sh b/scripts/fly.io/app-status.sh index f9a2ba94..23790df2 100755 --- a/scripts/fly.io/app-status.sh +++ b/scripts/fly.io/app-status.sh @@ -4,6 +4,42 @@ # to give permission to execute the file locally run: # chmod +x scripts/fly.io/app-status.sh +# Check if the script is running in production environment +if [ "$NODE_ENV" = "production" ]; then + echo "This script should not run in production." + exit 1 +fi + +# Default environment +ENV="development" + +# Parse arguments +for ARG in "$@" +do + case $ARG in + --env=*) + ENV="${ARG#*=}" + shift + ;; + esac +done + +# Source environment variables source .env +# Check if the environment variable is set +if [ -z "$FLY_APP_NAME" ]; then + echo "Error: FLY_APP_NAME environment variable is not set." + exit 1 +fi + +# Modify the app name for staging environment +if [ "$ENV" = "staging" ]; then + FLY_APP_NAME="${FLY_APP_NAME}-staging" +elif [ "$ENV" != "production" ]; then + echo "Unknown environment: $ENV" + exit 1 +fi + +# Run the Fly.io status command fly status --app $FLY_APP_NAME \ No newline at end of file