-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdx
More file actions
317 lines (281 loc) · 14.4 KB
/
Copy pathdx
File metadata and controls
317 lines (281 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env bash
# ================================================================
# dx — Docker eXecutioner CLI
# Works with any Docker Compose project.
#
# First time:
# bash dx setup # copy .env.example files to .env
#
# Daily dev:
# bash dx dev # start all containers
# bash dx devb # start all dev containers with rebuild
# bash dx logs api # watch API logs
# bash dx logs web # watch Next.js logs
# bash dx shell api # shell into API container
# bash dx shell postgres psql -U user # open psql session
# bash dx run db:api:reset:dx # reset & seed database (Docker)
# bash dx run prisma:api:migrate:dev:dx # create new migration (Docker)
# bash dx run prisma:api:generate:dx # regenerate Prisma client (Docker)
# bash dx run prisma:api:studio:dx # open Prisma Studio (Docker)
# bash dx run test:dx # run all services tests (Docker) shell
# bash dx run test:api:dx # run API service tests (Docker) shell
# bash dx run check-types # run TS type checks
# bash dx run lint # lint the whole monorepo (proxied to runner)
# bash dx restart api # restart API after .env change
# bash dx down # stop and remove containers
#
# Deploy (prod):
# bash dx pbuild # build all images
# bash dx prod # start prod stack
# bash dx prodb # start prod stack with rebuild
# bash dx plogs api # tail prod API logs
# bash dx pshell api # shell into prod API
# bash dx pshell postgres psql -U user # psql into prod database
# bash dx prestart api # restart prod API after deploy
# bash dx ps # check container health
#
# Smart Proxy:
# Commands are intelligently routed between Host and Containers.
# Scripts in package.json containing 'dx' run on Host to manage Docker.
# Other scripts (lint, test, etc.) are forwarded to the RUNNER container.
#
# Run bash dx help for the full command reference.
# ================================================================
set -euo pipefail
# ── Environment Detection ────────────────────────────────────────
IS_CONTAINER=false
[[ -f "/.dockerenv" ]] && IS_CONTAINER=true
# ── Configuration (override via env vars) ────────────────────────
DEV_COMPOSE="${DX_DEV_COMPOSE:-compose.dev.yaml}"
PROD_COMPOSE="${DX_PROD_COMPOSE:-compose.yaml}"
RUNNER_SERVICE="${DX_RUNNER:-runner}"
RUNNER_PROFILE="${DX_RUNNER_PROFILE:-tools}"
# ── Task Runner Detection ────────────────────────────────────────
detect_task_runner() {
if [[ -f "package.json" ]]; then
if [[ -f "pnpm-lock.yaml" ]]; then echo "pnpm run"
elif [[ -f "yarn.lock" ]]; then echo "yarn run"
elif [[ -f "bun.lockb" || -f "bun.lock" ]]; then echo "bun run"
else echo "npm run"; fi
elif [[ -f "Makefile" ]]; then echo "make"
else echo ""; fi
}
# ── Helpers ───────────────────────────────────────────────────────
log() { printf '\033[0;36m▸\033[0m %s\n' "$*"; }
warn() { printf '\033[0;33m⚠\033[0m %s\n' "$*" >&2; }
die() { printf '\033[0;31m✖\033[0m %s\n' "$*" >&2; exit 1; }
confirm() {
local msg="${1:-Are you sure?}"
printf "\033[0;33m⚠\033[0m %s [y/N] " "$msg"
read -r response
case "$response" in
[yY][eE][sS]|[yY]) return 0 ;;
*) log "Action cancelled."; exit 0 ;;
esac
}
require_cmd() {
command -v "$1" &>/dev/null || die "Command '$1' is required but not installed. Please install it to continue."
}
require_file() {
[[ -f "$1" ]] || die "Configuration file '$1' not found. Is the project initialized? Run 'bash dx setup' if needed."
}
dev_compose() { require_file "$DEV_COMPOSE"; docker compose -f "$DEV_COMPOSE" "$@"; }
prod_compose() { require_file "$PROD_COMPOSE"; docker compose -f "$PROD_COMPOSE" "$@"; }
runner() {
if [[ "$IS_CONTAINER" == "true" ]]; then
eval "$@"
else
require_file "$DEV_COMPOSE"
docker compose \
-f "$DEV_COMPOSE" \
--profile "$RUNNER_PROFILE" \
run --rm \
-e NPM_CONFIG_UPDATE_NOTIFIER=false \
"$RUNNER_SERVICE" "$@"
fi
}
# shell_into <compose_fn> <prod?> <svc> [cmd...]
shell_into() {
local compose_fn="$1"
local is_prod="$2"
local svc="$3"
shift 3
# If we are already inside the target service, run locally
if [[ "${DX_SERVICE_NAME:-}" == "$svc" ]]; then
if [[ $# -ge 1 ]]; then
eval "$@"
else
sh
fi
return
fi
if [[ $# -ge 1 ]]; then
"$compose_fn" exec "$svc" "$@"
elif "$compose_fn" exec "$svc" sh -c 'exit 0' 2>/dev/null; then
"$compose_fn" exec "$svc" sh
elif [[ "$is_prod" == "true" ]]; then
die "Container '$svc' is not running. Start it first with: bash dx prod"
else
warn "Container '$svc' is not running — starting a one-off instance instead."
"$compose_fn" run --rm "$svc" sh
fi
}
# Run npm script (auto-detects environment)
run_script() {
local script_name=$1
shift
local task_runner
task_runner=$(detect_task_runner)
[[ -z "$task_runner" ]] && die "No supported task manager (npm/yarn/pnpm/bun/make) detected in this directory."
if [ "$IS_CONTAINER" = true ]; then
log "Running '$script_name' locally in container via $task_runner..."
$task_runner "$script_name" -- "$@"
else
# Host: check if script uses 'dx' to avoid unnecessary runner nesting
if [[ -f "package.json" ]] && grep "\"$script_name\"" package.json | grep -q "dx "; then
log "Detected host-native dx script. Running via $task_runner..."
$task_runner "$script_name" -- "$@"
else
log "Forwarding '$script_name' to runner via $task_runner..."
runner $task_runner "$script_name" -- "$@"
fi
fi
}
CMD="${1:-help}"
shift || true
# ── Commands ─────────────────────────────────────────────────────
case "$CMD" in
# ── Help ────────────────────────────────────────────────────────
help|-h|--help)
cat <<'EOF'
dx — Docker eXecutioner CLI
Commands target dev by default. Prefix any lifecycle or shell
command with [p] to run it against the production stack instead.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ENVIRONMENT OVERRIDES
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
DX_DEV_COMPOSE Dev compose file (default: compose.dev.yaml)
DX_PROD_COMPOSE Prod compose file (default: compose.yaml)
DX_RUNNER Runner service name (default: runner)
DX_RUNNER_PROFILE Runner profile name (default: tools)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
SETUP
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
setup
Copy all *.example env files to their real counterparts.
Run once after cloning, then edit the generated files.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
LIFECYCLE
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
dev [args] Start all containers in detached mode.
devb [args] Start dev containers in detached mode with --build.
prod [args] Start the production stack in detached mode.
prodb [args] Start the production stack in detached mode with --build.
[p]stop [svc] Stop running containers (keeps volumes).
[p]down [args] Stop and remove containers.
[p]downv [args] Stop and remove containers + volumes.
[p]restart [svc] Restart one or all services.
[p]build [args] Build images.
[p]rebuild [args] Build images without cache.
[p]logs [svc] Follow log output (Ctrl-C to stop).
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
PROCESS STATUS
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ps List containers with status and health.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
SHELL & EXEC
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[p]shell <svc> [cmd...] Open an interactive sh or run a command.
Falls back to run --rm if not running (dev only).
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
RUNNER (proxied through the runner container)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
run <script> [args] npm run <script> at monorepo root.
exec <cmd> [args] Run any arbitrary command in the runner.
sh Open an interactive shell in the runner.
EOF
;;
# ── First-time setup ────────────────────────────────────────────
setup)
log "Copying .env example files..."
count=0
while IFS= read -r -d '' example; do
target="${example%.example}"
if [[ "$target" == "$example" ]]; then continue; fi
if [[ -f "$target" ]]; then
warn "Skipping (already exists): $target"
else
cp "$example" "$target"
log "Created: $target"
count=$((count + 1))
fi
done < <(find . \
\( -path "*/node_modules" -o -path "*/.git" -o -path "*/.next" \) \
-prune -o \
-name "*.example" -print0)
log "Done. Created $count file(s). Review your .env files before starting."
;;
# ── Dev lifecycle ───────────────────────────────────────────────
dev) require_cmd docker; dev_compose up -d "$@" ;;
devb) require_cmd docker; dev_compose up -d --build "$@" ;;
stop) require_cmd docker; dev_compose stop "$@" ;;
down) require_cmd docker; dev_compose down "$@" ;;
downv)
require_cmd docker
confirm "This will REMOVE containers and VOLUMES (data loss). Continue?"
dev_compose down -v "$@"
;;
restart) require_cmd docker; dev_compose restart "$@" ;;
build) require_cmd docker; dev_compose build "$@" ;;
rebuild) require_cmd docker; dev_compose build --no-cache "$@" ;;
logs) require_cmd docker; dev_compose logs -f "$@" ;;
shell)
require_cmd docker
[[ $# -ge 1 ]] || die "Usage: bash dx shell <service> [command...]"
svc="$1"; shift
shell_into dev_compose false "$svc" "$@"
;;
# ── Prod lifecycle ──────────────────────────────────────────────
prod) require_cmd docker; prod_compose up -d "$@" ;;
prodb) require_cmd docker; prod_compose up -d --build "$@" ;;
pstop) require_cmd docker; prod_compose stop "$@" ;;
pdown)
require_cmd docker
confirm "Stop production service and remove containers? (Downtime)"
prod_compose down "$@"
;;
pdownv)
require_cmd docker
confirm "EXTREME DANGER: Remove production containers AND VOLUMES? Continue?"
prod_compose down -v "$@"
;;
prestart) require_cmd docker; prod_compose restart "$@" ;;
pbuild) require_cmd docker; prod_compose build "$@" ;;
prebuild) require_cmd docker; prod_compose build --no-cache "$@" ;;
plogs) require_cmd docker; prod_compose logs -f "$@" ;;
pshell)
require_cmd docker
[[ $# -ge 1 ]] || die "Usage: bash dx pshell <service> [command...]"
svc="$1"; shift
shell_into prod_compose true "$svc" "$@"
;;
# ── Process status ──────────────────────────────────────────────────────
ps) require_cmd docker; docker compose ps -a "$@" ;;
# ── Runner ──────────────────────────────────────────────────────
run)
[[ $# -ge 1 ]] || die "Usage: bash dx run <npm-script> [args...]"
run_script "$@"
;;
exec)
[[ $# -ge 1 ]] || die "Usage: bash dx exec <cmd> [args...]"
runner "$@"
;;
sh)
runner sh
;;
# ── Service shortcuts (api, web, etc.) ──────────────────────────
*)
svc="$CMD"
shell_into dev_compose false "$svc" "$@"
;;
esac