Class: ReactOnRails::Generators::BaseGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
GeneratorHelper
Defined in:
lib/generators/react_on_rails/base_generator.rb

Constant Summary collapse

CONFIGURE_RSPEC_TO_COMPILE_ASSETS =
"  RSpec.configure do |config|\n    # Ensure that if we are running js tests, we are using latest webpack assets\n    # This will use the defaults of :js and :server_rendering meta tags\n    ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)\n".strip_heredoc

Instance Method Summary collapse

Methods included from GeneratorHelper

#add_documentation_reference, #copy_file_and_missing_parent_directories, #dest_dir_exists?, #dest_file_exists?, #empty_directory_with_keep_file, #keep_file, #setup_file_error, #symlink_dest_file_to_dest_file

Instance Method Details

#add_base_gems_to_gemfileObject



75
76
77
# File 'lib/generators/react_on_rails/base_generator.rb', line 75

def add_base_gems_to_gemfile
  run "bundle"
end

#add_hello_world_routeObject



21
22
23
# File 'lib/generators/react_on_rails/base_generator.rb', line 21

def add_hello_world_route
  route "get 'hello_world', to: 'hello_world#index'"
end

#add_yarn_dependenciesObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/generators/react_on_rails/base_generator.rb', line 79

def add_yarn_dependencies
  major_minor_patch_only = /\A\d+\.\d+\.\d+\z/
  if ReactOnRails::VERSION.match?(major_minor_patch_only)
    run "yarn add react-on-rails@#{ReactOnRails::VERSION} --exact"
  else
    # otherwise add latest
    puts "Adding the lastest react-on-rails NPM module. Double check this is correct in package.json"
    run "yarn add react-on-rails --exact"
  end

  puts "Adding React dependencies"
  run "yarn add react react-dom @babel/preset-react prop-types babel-plugin-transform-react-remove-prop-types \
      babel-plugin-macros"

  puts "Adding CSS handlers"

  run "yarn add css-loader css-minimizer-webpack-plugin mini-css-extract-plugin style-loader@"

  puts "Adding dev dependencies"
  run "yarn add -D @pmmmwh/react-refresh-webpack-plugin react-refresh"
end

#append_to_spec_rails_helperObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/generators/react_on_rails/base_generator.rb', line 101

def append_to_spec_rails_helper
  rails_helper = File.join(destination_root, "spec/rails_helper.rb")
  if File.exist?(rails_helper)
    add_configure_rspec_to_compile_assets(rails_helper)
  else
    spec_helper = File.join(destination_root, "spec/spec_helper.rb")
    if File.exist?(spec_helper)
      add_configure_rspec_to_compile_assets(spec_helper)
    else
      # rubocop:disable Layout/EmptyLinesAroundArguments
      GeneratorMessages.add_info(
        "\n        We did not find a spec/rails_helper.rb or spec/spec_helper.rb to add\n        the React on Rails Test helper, which ensures that if we are running\n        js tests, then we are using latest webpack assets. You can later add\n        this to your rspec config:\n\n        # This will use the defaults of :js and :server_rendering meta tags\n        ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)\n        MSG\n      )\n      # rubocop:enable Layout/EmptyLinesAroundArguments\n\n    end\n  end\nend\n".strip_heredoc

#copy_base_filesObject



30
31
32
33
34
35
36
37
38
# File 'lib/generators/react_on_rails/base_generator.rb', line 30

def copy_base_files
  base_path = "base/base/"
  base_files = %w[app/controllers/hello_world_controller.rb
                  app/views/layouts/hello_world.html.erb
                  config/initializers/react_on_rails.rb
                  Procfile.dev
                  Procfile.dev-static]
  base_files.each { |file| copy_file("#{base_path}#{file}", file) }
end

#copy_js_bundle_filesObject



40
41
42
43
44
45
46
# File 'lib/generators/react_on_rails/base_generator.rb', line 40

def copy_js_bundle_files
  base_path = "base/base/"
  base_files = %w[app/javascript/packs/server-bundle.js
                  app/javascript/bundles/HelloWorld/components/HelloWorldServer.js
                  app/javascript/bundles/HelloWorld/components/HelloWorld.module.css]
  base_files.each { |file| copy_file("#{base_path}#{file}", file) }
end

#copy_shakapacker_configObject



68
69
70
71
72
73
# File 'lib/generators/react_on_rails/base_generator.rb', line 68

def copy_shakapacker_config
  puts "Adding Shakapacker v7 config file"
  base_path = "base/base/"
  base_files = %w[config/shakapacker.yml]
  base_files.each { |file| copy_file("#{base_path}#{file}", file) }
end

#copy_webpack_configObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/generators/react_on_rails/base_generator.rb', line 48

def copy_webpack_config
  puts "Adding Webpack config"
  base_path = "base/base"
  base_files = %w[babel.config.js
                  config/webpack/clientWebpackConfig.js
                  config/webpack/commonWebpackConfig.js
                  config/webpack/test.js
                  config/webpack/development.js
                  config/webpack/production.js
                  config/webpack/serverWebpackConfig.js
                  config/webpack/webpack.config.js
                  config/webpack/webpackConfig.js]
  config = {
    message: "// The source code including full typescript support is available at:"
  }
  base_files.each do |file|
    template("#{base_path}/#{file}.tt", file, config)
  end
end

#create_react_directoriesObject



25
26
27
28
# File 'lib/generators/react_on_rails/base_generator.rb', line 25

def create_react_directories
  dirs = %w[components]
  dirs.each { |name| empty_directory("app/javascript/bundles/HelloWorld/#{name}") }
end