From 5cc1ae7488bfe97856a5500391a468db5d29c77c Mon Sep 17 00:00:00 2001 From: Benjamin Lupton Date: Wed, 27 Nov 2024 20:10:42 +0800 Subject: [PATCH] fs-trim: split junk into cache and module --- commands.deprecated/rm-junk | 4 +- commands.deprecated/rm-modules | 4 +- commands/fs-trim | 95 ++++++++++++++++++++++++---------- 3 files changed, 72 insertions(+), 31 deletions(-) diff --git a/commands.deprecated/rm-junk b/commands.deprecated/rm-junk index 727965a80..f86711f8e 100755 --- a/commands.deprecated/rm-junk +++ b/commands.deprecated/rm-junk @@ -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 @@ -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 ) diff --git a/commands.deprecated/rm-modules b/commands.deprecated/rm-modules index bd29593e5..71e91173e 100755 --- a/commands.deprecated/rm-modules +++ b/commands.deprecated/rm-modules @@ -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 @@ -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 ) diff --git a/commands/fs-trim b/commands/fs-trim index e83553690..81b1c605d 100755 --- a/commands/fs-trim +++ b/commands/fs-trim @@ -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 @@ -115,8 +110,10 @@ function fs_trim() ( --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 @@ -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 @@ -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")" @@ -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') @@ -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' @@ -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' ;; @@ -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 @@ -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 @@ -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 @@ -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