Fix pre-commit script to properly handle multiple files and resolve ESLint warnings
This commit is contained in:
@@ -55,17 +55,17 @@ OPTIONAL_VARS=(
|
||||
validate_file() {
|
||||
local file="$1"
|
||||
local file_type="$2"
|
||||
|
||||
|
||||
print_section "Validating $file ($file_type)"
|
||||
|
||||
|
||||
if [[ ! -f "$file" ]]; then
|
||||
print_error "File not found: $file"
|
||||
return 1
|
||||
fi
|
||||
|
||||
|
||||
local missing_vars=()
|
||||
local found_vars=()
|
||||
|
||||
|
||||
# Check core variables
|
||||
for var in "${CORE_VARS[@]}"; do
|
||||
if grep -q "^${var}=" "$file" || grep -q "^#.*${var}=" "$file"; then
|
||||
@@ -74,7 +74,7 @@ validate_file() {
|
||||
missing_vars+=("$var")
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
# Check K8s variables for relevant files
|
||||
if [[ "$file_type" != "template" ]]; then
|
||||
for var in "${K8S_VARS[@]}"; do
|
||||
@@ -85,22 +85,22 @@ validate_file() {
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
|
||||
# Report results
|
||||
print_success "Found ${#found_vars[@]} variables"
|
||||
|
||||
|
||||
if [[ ${#missing_vars[@]} -gt 0 ]]; then
|
||||
print_warning "Missing variables:"
|
||||
for var in "${missing_vars[@]}"; do
|
||||
echo " - $var"
|
||||
done
|
||||
fi
|
||||
|
||||
|
||||
# Check for old VITE_MAILGUN variables
|
||||
if grep -q "VITE_MAILGUN" "$file"; then
|
||||
print_error "Found deprecated VITE_MAILGUN variables (should be MAILGUN_*)"
|
||||
fi
|
||||
|
||||
|
||||
# Check variable format
|
||||
local malformed_vars=()
|
||||
while IFS= read -r line; do
|
||||
@@ -111,25 +111,25 @@ validate_file() {
|
||||
fi
|
||||
fi
|
||||
done < "$file"
|
||||
|
||||
|
||||
if [[ ${#malformed_vars[@]} -gt 0 ]]; then
|
||||
print_warning "Malformed variable names:"
|
||||
for var in "${malformed_vars[@]}"; do
|
||||
echo " - $var"
|
||||
done
|
||||
fi
|
||||
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
validate_consistency() {
|
||||
print_section "Cross-file Consistency Check"
|
||||
|
||||
|
||||
# Extract variable names from each file
|
||||
local example_vars=()
|
||||
local env_vars=()
|
||||
local prod_vars=()
|
||||
|
||||
|
||||
if [[ -f ".env.example" ]]; then
|
||||
while IFS= read -r line; do
|
||||
if [[ "$line" =~ ^[A-Z_]+=.* ]]; then
|
||||
@@ -137,7 +137,7 @@ validate_consistency() {
|
||||
fi
|
||||
done < ".env.example"
|
||||
fi
|
||||
|
||||
|
||||
if [[ -f ".env" ]]; then
|
||||
while IFS= read -r line; do
|
||||
if [[ "$line" =~ ^[A-Z_]+=.* ]]; then
|
||||
@@ -145,7 +145,7 @@ validate_consistency() {
|
||||
fi
|
||||
done < ".env"
|
||||
fi
|
||||
|
||||
|
||||
if [[ -f ".env.production" ]]; then
|
||||
while IFS= read -r line; do
|
||||
if [[ "$line" =~ ^[A-Z_]+=.* ]]; then
|
||||
@@ -153,11 +153,11 @@ validate_consistency() {
|
||||
fi
|
||||
done < ".env.production"
|
||||
fi
|
||||
|
||||
|
||||
# Check if .env and .env.production have all variables from .env.example
|
||||
local missing_in_env=()
|
||||
local missing_in_prod=()
|
||||
|
||||
|
||||
for var in "${example_vars[@]}"; do
|
||||
if [[ ! " ${env_vars[@]} " =~ " ${var} " ]]; then
|
||||
missing_in_env+=("$var")
|
||||
@@ -166,7 +166,7 @@ validate_consistency() {
|
||||
missing_in_prod+=("$var")
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
if [[ ${#missing_in_env[@]} -eq 0 ]]; then
|
||||
print_success ".env has all variables from .env.example"
|
||||
else
|
||||
@@ -175,7 +175,7 @@ validate_consistency() {
|
||||
echo " - $var"
|
||||
done
|
||||
fi
|
||||
|
||||
|
||||
if [[ ${#missing_in_prod[@]} -eq 0 ]]; then
|
||||
print_success ".env.production has all variables from .env.example"
|
||||
else
|
||||
@@ -184,20 +184,20 @@ validate_consistency() {
|
||||
echo " - $var"
|
||||
done
|
||||
fi
|
||||
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
validate_k8s_template() {
|
||||
print_section "Kubernetes Template Validation"
|
||||
|
||||
|
||||
local template_file="k8s/ingress.yaml.template"
|
||||
|
||||
|
||||
if [[ ! -f "$template_file" ]]; then
|
||||
print_error "Template file not found: $template_file"
|
||||
return 1
|
||||
fi
|
||||
|
||||
|
||||
# Check for template variables
|
||||
local template_vars=()
|
||||
while IFS= read -r line; do
|
||||
@@ -208,16 +208,16 @@ validate_k8s_template() {
|
||||
fi
|
||||
fi
|
||||
done < "$template_file"
|
||||
|
||||
|
||||
print_success "Found ${#template_vars[@]} template variables:"
|
||||
for var in "${template_vars[@]}"; do
|
||||
echo " - \${$var}"
|
||||
done
|
||||
|
||||
|
||||
# Check if template variables are defined in env files
|
||||
for var in "${template_vars[@]}"; do
|
||||
local found_in_files=()
|
||||
|
||||
|
||||
if grep -q "^${var}=" ".env.example" 2>/dev/null; then
|
||||
found_in_files+=(".env.example")
|
||||
fi
|
||||
@@ -227,39 +227,39 @@ validate_k8s_template() {
|
||||
if grep -q "^${var}=" ".env.production" 2>/dev/null; then
|
||||
found_in_files+=(".env.production")
|
||||
fi
|
||||
|
||||
|
||||
if [[ ${#found_in_files[@]} -gt 0 ]]; then
|
||||
print_success "$var defined in: ${found_in_files[*]}"
|
||||
else
|
||||
print_error "$var not defined in any environment file"
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
main() {
|
||||
print_header
|
||||
|
||||
|
||||
# Validate individual files
|
||||
if [[ -f ".env.example" ]]; then
|
||||
validate_file ".env.example" "template"
|
||||
fi
|
||||
|
||||
|
||||
if [[ -f ".env" ]]; then
|
||||
validate_file ".env" "development"
|
||||
fi
|
||||
|
||||
|
||||
if [[ -f ".env.production" ]]; then
|
||||
validate_file ".env.production" "production"
|
||||
fi
|
||||
|
||||
|
||||
# Cross-file validation
|
||||
validate_consistency
|
||||
|
||||
|
||||
# Kubernetes template validation
|
||||
validate_k8s_template
|
||||
|
||||
|
||||
print_section "Summary"
|
||||
print_success "Environment validation complete!"
|
||||
echo ""
|
||||
|
||||
Reference in New Issue
Block a user