From 893f2deab6d3af6a4f8d79661299785fb9636a79 Mon Sep 17 00:00:00 2001 From: Kael Shipman Date: Wed, 10 Jan 2024 17:08:01 -0700 Subject: [PATCH] docs and more --- .dockerignore | 3 ++- .gitignore | 3 ++- README.md | 4 ++++ scripts/docker-compose.sh | 13 ++++++++++++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.dockerignore b/.dockerignore index 8e206db..64ee24e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,4 @@ .git* node_modules -dist \ No newline at end of file +dist +.docker-compose-env \ No newline at end of file diff --git a/.gitignore b/.gitignore index f53bcbd..6edac66 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ **/*.local.yml **/.env/local node_modules -dist \ No newline at end of file +dist +.docker-compose-env \ No newline at end of file diff --git a/README.md b/README.md index 2628997..640155d 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,10 @@ Some key points: local monorepo linked in. If you want to run the actual built containers statically, try `pnpm docker:compose prod up -d`. You can bring the system back down by running `pnpm docker:compose [ENV] down`, and you can additionally pass any arguments you'd like to docker compose, e.g., `pnpm docker:compose [ENV] logs -f`. +* Note that I've implemented a small environment caching mechanism so you don't always have to pass the environment + along with `pnpm docker:compose`. This is just to avoid situations where you brought up the prod stack and then + accidentally ran a command against the (non-existent) dev stack or something because you forgot to include `prod` + again. ### ESLint diff --git a/scripts/docker-compose.sh b/scripts/docker-compose.sh index a48cbdb..a0bf14d 100755 --- a/scripts/docker-compose.sh +++ b/scripts/docker-compose.sh @@ -22,6 +22,7 @@ function echo_help() { } ENV= +SAVED_ENV="$([ -f "$ROOT/.docker-compose-env" ] && cat "$ROOT/.docker-compose-env" || true)" while [ $# -gt 0 ]; do case "$1" in @@ -29,6 +30,10 @@ while [ $# -gt 0 ]; do echo_help exit 0 ;; + -f|--force) + FORCE=1 + shift + ;; -*|build|config|cp|create|down|events|exec|images|kill|logs|ls|pause|port|ps|pull|push|restart|rm|run|start|stop|top|unpause|up|version|wait) break ;; @@ -46,7 +51,7 @@ while [ $# -gt 0 ]; do done if [ -z "$ENV" ]; then - ENV="dev" + ENV="$([ -n "$SAVED_ENV" ] && echo "$SAVED_ENV" || echo dev)" fi if ! [ -f "$ROOT/deploy/compose.${ENV}.yml" ]; then @@ -54,6 +59,12 @@ if ! [ -f "$ROOT/deploy/compose.${ENV}.yml" ]; then exit 1 fi +if [ -n "$SAVED_ENV" ] && [ "$ENV" != "$SAVED_ENV" ] && [ -z "$FORCE" ]; then + >&2 echo "E: Environment changed from '$SAVED_ENV' to '$ENV'. Please use the `-f\|--force` argument to confirm." + exit 1 +fi +echo -n "$ENV" > "$ROOT/.docker-compose-env" + ARGS=(-f "$ROOT/deploy/compose.base.yml" -f "$ROOT/deploy/compose.${ENV}.yml") if [ -f "$ROOT/deploy/compose.local.yml" ] && [ "$ENV" != 'local' ]; then ARGS+=(-f "$ROOT/deploy/compose.local.yml")