Module: ReactOnRails::Generators::ProSetup

Included in:
InstallGenerator, ProGenerator
Defined in:
lib/generators/react_on_rails/pro_setup.rb

Overview

Provides Pro setup functionality for React on Rails generators.

This module extracts Pro-specific setup methods that can be shared between:

  • InstallGenerator (when –pro or –rsc flags are used)

  • ProGenerator (standalone generator for upgrading existing apps)

Required Dependencies

Including classes must provide (typically via Rails::Generators::Base):

  • destination_root: Path to the target Rails application

  • template, copy_file, append_to_file: Thor file manipulation methods

  • options: Generator options hash

Including classes must also include GeneratorHelper which provides:

  • use_pro?, use_rsc?: Feature flag helpers

  • pro_gem_installed?: Pro gem detection (real lockfile / loaded-specs state)

  • pro_gem_install_deferred?, defer_pro_gem_install!: deferred-install tracking

  • invalidate_pro_gem_installed_cache!: invalidate memoized pro_gem_installed?

rubocop:disable Metrics/ModuleLength

Constant Summary collapse

PRO_GEM_NAME =
"react_on_rails_pro"
AUTO_INSTALL_TIMEOUT =

Version is appended dynamically via pro_gem_auto_install_command to ensure the installed version matches the current react_on_rails gem version.

120
TERMINATION_GRACE_PERIOD =
5

Instance Method Summary collapse

Instance Method Details

#missing_pro_gem?(force: false) ⇒ Boolean

Check if the Pro gem is missing. When the base react_on_rails gem is in the Gemfile, installation is deferred to the later Gemfile swap (which preserves the user’s version pin); otherwise auto-install via ‘bundle add` is attempted.

Parameters:

  • force (Boolean) (defaults to: false)

    When true, always checks (default: only if use_pro?).

Returns:

  • (Boolean)

    true only if the Pro gem is missing and could not be installed; false if it is present, was auto-installed, or the install is deferred to the Gemfile swap.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/generators/react_on_rails/pro_setup.rb', line 110

def missing_pro_gem?(force: false)
  return false unless force || use_pro?
  return false if pro_gem_installed? || pro_gem_install_deferred?
  return false if defer_pro_gem_install_to_gemfile_swap
  return false if attempt_pro_gem_auto_install

  optional_prerelease_line = prerelease_note.empty? ? "" : "\n#{prerelease_note}"

  GeneratorMessages.add_error(<<~MSG.strip)
    🚫 Failed to auto-install #{PRO_GEM_NAME} gem.

    #{pro_gem_requirement_context_line}#{optional_prerelease_line}

    Please add manually to your Gemfile:
      gem '#{PRO_GEM_NAME}', '#{pro_gem_version_requirement}'

    Then run: bundle install

    No license needed for evaluation or non-production use.
    Free or low-cost production licenses available for startups and small companies.
    See the upgrade guide: https://reactonrails.com/docs/pro/upgrading-to-pro/
  MSG
  true
end

#node_renderer_will_be_created?Boolean

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/generators/react_on_rails/pro_setup.rb', line 97

def node_renderer_will_be_created?
  !File.exist?(File.join(destination_root, "renderer/node-renderer.js")) &&
    !File.exist?(File.join(destination_root, "client/node-renderer.js"))
end

#pro_initializer_will_be_created?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/generators/react_on_rails/pro_setup.rb', line 93

def pro_initializer_will_be_created?
  !File.exist?(File.join(destination_root, "config/initializers/react_on_rails_pro.rb"))
end

#say_renderer_password_setup_summary(initializer_created) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/generators/react_on_rails/pro_setup.rb', line 73

def say_renderer_password_setup_summary(initializer_created)
  if @generated_renderer_password
    say ""
    say set_color("🔐 A random renderer password was written into your config files.", :yellow, :bold)
    say "   For production, set RENDERER_PASSWORD as an env var instead and"
    say "   remove the literal value from version control."
    say "   See: https://www.shakacode.com/react-on-rails/docs/pro/node-renderer/"
    say ""
  elsif initializer_created
    # Initializer was newly created but the Node renderer file already exists;
    # the new initializer falls back to ENV["RENDERER_PASSWORD"] only so it doesn't
    # disagree with whatever literal the existing renderer file contains.
    say ""
    say set_color("⚠️  Existing Node renderer detected — Rails initializer uses " \
                  "ENV[\"RENDERER_PASSWORD\"] only.", :yellow, :bold)
    say "   Set RENDERER_PASSWORD in your environment to match the password in your existing renderer."
    say ""
  end
end

#setup_proObject

Note:

NPM dependencies are handled separately by JsDependencyManager

Main entry point for Pro setup. Orchestrates creation of all Pro-related files and configuration.

Creates:

  • config/initializers/react_on_rails_pro.rb

  • renderer/node-renderer.js

  • Procfile.dev entry for node-renderer



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/generators/react_on_rails/pro_setup.rb', line 45

def setup_pro
  say "\n#{set_color('=' * 80, :cyan)}"
  say set_color("🚀 REACT ON RAILS PRO SETUP", :cyan, :bold)
  say set_color("=" * 80, :cyan)

  # The Rails initializer and Node renderer bootstrap must share the same
  # password literal. Only mint a fresh random password when BOTH files will
  # be created — otherwise nil so each template falls back to the env-only
  # branch, avoiding a literal mismatch with any existing file.
  # Always reassign so a stale value from a prior invocation on the same
  # instance can't leak into a later partial-install run.
  @generated_renderer_password = nil
  if pro_initializer_will_be_created? && node_renderer_will_be_created?
    @generated_renderer_password = SecureRandom.hex(32)
  end

  initializer_created = create_pro_initializer
  legacy_renderer_detected = create_node_renderer
  add_pro_to_procfiles unless legacy_renderer_detected
  update_webpack_config_for_pro

  say_renderer_password_setup_summary(initializer_created)

  say set_color("=" * 80, :cyan)
  say "✅ React on Rails Pro setup complete!", :green
  say set_color("=" * 80, :cyan)
end