@@ -44,34 +44,76 @@ find_existing_sandbox() {
4444 return 1
4545}
4646
47+ # Function to detect if we're in a sandbox directory
48+ detect_sandbox_dir () {
49+ local current_dir=" $( pwd) "
50+ local abs_current_dir=" $( cd " $current_dir " && pwd) "
51+
52+ # Check if current directory is within .sandbox/
53+ if [[ " $abs_current_dir " == * " /.sandbox/" * ]]; then
54+ # Find the .sandbox base directory
55+ local sandbox_base=" ${abs_current_dir%/ .sandbox/* } /.sandbox"
56+
57+ # Check if .sandbox directory exists
58+ if [ ! -d " $sandbox_base " ]; then
59+ return 1
60+ fi
61+
62+ # Get the path after .sandbox/
63+ local relative_path=" ${abs_current_dir# ${sandbox_base} / } "
64+
65+ # Extract the first directory name (sandbox name)
66+ local sandbox_name=" ${relative_path%%/* } "
67+
68+ # Construct full sandbox path
69+ local sandbox_dir=" $sandbox_base /$sandbox_name "
70+
71+ # Verify it's actually a git repository (sandbox marker)
72+ if [ -d " $sandbox_dir /.git" ]; then
73+ echo " $sandbox_dir "
74+ return 0
75+ fi
76+ fi
77+ return 1
78+ }
79+
4780# Function to display usage
4881show_usage () {
4982 echo " Usage: $0 [OPTIONS]"
5083 echo " "
5184 echo " Options:"
52- echo " --reset Quick reset: reset git state without full recreation"
53- echo " --clean Remove sandbox directory entirely"
54- echo " --no-config Create sandbox without copying config (start from scratch)"
55- echo " --help Show this help message"
85+ echo " --reset Quick reset: reset git state without full recreation"
86+ echo " --preserve-config Preserve .labcommitr.config.yaml during reset (use with --reset)"
87+ echo " --clean Remove sandbox directory entirely"
88+ echo " --no-config Create sandbox without copying config (start from scratch)"
89+ echo " --help Show this help message"
5690 echo " "
5791 echo " Examples:"
58- echo " $0 # Create or recreate sandbox (with config if available)"
59- echo " $0 --no-config # Create sandbox without config (run 'lab init' yourself)"
60- echo " $0 --reset # Quick reset (faster, keeps repo structure)"
61- echo " $0 --clean # Remove sandbox completely"
92+ echo " $0 # Create or recreate sandbox (with config if available)"
93+ echo " $0 --no-config # Create sandbox without config (run 'lab init' yourself)"
94+ echo " $0 --reset # Quick reset (faster, keeps repo structure)"
95+ echo " $0 --reset --preserve-config # Quick reset, preserve config file"
96+ echo " $0 --clean # Remove sandbox completely"
97+ echo " "
98+ echo " Note: Can be run from within sandbox directory or project root"
6299}
63100
64101# Parse arguments
65102RESET_MODE=false
66103CLEAN_MODE=false
67104NO_CONFIG=false
105+ PRESERVE_CONFIG=false
68106
69107while [[ $# -gt 0 ]]; do
70108 case $1 in
71109 --reset)
72110 RESET_MODE=true
73111 shift
74112 ;;
113+ --preserve-config)
114+ PRESERVE_CONFIG=true
115+ shift
116+ ;;
75117 --clean)
76118 CLEAN_MODE=true
77119 shift
112154
113155# Handle reset mode
114156if [ " $RESET_MODE " = true ]; then
115- SANDBOX_DIR=$( find_existing_sandbox)
116- if [ -z " $SANDBOX_DIR " ]; then
157+ # Try to detect if we're in a sandbox directory first
158+ DETECTED_SANDBOX=$( detect_sandbox_dir)
159+ if [ -n " $DETECTED_SANDBOX " ] && [ -d " $DETECTED_SANDBOX " ]; then
160+ SANDBOX_DIR=" $DETECTED_SANDBOX "
161+ else
162+ # Fall back to finding existing sandbox from project root
163+ SANDBOX_DIR=$( find_existing_sandbox)
164+ fi
165+
166+ if [ -z " $SANDBOX_DIR " ] || [ ! -d " $SANDBOX_DIR " ]; then
117167 echo -e " ${RED} Error: No existing sandbox found. Run without --reset to create one.${NC} "
118168 exit 1
119169 fi
@@ -125,10 +175,25 @@ if [ "$RESET_MODE" = true ]; then
125175 echo -e " ${GREEN} ✓${NC} Resetting sandbox: $SANDBOX_DIR "
126176 cd " $SANDBOX_DIR "
127177
178+ # Preserve config if requested
179+ CONFIG_BACKUP=" "
180+ if [ " $PRESERVE_CONFIG " = true ] && [ -f " .labcommitr.config.yaml" ]; then
181+ CONFIG_BACKUP=$( mktemp)
182+ cp " .labcommitr.config.yaml" " $CONFIG_BACKUP "
183+ echo -e " ${GREEN} ✓${NC} Preserving config file..."
184+ fi
185+
128186 # Reset git state
129187 git reset --hard HEAD 2> /dev/null || true
130188 git clean -fd 2> /dev/null || true
131189
190+ # Restore config if preserved
191+ if [ -n " $CONFIG_BACKUP " ] && [ -f " $CONFIG_BACKUP " ]; then
192+ cp " $CONFIG_BACKUP " " .labcommitr.config.yaml"
193+ rm " $CONFIG_BACKUP "
194+ echo -e " ${GREEN} ✓${NC} Config file restored"
195+ fi
196+
132197 # Ensure directories exist (they might have been removed by git clean)
133198 mkdir -p src docs lib utils
134199
201266 echo " # Pre-staged file" > pre-staged.ts
202267 git add pre-staged.ts
203268
269+ # Always ensure reset script is present and up-to-date
270+ if [ -f " $SCRIPT_DIR /reset-sandbox.sh" ]; then
271+ cp " $SCRIPT_DIR /reset-sandbox.sh" " reset-sandbox.sh"
272+ chmod +x " reset-sandbox.sh"
273+ echo -e " ${GREEN} ✓${NC} Reset script updated"
274+ fi
275+
204276 echo " "
205277 echo -e " ${GREEN} ✓${NC} Sandbox reset complete!"
206278 echo " "
210282 echo " cd $SANDBOX_DIR "
211283 echo " node $PROJECT_ROOT /dist/index.js commit"
212284 echo " "
285+ echo -e " ${YELLOW} To reset again (from within sandbox):${NC} "
286+ echo " bash $SCRIPT_DIR /labcommitr-sandbox.sh --reset"
287+ echo " bash $SCRIPT_DIR /labcommitr-sandbox.sh --reset --preserve-config"
288+ echo " "
213289 echo -e " ${BLUE} ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC} "
214290 exit 0
215291fi
@@ -254,6 +330,12 @@ else
254330 echo -e " ${YELLOW} ⚠ Sandbox created without config. Run 'lab init' to set up configuration.${NC} "
255331fi
256332
333+ # Copy reset script for convenience
334+ if [ -f " $SCRIPT_DIR /reset-sandbox.sh" ]; then
335+ cp " $SCRIPT_DIR /reset-sandbox.sh" " $SANDBOX_DIR /reset-sandbox.sh"
336+ chmod +x " $SANDBOX_DIR /reset-sandbox.sh"
337+ fi
338+
257339# Create initial commit
258340echo -e " ${GREEN} ✓${NC} Creating initial commit structure..."
259341cat > README.md << 'EOF '
@@ -457,14 +539,19 @@ echo -e "${YELLOW}To test:${NC}"
457539echo " cd $SANDBOX_DIR "
458540echo " node $PROJECT_ROOT /dist/index.js commit"
459541echo " "
460- echo -e " ${YELLOW} To reset (quick):${NC} "
461- echo " bash $SCRIPT_DIR /labcommitr-sandbox.sh --reset"
542+ echo -e " ${YELLOW} To reset (from within sandbox):${NC} "
543+ echo " bash reset-sandbox.sh # Reset, remove config"
544+ echo " bash reset-sandbox.sh --preserve-config # Reset, keep config"
545+ echo " "
546+ echo -e " ${YELLOW} To reset (from project root):${NC} "
547+ echo " pnpm run test:sandbox:reset # Reset, remove config"
548+ echo " bash $SCRIPT_DIR /labcommitr-sandbox.sh --reset --preserve-config # Reset, keep config"
462549echo " "
463550echo -e " ${YELLOW} To reset (full recreation):${NC} "
464- echo " bash $SCRIPT_DIR /labcommitr- sandbox.sh "
551+ echo " pnpm run test: sandbox"
465552echo " "
466553echo -e " ${YELLOW} To clean up:${NC} "
467- echo " bash $SCRIPT_DIR /labcommitr-sandbox.sh -- clean"
554+ echo " pnpm run test:sandbox: clean"
468555echo " "
469556echo -e " ${BLUE} ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC} "
470557
0 commit comments