Files
would-reformat/would-reformat.sh
2022-10-13 18:43:40 -05:00

86 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
set -uo pipefail
IFS=$'\n\t'
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
file="$1"
source "$DIR/_reformat-common.sh"
file_type=$(sniff_file_type $file)
if [[ $file_type = "javascript" || \
$file_type = "vue" || \
$file_type = "typescript" || \
$file_type = "css" || \
$file_type = "php" || \
$file_type = "html" || \
$file_type = "jsx" || \
$file_type = "tsx" || \
$file_type == "css" || \
$file_type == "scss" || \
$file_type = "sh" ]]; then
out=$(npx prettier --check $file 2>&1 >/dev/null)
retval="$?"
success_retval=0
would_reformat_retval=1
syntax_error_retval=2
elif [[ $file_type == "python" ]]; then
out=$(pipx run isort --check $file 2>/dev/null)
isort_retval="$?"
success_retval=0
would_reformat_retval=1
syntax_error_retval=123
if [[ x"$isort_retval" = x"0" ]] ; then
out=$(pipx run black --check $file 2>/dev/null)
retval="$?"
else
retval="$isort_retval"
fi
elif [[ $file_type == "golang" ]] ; then
success_retval=0
would_reformat_retval=1
syntax_error_retval=2
out=$(gofmt -l $file)
exitval="$?"
if [[ x"$exitval" = x"$syntax_error_retval" ]] ; then
retval="$exitval"
elif [[ x"$out" = x"$file" ]] ; then
retval="$would_reformat_retval"
else
# success
retval="$exitval"
fi
else
echo -n "ignoring"
exit 0
fi
if [[ x"$retval" == x"$success_retval" ]]; then
echo -n "would not change"
exit 0
elif [[ x"$retval" == x"$would_reformat_retval" ]]; then
echo -n "would change"
exit 1
elif [[ x"$retval" == x"$syntax_error_retval" ]]; then
echo -n "syntax error"
echo
echo "$out"
exit 2
else
echo -n "problem running reformatter"
exit 3
fi
echo -n "ok"
exit 0