-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-cli.sh
280 lines (275 loc) · 7.04 KB
/
react-cli.sh
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
pwd=$(pwd)
GREEN='\033[0;32m'
NC='\033[0m'
help_action='--help'
load_action='--load'
init_action='--init'
about_action='--about'
function_flag='-f'
arrow_flag='-a'
class_flag='-c'
component_action='component'
container_action='container'
page_action='page'
rootpath='src'
compath='src/components'
hookpath='src/hooks'
contpath='src/containers'
# greet the user
buildRoute() {
newpath=$1
action=$2
if [ ! -z $newpath ];
then echo "$rootpath/$newpath"
else
case $action in
"$container_action")
echo $contpath
;;
"$component_action")
echo $compath
;;
*)
echo $rootpath
;;
esac
fi
}
loadModule() {
echo -ne '\n'
echo -ne '🚚 # (5%)\r'
sleep 1
echo -ne '🚚 ####### (33%)\r'
sleep 1
echo -ne '🚚 ############### (66%)\r'
sleep 1
echo -ne '🚚 ######################### (100%)\r'
echo -ne '\n'
}
writeIndexFile(){
path_name=$2;
file_name=$1;
file_path_name=$path_name/$file_name/index.js
echo 'import' $file_name 'from' "'./$file_name';" >> $file_path_name
echo '' >> $file_path_name
echo $file_name'.defaultProps = {' >> $file_path_name
echo '' >> $file_path_name
echo " displayName:' $file_name'," >> $file_path_name
echo '' >> $file_path_name
echo '};' >> $file_path_name
echo '' >> $file_path_name
echo 'export default' $file_name >> $file_path_name
}
writeArrowFile(){
path_name=$2;
file_name=$1;
file_path_name=$path_name/$file_name/$file_name.jsx
echo "import React from 'react';" >> $file_path_name
echo "import Style from './$file_name.style.js';" >> $file_path_name
echo '' >> $file_path_name
echo "const" $file_name "= (props) => {" >> $file_path_name
echo " return( <h1> Hello $file_name </h1> );" >> $file_path_name
echo "}" >> $file_path_name
echo '' >> $file_path_name
echo 'export default' $file_name >> $file_path_name
}
writeFunctionFile(){
path_name=$2;
file_name=$1;
file_path_name=$path_name/$file_name/$file_name.jsx
echo "import React from 'react';" >> $file_path_name
echo "import Style from './$file_name.style.js';" >> $file_path_name
echo '' >> $file_path_name
echo "function" $file_name "(props) {" >> $file_path_name
echo " return( <h1> Hello $file_name </h1> );" >> $file_path_name
echo "}" >> $file_path_name
echo '' >> $file_path_name
echo 'export default' $file_name >> $file_path_name
}
writeClassFile(){
path_name=$2;
file_name=$1;
file_path_name=$path_name/$file_name/$file_name.jsx
echo "import React from 'react';" >> $file_path_name
echo "import Style from './$file_name.style.js';" >> $file_path_name
echo '' >> $file_path_name
echo "class" $file_name "extends React.Component {" >> $file_path_name
echo '' >> $file_path_name
echo ' constructor(props) {' >> $file_path_name
echo ' super(props);' >> $file_path_name
echo ' }' >> $file_path_name
echo '' >> $file_path_name
echo " render() {" >> $file_path_name
echo " return( <h1> Hello $file_name </h1> );" >> $file_path_name
echo " }" >> $file_path_name
echo "}" >> $file_path_name
echo '' >> $file_path_name
echo 'export default' $file_name >> $file_path_name
}
switchWrite() {
flag=$1
name=$2
path=$3
if [ -z $flag ];
then writeArrowFile $name $path
else
case $flag in
"$arrow_flag")
writeArrowFile $name $path
;;
"$class_flag")
writeClassFile $name $path
;;
"$function_flag")
writeFunctionFile $name $path
;;
*)
echo "Error: 🧨 Flag not found"
;;
esac
fi
}
createFilesFoldersComponent() {
name=$1;
path=$2;
flag=$3;
mkdir -p $path/$name
touch $path/$name/index.js
writeIndexFile $name $path
touch $path/$name/$name.jsx
switchWrite $flag $name $path
touch $path/$name/$name.style.js
echo -ne '\n'
echo -e "🎉 ${GREEN} The Component $name was created 🎉 ${NC}"
}
createFilesFoldersContainer() {
name=$1;
path=$2;
flag=$3;
mkdir -p $path/$name
touch $path/$name/index.js
writeIndexFile $name $path
touch $path/$name/$name.jsx
switchWrite $flag $name $path
echo -ne '\n'
echo -e "🎉 ${GREEN} The Container $name was created 🎉 ${NC}"
}
createInitProject() {
path=$1
mkdir -p $path/$rootpath/'components';
mkdir -p $path/$rootpath/'containers';
mkdir -p $path/$rootpath/'pages';
mkdir -p $path/$rootpath/'app';
mkdir -p $path/$rootpath/'hooks';
mkdir -p $path/$rootpath/'utils';
mkdir -p $path/$rootpath/'services';
mkdir -p $path/$rootpath/'redux';
mkdir -p $path/$rootpath/'providers';
createFilesFoldersComponent 'Hellow' "$1/src/components" '-f'
createFilesFoldersContainer 'HellowActions' "$1/src/containers" '-f'
echo -ne '\n'
echo -e "🎉 ${GREEN} The Project was built, Happy Hack 🎉 ${NC}"
}
switchAction() {
action=$1
component_name=$2
final_path=$3
flag=$4
case $action in
"$container_action")
createFilesFoldersContainer $component_name $final_path $flag;
;;
"$component_action")
createFilesFoldersComponent $component_name $final_path $flag;
;;
*)
echo "Error: 🧨 Something was wrong"
;;
esac
}
handlerCreator () {
action=$1
flag=$2
name=$3
path=$4
final_path=$(buildRoute "$path" "$action");
component_name="$(tr '[:lower:]' '[:upper:]' <<< ${name:0:1})${name:1}"
if [ -d ${final_path} ]
then
if [ ! -z $component_name ];
then echo "Status: 👩💻 Checking if the $action already exist..."
if [ ! -d ${final_path}/${component_name} ]
then
echo -e "Status: ${GREEN}✔ Successfully check${NC}"
echo "Status: 👨💻 Creating $action"
loadModule
switchAction $action $component_name $final_path $flag;
else
echo "Warning: 👮♂ The $action alreay exist"
fi
else
echo "Error: 🧨 The $action name can not be empty"
fi
else
echo "Error: 🧨 The path does not exist"
fi
}
handlerInit() {
component_name=$1
if [ ! -z $component_name ];
then
echo -e "Status: 👨💻 ${GREEN} Starting project...${NC}"
npx create-react-app "$component_name"
createInitProject "$component_name"
else
echo "Error: 🧨 reactlt --init needs the project name"
fi
}
printListCommands() {
echo -ne '\n'
echo "Commands:"
echo " component [flag]* [ name ]* [ custom_path ] for create presentational elements"
echo " container [flag]* [ name ]* [ custom_path ] for create funcional elements"
echo -ne '\n'
echo "Flags:"
echo " -f for create function element"
echo " -a for create arrow function element"
echo " -c for create class element"
echo -ne '\n'
echo "Options:"
echo " --help Print all commands and options"
echo " --load Load any file with your element template"
echo " --init Build your custom element rules"
echo " --about Print info about creator"
echo -ne '\n'
}
if [ ! -z $1 ];
then
case $1 in
"$container_action")
handlerCreator "$1" "$2" "$3" "$4"
;;
"$component_action")
handlerCreator "$1" "$2" "$3" "$4"
;;
"$help_action")
printListCommands
;;
"$init_action")
handlerInit "$2"
;;
"$load_action")
echo '🧞 Hey!: reactlt load'
;;
"$about_action")
echo '🧞 Hey!: reactlt about'
;;
*)
echo 'Error: 🧨 command' $1 'not found'
;;
esac
else
echo '🧞 Hey!: reactlt needs some command'
echo ' Check the commands list with reactlt --help'
fi