class ReactRailsWebpack::InstallGenerator

Public Instance Methods

add_react_helper() click to toggle source
# File lib/react_rails_webpack/install_generator.rb, line 38
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_json() click to toggle source
# File lib/react_rails_webpack/install_generator.rb, line 34
def add_root_package_dot_json
  copy_file 'package.json'
end
add_webpack_asset_inclusion() click to toggle source
# File lib/react_rails_webpack/install_generator.rb, line 54
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
check_for_node() click to toggle source
# File lib/react_rails_webpack/install_generator.rb, line 11
def check_for_node
  if `which node`.length == 0
    puts "Looks like you don't have node installed yet."
    puts "This generator cannot work without node."
    puts "Go here to install it: #{'https://nodejs.org'.blue}"
    exit
  end
end
check_for_npm() click to toggle source
# File lib/react_rails_webpack/install_generator.rb, line 20
def check_for_npm
  if `which npm`.length == 0
    puts "Looks like you don't have npm installed yet."
    puts "This generator cannot work without npm."
    puts "Go here to install the latest version of node (which will include npm): #{'https://nodejs.org'.blue}"
    exit
  end
end
run_new_fork_generator() click to toggle source
# File lib/react_rails_webpack/install_generator.rb, line 85
def run_new_fork_generator
  Rails::Generators.invoke('react_rails_webpack:new_fork')
end
run_npm_install_and_build_and_then_say_whats_next() click to toggle source
# File lib/react_rails_webpack/install_generator.rb, line 89
    def run_npm_install_and_build_and_then_say_whats_next
      puts
      puts "-" * `tput cols`.to_i # print line of dashes
      puts
      puts "Integration files all setup. Now running #{'npm run install'.white.bold} and \
#{'npm run build'.white.bold} for you..."
      puts
      puts "-" * `tput cols`.to_i # print line of dashes
      puts
      
      exec('npm run install && npm run build; rake react_rails_webpack:print_whats_next_instructions')
    end
setup_client_folder() click to toggle source
# File lib/react_rails_webpack/install_generator.rb, line 29
def setup_client_folder
  puts 'Adding client folder...'
  directory 'client'
end
update_gitignore() click to toggle source
# File lib/react_rails_webpack/install_generator.rb, line 47
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

Private Instance Methods

current_directory_path() click to toggle source
# File lib/react_rails_webpack/install_generator.rb, line 105
def current_directory_path
  File.join(File.expand_path(File.dirname(__FILE__)))
end
ensure_prepended(line, file) click to toggle source
# File lib/react_rails_webpack/install_generator.rb, line 109
def ensure_prepended(line, file)
  return if file_already_contains?(line, file)
  prepend_to_file(file, line)
end
file_already_contains?(string, file) click to toggle source
# File lib/react_rails_webpack/install_generator.rb, line 118
def file_already_contains?(string, file)
  File.readlines(file).join.include?(string)
end
file_missing?(file) click to toggle source
# File lib/react_rails_webpack/install_generator.rb, line 114
def file_missing?(file)
  !File.exist?(file)
end