Skip to content

Instantly share code, notes, and snippets.

@pearofducks
Last active October 3, 2020 22:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pearofducks/22aa906a550c762a7b7a582f6d4e14ff to your computer and use it in GitHub Desktop.
Save pearofducks/22aa906a550c762a7b7a582f6d4e14ff to your computer and use it in GitHub Desktop.
Prettier script for Vue files
#!/bin/bash
cd "$(dirname "$(readlink -f "$0")")"
SEDBIN="gsed"
command -v gsed >/dev/null 2>&1 || { SEDBIN="sed"; }
$SEDBIN --version >/dev/null 2>&1 || { echo >&2 "GNU-sed required but not installed. Maybe run: brew install gnu-sed"; exit 1; }
PROC="./node_modules/prettier/bin/prettier.js"
DIR="src/main/javascript"
VUE_FILES=$(find $DIR -iname "*.vue" -print)
open="<script>"
close="<\/script>"
pretty() {
echo "$3"
$SEDBIN -n "/^${1}/,/${2}/p" "$3" | \
$SEDBIN "1d;\$d" | \
$PROC --stdin-filepath | \
$SEDBIN -e "1i${1}" -e "\$a${2}" | \
$SEDBIN -i -e "/^${1}/r /dev/stdin" -e "/^${1}/,/${2}/d" "$3"
}
pretty_check() {
$SEDBIN -n "/^${1}/,/${2}/p" "$3" | \
$SEDBIN "1d;\$d" | \
${PROC} --stdin-filepath --list-different
}
usage() { echo "$0
-f SINGLE_FILE
-c (check all files)
-a (fix all files)"; exit 1; }
[ $# -eq 0 ] && usage
while getopts ":achf:" o; do
case "${o}" in
a)
for f in $VUE_FILES
do
pretty $open $close $f
done
;;
c)
(for f in $VUE_FILES
do
pretty_check $open $close $f &>/dev/null || { echo >&2 "$f IS NOT PRETTY ENOUGH"; exit 1; }
done);
;;
h)
usage
;;
f)
[ -z "${OPTARG}" ] && usage
pretty $open $close $OPTARG
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment