|
1 | 1 | #!/usr/bin/env ruby |
2 | 2 | # frozen_string_literal: true |
3 | 3 |
|
4 | | -def installed?(process) |
5 | | - IO.popen "#{process} -v" |
6 | | -rescue Errno::ENOENT |
7 | | - false |
8 | | -end |
| 4 | +# ReactOnRails Development Server |
| 5 | +# |
| 6 | +# This script provides a simple interface to the ReactOnRails development |
| 7 | +# server management. The core logic is implemented in ReactOnRails::Dev |
| 8 | +# classes for better maintainability and testing. |
| 9 | +# |
| 10 | +# Each command uses a specific Procfile for process management: |
| 11 | +# - bin/dev (default/hmr): Uses Procfile.dev |
| 12 | +# - bin/dev static: Uses Procfile.dev-static-assets |
| 13 | +# - bin/dev prod: Uses Procfile.dev-prod-assets |
| 14 | +# |
| 15 | +# To customize development environment: |
| 16 | +# 1. Edit the appropriate Procfile to modify which processes run |
| 17 | +# 2. Modify this script for project-specific command-line behavior |
| 18 | +# 3. Extend ReactOnRails::Dev classes in your Rails app for advanced customization |
| 19 | +# 4. Use classes directly: ReactOnRails::Dev::ServerManager.start(:development, "Custom.procfile") |
9 | 20 |
|
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 |
| 21 | +begin |
| 22 | + require "bundler/setup" |
| 23 | + require "react_on_rails/dev" |
| 24 | +rescue LoadError |
| 25 | + # Fallback for when gem is not yet installed |
| 26 | + puts "Loading ReactOnRails development tools..." |
| 27 | + require_relative "../../lib/react_on_rails/dev" |
18 | 28 | end |
19 | 29 |
|
20 | | -def clean_assets |
21 | | - puts "🧹 Cleaning old Shakapacker assets..." |
22 | | - system "bundle exec rake shakapacker:clobber" |
23 | | - |
24 | | - unless $?.success? |
25 | | - puts "⚠️ Asset cleanup failed, continuing anyway..." |
26 | | - else |
27 | | - puts "✅ Old assets cleaned successfully" |
28 | | - end |
29 | | -end |
| 30 | +# Parse verbose flag |
| 31 | +verbose = ARGV.include?("--verbose") || ARGV.include?("-v") |
| 32 | +args = ARGV.reject { |arg| arg == "--verbose" || arg == "-v" } |
30 | 33 |
|
31 | | -def run_production_assets |
32 | | - puts "🏭 Starting development server with production assets..." |
33 | | - puts " - Cleaning old assets with Shakapacker" |
34 | | - puts " - Generating React on Rails packs" |
35 | | - puts " - Precompiling assets with production optimizations" |
36 | | - puts " - Running Rails server on port 3001 (development environment)" |
37 | | - puts " - SSL automatically disabled (development environment)" |
38 | | - puts " - No HMR (Hot Module Replacement)" |
39 | | - puts " - CSS extracted to separate files (no FOUC)" |
40 | | - puts " - Using overmind/foreman for process management" |
41 | | - puts "" |
42 | | - puts "💡 Access at: http://localhost:3001" |
43 | | - puts "" |
44 | | - |
45 | | - # Clean old assets first |
46 | | - clean_assets |
47 | | - |
48 | | - # Generate React on Rails packs first |
49 | | - generate_packs |
50 | | - |
51 | | - # Precompile assets in production mode for optimization |
52 | | - puts "🔨 Precompiling assets with production optimizations..." |
53 | | - system "RAILS_ENV=production NODE_ENV=production bundle exec rails assets:precompile" |
54 | | - |
55 | | - if $?.success? |
56 | | - puts "✅ Assets precompiled successfully" |
57 | | - puts "🚀 Starting processes with development environment..." |
58 | | - puts " (Using production-optimized assets but development Rails config)" |
59 | | - puts "" |
60 | | - puts "🔒 SSL automatically disabled (development environment)" |
61 | | - puts "" |
62 | | - |
63 | | - if installed? "overmind" |
64 | | - system "RAILS_ENV=development overmind start -f Procfile.dev-prod-assets" |
65 | | - elsif installed? "foreman" |
66 | | - system "RAILS_ENV=development foreman start -f Procfile.dev-prod-assets" |
67 | | - else |
68 | | - puts "⚠️ overmind/foreman not found, falling back to direct Rails server..." |
69 | | - puts " For production apps with background jobs, install overmind or foreman" |
70 | | - puts "" |
71 | | - puts "Press Ctrl+C to stop the server" |
72 | | - puts "" |
73 | | - system "RAILS_ENV=development bundle exec rails server -p 3001" |
74 | | - end |
75 | | - else |
76 | | - puts "❌ Asset precompilation failed" |
77 | | - exit 1 |
78 | | - end |
79 | | -rescue Errno::ENOENT |
80 | | - warn <<~MSG |
81 | | - ERROR: |
82 | | - Please ensure `Procfile.dev-prod-assets` exists in your project! |
83 | | - MSG |
84 | | - exit! |
85 | | -end |
| 34 | +# Set environment variable for verbose mode |
| 35 | +ENV["VERBOSE"] = "true" if verbose |
86 | 36 |
|
87 | | -def run_dev_assets |
88 | | - puts "⚡ Starting development server with development assets..." |
89 | | - puts " - Generating React on Rails packs" |
90 | | - puts " - Using shakapacker --watch (no HMR)" |
91 | | - puts " - CSS extracted to separate files (no FOUC)" |
92 | | - puts " - Development environment (source maps, faster builds)" |
93 | | - puts " - Auto-recompiles on file changes" |
94 | | - puts "" |
95 | | - puts "💡 Access at: http://localhost:3000" |
96 | | - puts "" |
97 | | - |
98 | | - # Generate React on Rails packs first |
99 | | - generate_packs |
100 | | - |
101 | | - if installed? "overmind" |
102 | | - system "overmind start -f Procfile.dev-static-assets" |
103 | | - elsif installed? "foreman" |
104 | | - system "foreman start -f Procfile.dev-static-assets" |
105 | | - else |
106 | | - warn <<~MSG |
107 | | - NOTICE: |
108 | | - For this script to run, you need either 'overmind' or 'foreman' installed on your machine. Please try this script after installing one of them. |
109 | | - MSG |
110 | | - exit! |
111 | | - end |
112 | | -rescue Errno::ENOENT |
113 | | - warn <<~MSG |
114 | | - ERROR: |
115 | | - Please ensure `Procfile.dev-static-assets` exists in your project! |
116 | | - MSG |
117 | | - exit! |
118 | | -end |
119 | | - |
120 | | -def run_development(process) |
121 | | - generate_packs |
122 | | - |
123 | | - system "#{process} start -f Procfile.dev" |
124 | | -rescue Errno::ENOENT |
125 | | - warn <<~MSG |
126 | | - ERROR: |
127 | | - Please ensure `Procfile.dev` exists in your project! |
128 | | - MSG |
129 | | - exit! |
130 | | -end |
131 | | - |
132 | | -# Check for arguments |
133 | | -if ARGV[0] == "prod-assets" |
134 | | - run_production_assets |
135 | | -elsif ARGV[0] == "dev-assets" |
136 | | - run_dev_assets |
137 | | -elsif ARGV[0] == "help" || ARGV[0] == "--help" || ARGV[0] == "-h" |
138 | | - puts <<~HELP |
139 | | - Usage: bin/dev [command] |
140 | | - |
141 | | - Commands: |
142 | | - (none) / hmr Start development server with HMR (default) |
143 | | - dev-assets Start development server with development assets (no HMR, no FOUC) |
144 | | - prod-assets Start development server with production assets (optimized) |
145 | | - help Show this help message |
146 | | - |
147 | | - HMR Development mode (default): |
148 | | - • Hot Module Replacement (HMR) enabled |
149 | | - • Automatic React on Rails pack generation |
150 | | - • Source maps for debugging |
151 | | - • May have Flash of Unstyled Content (FOUC) |
152 | | - • Fast recompilation |
153 | | - • Access at: http://localhost:3000 |
154 | | - |
155 | | - Development assets mode: |
156 | | - • No HMR (development assets with auto-recompilation) |
157 | | - • Automatic React on Rails pack generation |
158 | | - • CSS extracted to separate files (no FOUC) |
159 | | - • Development environment (faster builds, source maps) |
160 | | - • Auto-recompiles on file changes |
161 | | - • Access at: http://localhost:3000 |
162 | | - |
163 | | - Production assets mode: |
164 | | - • Automatic Shakapacker asset cleaning (rake shakapacker:clobber) |
165 | | - • Automatic React on Rails pack generation |
166 | | - • Production-optimized, minified bundles |
167 | | - • Extracted CSS files (no FOUC) |
168 | | - • Development Rails environment (SSL disabled, no config issues) |
169 | | - • Uses overmind/foreman for process management (supports Sidekiq, Redis, etc.) |
170 | | - • Optimized for performance testing |
171 | | - • Access at: http://localhost:3001 |
172 | | - HELP |
173 | | -elsif ARGV[0] == "hmr" || ARGV[0].nil? |
174 | | - # Default development mode (HMR) |
175 | | - if installed? "overmind" |
176 | | - run_development "overmind" |
177 | | - elsif installed? "foreman" |
178 | | - run_development "foreman" |
| 37 | +# Main execution |
| 38 | +case args[0] |
| 39 | +when "production-assets", "prod" |
| 40 | + ReactOnRails::Dev::ServerManager.start(:production_like) |
| 41 | +when "static" |
| 42 | + ReactOnRails::Dev::ServerManager.start(:static, "Procfile.dev-static-assets") |
| 43 | +when "kill" |
| 44 | + ReactOnRails::Dev::ServerManager.kill_processes |
| 45 | +when "help", "--help", "-h" |
| 46 | + ReactOnRails::Dev::ServerManager.show_help |
| 47 | +when "hmr", nil |
| 48 | + ReactOnRails::Dev::ServerManager.start(:development, "Procfile.dev") |
| 49 | +else |
| 50 | + if args[0] |
| 51 | + puts "Unknown argument: #{args[0]}" |
| 52 | + puts "Run 'bin/dev help' for usage information" |
| 53 | + exit 1 |
179 | 54 | else |
180 | | - warn <<~MSG |
181 | | - NOTICE: |
182 | | - For this script to run, you need either 'overmind' or 'foreman' installed on your machine. Please try this script after installing one of them. |
183 | | - MSG |
184 | | - exit! |
| 55 | + ReactOnRails::Dev::ServerManager.start(:development, "Procfile.dev") |
185 | 56 | end |
186 | | -else |
187 | | - # Unknown argument |
188 | | - puts "Unknown argument: #{ARGV[0]}" |
189 | | - puts "Run 'bin/dev help' for usage information" |
190 | | - exit 1 |
191 | 57 | end |
0 commit comments