Created
December 13, 2023 17:50
-
-
Save a-c-m/403e77875e396fd9fa561d47a338b19c to your computer and use it in GitHub Desktop.
A nicer rm all node modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Define the directory from where to start searching for node_modules directories | |
START_DIR="." | |
# Find all node_modules directories and count them | |
NODE_MODULES_DIRS=$(find $START_DIR -name 'node_modules' -type d -prune) | |
TOTAL_DIRS=$(echo "$NODE_MODULES_DIRS" | wc -l) | |
# Check if there are any directories to remove | |
if [ $TOTAL_DIRS -eq 0 ]; then | |
echo "No node_modules directories to remove." | |
exit 0 | |
fi | |
printf "Total node_modules directories to be removed: %d\n" $TOTAL_DIRS | |
# Define the size of the progress bar | |
BAR_SIZE=20 | |
# Initialize the counter for the deleted directories | |
DELETED_DIRS=0 | |
# Define the spinner | |
spin='-\|/' | |
# Find the longest directory name | |
LONGEST_DIR_NAME=$(echo "$NODE_MODULES_DIRS" | awk '{ print length, $0 }' | sort -n -s -k1,1 | cut -d" " -f2 | tail -1) | |
# Loop through each directory | |
for DIR in $NODE_MODULES_DIRS | |
do | |
# Increase the counter for the deleted directories | |
DELETED_DIRS=$((DELETED_DIRS + 1)) | |
# Print the number of directories deleted | |
printf "[%02d / %02d] " $DELETED_DIRS $TOTAL_DIRS | |
# Print the progress bar | |
PROGRESS=$((DELETED_DIRS * 100 / TOTAL_DIRS)) | |
BAR_PROGRESS=$((PROGRESS * BAR_SIZE / 100)) | |
printf "Progress: [%-${BAR_SIZE}s] %02d%%" $(printf "%${BAR_PROGRESS}s" | tr ' ' '#') $PROGRESS | |
# Print the directory before removing it | |
printf " -- Removing: $DIR " | |
# Start the removal process in the background | |
rm -rf $DIR & | |
RM_PID=$! | |
# Start the spinner | |
i=0 | |
while kill -0 $RM_PID 2>/dev/null | |
do | |
i=$(( (i+1) %4 )) | |
printf "\b${spin:$i:1} " | |
printf "\b" | |
sleep .1 | |
done | |
printf "\n" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment