Skip to content

Commit ec9aed6

Browse files
committed
generator: adopt example in generator and
1 parent 6f9d19b commit ec9aed6

File tree

3 files changed

+320
-17
lines changed
  • lib/generators/react_on_rails/bin
  • spec

3 files changed

+320
-17
lines changed

lib/generators/react_on_rails/bin/dev

Lines changed: 137 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,151 @@ rescue Errno::ENOENT
77
false
88
end
99

10-
def run(process)
11-
system "#{process} start -f Procfile.dev"
10+
def generate_packs
11+
puts "Generating React on Rails packs..."
12+
system "bundle exec rake react_on_rails:generate_packs"
13+
14+
unless $?.success?
15+
puts "Pack generation failed"
16+
exit 1
17+
end
18+
end
19+
20+
def run_production_like
21+
puts "Starting production-like development server..."
22+
puts " - Generating React on Rails packs"
23+
puts " - Precompiling assets with production optimizations"
24+
puts " - Running Rails server on port 3001"
25+
puts " - No HMR (Hot Module Replacement)"
26+
puts " - CSS extracted to separate files (no FOUC)"
27+
puts ""
28+
puts "Access at: http://localhost:3001"
29+
puts ""
30+
31+
# Generate React on Rails packs first
32+
generate_packs
33+
34+
# Precompile assets in production mode
35+
puts "Precompiling assets..."
36+
system "RAILS_ENV=production NODE_ENV=production bundle exec rails assets:precompile"
37+
38+
if $?.success?
39+
puts "Assets precompiled successfully"
40+
puts "Starting Rails server in production mode..."
41+
puts ""
42+
puts "Press Ctrl+C to stop the server"
43+
puts "To clean up: rm -rf public/packs && bin/dev"
44+
puts ""
45+
46+
# Start Rails in production mode
47+
system "RAILS_ENV=production bundle exec rails server -p 3001"
48+
else
49+
puts "Asset precompilation failed"
50+
exit 1
51+
end
52+
end
53+
54+
def run_static_development
55+
puts "Starting development server with static assets..."
56+
puts " - Generating React on Rails packs"
57+
puts " - Using shakapacker --watch (no HMR)"
58+
puts " - CSS extracted to separate files (no FOUC)"
59+
puts " - Development environment (source maps, faster builds)"
60+
puts " - Auto-recompiles on file changes"
61+
puts ""
62+
puts "Access at: http://localhost:3000"
63+
puts ""
64+
65+
# Generate React on Rails packs first
66+
generate_packs
67+
68+
if installed? "overmind"
69+
system "overmind start -f Procfile.dev-static"
70+
elsif installed? "foreman"
71+
system "foreman start -f Procfile.dev-static"
72+
else
73+
warn <<~MSG
74+
NOTICE:
75+
For this script to run, you need either 'overmind' or 'foreman' installed on your machine. Please try this script after installing one of them.
76+
MSG
77+
exit!
78+
end
1279
rescue Errno::ENOENT
1380
warn <<~MSG
1481
ERROR:
15-
Please ensure `Procfile.dev` exists in your project!
82+
Please ensure `Procfile.dev-static` exists in your project!
1683
MSG
1784
exit!
1885
end
1986

20-
if installed? "overmind"
21-
run "overmind"
22-
elsif installed? "foreman"
23-
run "foreman"
24-
else
87+
def run_development(process)
88+
generate_packs
89+
90+
system "#{process} start -f Procfile.dev"
91+
rescue Errno::ENOENT
2592
warn <<~MSG
26-
NOTICE:
27-
For this script to run, you need either 'overmind' or 'foreman' installed on your machine. Please try this script after installing one of them.
93+
ERROR:
94+
Please ensure `Procfile.dev` exists in your project!
2895
MSG
2996
exit!
3097
end
98+
99+
# Check for arguments
100+
if ARGV[0] == "production-assets" || ARGV[0] == "prod"
101+
run_production_like
102+
elsif ARGV[0] == "static"
103+
run_static_development
104+
elsif ARGV[0] == "help" || ARGV[0] == "--help" || ARGV[0] == "-h"
105+
puts <<~HELP
106+
Usage: bin/dev [command]
107+
108+
Commands:
109+
(none) / hmr Start development server with HMR (default)
110+
static Start development server with static assets (no HMR, no FOUC)
111+
production-assets Start with production-optimized assets (no HMR)
112+
prod Alias for production-assets
113+
help Show this help message
114+
115+
HMR Development mode (default):
116+
- Hot Module Replacement (HMR) enabled
117+
- Automatic React on Rails pack generation
118+
- Source maps for debugging
119+
- May have Flash of Unstyled Content (FOUC)
120+
- Fast recompilation
121+
- Access at: http://localhost:3000
122+
123+
Static development mode:
124+
- No HMR (static assets with auto-recompilation)
125+
- Automatic React on Rails pack generation
126+
- CSS extracted to separate files (no FOUC)
127+
- Development environment (faster builds than production)
128+
- Source maps for debugging
129+
- Access at: http://localhost:3000
130+
131+
Production-like mode:
132+
- Automatic React on Rails pack generation
133+
- Optimized, minified bundles
134+
- Extracted CSS files (no FOUC)
135+
- No HMR (static assets)
136+
- Slower recompilation
137+
- Access at: http://localhost:3001
138+
HELP
139+
elsif ARGV[0] == "hmr" || ARGV[0].nil?
140+
# Default development mode (HMR)
141+
if installed? "overmind"
142+
run_development "overmind"
143+
elsif installed? "foreman"
144+
run_development "foreman"
145+
else
146+
warn <<~MSG
147+
NOTICE:
148+
For this script to run, you need either 'overmind' or 'foreman' installed on your machine. Please try this script after installing one of them.
149+
MSG
150+
exit!
151+
end
152+
else
153+
# Unknown argument
154+
puts "Unknown argument: #{ARGV[0]}"
155+
puts "Run 'bin/dev help' for usage information"
156+
exit 1
157+
end

spec/dummy/bin/dev

Lines changed: 155 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,157 @@
1-
#!/usr/bin/env bash
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
23

3-
if ! command -v foreman &> /dev/null
4-
then
5-
echo "Installing foreman..."
6-
gem install foreman
7-
fi
4+
def installed?(process)
5+
IO.popen "#{process} -v"
6+
rescue Errno::ENOENT
7+
false
8+
end
89

9-
foreman start -f Procfile.dev
10+
def generate_packs
11+
puts "Generating React on Rails packs..."
12+
system "bundle exec rake react_on_rails:generate_packs"
13+
14+
unless $?.success?
15+
puts "Pack generation failed"
16+
exit 1
17+
end
18+
end
19+
20+
def run_production_like
21+
puts "Starting production-like development server..."
22+
puts " - Generating React on Rails packs"
23+
puts " - Precompiling assets with production optimizations"
24+
puts " - Running Rails server on port 3001"
25+
puts " - No HMR (Hot Module Replacement)"
26+
puts " - CSS extracted to separate files (no FOUC)"
27+
puts ""
28+
puts "Access at: http://localhost:3001"
29+
puts ""
30+
31+
# Generate React on Rails packs first
32+
generate_packs
33+
34+
# Precompile assets in production mode
35+
puts "Precompiling assets..."
36+
system "RAILS_ENV=production NODE_ENV=production bundle exec rails assets:precompile"
37+
38+
if $?.success?
39+
puts "Assets precompiled successfully"
40+
puts "Starting Rails server in production mode..."
41+
puts ""
42+
puts "Press Ctrl+C to stop the server"
43+
puts "To clean up: rm -rf public/packs && bin/dev"
44+
puts ""
45+
46+
# Start Rails in production mode
47+
system "RAILS_ENV=production bundle exec rails server -p 3001"
48+
else
49+
puts "Asset precompilation failed"
50+
exit 1
51+
end
52+
end
53+
54+
def run_static_development
55+
puts "Starting development server with static assets..."
56+
puts " - Generating React on Rails packs"
57+
puts " - Using shakapacker --watch (no HMR)"
58+
puts " - CSS extracted to separate files (no FOUC)"
59+
puts " - Development environment (source maps, faster builds)"
60+
puts " - Auto-recompiles on file changes"
61+
puts ""
62+
puts "Access at: http://localhost:3000"
63+
puts ""
64+
65+
# Generate React on Rails packs first
66+
generate_packs
67+
68+
if installed? "overmind"
69+
system "overmind start -f Procfile.dev-static"
70+
elsif installed? "foreman"
71+
system "foreman start -f Procfile.dev-static"
72+
else
73+
warn <<~MSG
74+
NOTICE:
75+
For this script to run, you need either 'overmind' or 'foreman' installed on your machine. Please try this script after installing one of them.
76+
MSG
77+
exit!
78+
end
79+
rescue Errno::ENOENT
80+
warn <<~MSG
81+
ERROR:
82+
Please ensure `Procfile.dev-static` exists in your project!
83+
MSG
84+
exit!
85+
end
86+
87+
def run_development(process)
88+
generate_packs
89+
90+
system "#{process} start -f Procfile.dev"
91+
rescue Errno::ENOENT
92+
warn <<~MSG
93+
ERROR:
94+
Please ensure `Procfile.dev` exists in your project!
95+
MSG
96+
exit!
97+
end
98+
99+
# Check for arguments
100+
if ARGV[0] == "production-assets" || ARGV[0] == "prod"
101+
run_production_like
102+
elsif ARGV[0] == "static"
103+
run_static_development
104+
elsif ARGV[0] == "help" || ARGV[0] == "--help" || ARGV[0] == "-h"
105+
puts <<~HELP
106+
Usage: bin/dev [command]
107+
108+
Commands:
109+
(none) / hmr Start development server with HMR (default)
110+
static Start development server with static assets (no HMR, no FOUC)
111+
production-assets Start with production-optimized assets (no HMR)
112+
prod Alias for production-assets
113+
help Show this help message
114+
115+
HMR Development mode (default):
116+
- Hot Module Replacement (HMR) enabled
117+
- Automatic React on Rails pack generation
118+
- Source maps for debugging
119+
- May have Flash of Unstyled Content (FOUC)
120+
- Fast recompilation
121+
- Access at: http://localhost:3000
122+
123+
Static development mode:
124+
- No HMR (static assets with auto-recompilation)
125+
- Automatic React on Rails pack generation
126+
- CSS extracted to separate files (no FOUC)
127+
- Development environment (faster builds than production)
128+
- Source maps for debugging
129+
- Access at: http://localhost:3000
130+
131+
Production-like mode:
132+
- Automatic React on Rails pack generation
133+
- Optimized, minified bundles
134+
- Extracted CSS files (no FOUC)
135+
- No HMR (static assets)
136+
- Slower recompilation
137+
- Access at: http://localhost:3001
138+
HELP
139+
elsif ARGV[0] == "hmr" || ARGV[0].nil?
140+
# Default development mode (HMR)
141+
if installed? "overmind"
142+
run_development "overmind"
143+
elsif installed? "foreman"
144+
run_development "foreman"
145+
else
146+
warn <<~MSG
147+
NOTICE:
148+
For this script to run, you need either 'overmind' or 'foreman' installed on your machine. Please try this script after installing one of them.
149+
MSG
150+
exit!
151+
end
152+
else
153+
# Unknown argument
154+
puts "Unknown argument: #{ARGV[0]}"
155+
puts "Run 'bin/dev help' for usage information"
156+
exit 1
157+
end

spec/react_on_rails/binstubs/dev_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,35 @@
1616
$stdout = original_stdout
1717
end
1818

19+
it "includes pack generation function" do
20+
script_content = File.read(script_path)
21+
expect(script_content).to include("def generate_packs")
22+
expect(script_content).to include("bundle exec rake react_on_rails:generate_packs")
23+
end
24+
25+
it "supports static development mode" do
26+
script_content = File.read(script_path)
27+
expect(script_content).to include("run_static_development")
28+
expect(script_content).to include("Procfile.dev-static")
29+
end
30+
31+
it "supports production-like mode" do
32+
script_content = File.read(script_path)
33+
expect(script_content).to include("run_production_like")
34+
expect(script_content).to include("RAILS_ENV=production NODE_ENV=production bundle exec rails assets:precompile")
35+
expect(script_content).to include("rails server -p 3001")
36+
end
37+
38+
it "supports help command" do
39+
script_content = File.read(script_path)
40+
expect(script_content).to include('ARGV[0] == "help" || ARGV[0] == "--help" || ARGV[0] == "-h"')
41+
expect(script_content).to include("Usage: bin/dev [command]")
42+
end
43+
1944
it "with Overmind installed, uses Overmind" do
2045
allow(IO).to receive(:popen).with("overmind -v").and_return("Some truthy result")
2146

47+
expect_any_instance_of(Kernel).to receive(:system).with("bundle exec rake react_on_rails:generate_packs").and_return(true)
2248
expect_any_instance_of(Kernel).to receive(:system).with("overmind start -f Procfile.dev")
2349

2450
load script_path
@@ -28,6 +54,7 @@
2854
allow(IO).to receive(:popen).with("overmind -v").and_raise(Errno::ENOENT)
2955
allow(IO).to receive(:popen).with("foreman -v").and_return("Some truthy result")
3056

57+
expect_any_instance_of(Kernel).to receive(:system).with("bundle exec rake react_on_rails:generate_packs").and_return(true)
3158
expect_any_instance_of(Kernel).to receive(:system).with("foreman start -f Procfile.dev")
3259

3360
load script_path
@@ -49,6 +76,7 @@
4976
it "With Overmind and without Procfile, exits with error message" do
5077
allow(IO).to receive(:popen).with("overmind -v").and_return("Some truthy result")
5178

79+
allow_any_instance_of(Kernel).to receive(:system).with("bundle exec rake react_on_rails:generate_packs").and_return(true)
5280
allow_any_instance_of(Kernel)
5381
.to receive(:system)
5482
.with("overmind start -f Procfile.dev")

0 commit comments

Comments
 (0)