forked from haldunanil/create-reactjs-component
/
build.sh
executable file
·140 lines (123 loc) · 3.42 KB
/
build.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
#!/bin/bash
validateCWD() {
# check that there is a src directory i.e. we're in the parent dir
dir="src"
if [ -d "${dir}" ]
then
printf "Found ${dir} directory.\n"
else
printf "You are in the wrong directory. You should be in the root of the React project, and ${dir}/ should be a subdirectory.\n"
exit 1
fi
}
validateTemplateDir() {
# check that there is a src directory i.e. we're in the parent dir
repo_path="$1"
template="$2"
dir="${repo_path}/templates/${template}"
pwd
printf "$repo_path"
pwd
if [ -d "${dir}" ]
then
printf "Found '${template}' template.\n"
else
printf "There is no template called '${template}'.\n"
exit 1
fi
}
makeComponentsDir() {
# check if components folder already exists, if not create
if [ -d "src/components" ]; then
# components folder exists, so do nothing
printf 'Found src/components subdirectory.\n'
else
# components folder doesn't exist, so create
printf "src/components doesn\'t exist, creating.\n"
mkdir src/components
fi
}
makeComponent() {
# args
REPO_DIR="$1"
TEMPLATE="$2"
COMPONENT="$3"
# initialize files and folders for the component
if [ -d src/components/$COMPONENT ]; then
# folder already exists
printf "$COMPONENT folder already exists, skipping.\n"
else
# create the component directory
mkdir src/components/$COMPONENT
# create index.js
sh $REPO_DIR/templates/$TEMPLATE/create-index.js.sh $COMPONENT
# create $COMPONENT.js
sh $REPO_DIR/templates/$TEMPLATE/create-component.js.sh $COMPONENT
# create $COMPONENT.scss (if it exists)
sh $REPO_DIR/templates/$TEMPLATE/create-component.scss.sh $COMPONENT
# create styles.js (if it exists)
sh $REPO_DIR/templates/$TEMPLATE/create-styles.js.sh $COMPONENT
# create $COMPONENT.test.js
sh $REPO_DIR/templates/$TEMPLATE/create-component.test.js.sh $COMPONENT
# Inform user that everything was created
printf "$COMPONENT component was created. Looks like:\n"
printf "
src
├── components
├── $COMPONENT
├── index.js
├── $COMPONENT.js
├── $COMPONENT.test.js
├── $COMPONENT.scss
├── README.md
"
fi
}
printUsage() {
printf "Usage: create-react-component [-t template-name] NewComponentName\n"
}
run() {
# args
repo_path="$1"
template="$2"
component="$3"
# check that there is a src directory i.e. we're in the parent dir
validateCWD
# chacks that the template you requested exists
validateTemplateDir "${repo_path}" "${template}"
# check if components folder already exists, if not create
makeComponentsDir
# initialize files and folders for the component
makeComponent "${repo_path}" "${template}" "${component}"
}
# MAIN script starts here
# default args
template='default-template'
component='DefaultComponentName'
while test $# -gt 0; do
case "$1" in
-t|--template)
shift
if test $# -gt 0; then
template="$1"
fi
shift
;;
*)
if test $# -gt 0; then
component="$1"
fi
shift
break
;;
esac
done
# get location of build.sh
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
repo_path="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
run "${repo_path}" "${template}" "${component}"