Class: WebpackNative::InstallGenerator

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

Instance Method Summary collapse

Instance Method Details

#add_node_modules_to_gitignoreObject


43
44
45
46
47
48
49
# File 'lib/generators/webpack_native/install_generator.rb', line 43

def add_node_modules_to_gitignore
  puts "\nAdding /app/webpack_native/node_modules to .gitignore...\n"
  folder_to_ignore = '/app/webpack_native/node_modules'
  # add webpack_native's node_modules folder to gitignore if it isn't added yet
  %x{ grep -qxF '#{folder_to_ignore}' .gitignore || echo '#{folder_to_ignore}' >> .gitignore }
  puts "=> Done!"
end

#add_webpack_native_folderObject

(1) we copy webpack_native folder into Rails /app folder


11
12
13
14
15
16
# File 'lib/generators/webpack_native/install_generator.rb', line 11

def add_webpack_native_folder
  directory 'webpack_native', 'app/webpack_native'
  # Not sure but using the gem from rubygems... seems like the "images" directory isn't created when this generator runs, in the meantime let's add it (once again maybe?)
  images_directory = 'app/webpack_native/src/images'
  empty_directory(images_directory) unless Dir.exist?(images_directory)
end

#inject_stylesheets_and_javascript_tagsObject

(2) insert necessary helpers in the layouts/application.html.erb to render the <link> and <javascript> tags


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

def inject_stylesheets_and_javascript_tags
  application_layout = "#{Rails.root}/app/views/layouts/application.html.erb"

  stylesheets_tag = "<%= webpack_stylesheet_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>"

  javascripts_tag = "<%= webpack_javascript_tag 'application', 'data-turbolinks-track': 'reload' %>"

  inject_into_file application_layout, "\n\t\t#{stylesheets_tag}\n\t\t#{javascripts_tag}\n", :before => '</head>'
end

#output_installation_finished_msgObject


51
52
53
# File 'lib/generators/webpack_native/install_generator.rb', line 51

def output_installation_finished_msg
  puts "\n\e[32m*** WebpackNative - installation finished. ***\e[0m\n"
end

#run_npm_installObject

(3) run ‘npm install’ inside app/webpack_native folder to install the base modules


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/generators/webpack_native/install_generator.rb', line 30

def run_npm_install
  install_notice = "\n||==>> Installing Node Modules... \n"
  wait_notice = "** This can take a while. Please be patient!\n\n"

  puts "\e[36m#{install_notice}\e[0m" # colorize output in cyan
  puts "\e[33m#{wait_notice}\e[0m" # colorize output in brown
  # more colors can be found at: https://stackoverflow.com/questions/1489183/colorized-ruby-output-to-the-terminal

  Dir.chdir "#{Rails.root}/app/webpack_native" do
    %x{ yarn install } # 88% + 80% faster than npm install
  end
end