1
1
#! /bin/bash
2
2
3
+ # Strict mode, exit on error, undefined variables, and pipe failures
4
+ set -euo pipefail
5
+
3
6
# Print some information about the environment to aid in case of troubleshooting
4
7
5
8
echo " node version:"
@@ -27,60 +30,63 @@ if (( CURRENT_NODE_VERSION < REQUIRED_NODE_VERSION )); then
27
30
fi
28
31
29
32
# Autodetect project directory relative to this script's path
30
- PROJECT_DIR=" $0 "
31
- while [ -h " $PROJECT_DIR " ]; do
32
- ls=$( ls -ld " $PROJECT_DIR " )
33
- link=$( expr " $ls " : ' .*-> \(.*\)$' )
34
- if expr " $link " : ' /.*' > /dev/null; then
35
- PROJECT_DIR=" $link "
36
- else
37
- PROJECT_DIR=" $( dirname " $PROJECT_DIR " ) /$link "
38
- fi
39
- done
40
- PROJECT_DIR=" $( dirname " $PROJECT_DIR " ) /.."
41
- PROJECT_DIR=" $( cd " $PROJECT_DIR " ; pwd) "
33
+ PROJECT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) /.." && pwd) "
42
34
43
- cd $PROJECT_DIR
35
+ cd " $PROJECT_DIR "
44
36
45
37
cp .env.example .env
46
38
47
39
pnpm install -r
48
40
49
41
pnpm build
50
42
43
+ # Create temp file and ensure cleanup
51
44
OUTFILE=" $( mktemp) "
52
- echo $OUTFILE
45
+ trap ' rm -f "$OUTFILE"' EXIT
46
+ echo " Using temporary output file: $OUTFILE "
47
+
48
+ # Add timeout configuration
49
+ TIMEOUT=30
50
+ INTERVAL=0.5
51
+ TIMER=0
52
+
53
53
(
54
- # Wait for the ready message
54
+ # Wait for the ready message with timeout
55
55
while true ; do
56
+ if [[ $TIMER -ge $TIMEOUT ]]; then
57
+ echo " Error: Timeout waiting for application to start after $TIMEOUT seconds"
58
+ kill $$
59
+ exit 1
60
+ fi
61
+
56
62
if grep -q " Chat started" " $OUTFILE " ; then
57
63
echo " exit" ; sleep 2
58
64
break
59
65
fi
60
- sleep 0.5
66
+
67
+ sleep $INTERVAL
68
+ TIMER=$( echo " $TIMER + $INTERVAL " | bc)
61
69
done
62
70
) | pnpm start --character=characters/trump.character.json > " $OUTFILE " &
63
71
64
72
# Wait for process to finish
65
73
wait $!
66
74
RESULT=$?
75
+
67
76
echo " ----- OUTPUT START -----"
68
77
cat " $OUTFILE "
69
78
echo " ----- OUTPUT END -----"
70
79
71
80
# Check the exit code of the last command
72
81
if [[ $RESULT -ne 0 ]]; then
73
- echo " Error: 'start' command exited with an error. "
82
+ echo " Error: 'start' command exited with an error (code: $RESULT ) "
74
83
exit 1
75
84
fi
76
85
77
- # Check if output.txt contains "Terminating and cleaning up resources..."
86
+ # Check if output contains expected termination message
78
87
if grep -q " Terminating and cleaning up resources..." " $OUTFILE " ; then
79
88
echo " Script completed successfully."
80
89
else
81
- echo " Error: The output does not contain the expected string ."
90
+ echo " Error: The output does not contain the expected termination message ."
82
91
exit 1
83
- fi
84
-
85
- # Clean up
86
- rm " $OUTFILE "
92
+ fi
0 commit comments