-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact_generator.rb
36 lines (28 loc) · 991 Bytes
/
react_generator.rb
1
2
3
4
5
6
7
8
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
35
36
module Rolemodel
class ReactGenerator < Rails::Generators::Base
source_root File.expand_path('templates', __dir__)
def add_npm_packages
@add_react = yes?('Would you like to add react?')
if @add_react
run 'yarn add react react-dom'
end
end
def add_files
if @add_react
say 'Copying files'
template 'app/helpers/react_helper.rb', 'app/helpers/react_helper.rb'
template 'app/javascript/components/HelloReact.jsx', 'app/javascript/components/HelloReact.jsx'
template 'app/javascript/controllers/react_controller.js', 'app/javascript/controllers/react_controller.js'
end
end
def import_react_controller
if @add_react
say 'Importing react_controller.js'
append_to_file 'app/javascript/controllers/index.js', <<~JS
import ReactController from './react_controller.js'
application.register('react', ReactController)
JS
end
end
end
end