#!/usr/bin/env bash # program info version="1.0.2" homepage="https://github.com/simbo/rm-node_modules" license="MIT © 2021 Simon Lepel " # formatting vars term=${TERM:-"dumb"} red=$(tput -T$term setaf 9) green=$(tput -T$term setaf 10) yellow=$(tput -T$term setaf 11) blue=$(tput -T$term setaf 12) gray=$(tput -T$term setaf 8) white=$(tput -T$term setaf 15) bold=$(tput -T$term bold) underlined=$(tput -T$term smul) normal=$(tput -T$term sgr0) # welcome message function welcome() { printf "${white}${bold}rm-node_modules${normal} $version\n${gray}${underlined}$homepage${normal}\n${gray}$license${normal}\n" } # help message function usage() { welcome printf "\nUsage: $(basename "$0") [-yuv] [-d | -c ]\n\n" printf "\"Search for node_modules directories within given path and delete them.\"\n\n" printf "Options:\n" printf " -d directory to search within\n" printf " (default: current directory or configured default directory)\n" printf " -y delete without confirmation\n" printf " -u determine disk usage; enable by default when using together with -c\n" printf " -c configure default directory to search within\n" printf " -v display version info only\n" exit 0 } # file to save default base dir configFile=$HOME/.rm-node_modules-defaults # default options configFileContents=$([ -f "$configFile" ] && cat $configFile || echo ".;false") yes=false ddu="null" dir="$(echo $configFileContents | cut -d ';' -f 1)" configDefaultDir="" # set options while getopts yuvc:d: opt 2>/dev/null do case $opt in y) yes=true;; u) ddu=true;; d) dir=$OPTARG;; v) printf "$version"; exit 0;; c) configDefaultDir=$OPTARG;; ?) usage;; esac done # print welcome message welcome # configure defaults if [[ $configDefaultDir != "" ]]; then cd "$configDefaultDir" configDefaultDir="$(pwd)" echo "$configDefaultDir;$([[ $ddu = true ]] && echo true || echo false)" > $configFile printf "\nDefault base directory is now set to ${white}${bold}$configDefaultDir${normal}.\n" printf "Determining disk usage is ${white}${bold}$([[ $ddu = true ]] && echo "ENABLED" || echo "DISABLED")${normal} by default.\n" exit 0 fi # set disk usage option depending on default config if [[ $ddu = "null" ]]; then ddu=$(echo $configFileContents | cut -d ';' -f 2) fi # change to base dir cd "$dir" baseDir="$(pwd)" # warn if baseDir is HOME if [[ $baseDir = $HOME ]]; then printf "\n${red}${bold}!!! WARNING !!!${normal}\nIt is not recommended to use your home directory as base directory for cleaning node_modules.\nYou may end up with broken applications.\n" if [[ $yes = true ]]; then printf "Auto-Confirmation is therefor disabled.\n" yes=false fi printf "${bold}Press CTRL-C to cancel.${normal}\n" fi # directory name to search for searchDir="node_modules" # searching directories printf "\nSearching for ${yellow}$searchDir${normal} directories within ${blue}$baseDir${normal}...\n" items="$(find . -name "$searchDir" -type d -prune)" # count found items if [[ $items = "" ]]; then itemsCount="0" else itemsCount="$(echo "$items" | wc -l | xargs)" fi # print search results printf "Found ${white}${bold}$itemsCount directories${normal}.\n" # exit if zero if [[ $itemsCount = 0 ]]; then printf "\n${gray}Nothing to do. Quitting...${normal}\n" exit 0 fi # if determining disk usage if [[ $ddu = true ]]; then # display info about determining disk usage printf "\nDetermining disk usage of $itemsCount ${yellow}$searchDir${normal} directories...\n" # get disk usage of directories diskUsage="$(echo "$items" | sed 's/./\\&/g' | xargs du -chs)" # get total value of disk usage diskUsageTotal="$(echo "$diskUsage" | tail -n1 | cut -d$'\t' -f 1 | xargs)" # display disk usage printf "\n$diskUsage\n" # if determining disk usage skipped else # display found directories printf "\n$items\n" fi # if confirmation not skipped if [[ $yes = false ]]; then # ask for action printf "\nPress ${red}${bold}[ENTER]${normal}${red} to delete${normal} the directories or any other key to exit.\n" # if determined disk usage if [[ $ddu = true ]]; then # display space to be freed printf "${gray}(${bold}$diskUsageTotal of data${normal}" # if determining disk usage skipped else # display number of found directories printf "${gray}(${bold}$itemsCount directories${normal}" fi # display deletion info printf "${gray} will be irreversibly deleted.)${normal}\n" # read input IFS= read -s -n 1 key # if enter key pressed if [[ $key = "" ]]; then yes=true fi fi # if deletion confirmed if [[ $yes = true ]]; then # display info about deletion printf "\nDeleting ${yellow}$searchDir${normal} directories within ${blue}$baseDir${normal}...\n" # delete all found directories echo "$items" | sed 's/./\\&/g' | xargs rm -rf -- # display deletion result printf "\nDeleted ${white}${bold}$itemsCount directories${normal}" # if determined disk usage if [[ $ddu = true ]]; then # display freed disk space printf " and freed ${green}${bold}$diskUsageTotal of disk space${normal}" fi printf ".\n" # if deletion not confirmed else # exit printf "\n${gray}Canceled.${normal}\n" fi