Skip to content

Commit 5af55f7

Browse files
committed
fix(scripts): resolve exit code 1 failures in documentation cleanup
CHANGES - Add division by zero protection for percentage calculations - Implement bc command fallback using integer arithmetic - Add error handling for file counting and removal operations IMPACT - Prevents script exit on arithmetic errors and missing bc command - Enables processing to continue when individual file operations fail TECHNICAL NOTES - Added conditional checks before percentage calculations - Enhanced error handling while maintaining strict pipefail behavior
1 parent 2538072 commit 5af55f7

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

scripts/clean-assets.sh

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ clean_directory() {
130130

131131
# Count files before cleanup
132132
local total_before
133-
total_before=$(find "$target_dir" -type f 2>/dev/null | wc -l | tr -d ' ')
133+
total_before=$(find "$target_dir" -type f 2>/dev/null | wc -l | tr -d ' ') || total_before=0
134134

135135
# Find and remove non-documentation files using configurable include/exclude lists
136136
local removed_count=0
@@ -143,9 +143,12 @@ clean_directory() {
143143
fi
144144

145145
# Remove the file
146-
rm -f "$file"
147-
((removed_count++))
148-
log_info "Removed: ${file#"$target_dir"/}"
146+
if rm -f "$file" 2>/dev/null; then
147+
((removed_count++))
148+
log_info "Removed: ${file#"$target_dir"/}"
149+
else
150+
log_warning "Failed to remove: ${file#"$target_dir"/}"
151+
fi
149152
done < <(find "$target_dir" -type f -print0 2>/dev/null)
150153

151154
# Remove empty directories (but keep the main structure)
@@ -154,11 +157,22 @@ clean_directory() {
154157
local kept_count
155158
kept_count=$((total_before - removed_count))
156159

160+
# Calculate percentage safely
161+
local percentage="N/A"
162+
if [[ $total_before -gt 0 ]]; then
163+
if command -v bc >/dev/null 2>&1; then
164+
percentage=$(echo "scale=1; $removed_count * 100 / $total_before" | bc -l 2>/dev/null || echo "N/A")
165+
else
166+
# Fallback calculation without bc (using integer arithmetic)
167+
percentage=$(( (removed_count * 100) / total_before )) 2>/dev/null || percentage="N/A"
168+
fi
169+
fi
170+
157171
log_success "Cleanup complete for $dir_name:"
158172
log_success " Files before: $total_before"
159173
log_success " Files removed: $removed_count"
160174
log_success " Files kept: $kept_count"
161-
log_success " Space saved: $(echo "scale=1; $removed_count * 100 / $total_before" | bc -l 2>/dev/null || echo "N/A")%"
175+
log_success " Space saved: ${percentage}%"
162176
}
163177

164178
# Main execution

0 commit comments

Comments
 (0)