Skip to content

Improvements for local gem swapper utility #37

@justin808

Description

@justin808

Local Gem Swapper Improvements

Based on code review, the following improvements should be implemented in future PRs:

High Priority

1. Additional Test Coverage

File: spec/demo_scripts/local_gems_cli_spec.rb (NEW)

Add tests for LocalGemsCLI class covering:

  • Option parsing (--github, --branch, --demo, etc.)
  • Context detection (running from demo directory)
  • Config file application
  • Error handling

2. Watch Mode Process Management

File: lib/demo_scripts/gem_swapper.rb:286

Issue: system('npm run watch &') backgrounds processes without tracking. Multiple invocations could leave orphaned processes.

Solution:

  • Use spawn with explicit PID tracking
  • Store PIDs in ~/.cache/local-gems/watch_pids.json
  • Add bin/use-local-gems --kill-watch command to cleanup
  • Add bin/use-local-gems --list-watch to show running processes
  • Warn users about orphaned processes during --restore

Medium Priority

3. Improved Error Handling for npm install

File: lib/demo_scripts/gem_swapper.rb:248

Issue: system('npm install --silent 2>/dev/null') discards stderr, making troubleshooting difficult.

Solution:

  • Use Open3.capture3 to capture stdout/stderr
  • Display stderr only on failure
  • Optionally add --strict mode that exits on install failures

Low Priority

4. Preserve JSON Formatting

File: lib/demo_scripts/gem_swapper.rb:178

Issue: JSON.pretty_generate may not match original package.json formatting.

Solution:

  • Detect original indentation (2 spaces vs 4 spaces vs tabs)
  • Preserve trailing newline

5. Enhanced Path Validation

File: lib/demo_scripts/gem_swapper.rb:79

Add explicit validation that expanded paths don't escape expected directories.

6. Git Hook Safety

File: lefthook.yml:46

Issue: Git diff commands could fail if remote branch doesn't exist.

Solution: Add existence checks for remote branches.

Implementation Priority

  1. Watch mode process management - Prevents orphaned processes
  2. npm install error handling - Improves debugging experience
  3. Additional tests - Ensures reliability
  4. JSON formatting - Nice-to-have for clean commits
  5. Path validation - Defense in depth
  6. Git hook safety - Edge case handling

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions