Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Run static checks and build on githooks. Bash script with progress display
#!/bin/bash
######################################
## Utilities
######################################
success () {
printf "\r\033[2K [ \033[00;32mOK\033[0m ] $1\n"
}
info () {
printf " [\033[00;34mINFO\033[0m] $1\n"
}
fail () {
printf "\r\033[2K [\033[0;31mFAIL\033[0m] $1\n"
echo ''
exit 1
}
safe_run () {
set +e
local log="&>/dev/null &"
eval "$1 $log"
pid=$! # Process Id of the previous running command
# Show progress bar
spin[0]="-"
spin[1]="\\"
spin[2]="|"
spin[3]="/"
echo -n " ${spin[0]}"
while kill -0 $pid 2>/dev/random; do
for i in "${spin[@]}"; do
echo -ne "\b$i"
sleep 0.1
done
done
# read the status of background process
wait $pid
# Handle the error
if [[ $? -ne 0 ]]; then
if [[ -n "${2+x}" ]]; then
fail "$2"
else
fail "$1 failed"
fi
set -e
exit 1
fi
}
######################################
## Checks
######################################
# Try to build the app
info "Building the app"
cd app
safe_run "npm run build -s", "Build Failed"
success "Build Done"
# Linter
info "Linting"
safe_run "npm run lint -s", "Lint failed"
success "Lint Successful"
# Unittests
info "Runnin Unittests"
safe_run "npm test -s", "Unitests have failed"
success "Unittests Passed"
# Do checks on client
info "Linting"
safe_run "npm run lint -s", "Client ESLint Failed"
success "Lint Successful"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment