2018-08-24 13:38:05 -06:00
|
|
|
#!/usr/bin/env bash
|
2018-05-18 15:35:32 +01:00
|
|
|
|
2018-08-29 11:24:17 +01:00
|
|
|
# Run prettier on each staged file that needs it without touching the working tree copy if they differ.
|
2018-08-29 10:31:15 +01:00
|
|
|
|
2019-05-29 17:48:32 +01:00
|
|
|
source ./scripts/common.sh
|
2018-05-18 15:35:32 +01:00
|
|
|
|
2018-08-29 10:31:15 +01:00
|
|
|
set -Eeuo pipefail
|
|
|
|
|
|
|
|
echoe() {
|
|
|
|
echo "$@" >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
lock() {
|
|
|
|
local lockfile="$1"
|
|
|
|
if [ -e "$lockfile" ]; then
|
|
|
|
echoe "Lockfile '$lockfile' already exists. Check no other operation is occuring or delete lockfile"
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
touch "$lockfile"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock() {
|
|
|
|
rm "$1"
|
|
|
|
}
|
|
|
|
|
2019-05-29 21:09:58 +01:00
|
|
|
trap 'unlock $(git rev-parse --show-toplevel)/.git/index.lock || true' ERR
|
2018-08-29 10:31:15 +01:00
|
|
|
|
|
|
|
main() {
|
2019-05-29 21:09:58 +01:00
|
|
|
local stagedFiles
|
|
|
|
stagedFiles="$(cachedTSLintFiles)"$'\n'"$(cachedPrettierFiles)"
|
2018-08-29 10:31:15 +01:00
|
|
|
|
|
|
|
if [ -n "$stagedFiles" ]; then
|
|
|
|
# Could use git-update-index --cacheinfo to add a file without creating directories and stuff.
|
|
|
|
IFS=$'\n'
|
|
|
|
for file in $stagedFiles; do
|
2018-08-29 11:24:17 +01:00
|
|
|
if cmp -s <(staged "$file") "$file"; then
|
2018-08-29 11:32:07 +01:00
|
|
|
echo "WARN: Staged copy of '$file' matches working copy. Modifying both"
|
2020-06-18 22:08:02 +01:00
|
|
|
echo "WARN: Modifications may break builds: check that your code still builds"
|
2018-08-29 11:24:17 +01:00
|
|
|
lock .git/index.lock
|
2019-04-06 22:50:01 +02:00
|
|
|
case "$file" in
|
|
|
|
*.md | *.css) prettier --write "$file";;
|
2020-06-16 20:50:12 +02:00
|
|
|
*) eslint --fix "$file";;
|
2019-04-06 22:50:01 +02:00
|
|
|
esac
|
2018-08-29 11:24:17 +01:00
|
|
|
unlock .git/index.lock
|
|
|
|
git add "$file"
|
|
|
|
else
|
|
|
|
echo "WARN: Staged copy of '$file' does not match working copy: only prettifying staged copy."
|
2020-06-18 22:08:02 +01:00
|
|
|
echo "WARN: Modifications may break builds: check that your code still builds"
|
2018-08-29 11:24:17 +01:00
|
|
|
(
|
2019-06-01 18:47:14 +01:00
|
|
|
local tmpdir
|
|
|
|
tmpdir=$(mktemp -d "pretty.XXXXXXXXX")
|
2018-08-29 11:24:17 +01:00
|
|
|
cd "$tmpdir"
|
2019-05-29 21:09:58 +01:00
|
|
|
mkdir -p "$(dirname "$file")"
|
2018-08-29 11:24:17 +01:00
|
|
|
lock ../.git/index.lock
|
2019-06-01 18:47:14 +01:00
|
|
|
tmpfile=$(mktemp "pretty.XXXXXXXXX")
|
2019-04-06 22:50:01 +02:00
|
|
|
case "$file" in
|
2019-06-01 18:47:14 +01:00
|
|
|
*.md | *.css)
|
|
|
|
staged "$file" | prettier --stdin-filepath "$file" > "$tmpfile" &&
|
|
|
|
mv "$tmpfile" "$file";;
|
|
|
|
*)
|
|
|
|
staged "$file" > "$tmpfile"
|
2020-06-16 20:50:12 +02:00
|
|
|
eslint -c ../.eslintrc.js --fix -o /dev/null "$tmpfile" &&
|
2019-06-01 18:47:14 +01:00
|
|
|
mv "$tmpfile" "$file";;
|
2019-04-06 22:50:01 +02:00
|
|
|
esac
|
2018-08-29 11:24:17 +01:00
|
|
|
chmod --reference="../$file" "$file" # match permissions
|
|
|
|
# Can't hold lock while git add occurs. Hopefully release and reacquire happen fast enough to prevent race.
|
|
|
|
unlock ../.git/index.lock
|
|
|
|
GIT_WORK_TREE=. git add "$file"
|
2019-06-01 18:47:14 +01:00
|
|
|
rm -rf "$tmpdir"
|
2018-08-29 11:24:17 +01:00
|
|
|
)
|
|
|
|
fi
|
2018-08-29 10:31:15 +01:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
main
|