-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpre-commit
28 lines (22 loc) · 961 Bytes
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/sh
# Change to the project root directory
cd "$(git rev-parse --show-toplevel)" || exit 1
# Use git diff to list changed package.json files
changedPackageJsonFiles=$(git diff --name-only HEAD^ HEAD | grep -E 'package\.json$')
# Iterate through the changed package.json files and change the
# Windows remove command to the Unix remove command
for file in $changedPackageJsonFiles; do
to_replace="rd \/s \/q dist>nul 2>&1|echo.>nul"
replacement="rm -rf .\/dist\/*"
# https://stackoverflow.com/a/61808364/6456163
changes=""
changes+=$(sed -i '' "s/$to_replace/$replacement/g w /dev/stdout" "$file")
if [ "$changes" != "" ]; then
# NOTE: This pushes *all* changes to the affected `package.json` files,
# not just the changes made by the `sed` command above. Be careful!
git add "$file"
fi
done
echo "Replacements completed for changed package.json files."
# Continue with the default pre-commit actions
exit $?