@@ -10,6 +10,54 @@ source "${SCRIPT_DIR}/api_utils.sh"
1010# Default paths
1111DEPENDENCIES_DIR=" /usr/local/unraid-api/node_modules"
1212
13+ # Function to cleanup old dependency archives
14+ # Removes all node_modules archives except the one for the current API version
15+ cleanup () {
16+ local info
17+ local api_version=" "
18+ local vendor_store_path=" "
19+ local vendor_dir=" "
20+ local current_archive=" "
21+
22+ # Get archive information
23+ if ! mapfile -t info < <( get_archive_information) ; then
24+ echo " Error: Failed to get vendor archive information. Cannot proceed with cleanup." >&2
25+ return 1
26+ fi
27+
28+ api_version=" ${info[0]} "
29+ vendor_store_path=" ${info[2]} "
30+
31+ echo " Cleaning up node_modules archives that don't match current API version: $api_version "
32+
33+ # Extract the directory path from the full vendor_store_path
34+ vendor_dir=" $( dirname " $vendor_store_path " ) "
35+
36+ # Extract the filename from the full vendor_store_path - this is our current archive
37+ current_archive=" $( basename " $vendor_store_path " ) "
38+
39+ # Check if vendor directory exists
40+ if [ ! -d " $vendor_dir " ]; then
41+ echo " Vendor directory $vendor_dir does not exist. Nothing to clean up."
42+ return 0
43+ fi
44+
45+ echo " Current archive to keep: $current_archive "
46+
47+ # Find and remove all node_modules archives except the current one
48+ find " $vendor_dir " -name " node_modules-for-*.tar.xz" | while read -r archive; do
49+ if [ " $( basename " $archive " ) " != " $current_archive " ]; then
50+ echo " Removing archive: $archive "
51+ rm -f " $archive "
52+ else
53+ echo " Keeping current archive: $archive "
54+ fi
55+ done
56+
57+ echo " Cleanup completed."
58+ return 0
59+ }
60+
1361# Function to attempt redownload of vendor archive if missing
1462# Args:
1563# $1 - Path to vendor archive to download (ignored, kept for backward compatibility)
@@ -228,6 +276,10 @@ case "$1" in
228276 ensure " $2 "
229277 exit $?
230278 ;;
279+ ' cleanup' )
280+ cleanup
281+ exit $?
282+ ;;
231283 ' redownload' )
232284 # The path argument is ignored but kept for backward compatibility
233285 if downloaded_archive=$( redownload_vendor_archive) && [ -n " $downloaded_archive " ]; then
@@ -239,7 +291,7 @@ case "$1" in
239291 fi
240292 ;;
241293 * )
242- echo " Usage: $0 {restore|archive|ensure|redownload}"
294+ echo " Usage: $0 {restore|archive|ensure|cleanup| redownload}"
243295 exit 1
244296 ;;
245297esac
0 commit comments