mirror of
https://github.com/vale981/tridactyl
synced 2025-03-05 17:41:40 -05:00
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.
This commit is contained in:
parent
a9bb149bfa
commit
54faf82428
2 changed files with 19 additions and 11 deletions
|
@ -1,19 +1,26 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
cachedJS() {
|
# Accepts no arguments
|
||||||
git diff --cached --name-only --diff-filter=ACM "*.js" "*.jsx" "*.ts" "*.tsx" "*.md" "*.css" | tr '\n' ' '
|
# 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() {
|
staged() {
|
||||||
git show :"$1"
|
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() {
|
ugly() {
|
||||||
local acc=()
|
local acc=""
|
||||||
for jsfile in "$@"; do
|
local IFS=$'\n'
|
||||||
diff <(staged $jsfile) <(staged $jsfile | $(npm bin)/prettier --stdin-filepath $jsfile) >/dev/null || acc+=("$jsfile")
|
for jsfile in $1; do
|
||||||
|
diff <(staged "$jsfile") <(staged "$jsfile" | "$(npm bin)/prettier" --stdin-filepath "$jsfile") >/dev/null || acc="$jsfile"$'\n'"$acc"
|
||||||
done
|
done
|
||||||
echo ${acc[@]}
|
echo "$acc"
|
||||||
}
|
}
|
||||||
|
|
||||||
noisy() {
|
noisy() {
|
||||||
|
|
|
@ -4,18 +4,19 @@ source ./scripts/common
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
uglyFiles=$(ugly $(cachedJS))
|
uglyFiles="$(ugly "$(cachedFiles)")"
|
||||||
|
|
||||||
if [ -n "$uglyFiles" ]; then
|
if [ -n "$uglyFiles" ]; then
|
||||||
for file in "$uglyFiles"; do
|
IFS=$'\n'
|
||||||
if cmp -s <(staged $file) "$file"; then
|
for file in $uglyFiles; do
|
||||||
|
if cmp -s <(staged "$file") "$file"; then
|
||||||
prettier --write "$file"
|
prettier --write "$file"
|
||||||
git add "$file"
|
git add "$file"
|
||||||
else
|
else
|
||||||
echo "WARN: Staged and working file differ: '$file'"
|
echo "WARN: Staged and working file differ: '$file'"
|
||||||
echo "WARN: Moving working file temporarily (this might upset your editor)"
|
echo "WARN: Moving working file temporarily (this might upset your editor)"
|
||||||
# Get crazy: backup the working copy of the file, paste the staged version in its place, prettify, add, restore backup
|
# Get crazy: backup the working copy of the file, paste the staged version in its place, prettify, add, restore backup
|
||||||
backup=$(mktemp -p . $file.XXXXXXXXX)
|
backup=$(mktemp -p . "$file.XXXXXXXXX")
|
||||||
mv "$file" "$backup"
|
mv "$file" "$backup"
|
||||||
staged "$file" | prettier --stdin-filepath "$file" > "$file"
|
staged "$file" | prettier --stdin-filepath "$file" > "$file"
|
||||||
git add "$file"
|
git add "$file"
|
||||||
|
|
Loading…
Add table
Reference in a new issue