22
33NPM_INSTALL_OUTPUT_FILTER=" up to date in|added [0-9]* packages, removed [0-9]* packages, and changed [0-9]* packages in|removed [0-9]* packages, and changed [0-9]* packages in|added [0-9]* packages in|removed [0-9]* packages in"
44
5+ # Function to check and kill any Node process running on port 3000 (React development server)
6+ check_and_kill_node_server () {
7+ # Find process listening on port 3000
8+ local pid=$( lsof -i :3000 -t 2> /dev/null)
9+ if [ ! -z " $pid " ]; then
10+ if ps -p $pid -o comm= | grep -q " node" ; then
11+ printf " Found Node server running on port 3000. Killing it...\n"
12+ kill $pid 2> /dev/null
13+ sleep 1 # Give the process time to terminate
14+ if [ " ${VERBOSE:- } " -eq 1 ] 2> /dev/null; then
15+ printf " Node server terminated.\n"
16+ fi
17+ fi
18+ fi
19+ }
20+
521# Function to get all child processes of a given PID and store them in a list
622get_children () {
723 local parent_pid=$1
@@ -16,6 +32,43 @@ get_children() {
1632 done
1733}
1834
35+ # Cleanup function to ensure all processes are terminated
36+ cleanup () {
37+ # Kill any running npm processes started by this script
38+ if [ ! -z " ${NPM_PID+x} " ]; then
39+ pkill -P $NPM_PID 2> /dev/null
40+ kill $NPM_PID 2> /dev/null
41+ fi
42+
43+ # Kill React app and its children if they exist
44+ if [ ! -z " ${REACT_APP_PID+x} " ]; then
45+ local processes_to_kill=()
46+ get_children $REACT_APP_PID
47+
48+ # Kill the main process
49+ kill $REACT_APP_PID 2> /dev/null
50+
51+ # Kill all the subprocesses
52+ for pid in " ${processes_to_kill[@]} "
53+ do
54+ kill $pid 2> /dev/null
55+ done
56+
57+ if [ " ${VERBOSE:- } " -eq 1 ] 2> /dev/null; then
58+ printf " React app is terminated!\n"
59+ fi
60+ fi
61+
62+ # Remove temporary files if they exist
63+ [ -f " $build_output " ] && rm " $build_output " 2> /dev/null
64+ }
65+
66+ # Set up trap to call cleanup function on script exit, interrupt, or termination
67+ trap cleanup EXIT SIGINT SIGTERM
68+
69+ # Check for and kill any existing Node server from previous runs
70+ check_and_kill_node_server
71+
1972# Check if build folder name is provided
2073if [ -z " $1 " ]; then
2174 printf " Error: No build folder name provided.\n"
@@ -109,6 +162,7 @@ BROWSER=none npm start -- --no-open > app.log 2>&1 &
109162
110163# Capture the process ID of the npm start command
111164REACT_APP_PID=$!
165+ NPM_PID=$( pgrep -P $REACT_APP_PID npm)
112166
113167# Wait for the "compiled successfully!" message in the log file
114168while true ; do
@@ -168,28 +222,9 @@ if [ "${VERBOSE:-}" -eq 1 ] 2>/dev/null; then
168222 printf " Running Cypress conformance tests...\n"
169223fi
170224
171- BROWSERSLIST_IGNORE_OLD_DATA=1 npx cypress run --config video=false 2> /dev/null
225+ BROWSERSLIST_IGNORE_OLD_DATA=1 npx cypress run --browser=chrome -- config video=false 2> /dev/null
172226cypress_run_result=$?
173227
174- # Initialize an empty array to hold the PIDs
175- processes_to_kill=()
176-
177- # Start the recursive search from the given parent PID
178- get_children $REACT_APP_PID
179-
180- # Kill the main process
181- kill $REACT_APP_PID
182-
183- # Kill all the subprocesses
184- for pid in " ${processes_to_kill[@]} "
185- do
186- kill $pid
187- done
188-
189- if [ " ${VERBOSE:- } " -eq 1 ] 2> /dev/null; then
190- printf " React app is terminated!\n"
191- fi
192-
193228if [ $cypress_run_result -ne 0 ]; then
194229 if [ " ${VERBOSE:- } " -eq 1 ] 2> /dev/null; then
195230 printf " Error: Cypress conformance tests have failed.\n"
0 commit comments