From fc6519cd28ebb870846b80c4a0c1843e40788ff8 Mon Sep 17 00:00:00 2001 From: Snider Date: Mon, 2 Feb 2026 07:31:20 +0000 Subject: [PATCH] feat(code): add /code:clean command for project cleanup (#100) Add cleanup command to remove generated files: - Clears cache directories (storage/framework/cache, bootstrap/cache) - Removes build artifacts (public/build, public/hot) - Optional --deps flag to remove vendor/node_modules - --cache flag for cache-only cleanup - --dry-run for safe preview Migrated from core-claude PR #45. Co-authored-by: Claude Opus 4.5 --- claude/code/commands/clean.md | 24 ++++++ claude/code/scripts/cleanup.sh | 135 +++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 claude/code/commands/clean.md create mode 100755 claude/code/scripts/cleanup.sh diff --git a/claude/code/commands/clean.md b/claude/code/commands/clean.md new file mode 100644 index 0000000..7385d30 --- /dev/null +++ b/claude/code/commands/clean.md @@ -0,0 +1,24 @@ +--- +name: clean +description: Clean up generated files, caches, and build artifacts. +args: "[--deps] [--cache] [--dry-run]" +--- + +# Clean Project + +This command cleans up generated files from the current project. + +## Usage + +``` +/code:clean # Clean all +/code:clean --deps # Remove vendor/node_modules +/code:clean --cache # Clear caches only +/code:clean --dry-run # Show what would be deleted +``` + +## Action + +```bash +"${CLAUDE_PLUGIN_ROOT}/scripts/cleanup.sh" "$@" +``` diff --git a/claude/code/scripts/cleanup.sh b/claude/code/scripts/cleanup.sh new file mode 100755 index 0000000..009ca15 --- /dev/null +++ b/claude/code/scripts/cleanup.sh @@ -0,0 +1,135 @@ +#!/bin/bash + +# Default options +CLEAN_DEPS=false +CLEAN_CACHE_ONLY=false +DRY_RUN=false + +# Parse arguments +for arg in "$@" +do + case $arg in + --deps) + CLEAN_DEPS=true + shift + ;; + --cache) + CLEAN_CACHE_ONLY=true + shift + ;; + --dry-run) + DRY_RUN=true + shift + ;; + esac +done + +# --- Configuration --- +CACHE_PATHS=( + "storage/framework/cache/*" + "bootstrap/cache/*" + ".phpunit.cache" +) + +BUILD_PATHS=( + "public/build/*" + "public/hot" +) + +DEP_PATHS=( + "vendor" + "node_modules" +) + +# --- Logic --- +total_freed=0 + +delete_path() { + local path_pattern=$1 + local size_bytes=0 + local size_human="" + + # Use a subshell to avoid affecting the main script's globbing settings + ( + shopt -s nullglob + local files=( $path_pattern ) + + if [ ${#files[@]} -eq 0 ]; then + return # No files matched the glob + fi + + # Calculate total size for all matched files + for file in "${files[@]}"; do + if [ -e "$file" ]; then + size_bytes=$((size_bytes + $(du -sb "$file" | cut -f1))) + fi + done + ) + + total_freed=$((total_freed + size_bytes)) + size_human=$(echo "$size_bytes" | awk '{ + if ($1 >= 1024*1024*1024) { printf "%.2f GB", $1/(1024*1024*1024) } + else if ($1 >= 1024*1024) { printf "%.2f MB", $1/(1024*1024) } + else if ($1 >= 1024) { printf "%.2f KB", $1/1024 } + else { printf "%d Bytes", $1 } + }') + + + if [ "$DRY_RUN" = true ]; then + echo " ✓ (dry run) $path_pattern ($size_human)" + else + # Suppress "no such file or directory" errors if glob doesn't match anything + rm -rf $path_pattern 2>/dev/null + echo " ✓ $path_pattern ($size_human)" + fi +} + + +echo "Cleaning project..." +echo "" + +if [ "$CLEAN_CACHE_ONLY" = true ]; then + echo "Cache:" + for path in "${CACHE_PATHS[@]}"; do + delete_path "$path" + done +else + echo "Cache:" + for path in "${CACHE_PATHS[@]}"; do + delete_path "$path" + done + echo "" + echo "Build:" + for path in "${BUILD_PATHS[@]}"; do + delete_path "$path" + done +fi + +if [ "$CLEAN_DEPS" = true ]; then + if [ "$DRY_RUN" = false ]; then + echo "" + read -p "Delete vendor/ and node_modules/? [y/N] " -n 1 -r + echo "" + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + echo "Aborted." + exit 1 + fi + fi + echo "" + echo "Dependencies (--deps):" + for path in "${DEP_PATHS[@]}"; do + delete_path "$path" + done +fi + +# Final summary +if [ "$total_freed" -gt 0 ]; then + total_freed_human=$(echo "$total_freed" | awk '{ + if ($1 >= 1024*1024*1024) { printf "%.2f GB", $1/(1024*1024*1024) } + else if ($1 >= 1024*1024) { printf "%.2f MB", $1/(1024*1024) } + else if ($1 >= 1024) { printf "%.2f KB", $1/1024 } + else { printf "%d Bytes", $1 } + }') + echo "" + echo "Total freed: $total_freed_human" +fi