Class: Repack::InstallGenerator

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

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_to_gitignoreObject



116
117
118
119
120
121
122
123
124
# File 'lib/generators/repack/install_generator.rb', line 116

def add_to_gitignore
  append_to_file ".gitignore" do
    "    /node_modules\n    /public/webpack\n    npm-debug.log\n    EOF\n  end\nend\n".strip_heredoc

#copy_package_jsonObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generators/repack/install_generator.rb', line 9

def copy_package_json
  copy_file "package.json", "package.json"
  if options[:router]
    insert_into_file './package.json', after: /dependencies\": {\n/ do
      "\"react-router\": \"^2.4.1\",\n      RUBY\n    end\n  end\n  if options[:redux]\n    insert_into_file './package.json', after: /dependencies\\\": {\\n/ do\n      <<-'RUBY'\n\"react-redux\": \"^4.4.5\",\n\"redux\": \"^3.5.2\",\n\"redux-thunk\": \"^2.1.0\",\n      RUBY\n    end\n  end\n  if options[:router] && options[:redux]\n      insert_into_file './package.json', after: /dependencies\\\": {\\n/ do\n      <<-'RUBY'\n\"react-router-redux\": \"^4.0.5\",\n      RUBY\n     end\n  end\nend\n"

#copy_webpack_confObject



35
36
37
38
39
40
41
# File 'lib/generators/repack/install_generator.rb', line 35

def copy_webpack_conf
  copy_file "webpack.config.js", "config/webpack.config.js"
  if yes?('Are you going to be deploying to heroku? (yes \ no)')
    puts 'copying heroku webpack config!'
    copy_file "webpack.config.heroku.js", "config/webpack.config.heroku.js"
  end
end

#create_webpack_application_jsObject



42
43
44
45
46
47
48
49
50
51
52
53
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/generators/repack/install_generator.rb', line 42

def create_webpack_application_js
  empty_directory "client"
  empty_directory "client/containers"
  empty_directory "client/components"
  if options[:router] && options[:redux]
    copy_file "boilerplate/router_redux/application.js", "client/application.js"
    copy_file "boilerplate/routes.js", "client/routes.js"
    copy_file "boilerplate/router_redux/store.js", "client/store.js"
    copy_file "boilerplate/router_redux/reducers.js", "client/reducers/index.js"
    create_file "client/actions.js"
    copy_file "boilerplate/router/App.js", "client/containers/App.js"
    copy_file "boilerplate/router/NoMatch.js", "client/components/NoMatch.js"
  elsif options[:router]
    copy_file "boilerplate/router/application.js", "client/application.js"
    copy_file "boilerplate/routes.js", "client/routes.js"
    copy_file "boilerplate/router/App.js", "client/containers/App.js"
    copy_file "boilerplate/router/NoMatch.js", "client/components/NoMatch.js"
  elsif options[:redux]
    copy_file "boilerplate/redux/application.js", "client/application.js"
    copy_file "boilerplate/redux/store.js", "client/store.js"
    copy_file "boilerplate/redux/reducers.js", "client/reducers/index.js"
    create_file "client/actions.js"
    copy_file "boilerplate/App.js", "client/containers/App.js"
  else
    copy_file "boilerplate/application.js", "client/application.js"
    copy_file "boilerplate/App.js", "client/containers/App.js"
  end

  haml_installed = Gem.loaded_specs.has_key? 'haml-rails'
  layouts_dir = 'app/views/layouts'
       haml_installed = Gem.loaded_specs.has_key? 'haml-rails'
  layouts_dir = 'app/views/layouts'

  application_view = haml_installed ? "#{layouts_dir}/application.html.haml" : "#{layouts_dir}/application.html.erb"

  if haml_installed
    if yes?('Convert all existing ERB views into HAML? (yes / no)')
      begin
        require 'html2haml'
      rescue LoadError
        `gem install html2haml`
      end
      `find . -name \*.erb -print | sed 'p;s/.erb$/.haml/' | xargs -n2 html2haml`
      `rm #{layouts_dir}/application.html.erb`
    end

    insert_into_file application_view, before: /%body/ do
      "- if Rails.env.development?\n  %script{:src => \"http://localhost:3808/webpack-dev-server.js\"}\n      RUBY\n    end\n\n    insert_into_file application_view, after: /= yield/ do\n        <<-'RUBY'\n\n= javascript_include_tag *webpack_asset_paths('application')\n        RUBY\n    end\n  else\n    insert_into_file application_view, before: /<\\/head>/ do\n      <<-'RUBY'\n<% if Rails.env.development? %>\n  <script src=\"http://localhost:3808/webpack-dev-server.js\"></script>\n<% end %>\n      RUBY\n    end\n    insert_into_file application_view, before: /<\\/body>/ do\n      <<-'RUBY'\n<%= javascript_include_tag *webpack_asset_paths('application') %>\n      RUBY\n    end\n  end\nend\n"

#install_yarnObject



126
127
128
129
130
131
# File 'lib/generators/repack/install_generator.rb', line 126

def install_yarn
  if yes?('Do you want to install and use yarn as your package manager? (yes / no)')
    @yarn_installed = true
    run "npm install yarn --save-dev"
  end
end

#run_package_manager_installObject



133
134
135
136
137
138
139
# File 'lib/generators/repack/install_generator.rb', line 133

def run_package_manager_install
  if @yarn_installed
    run "yarn install" if yes?("Would you like us to run 'yarn install' for you?")
  else
    run "npm install" if yes?("Would you like us to run 'npm install' for you?")
  end
end

#whats_nextObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/generators/repack/install_generator.rb', line 141

def whats_next
  puts "    We've set up the basics of repack for you, but you'll still\n    need to:\n      1. Add an element with an id of 'app' to your layout\n      2. To disable hot module replacement remove <script src=\"http://localhost:3808/webpack-dev-server.js\"></script> from layout\n      3. Run 'yarn run dev_server' to run the webpack-dev-server\n      4. Run 'bundle exec rails s' to run the rails server (both servers must be running)\n      5. If you are using react-router and want to sync server routes add:\n         get '*unmatched_route', to: <your client controller>#<default action>\n         This must be the very last route in your routes.rb file\n         e.g. get '*unmatched_route', to: 'home#index'\n      FOR HEROKU DEPLOYS:\n      1.  yarn run heroku-setup\n      2.  Push to heroku the post-build hook will take care of the rest\n    See the README.md for this gem at\n    https://github.com/cottonwoodcoding/repack/blob/master/README.md\n    for more info.\n    Thanks for using repack!\n  EOF\nend\n".strip_heredoc