dlx-claude/scripts/cleanup-plans.sh

157 lines
5.3 KiB
Bash
Executable File

#!/bin/bash
# cleanup-plans.sh - Archive old Claude plan files
#
# Claude Code creates plan files in ~/.claude/plans/ for each planning session.
# This script helps manage these files by archiving old plans to keep the directory clean.
#
# Usage:
# ./scripts/cleanup-plans.sh # List plans and show what would be archived
# ./scripts/cleanup-plans.sh --archive # Archive plans older than 30 days
# ./scripts/cleanup-plans.sh --archive-all # Archive all plans
# ./scripts/cleanup-plans.sh --days 60 # Change age threshold (default: 30)
set -e
PLANS_DIR="$HOME/.claude/plans"
ARCHIVE_DIR="$HOME/.claude/plans-archive"
DAYS_OLD=30
ACTION="list"
# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--archive)
ACTION="archive"
shift
;;
--archive-all)
ACTION="archive_all"
shift
;;
--days)
DAYS_OLD="$2"
shift 2
;;
*)
echo "Unknown option: $1"
echo "Usage: $0 [--archive] [--archive-all] [--days N]"
exit 1
;;
esac
done
echo -e "${BLUE}╔════════════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ Claude Plan Files Cleanup ║${NC}"
echo -e "${BLUE}╚════════════════════════════════════════════════════════════════╝${NC}"
echo
# Check if plans directory exists
if [ ! -d "$PLANS_DIR" ]; then
echo -e "${YELLOW}No plans directory found at: $PLANS_DIR${NC}"
exit 0
fi
# Count files
TOTAL_COUNT=$(find "$PLANS_DIR" -type f -name "*.md" | wc -l)
echo -e "Total plan files: ${GREEN}$TOTAL_COUNT${NC}"
if [ $TOTAL_COUNT -eq 0 ]; then
echo -e "${GREEN}No plan files to clean up.${NC}"
exit 0
fi
# Find old files
if [ "$ACTION" = "archive_all" ]; then
OLD_FILES=$(find "$PLANS_DIR" -type f -name "*.md")
OLD_COUNT=$TOTAL_COUNT
else
OLD_FILES=$(find "$PLANS_DIR" -type f -name "*.md" -mtime +$DAYS_OLD)
if [ -z "$OLD_FILES" ]; then
OLD_COUNT=0
else
OLD_COUNT=$(echo "$OLD_FILES" | wc -l)
fi
fi
RECENT_COUNT=$((TOTAL_COUNT - OLD_COUNT))
echo -e "Plans older than $DAYS_OLD days: ${YELLOW}$OLD_COUNT${NC}"
echo -e "Recent plans: ${GREEN}$RECENT_COUNT${NC}"
echo
# List mode
if [ "$ACTION" = "list" ]; then
echo -e "${BLUE}Plan Files by Age:${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
# List all files sorted by modification time
find "$PLANS_DIR" -type f -name "*.md" -printf "%T+ %p\n" | sort -r | while read -r line; do
timestamp=$(echo "$line" | cut -d' ' -f1 | cut -d'+' -f1)
filepath=$(echo "$line" | cut -d' ' -f2-)
filename=$(basename "$filepath")
# Calculate age in days
file_time=$(stat -c %Y "$filepath")
current_time=$(date +%s)
age_days=$(( (current_time - file_time) / 86400 ))
if [ $age_days -gt $DAYS_OLD ]; then
echo -e "${YELLOW}📄${NC} $timestamp $filename (${age_days}d old)"
else
echo -e "${GREEN}📄${NC} $timestamp $filename (${age_days}d old)"
fi
done
echo
echo -e "${YELLOW}To archive old plans, run:${NC}"
echo -e " ./scripts/cleanup-plans.sh --archive"
echo
echo -e "${YELLOW}To archive all plans, run:${NC}"
echo -e " ./scripts/cleanup-plans.sh --archive-all"
exit 0
fi
# Archive mode
if [ "$ACTION" = "archive" ] || [ "$ACTION" = "archive_all" ]; then
if [ $OLD_COUNT -eq 0 ]; then
echo -e "${GREEN}No old plan files to archive.${NC}"
exit 0
fi
# Create archive directory with timestamp
TIMESTAMP=$(date '+%Y-%m-%d')
ARCHIVE_PATH="$ARCHIVE_DIR/$TIMESTAMP"
mkdir -p "$ARCHIVE_PATH"
echo -e "${BLUE}Archiving $OLD_COUNT plan files to:${NC}"
echo -e " $ARCHIVE_PATH"
echo
# Move files
ARCHIVED=0
echo "$OLD_FILES" | while read -r filepath; do
if [ -f "$filepath" ]; then
filename=$(basename "$filepath")
mv "$filepath" "$ARCHIVE_PATH/"
echo -e "${GREEN}${NC} Archived: $filename"
ARCHIVED=$((ARCHIVED + 1))
fi
done
echo
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "Archived: $OLD_COUNT files"
echo -e "Archive location: $ARCHIVE_PATH"
echo -e "Remaining: $RECENT_COUNT files"
echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo
echo -e "${GREEN}Cleanup complete!${NC}"
fi