Skip to content

Commit 9d06132

Browse files
committed
chore: avoid using trap not available on non-unix env
1 parent 933c496 commit 9d06132

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

packages/client/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"scripts": {
2828
"dev": "pkgroll --watch",
2929
"build": "pkgroll --minify",
30-
"test": "../internal/dev-infra/with-test-redis-and-db.sh vitest",
31-
"test:coverage": "../internal/dev-infra/with-test-redis-and-db.sh vitest --coverage",
30+
"test": "bash ../internal/dev-infra/with-test-redis-and-db.sh vitest",
31+
"test:coverage": "bash ../internal/dev-infra/with-test-redis-and-db.sh vitest --coverage",
3232
"test:typescript": "tsc --noEmit"
3333
},
3434
"dependencies": {

packages/internal/dev-infra/_common.sh

+25-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env sh
1+
#!/usr/bin/env bash
22

33
# Exit if any command fails
44
set -e
@@ -59,6 +59,16 @@ redis_clear() {
5959
}
6060

6161
main_native() {
62+
# check if Ctrl+C is pressed
63+
(
64+
while true; do
65+
sleep 1
66+
kill -0 $$ || break
67+
done
68+
cleanup ${services}
69+
)&
70+
pid=$!
71+
6272
local services=${SERVICES}
6373
local postgres_url_env_var=`[[ $services == *"db_test"* ]] && echo "DB_TEST_POSTGRES_URL" || echo "DB_POSTGRES_URL"`
6474
local redis_host_env_var=`[[ $services == *"redis_test"* ]] && echo "REDIS_TEST_HOST" || echo "REDIS_HOST"`
@@ -82,6 +92,7 @@ main_native() {
8292
fi
8393

8494
cleanup() {
95+
kill ${pid} &>/dev/null
8596
local services=$@
8697

8798
if [ -n "${redis_host}" ] && [[ $services == *"redis_test"* ]]; then
@@ -93,25 +104,28 @@ main_native() {
93104
fi
94105
}
95106

96-
# trap SIGINT and performs cleanup
97-
trap "on_sigint ${services}" INT
98-
on_sigint() {
99-
cleanup $@
100-
exit $?
101-
}
102-
103107
# Run the arguments as a command
104108
DB_POSTGRES_URL="${postgres_url}" \
105109
REDIS_HOST="${redis_host}" \
106110
"$@"
107111
code=$?
108112

109113
cleanup ${services}
110-
114+
sleep 1
111115
exit ${code}
112116
}
113117

114118
main_docker() {
119+
# check if Ctrl+C is pressed
120+
(
121+
while true; do
122+
sleep 1
123+
kill -0 $$ || break
124+
done
125+
echo "Ctrl+C detected"
126+
cleanup ${services}
127+
)&
128+
pid=$!
115129
# Expect a SERVICES env var to be set with the docker service names
116130
local services=${SERVICES}
117131

@@ -124,20 +138,14 @@ main_docker() {
124138
# performs cleanup as necessary, i.e. taking down containers
125139
# if this script started them
126140
cleanup() {
141+
kill ${pid} &>/dev/null
127142
local services=$@
128143
echo # newline
129144
if $started_container; then
130145
docker compose --file $compose_file rm --force --stop --volumes ${services}
131146
fi
132147
}
133148

134-
# trap SIGINT and performs cleanup
135-
trap "on_sigint ${services}" INT
136-
on_sigint() {
137-
cleanup $@
138-
exit $?
139-
}
140-
141149
# check if all services are running already
142150
not_running=false
143151
for service in $services; do
@@ -167,6 +175,7 @@ main_docker() {
167175

168176
# performs cleanup as necessary
169177
cleanup ${services}
178+
sleep 1
170179
exit ${code}
171180
}
172181

0 commit comments

Comments
 (0)