Module: GeneratorMessages

Defined in:
lib/generators/react_on_rails/generator_messages.rb

Constant Summary collapse

PRO_UPGRADE_HINT =
"\n\n    💎 For RSC, streaming SSR, and 10-100x faster SSR, try React on Rails Pro:" \
"\n       #{Rainbow('https://reactonrails.com/docs/pro/upgrading-to-pro/').cyan.underline}".freeze
SUPPORTED_PACKAGE_MANAGERS =
%w[npm pnpm yarn bun].freeze

Class Method Summary collapse

Class Method Details

.add_error(message) ⇒ Object



16
17
18
# File 'lib/generators/react_on_rails/generator_messages.rb', line 16

def add_error(message)
  output << format_error(message)
end

.add_info(message) ⇒ Object



24
25
26
# File 'lib/generators/react_on_rails/generator_messages.rb', line 24

def add_info(message)
  output << format_info(message)
end

.add_warning(message) ⇒ Object



20
21
22
# File 'lib/generators/react_on_rails/generator_messages.rb', line 20

def add_warning(message)
  output << format_warning(message)
end

.clearObject



44
45
46
# File 'lib/generators/react_on_rails/generator_messages.rb', line 44

def clear
  @output = []
end

.detect_package_managerObject

Uses relative lockfile paths resolved against Dir.pwd, so callers must invoke this while the current working directory is the target Rails app root.



108
109
110
111
112
113
114
# File 'lib/generators/react_on_rails/generator_messages.rb', line 108

def detect_package_manager
  env_package_manager = ENV.fetch("REACT_ON_RAILS_PACKAGE_MANAGER", nil)&.strip&.downcase
  return env_package_manager if supported_package_manager?(env_package_manager)

  # Default to npm (Shakapacker 8.x default) - covers package-lock.json and no lockfile
  detect_package_manager_from_lockfiles || "npm"
end

.detect_package_manager_from_lockfilesObject



116
117
118
119
120
121
122
123
# File 'lib/generators/react_on_rails/generator_messages.rb', line 116

def detect_package_manager_from_lockfiles
  return "yarn" if File.exist?("yarn.lock")
  return "pnpm" if File.exist?("pnpm-lock.yaml")
  return "bun" if File.exist?("bun.lock") || File.exist?("bun.lockb")
  return "npm" if File.exist?("package-lock.json")

  nil
end

.format_error(msg) ⇒ Object



32
33
34
# File 'lib/generators/react_on_rails/generator_messages.rb', line 32

def format_error(msg)
  Rainbow("ERROR: #{msg}").red
end

.format_info(msg) ⇒ Object



40
41
42
# File 'lib/generators/react_on_rails/generator_messages.rb', line 40

def format_info(msg)
  Rainbow(msg.to_s).green
end

.format_warning(msg) ⇒ Object



36
37
38
# File 'lib/generators/react_on_rails/generator_messages.rb', line 36

def format_warning(msg)
  Rainbow("WARNING: #{msg}").orange
end

.helpful_message_after_installation(component_name: "HelloWorld", route: "hello_world", pro: false, rsc: false, shakapacker_just_installed: false, landing_page: false) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/generators/react_on_rails/generator_messages.rb', line 48

def helpful_message_after_installation(component_name: "HelloWorld", route: "hello_world", pro: false,
                                       rsc: false, shakapacker_just_installed: false, landing_page: false)
  process_manager_section = build_process_manager_section
  testing_section = build_testing_section
  package_manager = detect_package_manager
  shakapacker_status = build_shakapacker_status_section(shakapacker_just_installed: shakapacker_just_installed)
  render_example = build_render_example(component_name: component_name, route: route, rsc: rsc)
  render_label = build_render_label(route: route, rsc: rsc)
  normalized_route = route.to_s.sub(%r{\A/+}, "")
  visit_url = if landing_page || normalized_route.empty?
                "http://localhost:3000"
              else
                "http://localhost:3000/#{normalized_route}"
              end
  landing_page_hint = landing_page ? "\n       Home page includes links to the generated example pages." : ""
  # rsc guard is defensive; callers via install_generator already pass pro: true when rsc is set
  pro_hint = pro || rsc ? "" : PRO_UPGRADE_HINT

  <<~MSG

    ╔════════════════════════════════════════════════════════════════════════╗
    ║  🎉 React on Rails Successfully Installed!                             ║
    ╚════════════════════════════════════════════════════════════════════════╝
    #{process_manager_section}#{shakapacker_status}

    📋 QUICK START:
    ─────────────────────────────────────────────────────────────────────────
    1. Install dependencies:
       #{Rainbow("bundle && #{package_manager} install").cyan}

    2. Prepare database:
       #{Rainbow('bin/rails db:prepare').cyan}

    3. Start the app:
       ./bin/dev              # HMR (Hot Module Replacement) mode
       ./bin/dev static       # Static bundles (no HMR, faster initial load)
       ./bin/dev prod         # Production-like mode for testing
       ./bin/dev help         # See all available options

    4. Visit: #{Rainbow(visit_url).cyan.underline}#{landing_page_hint}
    ✨ KEY FEATURES:
    ─────────────────────────────────────────────────────────────────────────
    • Auto-registration enabled - Your layout only needs:
      <%= javascript_pack_tag %>
      <%= stylesheet_pack_tag %>

    #{render_label}
      #{render_example}

    📚 LEARN MORE:
    ─────────────────────────────────────────────────────────────────────────
    • Documentation: #{Rainbow('https://reactonrails.com/docs/').cyan.underline}
    • Webpack customization: #{Rainbow('https://github.com/shakacode/shakapacker#webpack-configuration').cyan.underline}

    💡 TIP: Run 'bin/dev help' for development server options and troubleshooting#{testing_section}#{pro_hint}
  MSG
end

.messagesObject



28
29
30
# File 'lib/generators/react_on_rails/generator_messages.rb', line 28

def messages
  output
end

.outputObject



12
13
14
# File 'lib/generators/react_on_rails/generator_messages.rb', line 12

def output
  @output ||= []
end

.supported_package_manager?(package_manager) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/generators/react_on_rails/generator_messages.rb', line 125

def supported_package_manager?(package_manager)
  SUPPORTED_PACKAGE_MANAGERS.include?(package_manager)
end