Skip to content

Commit eff77a4

Browse files
authored
Update pre-push
1 parent 0e78bd3 commit eff77a4

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

.husky/pre-push

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
1-
# Configuration: Each entry is an array with [pattern, command, description]
2-
# Format: "pattern" "command" "pattern description"
3-
CHECKS=('^(package\.json|package-lock\.json)$' 'npm install --package-lock-only --ignore-scripts' 'package.json or package-lock.json – please run npm install to update dependencies')
1+
# Configuration: Define checks as functions for better maintainability
2+
# Each check function should:
3+
# - Define a PATTERN variable for file matching
4+
# - Define a COMMAND variable for the command to run
5+
# - Define a DESCRIPTION variable for user feedback
6+
7+
check_npm_files() {
8+
PATTERN='^(package\.json|package-lock\.json)$'
9+
COMMAND='npm install --package-lock-only --ignore-scripts'
10+
DESCRIPTION='package.json or package-lock.json – please run npm install to update dependencies'
11+
}
12+
13+
check_pnpm_files() {
14+
PATTERN='^(package\.json|pnpm-lock\.yaml)$'
15+
COMMAND='pnpm install --lockfile-only --ignore-scripts'
16+
DESCRIPTION='package.json or pnpm-lock.yaml – please run pnpm install to update dependencies'
17+
}
18+
19+
# List of all check functions
20+
CHECK_FUNCTIONS=(
21+
"check_npm_files"
22+
"check_pnpm_files"
23+
)
424

525
# Check for changes in specified files before pushing and run corresponding commands
626
## Get the upstream branch
@@ -20,24 +40,29 @@ fi
2040
FILES=$(git diff --name-only $UPSTREAM..HEAD)
2141

2242
## Check each pattern and run corresponding command
23-
for ((i=0; i<${#CHECKS[@]}; i+=3)); do
24-
pattern="${CHECKS[i]}"
25-
command="${CHECKS[i+1]}"
26-
description="${CHECKS[i+2]}"
27-
28-
if echo "$FILES" | grep -qE "$pattern"; then
29-
echo "Detected changes in $description"
43+
for check_function in "${CHECK_FUNCTIONS[@]}"; do
44+
# Call the check function to set variables
45+
$check_function
46+
47+
if echo "$FILES" | grep -qE "$PATTERN"; then
48+
echo "Detected changes in $DESCRIPTION"
3049

3150
## Run the corresponding command
32-
eval "$command"
51+
eval "$COMMAND"
3352

3453
if [ $? -ne 0 ]; then
35-
echo "Command failed: $command. Aborting push."
54+
echo "Command failed: $COMMAND. Aborting push."
3655
exit 1
3756
fi
3857

39-
# Exit after first match to avoid running multiple commands
40-
exit 0
58+
# Check for file modifications after running the command
59+
MODIFIED_FILES=$(git diff --name-only)
60+
if [ -n "$MODIFIED_FILES" ]; then
61+
echo "Detected file modifications after running $COMMAND:"
62+
echo "$MODIFIED_FILES"
63+
echo "Please stage the changes before pushing."
64+
exit 1
65+
fi
4166
fi
4267
done
4368

0 commit comments

Comments
 (0)