#!/bin/bash

AVA_REPORT=$(nyc ava --timeout 10m --concurrency 2 --tap --reporter=html --reporter=text-summary)
STATUS_ECHO="$?"
echo "${AVA_REPORT}" | ./node_modules/tap-summary/bin/cmd.js

echo "${AVA_REPORT}" | ./node_modules/tap-xunit/bin/tap-xunit > results.xml

COV_STAT=$(grep 'All files' results.xml | awk '{ print $4 }')
COV_BRANCH=$(grep 'All files' results.xml | awk '{ print $6 }')
COV_FUNCS=$(grep 'All files' results.xml | awk '{ print $8 }')
COV_LINES=$(grep 'All files' results.xml | awk '{ print $10 }')
echo "Coverage: ${COV_STAT}/95%"

echo "cov-statements ${COV_STAT}" > metrics.txt
echo "cov-branches ${COV_BRANCH}" >> metrics.txt
echo "cov-functions ${COV_FUNCS}" >> metrics.txt
echo "cov-lines ${COV_LINES}" >> metrics.txt

if [ ${STATUS_ECHO} -ne 0 ]; then
  exit ${STATUS_ECHO}
fi
