Skip to content

Commit eea39f1

Browse files
justin808claude
andcommitted
feat: refactor bin/dev and add kill command
- Extracted common logic into reusable helper methods - Added 'kill' command to terminate all dev processes for clean restart - Simplified control flow with case statement - Improved code maintainability and reduced duplication 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 0cba558 commit eea39f1

File tree

1 file changed

+106
-66
lines changed
  • lib/generators/react_on_rails/bin

1 file changed

+106
-66
lines changed

lib/generators/react_on_rails/bin/dev

Lines changed: 106 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,51 @@ def generate_packs
1919
exit 1
2020
end
2121

22-
def run_production_like
23-
puts "🏭 Starting production-like development server..."
24-
puts " - Generating React on Rails packs"
25-
puts " - Precompiling assets with production optimizations"
26-
puts " - Running Rails server on port 3001"
27-
puts " - No HMR (Hot Module Replacement)"
28-
puts " - CSS extracted to separate files (no FOUC)"
22+
def ensure_procfile(procfile)
23+
return if File.exist?(procfile)
24+
25+
warn <<~MSG
26+
ERROR:
27+
Please ensure `#{procfile}` exists in your project!
28+
MSG
29+
exit 1
30+
end
31+
32+
def run_with_process_manager(procfile)
33+
if installed?("overmind")
34+
system "overmind start -f #{procfile}"
35+
elsif installed?("foreman")
36+
system "foreman start -f #{procfile}"
37+
else
38+
warn <<~MSG
39+
NOTICE:
40+
For this script to run, you need either 'overmind' or 'foreman' installed on your machine. Please try this script after installing one of them.
41+
MSG
42+
exit 1
43+
end
44+
end
45+
46+
def print_server_info(title, features, port = 3000)
47+
puts "#{title}"
48+
features.each { |feature| puts " - #{feature}" }
2949
puts ""
30-
puts "💡 Access at: http://localhost:3001"
50+
puts "💡 Access at: http://localhost:#{port}"
3151
puts ""
52+
end
53+
54+
def run_production_like
55+
print_server_info(
56+
"🏭 Starting production-like development server...",
57+
[
58+
"Generating React on Rails packs",
59+
"Precompiling assets with production optimizations",
60+
"Running Rails server on port 3001",
61+
"No HMR (Hot Module Replacement)",
62+
"CSS extracted to separate files (no FOUC)"
63+
],
64+
3001
65+
)
3266

33-
# Generate React on Rails packs first
3467
generate_packs
3568

3669
# Precompile assets in production mode
@@ -54,62 +87,68 @@ def run_production_like
5487
end
5588

5689
def run_static_development
57-
puts "⚡ Starting development server with static assets..."
58-
puts " - Generating React on Rails packs"
59-
puts " - Using shakapacker --watch (no HMR)"
60-
puts " - CSS extracted to separate files (no FOUC)"
61-
puts " - Development environment (source maps, faster builds)"
62-
puts " - Auto-recompiles on file changes"
63-
puts ""
64-
puts "💡 Access at: http://localhost:3000"
65-
puts ""
90+
print_server_info(
91+
"⚡ Starting development server with static assets...",
92+
[
93+
"Generating React on Rails packs",
94+
"Using shakapacker --watch (no HMR)",
95+
"CSS extracted to separate files (no FOUC)",
96+
"Development environment (source maps, faster builds)",
97+
"Auto-recompiles on file changes"
98+
]
99+
)
66100

67-
# Generate React on Rails packs first
68101
generate_packs
69102

70103
procfile = "Procfile.dev-static-assets"
71-
unless File.exist?(procfile)
72-
warn <<~MSG
73-
ERROR:
74-
Please ensure `#{procfile}` exists in your project!
75-
MSG
76-
exit 1
77-
end
78-
79-
if installed?("overmind")
80-
system "overmind start -f #{procfile}"
81-
elsif installed?("foreman")
82-
system "foreman start -f #{procfile}"
83-
else
84-
warn <<~MSG
85-
NOTICE:
86-
For this script to run, you need either 'overmind' or 'foreman' installed on your machine. Please try this script after installing one of them.
87-
MSG
88-
exit 1
89-
end
104+
ensure_procfile(procfile)
105+
run_with_process_manager(procfile)
90106
end
91107

92-
def run_development(process)
108+
def run_development
93109
generate_packs
94110

95111
procfile = "Procfile.dev"
96-
unless File.exist?(procfile)
97-
warn <<~MSG
98-
ERROR:
99-
Please ensure `#{procfile}` exists in your project!
100-
MSG
101-
exit 1
112+
ensure_procfile(procfile)
113+
run_with_process_manager(procfile)
114+
end
115+
116+
def kill_processes
117+
puts "🔪 Killing all development processes..."
118+
puts ""
119+
120+
processes = {
121+
"rails" => "Rails server",
122+
"node.*react[-_]on[-_]rails" => "React on Rails Node processes",
123+
"overmind" => "Overmind process manager",
124+
"foreman" => "Foreman process manager",
125+
"ruby.*puma" => "Puma server",
126+
"webpack-dev-server" => "Webpack dev server",
127+
"bin/shakapacker-dev-server" => "Shakapacker dev server"
128+
}
129+
130+
killed_any = false
131+
132+
processes.each do |pattern, description|
133+
pids = `pgrep -f "#{pattern}" 2>/dev/null`.split("\n").map(&:to_i).reject { |pid| pid == Process.pid }
134+
135+
if pids.any?
136+
puts " ☠️ Killing #{description} (PIDs: #{pids.join(', ')})"
137+
pids.each { |pid| Process.kill("TERM", pid) rescue nil }
138+
killed_any = true
139+
end
102140
end
103141

104-
system "#{process} start -f #{procfile}"
142+
if killed_any
143+
puts ""
144+
puts "✅ All processes terminated"
145+
puts "💡 You can now run 'bin/dev' for a clean start"
146+
else
147+
puts " ℹ️ No development processes found running"
148+
end
105149
end
106150

107-
# Check for arguments
108-
if ARGV[0] == "production-assets" || ARGV[0] == "prod"
109-
run_production_like
110-
elsif ARGV[0] == "static"
111-
run_static_development
112-
elsif ARGV[0] == "help" || ARGV[0] == "--help" || ARGV[0] == "-h"
151+
def show_help
113152
puts <<~HELP
114153
Usage: bin/dev [command]
115154
@@ -118,6 +157,7 @@ elsif ARGV[0] == "help" || ARGV[0] == "--help" || ARGV[0] == "-h"
118157
static Start development server with static assets (no HMR, no FOUC)
119158
production-assets Start with production-optimized assets (no HMR)
120159
prod Alias for production-assets
160+
kill Kill all development processes for a clean start
121161
help Show this help message
122162
#{' '}
123163
HMR Development mode (default):
@@ -144,21 +184,21 @@ elsif ARGV[0] == "help" || ARGV[0] == "--help" || ARGV[0] == "-h"
144184
• Slower recompilation
145185
• Access at: http://localhost:3001
146186
HELP
147-
elsif ARGV[0] == "hmr" || ARGV[0].nil?
148-
# Default development mode (HMR)
149-
if installed? "overmind"
150-
run_development "overmind"
151-
elsif installed? "foreman"
152-
run_development "foreman"
153-
else
154-
warn <<~MSG
155-
NOTICE:
156-
For this script to run, you need either 'overmind' or 'foreman' installed on your machine. Please try this script after installing one of them.
157-
MSG
158-
exit 1
159-
end
187+
end
188+
189+
# Main execution
190+
case ARGV[0]
191+
when "production-assets", "prod"
192+
run_production_like
193+
when "static"
194+
run_static_development
195+
when "kill"
196+
kill_processes
197+
when "help", "--help", "-h"
198+
show_help
199+
when "hmr", nil
200+
run_development
160201
else
161-
# Unknown argument
162202
puts "Unknown argument: #{ARGV[0]}"
163203
puts "Run 'bin/dev help' for usage information"
164204
exit 1

0 commit comments

Comments
 (0)