lighthouse.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/bin/bash
  2. # Change directory to the first argument passed to the script
  3. project_dir=$1
  4. shift
  5. pushd "$project_dir" || exit 1
  6. echo "Changed directory to $project_dir"
  7. # So we get stdout / stderr from Python ASAP. Without this, delays can be very long (e.g. on Windows, Github Actions)
  8. export PYTHONUNBUFFERED=1
  9. env_mode=$1
  10. shift
  11. check_ports=${1:-3000 8000}
  12. shift
  13. # Start the server in the background
  14. export TELEMETRY_ENABLED=false
  15. reflex run --loglevel debug --env "$env_mode" "$@" & pid=$!
  16. # Within the context of this bash, $pid_in_bash is what we need to pass to "kill" on exit
  17. # This is true on all platforms.
  18. pid_in_bash=$pid
  19. trap "kill -INT $pid_in_bash ||:" EXIT
  20. echo "Started server with PID $pid"
  21. # Assume we run from the root of the repo
  22. popd
  23. # In Windows, our Python script below needs to work with the WINPID
  24. if [ -f /proc/$pid/winpid ]; then
  25. pid=$(cat /proc/$pid/winpid)
  26. echo "Windows detected, passing winpid $pid to port waiter"
  27. fi
  28. python scripts/wait_for_listening_port.py $check_ports --timeout=600 --server-pid "$pid"
  29. # Check if something is running on port 3000
  30. if curl --output /dev/null --silent --head --fail "http://localhost:3000"; then
  31. echo "URL exists: http://localhost:3000"
  32. else
  33. echo "URL does not exist: https://localhost:3000"
  34. fi
  35. # Change to .web directory
  36. project_dir=$1
  37. shift
  38. pushd "$project_dir" || exit 1
  39. echo "Changed directory to $project_dir"
  40. cd .web
  41. # Create a lighthouserc.js file
  42. cat << EOF > lighthouserc.js
  43. module.exports = {
  44. ci: {
  45. collect: {
  46. isSinglePageApplication: true,
  47. numberOfRuns: 1,
  48. url: ['http://localhost:3000', "http://localhost:3000/docs/getting-started/introduction/", "http://localhost:3000/blog/2023-08-02-seed-annoucement/"]
  49. },
  50. upload: {
  51. target: 'temporary-public-storage',
  52. },
  53. },
  54. };
  55. EOF
  56. # Install and Run LHCI
  57. npm install -g @lhci/cli
  58. lhci autorun || echo "LHCI failed!"
  59. #!/bin/bash
  60. # Define the base URL where you want to send the POST requests
  61. base_url="https://app.posthog.com/capture/"
  62. # Directory containing JSON files
  63. json_dir=".lighthouseci"
  64. # API Key
  65. api_key="$POSTHOG"
  66. # Get the current timestamp
  67. timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
  68. # Loop through each JSON file in the directory
  69. for json_file in "$json_dir"/*.json; do
  70. if [ -f "$json_file" ]; then
  71. # Extract the file name without the extension
  72. file_name=$(basename "$json_file" .json)
  73. # Generate a random distinct_id (a random number)
  74. distinct_id=$((RANDOM))
  75. # Read the contents of the JSON file
  76. json_data=$(cat "$json_file")
  77. # Construct the event name with the JSON file name
  78. event="Lighthouse CI - $file_name"
  79. # Construct the JSON payload with the random distinct_id
  80. payload="{\"api_key\": \"$api_key\", \"event\": \"$event\", \"timestamp\": \"$timestamp\", \"distinct_id\": $distinct_id, \"properties\": $json_data}"
  81. # Create a temporary file for the payload
  82. tmpfile=$(mktemp)
  83. # Write the payload to the temporary file
  84. echo "$payload" > "$tmpfile"
  85. # Send the POST request with the constructed payload using curl
  86. response=$(curl -X POST -H "Content-Type: application/json" --data @"$tmpfile" "$base_url")
  87. # Clean up the temporary file
  88. rm "$tmpfile"
  89. # Print the response for each file
  90. echo "Response for $json_file:"
  91. echo "$response"
  92. fi
  93. done