Last active
October 3, 2020 22:33
-
-
Save pearofducks/22aa906a550c762a7b7a582f6d4e14ff to your computer and use it in GitHub Desktop.
Prettier script for Vue files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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