-
Notifications
You must be signed in to change notification settings - Fork 0
/
g
executable file
·102 lines (83 loc) · 2.64 KB
/
g
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
#!/usr/bin/env ruby
# React Component Generator
#
# Usage:
#
# Create a component in the src/components dir:
# $ ./g c form-input
#
# Create a page in the src/pages dir:
# $ ./g p contact
#
# Create redux reducer/action/types files in the src/redux dir:
# $ ./g r cart
#
# Use snake-case.
COMPONENTS_DIR = './src/components'
PAGES_DIR = './src/pages'
REDUX_DIR = './src/redux'
component_type = ARGV[0]
component_filename = ARGV[1]
component_base_name = component_filename.split('-')
.map { |w| w.capitalize() }
.join('')
component_name = if component_type == 'p'
"#{component_base_name}Page"
else
component_base_name
end
component_output_filename = "#{component_filename}.js";
styles_output_filename = "#{component_filename}.scss";
def write_template(d, fname, content)
File.open("#{d}/#{fname}", 'w') { |f| f.write(content) }
end
# This is the template for a React component.
component_template = %Q[
import React from "react";
import "./#{component_filename}.scss";
const #{component_name} = () => (
<div className="#{component_filename}">#{component_name}</div>
);
export default #{component_name};
].strip()
# This is the template for the component's scss file.
styles_template = %Q[
.#{component_filename} {
}
].strip()
reducer_template = %Q[
import { #{component_name}ActionTypes } from "./#{component_filename}.types";
].strip()
actions_template = %Q[
import { #{component_name}ActionTypes } from "./#{component_filename}.types";
].strip()
types_template = %Q[
export const #{component_name}ActionTypes = {
};
].strip()
if component_type == 'p'
dirname = "#{PAGES_DIR}/#{component_filename}"
elsif component_type == 'r'
dirname = "#{REDUX_DIR}/#{component_filename}"
else
dirname = "#{COMPONENTS_DIR}/#{component_filename}"
end
unless Dir.exist?(dirname)
Dir.mkdir(dirname)
if component_type == 'r'
# write redux files
write_template(dirname, "#{component_filename}.reducer.js", reducer_template)
write_template(dirname, "#{component_filename}.actions.js", actions_template)
write_template(dirname, "#{component_filename}.types.js", types_template)
write_template(dirname, "#{component_filename}.selectors.js", "");
write_template(dirname, "#{component_filename}.utils.js", "");
puts "created Redux files at #{dirname}"
else
# write component files
write_template(dirname, component_output_filename, component_template)
write_template(dirname, styles_output_filename, styles_template)
puts "created React component files at #{dirname}"
end
else
puts "files already exist at #{dirname}!"
end