module ReactOnRails::Utils

Constants

TRUNCATION_FILLER

Public Class Methods

bundle_js_file_path(bundle_name) click to toggle source
# File lib/react_on_rails/utils.rb, line 98
def self.bundle_js_file_path(bundle_name)
  if ReactOnRails::PackerUtils.using_packer? && bundle_name != "manifest.json"
    ReactOnRails::PackerUtils.bundle_js_uri_from_packer(bundle_name)
  else
    # Default to the non-hashed name in the specified output directory, which, for legacy
    # React on Rails, this is the output directory picked up by the asset pipeline.
    # For Shakapacker, this is the public output path defined in the (shaka/web)packer.yml file.
    File.join(generated_assets_full_path, bundle_name)
  end
end
find_most_recent_mtime(files) click to toggle source
# File lib/react_on_rails/utils.rb, line 201
def self.find_most_recent_mtime(files)
  files.reduce(1.year.ago) do |newest_time, file|
    mt = File.mtime(file)
    [mt, newest_time].max
  end
end
gem_available?(name) click to toggle source
# File lib/react_on_rails/utils.rb, line 156
def self.gem_available?(name)
  Gem.loaded_specs[name].present?
rescue Gem::LoadError
  false
rescue StandardError
  begin
    Gem.available?(name).present?
  rescue NoMethodError
    false
  end
end
generated_assets_full_path() click to toggle source
# File lib/react_on_rails/utils.rb, line 148
def self.generated_assets_full_path
  if ReactOnRails::PackerUtils.using_packer?
    ReactOnRails::PackerUtils.packer_public_output_path
  else
    File.expand_path(ReactOnRails.configuration.generated_assets_dir)
  end
end
invoke_and_exit_if_failed(cmd, failure_message) click to toggle source

Invokes command, exiting with a detailed message if there’s a failure.

# File lib/react_on_rails/utils.rb, line 45
    def self.invoke_and_exit_if_failed(cmd, failure_message)
      stdout, stderr, status = Open3.capture3(cmd)
      unless status.success?
        stdout_msg = stdout.present? ? "\nstdout:\n#{stdout.strip}\n" : ""
        stderr_msg = stderr.present? ? "\nstderr:\n#{stderr.strip}\n" : ""
        msg = <<~MSG
          React on Rails FATAL ERROR!
          #{failure_message}
          cmd: #{cmd}
          exitstatus: #{status.exitstatus}#{stdout_msg}#{stderr_msg}
        MSG

        puts wrap_message(msg)

        # Rspec catches exit without! in the exit callbacks
        exit!(1)
      end
      [stdout, stderr, status]
    end
object_to_boolean(value) click to toggle source
# File lib/react_on_rails/utils.rb, line 36
def self.object_to_boolean(value)
  [true, "true", "yes", 1, "1", "t"].include?(value.instance_of?(String) ? value.downcase : value)
end
prepend_cd_node_modules_directory(cmd) click to toggle source
# File lib/react_on_rails/utils.rb, line 129
def self.prepend_cd_node_modules_directory(cmd)
  "cd \"#{ReactOnRails.configuration.node_modules_location}\" && #{cmd}"
end
prepend_to_file_if_text_not_present(file:, text_to_prepend:, regex:) click to toggle source
# File lib/react_on_rails/utils.rb, line 208
def self.prepend_to_file_if_text_not_present(file:, text_to_prepend:, regex:)
  if File.exist?(file)
    file_content = File.read(file)

    return if file_content.match(regex)

    content_with_prepended_text = text_to_prepend + file_content
    File.write(file, content_with_prepended_text, mode: "w")
  else
    File.write(file, text_to_prepend, mode: "w+")
  end

  puts "Prepended\n#{text_to_prepend}to #{file}."
end
rails_version_less_than(version) click to toggle source
# File lib/react_on_rails/utils.rb, line 113
def self.rails_version_less_than(version)
  @rails_version_less_than ||= {}

  return @rails_version_less_than[version] if @rails_version_less_than.key?(version)

  @rails_version_less_than[version] = begin
    Gem::Version.new(Rails.version) < Gem::Version.new(version)
  end
end
react_on_rails_pro?() click to toggle source

Todo – remove this for v13, as we don’t need both boolean and number

# File lib/react_on_rails/utils.rb, line 169
def self.react_on_rails_pro?
  return @react_on_rails_pro if defined?(@react_on_rails_pro)

  @react_on_rails_pro = gem_available?("react_on_rails_pro")
end
react_on_rails_pro_version() click to toggle source

Return an empty string if React on Rails Pro is not installed

# File lib/react_on_rails/utils.rb, line 176
def self.react_on_rails_pro_version
  return @react_on_rails_pro_version if defined?(@react_on_rails_pro_version)

  @react_on_rails_pro_version = if react_on_rails_pro?
                                  Gem.loaded_specs["react_on_rails_pro"].version.to_s
                                else
                                  ""
                                end
end
running_on_windows?() click to toggle source
# File lib/react_on_rails/utils.rb, line 109
def self.running_on_windows?
  (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
end
server_bundle_js_file_path() click to toggle source
# File lib/react_on_rails/utils.rb, line 69
def self.server_bundle_js_file_path
  # Either:
  # 1. Using same bundle for both server and client, so server bundle will be hashed in manifest
  # 2. Using a different bundle (different Webpack config), so file is not hashed, and
  #    bundle_js_path will throw so the default path is used without a hash.
  # 3. The third option of having the server bundle hashed and a different configuration than
  #    the client bundle is not supported for 2 reasons:
  #    a. The webpack manifest plugin would have a race condition where the same manifest.json
  #       is edited by both the webpack-dev-server
  #    b. There is no good reason to hash the server bundle name.
  return @server_bundle_path if @server_bundle_path && !Rails.env.development?

  bundle_name = ReactOnRails.configuration.server_bundle_js_file
  @server_bundle_path = if ReactOnRails::PackerUtils.using_packer?
                          begin
                            bundle_js_file_path(bundle_name)
                          rescue Object.const_get(
                            ReactOnRails::PackerUtils.packer_type.capitalize
                          )::Manifest::MissingEntryError
                            File.expand_path(
                              File.join(ReactOnRails::PackerUtils.packer_public_output_path,
                                        bundle_name)
                            )
                          end
                        else
                          bundle_js_file_path(bundle_name)
                        end
end
server_bundle_path_is_http?() click to toggle source
# File lib/react_on_rails/utils.rb, line 65
def self.server_bundle_path_is_http?
  server_bundle_js_file_path =~ %r{https?://}
end
server_rendering_is_enabled?() click to toggle source
# File lib/react_on_rails/utils.rb, line 40
def self.server_rendering_is_enabled?
  ReactOnRails.configuration.server_bundle_js_file.present?
end
smart_trim(str, max_length = 1000) click to toggle source
# File lib/react_on_rails/utils.rb, line 186
def self.smart_trim(str, max_length = 1000)
  # From https://stackoverflow.com/a/831583/1009332
  str = str.to_s
  return str unless str.present? && max_length >= 1
  return str if str.length <= max_length

  return str[0, 1] + TRUNCATION_FILLER if max_length == 1

  midpoint = (str.length / 2.0).ceil
  to_remove = str.length - max_length
  lstrip = (to_remove / 2.0).ceil
  rstrip = to_remove - lstrip
  str[0..(midpoint - lstrip - 1)] + TRUNCATION_FILLER + str[(midpoint + rstrip)..]
end
source_path() click to toggle source
# File lib/react_on_rails/utils.rb, line 133
def self.source_path
  if ReactOnRails::PackerUtils.using_packer?
    ReactOnRails::PackerUtils.packer_source_path
  else
    ReactOnRails.configuration.node_modules_location
  end
end
truthy_presence(obj) click to toggle source

forum.shakacode.com/t/yak-of-the-week-ruby-2-4-pathname-empty-changed-to-look-at-file-size/901 return object if truthy, else return nil

# File lib/react_on_rails/utils.rb, line 15
def self.truthy_presence(obj)
  if obj.nil? || obj == false
    nil
  else
    obj
  end
end
using_packer_source_path_is_not_defined_and_custom_node_modules?() click to toggle source
# File lib/react_on_rails/utils.rb, line 141
def self.using_packer_source_path_is_not_defined_and_custom_node_modules?
  return false unless ReactOnRails::PackerUtils.using_packer?

  !ReactOnRails::PackerUtils.packer_source_path_explicit? &&
    ReactOnRails.configuration.node_modules_location.present?
end
wrap_message(msg, color = :red) click to toggle source

Wraps message and makes it colored. Pass in the msg and color as a symbol.

# File lib/react_on_rails/utils.rb, line 25
    def self.wrap_message(msg, color = :red)
      wrapper_line = ("=" * 80).to_s
      fenced_msg = <<~MSG
        #{wrapper_line}
        #{msg.strip}
        #{wrapper_line}
      MSG

      Rainbow(fenced_msg).color(color)
    end