-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.sh
executable file
·147 lines (127 loc) · 3.56 KB
/
server.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
# SCRIPT TO COPY SOURCE FILES TO A REMOTE
# SERVER AND BUILD/RUN THE SERVER REMOTELY.
clear
date
echo -e "===============================\n"
# {{{ CONSTANTS
# Change these accordingly:
remoteUser="delixir"
remoteIP="172.16.42.7"
remoteDir="/home/delixir/learnServant"
project="tic-tac-toe"
remoteProjectDir="$remoteUser@$remoteIP:$remoteDir/$project"
# Note the project directory is presumed to be structured as:
#
# $project # main directory
# | $project-frontend # frontend directory
# | $project-backend # backend directory
# | server.sh
# | README.md
# | etc.
# }}}
# {{{ FUNCTIONS
copyBackend() { # Copy the backend files to the remote server.
# {{{
echo "Copying backend files..."
rsync -a -e ssh \
--exclude=".*" \
$project-backend \
$remoteProjectDir
echo -e "Done.\n"
# }}}
}
copyFrontend() { # Copy the frontend files to the remote server.
# {{{
echo "Copying frontend files..."
rsync -a -e ssh \
--exclude=".*" \
--exclude="node_modules" \
--exclude="elm-stuff" \
$project-frontend \
$remoteProjectDir
echo -e "Done.\n"
# }}}
}
closeServer() { # Two attempts at finding the server process and killing it.
# {{{
echo -e "Closing the server..."
pid=$(runRemoteCommand "ps -eo pid,stat,command | grep $remoteDir/$project/$project-backend/.stack-work/install/* | grep 'Ssl' | awk '{print \$1}'")
runRemoteCommand "kill $pid"
pid=$(runRemoteCommand "ps -eo pid,stat,command | grep $remoteDir/$project/$project-backend/.stack-work/install/* | grep 'Sl' | awk '{print \$1}'")
runRemoteCommand "kill $pid"
echo -e "Done.\n"
# }}}
}
runRemoteCommand() { # Run a given command on the remote machine via ssh.
# {{{
ssh $remoteUser@$remoteIP $1
# }}}
}
runNpm() { # Run an npm script command inside the remote frontend folder.
# {{{
runRemoteCommand "cd $remoteDir/$project/$project-frontend && npm $1"
# }}}
}
copyConfig() { # Copy a given file from the remote frontend folder to the local machine.
# {{{
scp $remoteProjectDir/$project-frontend/$1 $project-frontend/$1
# }}}
}
# }}}
case $1 in
npm)
# {{{
copyFrontend
runNpm $2
;;
# }}}
start)
# {{{
closeServer
echo -e "/-------------------------------- hlint --------------------------------\\"
hlint $project-backend/src
echo -e "\-------------------------------- hlint --------------------------------/\n"
copyBackend
copyFrontend
case $2 in
npmStart)
runNpm start
;;
esac
runRemoteCommand "cd $remoteDir/$project/$project-backend && stack run &"
;;
# }}}
close)
# {{{
closeServer
;;
# }}}
copyBack)
# {{{
closeServer
echo -e "/-------------------------------- hlint --------------------------------\\"
hlint $project-backend/src
echo -e "\-------------------------------- hlint --------------------------------/\n"
copyBackend
;;
# }}}
copyConfigs)
# {{{
copyConfig webpack.*.js
copyConfig package.json
copyConfig tailwind.config.js
copyConfig postcss.config.js
copyConfig elm.json
;;
# }}}
*)
# {{{
echo -e "\nUse one of the following arguments:"
echo -e " • Build the frontend:\t\t\tnpm <script command>"
echo -e " • Start the server:\t\t\tstart (optionally add \`npmStart\` to also build the frontend)"
echo -e " • Close the server:\t\t\tclose"
echo -e " • Copy backend files:\t\t\tcopyBack"
echo -e " • Copy config files from server:\tcopyConfigs\n"
;;
# }}}
esac