-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexp.sh
More file actions
160 lines (130 loc) · 3.89 KB
/
exp.sh
File metadata and controls
160 lines (130 loc) · 3.89 KB
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
#!/bin/sh
# exp v1.0
# developed by Malcolm Kiano (https://malcolmkiano.com)
# credit to Newton Karanu (https://newtonkaranu.me/) for refactoring the installation process
setup_color() {
# Only use colors if connected to a terminal
if [ -t 1 ]; then
RED=$(printf '\033[31m')
GREEN=$(printf '\033[32m')
CYAN=$(printf '\033[36m')
RESET=$(printf '\033[m')
else
RED=""
GREEN=""
CYAN=""
RESET=""
fi
}
error() {
echo ${RED}"$@"${RESET} >&2
}
success() {
echo ${GREEN}"$@"${RESET} >&2
}
cyan() {
echo ${CYAN}"$@"${RESET} >&2
}
exp() {
setup_color
# replace this value with the url to your express/express-knex boilerplate
defaulturl="https://github.com/malcolmkiano/express-boilerplate.git"
defaulturl_knex="https://github.com/malcolmkiano/express-knex-boilerplate.git"
# replace this value with the value in "name" in your boilerplate's package.json
defaultname="express-boilerplate"
defaultname_knex="express-knex-boilerplate"
# replace this value with the value in "description" in your boilerplate's package.json
defaultdesc="A starting point for Express apps"
defaultdesc_knex="A starting point for Express apps with Knex and a PostgreSQL Database"
# You can also run this without changing the values, and it'll start up a project based off one of my boilerplates
# at https://github.com/malcolmkiano/express-boilerplate
# or https://github.com/malcolmkiano/express-knex-boilerplate
# Don't change anything beyond this line ======================================
url="$defaulturl"
name="$defaultname"
desc="$defaultdesc"
# Display Help for -h option
Help(){
echo "Creates an Express JS app using '$defaultname' repo at $url"
echo
echo "Syntax: ${CYAN}exp ${GREEN}project-name ${CYAN}'project description'${RESET} [-options]"
echo "Example: ${CYAN}exp ${GREEN}hello-world ${CYAN}'my first project'${RESET}"
echo "Options:"
cyan "k Scaffold an Express app with Knex"
cyan "h Print this help."
}
while getopts ":hk" option; do
case $option in
h) # display Help
Help
return 0;;
k)
url="$defaulturl_knex"
name="$defaultname_knex"
desc="$defaultdesc_knex";;
\?) # incorrect option
error "Usage: exp [-h|-k]"
return 1;;
esac
shift $((OPTIND -1));
done
# check if name is empty
if [ -z "$1" ]
then
# can't run without project name
error "Must provide project name"
echo "Syntax: ${CYAN}exp ${GREEN}project-name${RESET} ${CYAN}'project description'${RESET} [-options]"
return 1
else
# check validity of project name
if [[ "$1" =~ [A-Za-z0-9_-]+$ ]]
then
# get the values
projectname="$1"
else
error "Project name '$projectname' includes illegal characters"
return 1
fi
if [ -n "$2" ]
then
description="$2"
else
description="An Express app"
fi
# get it going
echo
cyan "Starting up a new project using the ${GREEN}$name ${CYAN}repo"
sleep 2s
# clone the repository & cd into it
git clone --depth=1 "$url" "$projectname"
cd "$projectname" || exit
echo
# reinitialize repo
cyan "Reinitializing git repo and installing node packages"
rm -rf .git && git init
# install the packages
npm install
# move the env file
mv example.env .env
# replace the title and description in package.json
sed -i -e "s/$name/$projectname/g" package-lock.json
sed -i -e "s/$name/$projectname/g" package.json
sed -i -e "s/$desc/$description/g" package.json
# clean up README.md
echo "# $projectname" > README.md
echo "$description" >> README.md
# get out of there!
cd ..
# tell them how to start
echo
echo "To get started:"
cyan " cd $projectname"
cyan " npm start"
# let's rock & roll, baby
echo
echo "Let's get it! ${CYAN}#Express${RESET}"
echo
return 0
fi
}
exp "$@"