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



38
39
40
41
42
43
44
45
# 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_jsonObject



34
35
36
# File 'lib/react_rails_webpack/install_generator.rb', line 34

def add_root_package_dot_json
  copy_file 'package.json'
end

#add_webpack_asset_inclusionObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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_nodeObject



11
12
13
14
15
16
17
18
# 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_npmObject



20
21
22
23
24
25
26
27
# 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_generatorObject



85
86
87
# 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_nextObject



89
90
91
92
93
94
95
96
97
98
99
100
# 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_folderObject



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

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

#update_gitignoreObject



47
48
49
50
51
52
# 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