Skip to content

Commit

Permalink
Clean up entrypoint (#whatwouldshellcheckdo)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakejarvis committed Sep 17, 2019
1 parent 94c3d91 commit a9175de
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,48 @@

set -e

mkdir report
# Check if we're being triggered by a pull request
PULL_REQUEST_NUMBER=$(jq .number "$GITHUB_EVENT_PATH")

# In case of push event this might be empty
PULL_REQUEST_NUMBER=$(jq .number ${GITHUB_EVENT_PATH})

if [ -n "${INPUT_NETLIFY_SITE}" ] && [ -n "${PULL_REQUEST_NUMBER}" ] && [ "${PULL_REQUEST_NUMBER}" != "null" ]
then
# PR + Netlify enabled: generate Netlify deploy preview URL
# If this is a PR and Netlify is configured, plan to check the deploy preview and generate its unique URL.
# Otherwise, simply check the plain URL.
if [ -n "$INPUT_NETLIFY_SITE" ] && [ -n "$PULL_REQUEST_NUMBER" ] && [ "$PULL_REQUEST_NUMBER" != "null" ]; then
REPORT_URL="https://deploy-preview-${PULL_REQUEST_NUMBER}--${INPUT_NETLIFY_SITE}"
echo "Running against Netlify Deploy Preview ${REPORT_URL}"
else
# Fallback to default UNPUT_URL
REPORT_URL=${INPUT_URL}
fi

lighthouse --port=9222 --chrome-flags="--headless --disable-gpu --no-sandbox --no-zygote" --output "html" --output "json" --output-path "report/lighthouse" ${REPORT_URL}
# Prepare directory for audit results.
# TODO: Name files after the tested URL to allow multiple tests.
# Right now, a second test would overwrite the first results.
OUTPUT_FOLDER="report"
OUTPUT_PATH="$OUTPUT_FOLDER/lighthouse"
mkdir $OUTPUT_FOLDER

# Clarify in logs which URL we're auditing.
printf "* Beginning audit of %s ...\n\n" "$REPORT_URL"

# Run Lighthouse!
lighthouse --port=9222 --chrome-flags="--headless --disable-gpu --no-sandbox --no-zygote" --output "html" --output "json" --output-path "${OUTPUT_PATH}" "${REPORT_URL}"

# Parse individual scores from JSON output
# Parse individual scores from JSON output.
# Unorthodox jq syntax because of dashes -- https://github.com/stedolan/jq/issues/38
SCORE_PERFORMANCE=$(jq '.categories["performance"].score' ./report/lighthouse.report.json)
SCORE_ACCESSIBILITY=$(jq '.categories["accessibility"].score' ./report/lighthouse.report.json)
SCORE_PRACTICES=$(jq '.categories["best-practices"].score' ./report/lighthouse.report.json)
SCORE_SEO=$(jq '.categories["seo"].score' ./report/lighthouse.report.json)
SCORE_PWA=$(jq '.categories["pwa"].score' ./report/lighthouse.report.json)

# Print scores to standard output (out of 100 instead of 0 to 1)
# TO DO: Don't use hacky bc b/c bash hates floating point arithmetic
printf "%s\n" "----------------------------------"
printf "| Performance: %.0f\t|\n" $(echo "$SCORE_PERFORMANCE*100" | bc -l)
printf "| Accessibility: %.0f\t|\n" $(echo "$SCORE_ACCESSIBILITY*100" | bc -l)
printf "| Best Practices: %.0f\t|\n" $(echo "$SCORE_PRACTICES*100" | bc -l)
printf "| SEO: %.0f\t|\n" $(echo "$SCORE_SEO*100" | bc -l)
printf "| Progressive Web App: %.0f\t|\n" $(echo "$SCORE_PWA*100" | bc -l)
printf "%s\n" "----------------------------------"
# Print scores to standard output (0 to 100 instead of 0 to 1).
# Using hacky bc b/c bash hates floating point arithmetic...
printf "\n* Completed audit of %s !" "$REPORT_URL"
printf "\n* Scores are printed below and detailed results are saved in ./report\n\n"
printf "%s\n" "+-------------------------------+"
printf "| Performance: %.0f\t|\n" "$(echo "$SCORE_PERFORMANCE*100" | bc -l)"
printf "| Accessibility: %.0f\t|\n" "$(echo "$SCORE_ACCESSIBILITY*100" | bc -l)"
printf "| Best Practices: %.0f\t|\n" "$(echo "$SCORE_PRACTICES*100" | bc -l)"
printf "| SEO: %.0f\t|\n" "$(echo "$SCORE_SEO*100" | bc -l)"
printf "| Progressive Web App: %.0f\t|\n" "$(echo "$SCORE_PWA*100" | bc -l)"
printf "%s\n" "+-------------------------------+"

exit 0

0 comments on commit a9175de

Please sign in to comment.