Skip to content

Instantly share code, notes, and snippets.

@binsarjr
Last active January 21, 2025 15:08
Show Gist options
  • Save binsarjr/25bf4663f9c6814266aa1d27bac1efd1 to your computer and use it in GitHub Desktop.
Save binsarjr/25bf4663f9c6814266aa1d27bac1efd1 to your computer and use it in GitHub Desktop.
This program is a Bash script designed to efficiently clean up specific directories in a project, such as node_modules and vendor folders, while providing real-time feedback through a loading animation.

Program Description

The script automates the process of cleaning up development-related directories, improving workspace organization and freeing up disk space. It supports the following features:

  1. Directory Scanning:

    • Scans a specified directory (or the current directory if no argument is provided) for node_modules and vendor folders.
  2. Selective Deletion:

    • node_modules: Removes all node_modules directories recursively within the specified path.
    • vendor: Removes vendor directories only if a composer.json file exists in the same level, ensuring it's part of a valid PHP project.
  3. Interactive Feedback:

    • Displays an animated loading spinner during each deletion process, making the script visually engaging and informative.
  4. Error Handling:

    • Validates the existence of the specified directory and provides user-friendly error messages if issues are detected.
  5. Usage Help:

    • Includes a help option (-h or --help) that explains how to use the script.
  6. Cross-Platform Compatibility:

    • Compatible with Unix-based systems, including Linux and macOS.

Usage Instructions

  1. Save the script to a file, e.g., cleanup.sh.

  2. Grant execution permissions:

    chmod +x cleanup.sh
  3. Execute the script, providing a directory path (optional):

    ./cleanup.sh /path/to/project

    If no path is specified, the current directory will be scanned.

  4. For help, run:

    ./cleanup.sh -h
#!/bin/bash
# Fungsi untuk mencetak pesan bantuan
function usage() {
echo "Usage: $0 [directory]"
echo " directory: directory to scan for node_modules, vendor, and __pycache__ folders."
}
# Memastikan argumen directory diisi
if [[ -z $1 ]]; then
echo "Error: Directory is required."
usage
exit 1
fi
# Memeriksa apakah pengguna meminta bantuan
if [[ $1 == "-h" || $1 == "--help" ]]; then
usage
exit 0
fi
# Menggunakan argumen sebagai direktori yang akan dipindai
SCAN_DIR=$1
# Memastikan direktori yang diberikan valid
if [[ ! -d "$SCAN_DIR" ]]; then
echo "Error: Directory '$SCAN_DIR' does not exist."
exit 1
fi
# Menampilkan direktori yang akan dipindai
echo "Scanning directory: $SCAN_DIR"
# Fungsi untuk menampilkan animasi loading
function loading_animation() {
local pid=$1
local delay=0.1
local spinner=("|" "/" "-" "\\")
while kill -0 $pid 2>/dev/null; do
for i in "${spinner[@]}"; do
echo -ne "\rProcessing $i"
sleep $delay
done
done
echo -ne "\r"
}
# Mencari dan menghapus semua folder node_modules
find "$SCAN_DIR" -type d -name "node_modules" -prune -print | while read -r NODE_MODULES_DIR; do
echo "Deleting node_modules folder: $NODE_MODULES_DIR"
(rm -rf "$NODE_MODULES_DIR" & loading_animation $!)
echo "Deleted: $NODE_MODULES_DIR"
done
# Mencari dan menghapus semua folder vendor dengan syarat ada composer.json di level yang sama
find "$SCAN_DIR" -type d -name "vendor" -prune | while read -r VENDOR_DIR; do
COMPOSER_FILE=$(dirname "$VENDOR_DIR")/composer.json
if [[ -f "$COMPOSER_FILE" ]]; then
echo "Deleting vendor folder: $VENDOR_DIR"
(rm -rf "$VENDOR_DIR" & loading_animation $!)
echo "Deleted: $VENDOR_DIR"
else
echo "Skipping vendor folder: $VENDOR_DIR (no composer.json found)"
fi
done
# Mencari dan menghapus semua folder __pycache__
find "$SCAN_DIR" -type d -name "__pycache__" -prune -print | while read -r PYCACHE_DIR; do
echo "Deleting __pycache__ folder: $PYCACHE_DIR"
(rm -rf "$PYCACHE_DIR" & loading_animation $!)
echo "Deleted: $PYCACHE_DIR"
done
# Konfirmasi selesai
echo "Cleanup complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment