-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-frontend.sh
109 lines (82 loc) · 2.91 KB
/
build-frontend.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
#!/usr/bin/env bash
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# BRIEF DOCUMENTATION #
# Run as is. #
# #
# OR: #
# #
# 1) Use keys --dev or --prod to specify development or production mode for webpack #
# (runs npm scripts 'build_andExit' or 'build_prod_andExit' respectively) #
# 2) Use key --install if the frontend submodule requires 'npm install' beforehand #
# #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
declare -r src_path="web/frontend"
########################
### Read from args ###
########################
declare needInstall=""
declare env="dev"
for arg in "$@"; do
case "$arg" in
-p|--prod|--production)
env="prod"
;;
-d|--dev|--development)
env="dev"
;;
-i|--install)
needInstall=True
;;
esac
done
########################
### Define scripts ###
########################
npmInstall(){
"${install_cmd[@]}" || npm audit fix
}
npmBuild(){
"${build_cmd[@]}" && "${after_build_cmd[@]}"
}
# have cd in and cd out (back) in this function
# because while it's needed to proceed depending on the build result,
# it's also needed to change dir back anyway (regardless of build result)
Build(){
local ec=""
cd "src/$src_path"
(npmInstall || npm audit fix) && npmBuild
ec="$?"
cd - 1>/dev/null
return "$ec"
}
########################
### Interpret args ###
########################
declare install_cmd=()
declare build_cmd=()
declare after_build_cmd=()
if [ -n "$needInstall" ]; then
install_cmd=(npm install --also=dev)
else
install_cmd=(:)
fi
if [ "$env" == "dev" ]; then
build_cmd=(npm run build_andExit)
after_build_cmd=(:)
elif [ "$env" == "prod" ]; then
build_cmd=(npm run build_prod_andExit)
after_build_cmd=(npm prune --production)
else
build_cmd=(false)
after_build_cmd=(false)
fi
########################
### Run Build ###
########################
# only if Build succeeds it proceeds to: remove, create dir, and move dir
Build && \
rm -rf "dist/$src_path" && \
mkdir -p "dist/$src_path" 1>/dev/null 2>&1
mv "src/$src_path/dist" "dist/$src_path"