diff --git a/.husky/pre-push b/.husky/pre-push index ad8f63b32..cca90f313 100644 --- a/.husky/pre-push +++ b/.husky/pre-push @@ -1,7 +1,25 @@ -# Configuration: Each entry is an array with [pattern, command, description] -# Format: "pattern" "command" "pattern description" -CHECKS=( - '^(package\.json|package-lock\.json)$' 'pnpm install --package-lock --ignore-scripts' 'package.json or package-lock.json – please run npm install to update dependencies' +# Configuration: Define checks as functions for better maintainability +# Each check function should: +# - Define a PATTERN variable for file matching +# - Define a COMMAND variable for the command to run +# - Define a DESCRIPTION variable for user feedback + +check_npm_files() { + PATTERN='^(package\.json|package-lock\.json)$' + COMMAND='npm install --package-lock-only --ignore-scripts' + DESCRIPTION='package.json or package-lock.json – please run npm install to update dependencies' +} + +check_pnpm_files() { + PATTERN='^(package\.json|pnpm-lock\.yaml)$' + COMMAND='pnpm install --lockfile-only --ignore-scripts' + DESCRIPTION='package.json or pnpm-lock.yaml – please run pnpm install to update dependencies' +} + +# List of all check functions +CHECK_FUNCTIONS=( + "check_npm_files" + "check_pnpm_files" ) # Check for changes in specified files before pushing and run corresponding commands @@ -22,24 +40,29 @@ fi FILES=$(git diff --name-only $UPSTREAM..HEAD) ## Check each pattern and run corresponding command -for ((i=0; i<${#CHECKS[@]}; i+=3)); do - pattern="${CHECKS[i]}" - command="${CHECKS[i+1]}" - description="${CHECKS[i+2]}" - - if echo "$FILES" | grep -qE "$pattern"; then - echo "Detected changes in $description" +for check_function in "${CHECK_FUNCTIONS[@]}"; do + # Call the check function to set variables + $check_function + + if echo "$FILES" | grep -qE "$PATTERN"; then + echo "Detected changes in $DESCRIPTION" ## Run the corresponding command - eval "$command" + eval "$COMMAND" if [ $? -ne 0 ]; then - echo "Command failed: $command. Aborting push." + echo "Command failed: $COMMAND. Aborting push." exit 1 fi - # Exit after first match to avoid running multiple commands - exit 0 + # Check for file modifications after running the command + MODIFIED_FILES=$(git diff --name-only) + if [ -n "$MODIFIED_FILES" ]; then + echo "Detected file modifications after running $COMMAND:" + echo "$MODIFIED_FILES" + echo "Please stage the changes before pushing." + exit 1 + fi fi done