-
Notifications
You must be signed in to change notification settings - Fork 2
/
start-firebase-emulator.sh
82 lines (72 loc) · 2.25 KB
/
start-firebase-emulator.sh
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
#!/bin/bash
EMULATORS=$1
PROJECT_ID=$2
MAX_RETRIES=$3
MAX_CHECKATTEMPTS=$4
CHECKATTEMPTS_WAIT=$5
CHECK_PORT=$6
if ! [ -x "$(command -v firebase)" ]; then
echo "❌ Firebase tools CLI is missing."
exit 1
fi
if ! [ -x "$(command -v node)" ]; then
echo "❌ Node.js is missing."
exit 1
fi
if ! [ -x "$(command -v npm)" ]; then
echo "❌ NPM is missing."
exit 1
fi
# Run NPM install if node modules does not exist.
if [[ -d "functions" && ! -d "functions/node_modules" ]]; then
cd functions
if npm i; then
echo "✅ NPM install successful."
else
if [[ -z "${CI}" ]]; then
echo "❌ NPM install failed."
exit 1
else
# TODO temporary workaround for GitHub Actions CI issue:
# npm ERR! Your cache folder contains root-owned files, due to a bug in
# npm ERR! previous versions of npm which has since been addressed.
sudo chown -R 501:20 "/Users/runner/.npm" || exit 1
npm i || exit 1
fi
fi
cd ..
fi
export STORAGE_EMULATOR_DEBUG=true
EMU_START_COMMAND="firebase emulators:start --only $EMULATORS --project $PROJECT_ID"
RETRIES=1
while [ $RETRIES -le $MAX_RETRIES ]; do
if [[ -z "${CI}" ]]; then
echo "Starting Firebase Emulator Suite in foreground."
$EMU_START_COMMAND
exit 0
else
echo "Starting Firebase Emulator Suite in background."
$EMU_START_COMMAND &
CHECKATTEMPTS=1
while [ $CHECKATTEMPTS -le $MAX_CHECKATTEMPTS ]; do
sleep $CHECKATTEMPTS_WAIT
if curl --output /dev/null --silent --fail http://localhost:$CHECK_PORT; then
# Check again since it can exit before the emulator is ready.
sleep 15
if curl --output /dev/null --silent --fail http://localhost:$CHECK_PORT; then
echo "Firebase Emulator Suite is online!"
exit 0
else
echo "❌ Firebase Emulator exited after startup."
exit 1
fi
fi
echo "Waiting for Firebase Emulator Suite to come online, check $CHECKATTEMPTS of $MAX_CHECKATTEMPTS..."
((CHECKATTEMPTS = CHECKATTEMPTS + 1))
done
fi
echo "Firebase Emulator Suite did not come online in $MAX_CHECKATTEMPTS checks. Try $RETRIES of $MAX_RETRIES."
((RETRIES = RETRIES + 1))
done
echo "Firebase Emulator Suite did not come online after $MAX_RETRIES attempts."
exit 1