Compare commits
1 Commits
f9bc85d5f8
...
49b00bd108
Author | SHA1 | Date |
---|---|---|
RameshT | 49b00bd108 |
|
@ -1,100 +1,60 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
. "$(dirname "$0")/_/husky.sh"
|
. "$(dirname "$0")/_/husky.sh"
|
||||||
|
|
||||||
|
# Get the list of staged PHP files
|
||||||
|
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.php$')
|
||||||
|
|
||||||
|
# If there are no staged PHP files, exit
|
||||||
|
if [ -z "$STAGED_FILES" ]; then
|
||||||
|
echo "No PHP files staged for commit."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
# Function to display errors
|
# Function to display errors
|
||||||
display_errors() {
|
display_errors() {
|
||||||
local errors="$1"
|
local errors="$1"
|
||||||
echo "Errors detected:"
|
echo "Errors detected:"
|
||||||
echo "---------------------------------------"
|
echo "---------------------------------------"
|
||||||
echo "$errors"
|
echo "$errors" | while IFS='|' read -r file line error; do
|
||||||
|
printf "%-60s | %-10s | %s\n" "$file" "$line" "$error"
|
||||||
|
done
|
||||||
echo "---------------------------------------"
|
echo "---------------------------------------"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get the list of staged files
|
# Run PHP lint to check for syntax errors
|
||||||
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(php|js|css|jsx|ts|tsx)$')
|
SYNTAX_ERRORS=0
|
||||||
|
echo "Checking PHP syntax errors..."
|
||||||
|
for FILE in $STAGED_FILES; do
|
||||||
|
php -l "$FILE"
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
SYNTAX_ERRORS=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
# If there are no staged files, exit
|
if [ $SYNTAX_ERRORS -ne 0 ]; then
|
||||||
if [ -z "$STAGED_FILES" ]; then
|
echo "Syntax errors detected. Please fix them before committing."
|
||||||
echo "No files staged for commit."
|
exit 1
|
||||||
exit 0
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Initialize error flags
|
# Run PHPCBF to auto-fix issues
|
||||||
ESLINT_ERRORS=""
|
echo "Running PHPCBF..."
|
||||||
PHP_ERRORS=0
|
for FILE in $STAGED_FILES; do
|
||||||
|
/home/aissel/.config/composer/vendor/bin/phpcbf --standard=/var/www/html/google_forms/phpcs.xml "$FILE" || true
|
||||||
|
done
|
||||||
|
|
||||||
# Function to run ESLint and Prettier on JavaScript files
|
# Run PHP CS Fixer to auto-fix issues
|
||||||
run_js_tools() {
|
echo "Running PHP CS Fixer..."
|
||||||
local files="$1"
|
for FILE in $STAGED_FILES; do
|
||||||
if [ -n "$files" ]; then
|
/home/aissel/.config/composer/vendor/bin/php-cs-fixer fix "$FILE"
|
||||||
echo "Running ESLint..."
|
done
|
||||||
for FILE in $files; do
|
|
||||||
ESLINT_OUTPUT=$(npx eslint "$FILE" 2>&1)
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
display_errors "$ESLINT_OUTPUT"
|
|
||||||
ESLINT_ERRORS=1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "Running Prettier..."
|
# Re-run PHPCS to check for unresolved coding standard violations
|
||||||
for FILE in $files; do
|
echo "Running PHPCS..."
|
||||||
npx prettier --write "$FILE"
|
echo "$STAGED_FILES" | xargs -n 1 /home/aissel/.config/composer/vendor/bin/phpcs --standard=/var/www/html/google_forms/phpcs.xml
|
||||||
done
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Function to run PHP tools
|
|
||||||
run_php_tools() {
|
|
||||||
local files="$1"
|
|
||||||
if [ -n "$files" ]; then
|
|
||||||
echo "Checking PHP syntax errors..."
|
|
||||||
SYNTAX_ERRORS=0
|
|
||||||
for FILE in $files; do
|
|
||||||
php -l "$FILE"
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
SYNTAX_ERRORS=1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
if [ $SYNTAX_ERRORS -ne 0 ]; then
|
|
||||||
PHP_ERRORS=1
|
|
||||||
echo "Syntax errors detected in PHP files."
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Running PHPCBF..."
|
|
||||||
for FILE in $files; do
|
|
||||||
/home/aissel/.config/composer/vendor/bin/phpcbf --standard=/var/www/html/google_forms/phpcs.xml "$FILE" || true
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "Running PHP CS Fixer..."
|
|
||||||
for FILE in $files; do
|
|
||||||
/home/aissel/.config/composer/vendor/bin/php-cs-fixer fix "$FILE"
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "Running PHPCS..."
|
|
||||||
for FILE in $files; do
|
|
||||||
PHPCS_OUTPUT=$(/home/aissel/.config/composer/vendor/bin/phpcs --standard=/var/www/html/google_forms/phpcs.xml "$FILE" 2>&1)
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
display_errors "$PHPCS_OUTPUT"
|
|
||||||
PHP_ERRORS=1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Run tools based on file types
|
|
||||||
run_js_tools "$(echo "$STAGED_FILES" | grep -E '\.(js|jsx|ts|tsx)$')"
|
|
||||||
run_php_tools "$(echo "$STAGED_FILES" | grep '\.php$')"
|
|
||||||
|
|
||||||
# Add the fixed files back to the staging area
|
# Add the fixed files back to the staging area
|
||||||
for FILE in $STAGED_FILES; do
|
for FILE in $STAGED_FILES; do
|
||||||
git add "$FILE"
|
git add "$FILE"
|
||||||
done
|
done
|
||||||
|
|
||||||
# Exit with error code if there were any errors
|
|
||||||
if [ $PHP_ERRORS -ne 0 ] || [ $ESLINT_ERRORS -eq 1 ]; then
|
|
||||||
echo "Pre-commit checks failed. Please fix the errors before committing."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Pre-commit checks completed."
|
echo "Pre-commit checks completed."
|
||||||
|
|
Loading…
Reference in New Issue