Skip to content

Commit

Permalink
fs-trim: split junk into cache and module
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Nov 27, 2024
1 parent e0cd9a0 commit 839a82c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 31 deletions.
4 changes: 2 additions & 2 deletions commands.deprecated/rm-junk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

function rm_junk() (
source "$DOROTHY/sources/bash.bash"
dorothy-warnings add --code='rm-junk' --bold=' has been deprecated in favor of ' --code='fs-trim --junk'
dorothy-warnings add --code='rm-junk' --bold=' has been deprecated in favor of ' --code='fs-trim --cache'

# =====================================
# Arguments
Expand Down Expand Up @@ -58,7 +58,7 @@ function rm_junk() (
# =====================================
# Action

fs-trim --junk --empty-directories="$option_empty" -- "$path"
fs-trim --cache --empty-directories="$option_empty" -- "$path"
return
)

Expand Down
4 changes: 2 additions & 2 deletions commands.deprecated/rm-modules
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function rm_modules() (
source "$DOROTHY/sources/bash.bash"
__require_globstar
dorothy-warnings add --code='rm-modules' --bold=' has been deprecated in favor of ' --code='fs-trim --junk'
dorothy-warnings add --code='rm-modules' --bold=' has been deprecated in favor of ' --code='fs-trim --module'

# =====================================
# Arguments
Expand Down Expand Up @@ -53,7 +53,7 @@ function rm_modules() (
# =====================================
# Action

fs-trim --junk --empty-directories="$option_empty" -- "$path"
fs-trim --module --empty-directories="$option_empty" -- "$path"
return
)

Expand Down
95 changes: 68 additions & 27 deletions commands/fs-trim
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,20 @@ function fs_trim_test() (
)
function fs_trim() (
source "$DOROTHY/sources/bash.bash"
local junk_filenames=(
local cache_filenames=(
'.DS_Store'
'._.DS_Store'
'Desktop.ini'
'Thumbs.db'
'.log'
)
local module_filenames=(
'node_modules'
'pnp'
'package-lock.json'
'yarn.lock'
'.pnp.js'
'.log'
)
local junk_find=() remove_filenames=() item
for item in "${junk_filenames[@]}"; do
item="$(__lowercase_string -- "$item")"
remove_filenames+=("$item")
junk_find+=(-iname "$item" -or)
done
junk_find=("${junk_find[@]:0:${#junk_find[@]}-1}")

# =====================================
# Arguments
Expand All @@ -115,8 +110,10 @@ function fs_trim() (
--group=<group>
If specified use this user and/or group for filesystem interactions.
--junk
If provided, paths of these case-insensitive filenames will be removed: ${junk_filenames[*]}
--cache
If provided, paths of these case-insensitive filenames will be removed: ${cache_filenames[*]}
--module
If provided, paths of these case-insensitive filenames will be removed: ${module_filenames[*]}
--empty-files
If provided, empty files will be removed.
--broken-symlinks
Expand All @@ -135,7 +132,7 @@ function fs_trim() (
}

# process
local item option_inputs=() option_sudo='no' option_user='' option_group='' option_confirm='' option_all='' option_junk='' option_empty_files='' option_broken_symlinks='' option_empty_directories=''
local item option_inputs=() option_sudo='no' option_user='' option_group='' option_confirm='' option_all='' option_cache='' option_module='' option_empty_files='' option_broken_symlinks='' option_empty_directories=''
while [[ $# -ne 0 ]]; do
item="$1"
shift
Expand All @@ -152,14 +149,18 @@ function fs_trim() (
'--no-all'* | '--all'*)
option_all="$(get-flag-value --affirmative --fallback="$option_all" -- "$item")"
if [[ $option_all == 'yes' ]]; then
option_junk='yes'
option_cache='yes'
option_module='yes'
option_empty_files='yes'
option_broken_symlinks='yes'
option_empty_directories='yes'
fi
;;
'--no-junk'* | '--junk'*)
option_junk="$(get-flag-value --affirmative --fallback="$option_junk" -- "$item")"
'--no-cache'* | '--cache'*)
option_cache="$(get-flag-value --affirmative --fallback="$option_cache" -- "$item")"
;;
'--no-module'* | '--module'*)
option_module="$(get-flag-value --affirmative --fallback="$option_module" -- "$item")"
;;
'--no-empty-files'* | '--empty-files'*)
option_empty_files="$(get-flag-value --affirmative --fallback="$option_empty_files" -- "$item")"
Expand All @@ -185,12 +186,28 @@ function fs_trim() (
option_inputs+=("$(pwd)")
fi

# convert filenames to lowercase, and prepare find arguments
local cache_find=() module_find=()
for item in "${!cache_filenames[@]}"; do
cache_filenames[item]="$(__lowercase_string -- "${cache_filenames[item]}")"
cache_find+=(-iname "$item" -or)
done
for item in "${!module_filenames[@]}"; do
module_filenames[item]="$(__lowercase_string -- "${module_filenames[item]}")"
module_find+=(-iname "$item" -or)
done
cache_find=("${cache_find[@]:0:${#cache_find[@]}-1}")
module_find=("${module_find[@]:0:${#module_find[@]}-1}")

# =====================================
# Action

local selection=()
if [[ $option_junk == 'yes' ]]; then
selection+=('junk')
if [[ $option_cache == 'yes' ]]; then
selection+=('cache')
fi
if [[ $option_module == 'yes' ]]; then
selection+=('module')
fi
if [[ $option_empty_files == 'yes' ]]; then
selection+=('files')
Expand Down Expand Up @@ -227,7 +244,8 @@ function fs_trim() (
fi
mapfile -t selection < <(
choose "$title" "$body" --truncate-body --multiple --defaults-exact="$(__print_lines "${selection[@]}")" --label -- \
junk "Junk files: $(echo-style --newline --dim="${junk_filenames[*]}")" \
cache "Caches: $(echo-style --newline --dim="${cache_filenames[*]}")" \
module "Modules: $(echo-style --newline --dim="${module_filenames[*]}")" \
files 'Empty files' \
broken 'Broken symlinks' \
directories 'Empty directories'
Expand All @@ -236,7 +254,8 @@ function fs_trim() (
# still apply defaults in no-confirm mode
for item in "${selection[@]}"; do
case "$item" in
'junk') option_junk='yes' ;;
'cache') option_cache='yes' ;;
'module') option_module='yes' ;;
'files') option_empty_files='yes' ;;
'broken') option_broken_symlinks='yes' ;;
'directories') option_empty_directories='yes' ;;
Expand All @@ -253,7 +272,10 @@ function fs_trim() (
if is-not-directory --sudo="$option_sudo" --user="$option_user" --group="$option_group" -- "$input"; then
break
fi
if [[ $option_junk == 'yes' ]] && __wrap find "$path" \( "${junk_find[@]}" \) -delete -print | ifne -n false >/dev/null; then
if [[ $option_cache == 'yes' ]] && __wrap find "$path" \( "${cache_find[@]}" \) -delete -print | ifne -n false >/dev/null; then
refresh='yes'
fi
if [[ $option_module == 'yes' ]] && __wrap find "$path" \( "${module_find[@]}" \) -delete -print | ifne -n false >/dev/null; then
refresh='yes'
fi
if [[ $option_empty_files == 'yes' ]] && __wrap find "$path" -type f -empty -delete -print | ifne -n false >/dev/null; then
Expand Down Expand Up @@ -283,7 +305,7 @@ function fs_trim() (
# confirm the user for action if still ambiguous
__confirm
# check we have something to do
if [[ $option_junk != 'yes' && $option_empty_files != 'yes' && $option_broken_symlinks != 'yes' && $option_empty_directories != 'yes' ]]; then
if [[ $option_cache != 'yes' && $option_module != 'yes' && $option_empty_files != 'yes' && $option_broken_symlinks != 'yes' && $option_empty_directories != 'yes' ]]; then
# no-op, user is aborting essentially
return 0
fi
Expand All @@ -301,11 +323,21 @@ function fs_trim() (
# get the target
target="$(fs-realpath -- "$input")"

# check if it is a junk file
if [[ $option_junk == 'yes' ]]; then
# check if it is a cache file
if [[ $option_cache == 'yes' ]]; then
target_lowercase_filename="$(__lowercase_string -- "$(fs-filename -- "$target")")"
input_lowercase_filename="$(__lowercase_string -- "$(fs-filename -- "$input")")"
if is-needle --needle="$input_lowercase_filename" -- "${cache_filenames[@]}" || is-needle --needle="$target_lowercase_filename" -- "${cache_filenames[@]}"; then
__wrap rm -f "$target" "$input"
continue
fi
fi

# check if it is a module file
if [[ $option_module == 'yes' ]]; then
target_lowercase_filename="$(__lowercase_string -- "$(fs-filename -- "$target")")"
input_lowercase_filename="$(__lowercase_string -- "$(fs-filename -- "$input")")"
if is-needle --needle="$input_lowercase_filename" -- "${remove_filenames[@]}" || is-needle --needle="$target_lowercase_filename" -- "${remove_filenames[@]}"; then
if is-needle --needle="$input_lowercase_filename" -- "${module_filenames[@]}" || is-needle --needle="$target_lowercase_filename" -- "${module_filenames[@]}"; then
__wrap rm -f "$target" "$input"
continue
fi
Expand All @@ -323,10 +355,19 @@ function fs_trim() (
__wrap rm -f "$target" "$input"
fi
else
# check if it is a junk file
if [[ $option_junk == 'yes' ]]; then
# check if it is a cache file
if [[ $option_cache == 'yes' ]]; then
input_lowercase_filename="$(__lowercase_string -- "$(fs-filename -- "$input")")"
if is-needle --needle="$input_lowercase_filename" -- "${cache_filenames[@]}"; then
__wrap rm -f "$input"
continue
fi
fi

# check if it is a module file
if [[ $option_module == 'yes' ]]; then
input_lowercase_filename="$(__lowercase_string -- "$(fs-filename -- "$input")")"
if is-needle --needle="$input_lowercase_filename" -- "${remove_filenames[@]}"; then
if is-needle --needle="$input_lowercase_filename" -- "${module_filenames[@]}"; then
__wrap rm -f "$input"
continue
fi
Expand Down

0 comments on commit 839a82c

Please sign in to comment.