|
1 |
| -# Configuration: Each entry is an array with [pattern, command, description] |
2 |
| -# Format: "pattern" "command" "pattern description" |
3 |
| -CHECKS=( |
4 |
| - '^(package\.json|package-lock\.json)$' 'pnpm install --package-lock --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" |
5 | 23 | )
|
6 | 24 |
|
7 | 25 | # Check for changes in specified files before pushing and run corresponding commands
|
|
22 | 40 | FILES=$(git diff --name-only $UPSTREAM..HEAD)
|
23 | 41 |
|
24 | 42 | ## Check each pattern and run corresponding command
|
25 |
| -for ((i=0; i<${#CHECKS[@]}; i+=3)); do |
26 |
| - pattern="${CHECKS[i]}" |
27 |
| - command="${CHECKS[i+1]}" |
28 |
| - description="${CHECKS[i+2]}" |
29 |
| - |
30 |
| - if echo "$FILES" | grep -qE "$pattern"; then |
31 |
| - 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" |
32 | 49 |
|
33 | 50 | ## Run the corresponding command
|
34 |
| - eval "$command" |
| 51 | + eval "$COMMAND" |
35 | 52 |
|
36 | 53 | if [ $? -ne 0 ]; then
|
37 |
| - echo "Command failed: $command. Aborting push." |
| 54 | + echo "Command failed: $COMMAND. Aborting push." |
38 | 55 | exit 1
|
39 | 56 | fi
|
40 | 57 |
|
41 |
| - # Exit after first match to avoid running multiple commands |
42 |
| - 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 |
43 | 66 | fi
|
44 | 67 | done
|
45 | 68 |
|
|
0 commit comments