mirror of
https://github.com/vale981/tridactyl
synced 2025-03-04 17:11:40 -05:00
Teach lint,pretty to operate on staged copy
Gets a bit crazy.
This commit is contained in:
parent
99e5475d5e
commit
6e44bef6bd
2 changed files with 22 additions and 3 deletions
|
@ -4,10 +4,14 @@ cachedJS() {
|
|||
git diff --cached --name-only --diff-filter=ACM "*.js" "*.jsx" "*.ts" "*.tsx" | tr '\n' ' '
|
||||
}
|
||||
|
||||
staged() {
|
||||
git show :"$1"
|
||||
}
|
||||
|
||||
ugly() {
|
||||
local acc=()
|
||||
for jsfile in "$@"; do
|
||||
diff "$jsfile" <($(npm bin)/prettier $jsfile) >/dev/null || acc+=("$jsfile")
|
||||
diff <(staged $jsfile) <(staged $jsfile | $(npm bin)/prettier --stdin-filepath $jsfile) >/dev/null || acc+=("$jsfile")
|
||||
done
|
||||
echo ${acc[@]}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,21 @@ set -e
|
|||
uglyFiles=$(ugly $(cachedJS))
|
||||
|
||||
if [ -n "$uglyFiles" ]; then
|
||||
prettier --write $uglyFiles
|
||||
git add $uglyFiles
|
||||
for file in "$uglyFiles"; do
|
||||
if cmp -s <(staged $file) "$file"; then
|
||||
prettier --write "$file"
|
||||
git add "$file"
|
||||
else
|
||||
echo "WARN: Staged and working file differ: '$file'"
|
||||
echo "WARN: Moving working file temporarily..."
|
||||
# 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)
|
||||
mv "$file" "$backup"
|
||||
staged "$file" > "$file"
|
||||
prettier --write "$file"
|
||||
git add "$file"
|
||||
mv "$backup" "$file"
|
||||
echo "WARN: Working file restored"
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
|
Loading…
Add table
Reference in a new issue