feat: implement /core:clean command (#81)

This commit introduces a new /core:clean command to clean up generated files, caches, and build artifacts.

The command provides the following options:
- /core:clean: Cleans all caches and build artifacts.
- /core:clean --cache: Cleans caches only.
- /core:clean --deps: Performs a dry-run for dependency cleanup.
- /core:clean --deps --force: Deletes dependency directories after a confirmation prompt.
- /core:clean --dry-run: Shows what would be deleted without performing any actions.

The implementation includes safety features such as a "dry-run by default" for dependency cleaning and a confirmation prompt for destructive operations.
This commit is contained in:
Snider 2026-02-02 07:18:29 +00:00 committed by GitHub
parent 0f15a1fdfd
commit 6b850fb3f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 181 additions and 0 deletions

View file

@ -6,6 +6,12 @@
"email": "hello@host.uk.com" "email": "hello@host.uk.com"
}, },
"plugins": [ "plugins": [
{
"name": "core",
"source": "./claude/core",
"description": "Core commands for project maintenance.",
"version": "0.1.0"
},
{ {
"name": "code", "name": "code",
"source": "./claude/code", "source": "./claude/code",

View file

@ -0,0 +1,25 @@
---
name: clean
description: Clean up generated files, caches, and build artifacts
args: "[--cache] [--deps [--force]] [--dry-run]"
---
# Clean Project
Cleans up generated files, caches, and build artifacts for the project.
## Usage
- `/core:clean` - Clean all caches and build artifacts.
- `/core:clean --cache` - Clean caches only.
- `/core:clean --deps` - Dry-run dependency cleanup.
- `/core:clean --deps --force` - **Permanently delete** dependencies (`vendor`, `node_modules`).
- `/core:clean --dry-run` - Show what would be deleted without actually deleting anything.
## Action
This command executes the `clean.sh` script to perform the cleanup.
```bash
"${CLAUDE_PLUGIN_ROOT}/scripts/clean.sh" "$@"
```

150
claude/core/scripts/clean.sh Executable file
View file

@ -0,0 +1,150 @@
#!/bin/bash
# core:clean script
# Cleans generated files, caches, and build artifacts.
# --- Configuration ---
CACHE_PATHS=(
"storage/framework/cache"
"bootstrap/cache"
".phpunit.cache"
)
BUILD_PATHS=(
"public/build"
"public/hot"
)
DEP_PATHS=(
"vendor"
"node_modules"
)
# --- Argument Parsing ---
CLEAN_CACHE=false
CLEAN_BUILD=false
CLEAN_DEPS=false
FORCE=false
DRY_RUN=false
ACTION_SPECIFIED=false
while [[ "$#" -gt 0 ]]; do
case $1 in
--cache) CLEAN_CACHE=true; ACTION_SPECIFIED=true; shift ;;
--deps) CLEAN_DEPS=true; ACTION_SPECIFIED=true; shift ;;
--force) FORCE=true; shift ;;
--dry-run) DRY_RUN=true; shift ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
done
if [ "$ACTION_SPECIFIED" = false ]; then
CLEAN_CACHE=true
CLEAN_BUILD=true
fi
if [ "$DRY_RUN" = true ]; then
FORCE=false
fi
# --- Functions ---
get_size() {
du -sb "$1" 2>/dev/null | cut -f1
}
format_size() {
local size=$1
if [ -z "$size" ] || [ "$size" -eq 0 ]; then
echo "0 B"
return
fi
if (( size < 1024 )); then
echo "${size} B"
elif (( size < 1048576 )); then
echo "$((size / 1024)) KB"
else
echo "$((size / 1048576)) MB"
fi
}
# --- Main Logic ---
TOTAL_FREED=0
echo "Cleaning core-tenant..."
echo
# Cache cleanup
if [ "$CLEAN_CACHE" = true ]; then
echo "Cache:"
for path in "${CACHE_PATHS[@]}"; do
if [ -e "$path" ]; then
SIZE=$(get_size "$path")
if [ "$DRY_RUN" = true ]; then
echo " - $path ($(format_size "$SIZE")) would be cleared"
else
rm -rf "${path:?}"/*
echo "$path cleared"
TOTAL_FREED=$((TOTAL_FREED + SIZE))
fi
fi
done
echo
fi
# Build cleanup
if [ "$CLEAN_BUILD" = true ]; then
echo "Build:"
for path in "${BUILD_PATHS[@]}"; do
if [ -e "$path" ]; then
SIZE=$(get_size "$path")
if [ "$DRY_RUN" = true ]; then
echo " - $path ($(format_size "$SIZE")) would be deleted"
else
rm -rf "$path"
echo "$path deleted"
TOTAL_FREED=$((TOTAL_FREED + SIZE))
fi
fi
done
echo
fi
# Dependency cleanup
if [ "$CLEAN_DEPS" = true ]; then
DEPS_SIZE=0
DEPS_TO_DELETE=()
for path in "${DEP_PATHS[@]}"; do
if [ -d "$path" ]; then
DEPS_TO_DELETE+=("$path")
SIZE=$(get_size "$path")
DEPS_SIZE=$((DEPS_SIZE + SIZE))
fi
done
echo "Dependencies:"
if [ ${#DEPS_TO_DELETE[@]} -eq 0 ]; then
echo " No dependency directories found."
elif [ "$FORCE" = false ] || [ "$DRY_RUN" = true ]; then
echo "This is a dry-run. Use --deps --force to delete."
for path in "${DEPS_TO_DELETE[@]}"; do
echo " - $path ($(format_size "$(get_size "$path")")) would be deleted"
done
else
echo "The following directories will be permanently deleted:"
for path in "${DEPS_TO_DELETE[@]}"; do
echo " - $path ($(format_size "$(get_size "$path")"))"
done
echo
read -p "Are you sure? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
for path in "${DEPS_TO_DELETE[@]}"; do
rm -rf "$path"
echo "$path deleted."
done
TOTAL_FREED=$((TOTAL_FREED + DEPS_SIZE))
else
echo "Aborted by user."
fi
fi
echo
fi
echo "Total freed: $(format_size "$TOTAL_FREED")"