A simple script to cleanup node_modules from a development directory. If no development directory is passed it will default to the current directory with a depth limit of 15.

#!/bin/bash

DEVELOPMENT_DIRECTORY=${1:-.}
MAX_DEPTH=${2:-15}

deleted_sizes=()

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'

get_dir_size() {
    du -sh "$1" 2>/dev/null | awk '{print $1}'
}

get_dir_size_kb() {
    du -sk "$1" 2>/dev/null | awk '{print $1}'
}

traverse_directories() {
    local dir=$1
    local depth=$2
    local package_found=false

    if [ "$depth" -gt "$MAX_DEPTH" ]; then
        printf "${WHITE}Max depth            %-50s Depth: %d${NC}\n" "$dir" "$depth"
        return
    fi

    # Check if package.json exists in the current directory
    if [ -f "$dir/package.json" ]; then
        package_found=true

        if [ -d "$dir/node_modules" ]; then
            size=$(get_dir_size "$dir/node_modules")
            size_kb=$(get_dir_size_kb "$dir/node_modules")
            rm -rf "$dir/node_modules"
            deleted_sizes+=("$size_kb")

            printf "${RED}Deleted node_modules  %-50s %-10s${NC}\n" "$dir" "$size"
            return
        else
            printf "${YELLOW}Found package.json   %-50s${NC}\n" "$dir"
            return
        fi
    fi

    if ! $package_found; then
        printf "${WHITE}Checked              %-50s ${NC}\n" "$dir"
    fi

    for subdir in "$dir"/*/; do
        subdir=${subdir%/}
        [ -d "$subdir" ] && traverse_directories "$subdir" $((depth + 1))
    done
}

traverse_directories "$DEVELOPMENT_DIRECTORY" 1

total_size_kb=0

for size_kb in "${deleted_sizes[@]}"; do
    total_size_kb=$((total_size_kb + size_kb))
done

total_size_human=$(echo "$total_size_kb" | awk '{ byte =$1 /1024/1024; print byte "G" }')

echo -e "${GREEN}Total size of deleted node_modules: $total_size_human${NC}"s