diff --git a/.gitignore b/.gitignore index 9aea658..fefeb05 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .dir-locals.el bin/ -node_modules/ \ No newline at end of file +node_modules/ +.root diff --git a/_reformat-common.bash b/_reformat-common.bash index 4cff1bb..20f22ff 100644 --- a/_reformat-common.bash +++ b/_reformat-common.bash @@ -2,6 +2,24 @@ function root() { echo "$(git rev-parse --show-toplevel)" } +wfroot() { + dir="$1" + cat "$dir/.root" +} + +function custom_formatter() { + root="$1" + file_type="$2" + + maybe="$root/.would-reformat/$file_type" + if [[ -f "$maybe" ]] ; then + echo "$maybe" + return + fi + + echo "use-default" +} + function sniff_file_type() { ff=$1 shift @@ -39,3 +57,8 @@ function sniff_file_type() { echo $file_type } + + +function wrflog() { + echo "$@" >> /tmp/wrflog +} diff --git a/install.sh b/install.sh index 54093d4..cf2086b 100755 --- a/install.sh +++ b/install.sh @@ -7,13 +7,18 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" here=$(pwd) +pushd $DIR +rm -f .root +echo "$(pwd)" > .root +popd + ret=0 # If this use of readlink breaks, see # https://unix.stackexchange.com/questions/47710/find-only-destination-of-symlink # for discussion of alternatives. -for i in _reformat-common.sh do-reformat.sh would-reformat.sh; do +for i in _reformat-common.bash do-reformat.sh would-reformat.sh .root; do if [[ -f "$here"/bin/"$i" ]] ; then dest=$(readlink bin/$i) if [[ x"$dest" == x"$DIR/$i" ]] ; then diff --git a/prettier.sh b/prettier.sh new file mode 100755 index 0000000..24df7f0 --- /dev/null +++ b/prettier.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +set -uo pipefail +IFS=$'\n\t' + +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +source "$DIR/_reformat-common.bash" + +file="$1" + +wrflog running against file $file + +out=$(npx prettier --check $file 2>&1 >/dev/null) + +if [[ x"$WOULD_REFORMAT" = x"would_reformat" ]] ; then + out=$(npx prettier --check $file 2>&1 >/dev/null) + retval="$?" + exit "$retval" +fi + +if [[ x"$WOULD_REFORMAT" = x"do_reformat" ]] ; then # + out=$(npx prettier --write $file 2>&1 >/dev/null) + retval="$?" + exit "$retval" +fi + +exit 255 diff --git a/would-reformat.sh b/would-reformat.sh index ebd39aa..657daf6 100755 --- a/would-reformat.sh +++ b/would-reformat.sh @@ -5,12 +5,20 @@ IFS=$'\n\t' DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "$DIR/_reformat-common.bash" + +wrflog asdf asdf asdf + file="$1" -source "$DIR/_reformat-common.sh" +## fixme use realpath or something on "$file" + + file_type=$(sniff_file_type $file) +wrflog got file_type $file_type + if [[ $file_type = "javascript" || \ $file_type = "vue" || \ $file_type = "typescript" || \ @@ -22,7 +30,10 @@ if [[ $file_type = "javascript" || \ $file_type == "css" || \ $file_type == "scss" || \ $file_type = "sh" ]]; then - out=$(npx prettier --check $file 2>&1 >/dev/null) +wrflog will run thing +wrflog "$(wfroot $DIR)" +wrflog "prettier: $(ls -l $(wfroot $DIR)/./prettier.sh)" + out=$(WOULD_REFORMAT=would_reformat $(wfroot $DIR)/./prettier.sh $file 2>&1 > /dev/null) retval="$?" success_retval=0 would_reformat_retval=1