tridactyl/scripts/common
glacambre 54faf82428
scripts/{pretty,common}: Fix pretty failing on multiple files
When trying to run scripts/pretty with multiple ugly files staged, the
for loop would treat two space-separated file names as a single file
name. This prevented pretty from working on my machine.

I fixed this issue by making cachedJS() (renamed to cachedFiles since it
returns more than js files) return file names as a newline-separated
list and using IFS=$'\n' to split the filenames on this character.
2018-06-03 16:08:45 +02:00

34 lines
1,016 B
Bash
Executable file

#!/bin/bash
# Accepts no arguments
# Returns git-add'ed files as a list of filenames separated by a newline character
cachedFiles() {
git diff --cached --name-only --diff-filter=ACM "*.js" "*.jsx" "*.ts" "*.tsx" "*.md" "*.css"
}
# Accepts a single argument which is the name of a file tracked by git
# Returns a string which is the content of the file as stored in the git index
staged() {
git show :"$1"
}
# Accepts a single string argument made of multiple file names separated by a newline
# Returns an array of files that prettier wants to lint
ugly() {
local acc=""
local IFS=$'\n'
for jsfile in $1; do
diff <(staged "$jsfile") <(staged "$jsfile" | "$(npm bin)/prettier" --stdin-filepath "$jsfile") >/dev/null || acc="$jsfile"$'\n'"$acc"
done
echo "$acc"
}
noisy() {
local acc=()
for jsfile in "$@"; do
if [ "$(git diff --cached "$jsfile" | grep '^+.*console.log' -c)" -gt '0' ] ; then
acc+=("jsfile")
fi
done
echo ${acc[@]}
}