From 4cda5e0951f4b9cdeeda8dc0b73186ce12b616e8 Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Wed, 12 Aug 2020 14:03:26 -0500 Subject: [PATCH] Run files through prettier; add rudimentary .sh support --- _reformat-common.sh | 2 ++ do-reformat.sh | 21 +++++++++++---------- would-reformat.sh | 29 +++++++++++++++-------------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/_reformat-common.sh b/_reformat-common.sh index d2a1311..b37724b 100644 --- a/_reformat-common.sh +++ b/_reformat-common.sh @@ -21,6 +21,8 @@ function sniff_file_type() { file_type="jsx" elif [[ $ff == *.tsx ]]; then file_type="tsx" + elif [[ $ff = *.sh ]]; then + file_type="sh" fi echo $file_type diff --git a/do-reformat.sh b/do-reformat.sh index c43ea2a..e5ca718 100755 --- a/do-reformat.sh +++ b/do-reformat.sh @@ -3,7 +3,7 @@ set -uo pipefail IFS=$'\n\t' -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$DIR/_reformat-common.sh" @@ -12,17 +12,18 @@ file="$1" file_type=$(sniff_file_type $file) if [[ $file_type == "javascript" || \ - $file_type == "vue" || \ - $file_type == "css" || \ - $file_type == "typescript" || \ - $file_type == "php" || \ - $file_type == "html" || \ - $file_type == "jsx" || \ - $file_type == "tsx" ]] ; then + $file_type == "vue" || \ + $file_type == "css" || \ + $file_type == "typescript" || \ + $file_type == "php" || \ + $file_type == "html" || \ + $file_type == "jsx" || \ + $file_type == "tsx" || \ + $file_type == "sh" ]]; then out=$(npx prettier --write $file) retval="$?" success_retval=0 -elif [[ $file_type == "python" ]] ; then +elif [[ $file_type == "python" ]]; then out=$(pipx run black $file 2>/dev/null) retval="$?" success_retval=0 @@ -31,7 +32,7 @@ else exit 0 fi -if [[ x"$retval" == x"$success_retval" ]] ; then +if [[ x"$retval" == x"$success_retval" ]]; then echo "$out" exit 0 else diff --git a/would-reformat.sh b/would-reformat.sh index f9bb601..063ea18 100755 --- a/would-reformat.sh +++ b/would-reformat.sh @@ -3,7 +3,7 @@ set -uo pipefail IFS=$'\n\t' -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" file="$1" @@ -11,20 +11,21 @@ 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" ]] ; then - out=$(npx prettier --check $file 2>&1 > /dev/null) +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 = "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 +elif [[ $file_type == "python" ]]; then out=$(pipx run black --check $file 2>/dev/null) retval="$?" success_retval=0 @@ -36,13 +37,13 @@ else fi -if [[ x"$retval" == x"$success_retval" ]] ; then +if [[ x"$retval" == x"$success_retval" ]]; then echo -n "would not change" exit 0 -elif [[ x"$retval" == x"$would_reformat_retval" ]] ; then +elif [[ x"$retval" == x"$would_reformat_retval" ]]; then echo -n "would change" exit 1 -elif [[ x"$retval" == x"$syntax_error_retval" ]] ; then +elif [[ x"$retval" == x"$syntax_error_retval" ]]; then echo -n "syntax error" echo echo "$out"