-
Notifications
You must be signed in to change notification settings - Fork 756
/
Copy pathcomponent_generator.rb
187 lines (155 loc) · 5.61 KB
/
component_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
37
38
39
40
41
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
module React
module Generators
class ComponentGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path '../../templates', __FILE__
desc <<-DESC.strip_heredoc
Description:
Scaffold a React component into `components/` of your Webpacker source or asset pipeline.
The generated component will include a basic render function and a PropTypes
hash to help with development.
Available field types:
Basic prop types do not take any additional arguments. If you do not specify
a prop type, the generic node will be used. The basic types available are:
any
array
bool
element
func
number
object
node
shape
string
Special PropTypes take additional arguments in {}, and must be enclosed in
single quotes to keep bash from expanding the arguments in {}.
instanceOf
takes an optional class name in the form of {className}
oneOf
behaves like an enum, and takes an optional list of strings that will
be allowed in the form of 'name:oneOf{one,two,three}'.
oneOfType.
oneOfType takes an optional list of react and custom types in the form of
'model:oneOfType{string,number,OtherType}'
Examples:
rails g react:component person name
rails g react:component restaurant name:string rating:number owner:instanceOf{Person}
rails g react:component food 'kind:oneOf{meat,cheese,vegetable}'
rails g react:component events 'location:oneOfType{string,Restaurant}'
DESC
argument :attributes,
:type => :array,
:default => [],
:banner => "field[:type] field[:type] ..."
class_option :es6,
type: :boolean,
default: false,
desc: 'Output es6 class based component'
class_option :coffee,
type: :boolean,
default: false,
desc: 'Output coffeescript based component'
REACT_PROP_TYPES = {
"node" => 'React.PropTypes.node',
"bool" => 'React.PropTypes.bool',
"boolean" => 'React.PropTypes.bool',
"string" => 'React.PropTypes.string',
"number" => 'React.PropTypes.number',
"object" => 'React.PropTypes.object',
"array" => 'React.PropTypes.array',
"shape" => 'React.PropTypes.shape({})',
"element" => 'React.PropTypes.element',
"func" => 'React.PropTypes.func',
"function" => 'React.PropTypes.func',
"any" => 'React.PropTypes.any',
"instanceOf" => ->(type) {
'React.PropTypes.instanceOf(%s)' % type.to_s.camelize
},
"oneOf" => ->(*options) {
enums = options.map{|k| "'#{k.to_s}'"}.join(',')
'React.PropTypes.oneOf([%s])' % enums
},
"oneOfType" => ->(*options) {
types = options.map{|k| "#{lookup(k.to_s, k.to_s)}" }.join(',')
'React.PropTypes.oneOfType([%s])' % types
},
}
def create_component_file
template_extension = case
when options[:es6]
'es6.jsx'
when options[:coffee]
'js.jsx.coffee'
else
'js.jsx'
end
# Prefer webpacker to sprockets:
if webpacker?
new_file_name = file_name.camelize
extension = options[:coffee] ? "coffee" : "js"
target_dir = webpack_configuration.source_path
.join("components")
.relative_path_from(::Rails.root)
.to_s
else
new_file_name = file_name
extension = template_extension
target_dir = 'app/assets/javascripts/components'
end
file_path = File.join(target_dir, "#{new_file_name}.#{extension}")
template("component.#{template_extension}", file_path)
end
private
def webpack_configuration
Webpacker.respond_to?(:config) ? Webpacker.config : Webpacker::Configuration
end
def component_name
file_name.camelize
end
def file_header
if webpacker?
%|var React = require("react")\n|
else
""
end
end
def file_footer
if webpacker?
%|module.exports = #{component_name}|
else
""
end
end
def webpacker?
defined?(Webpacker)
end
def parse_attributes!
self.attributes = (attributes || []).map do |attr|
name, type, options = "", "", ""
options_regex = /(?<options>{.*})/
name, type = attr.split(':')
if matchdata = options_regex.match(type)
options = matchdata[:options]
type = type.gsub(options_regex, '')
end
{ :name => name, :type => lookup(type, options) }
end
end
def self.lookup(type = "node", options = "")
react_prop_type = REACT_PROP_TYPES[type]
if react_prop_type.blank?
if type =~ /^[[:upper:]]/
react_prop_type = REACT_PROP_TYPES['instanceOf']
else
react_prop_type = REACT_PROP_TYPES['node']
end
end
options = options.to_s.gsub(/[{}]/, '').split(',')
react_prop_type = react_prop_type.call(*options) if react_prop_type.respond_to? :call
react_prop_type
end
def lookup(type = "node", options = "")
self.class.lookup(type, options)
end
end
end
end