Class: ReactRailsWebpack::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/react_rails_webpack/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_react_helperObject



19
20
21
22
23
24
25
26
# File 'lib/react_rails_webpack/install_generator.rb', line 19

def add_react_helper
  puts 'Adding react_helper.rb...'
  inside 'app' do
    inside 'helpers' do
      copy_file 'react_helper.rb'
    end
  end
end

#add_root_package_dot_jsonObject



15
16
17
# File 'lib/react_rails_webpack/install_generator.rb', line 15

def add_root_package_dot_json
  copy_file 'package.json'
end

#add_webpack_asset_inclusionObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/react_rails_webpack/install_generator.rb', line 35

def add_webpack_asset_inclusion
  puts 'Adding asset includes...'
  # Add webpack folder to application asset paths
  app_dot_rb_insert = "    config.assets.paths << \"\#\{config.root\}/app/assets/webpack\"\n"
  insert_into_file(
    'config/application.rb',
    app_dot_rb_insert,
    after: "  class Application < Rails::Application\n"
  ) unless file_already_contains?(app_dot_rb_insert, 'config/application.rb')

  # Add webpack folder require to application.js
  app_dot_js_insert = "//= require_tree ../webpack\n"
  insert_into_file(
    'app/assets/javascripts/application.js',
    app_dot_js_insert,
    after: "//= require_tree .\n"
  ) unless file_already_contains?(app_dot_js_insert, 'app/assets/javascripts/application.js')

  # Add webpack folder require to application.css or .scss or .sass
  if File.exist?('app/assets/stylesheets/application.css')
    insert_into_file(
      'app/assets/stylesheets/application.css',
      "\n *= require_tree ../webpack\n",
      before: ' *= require_self'
    ) unless file_already_contains?("\n *= require_tree ../webpack\n", 'app/assets/stylesheets/application.css')
  end

  ensure_prepended "@import '../webpack/*\n", 'app/assets/stylesheets/application.scss' if File.exist?('app/assets/stylesheets/application.scss')
  ensure_prepended "@import '../webpack/*\n", 'app/assets/stylesheets/application.sass' if File.exist?('app/assets/stylesheets/application.sass')
end


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/react_rails_webpack/install_generator.rb', line 66

def print_reminders
  puts
  puts "-" * `tput cols`.to_i # print line of dashes
  puts
  puts "Done! Now #{"make sure you have node and npm installed".red.bold}, and then #{"run the".red.bold} #{"npm run install".white.bold} #{"and".red.bold} #{"npm run build".white.bold} #{"commands".red.bold} to finish setting up."
  puts
  puts "Also, #{"don't forget to set your hostname".red.bold} in the #{"client/environment.json".white.bold} file. Instructions for this can be found here: #{"https://github.com/neurodynamic/react_rails_webpack#setting-the-dev-server-hostname".blue}"
  puts
  puts "-" * `tput cols`.to_i # print line of dashes
  puts
end

#setup_client_folderObject



10
11
12
13
# File 'lib/react_rails_webpack/install_generator.rb', line 10

def setup_client_folder
  puts 'Adding client folder...'
  directory 'client'
end

#update_gitignoreObject



28
29
30
31
32
33
# File 'lib/react_rails_webpack/install_generator.rb', line 28

def update_gitignore
  puts 'updating .gitignore...'
  append_to_file '.gitignore' do
    "\n\n\# react_rails_webpack ignores\nclient/node_modules\nclient/environment.json\nnpm-debug.log"
  end
end