forked from aws-samples/step-up-auth
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.sh
executable file
·324 lines (284 loc) · 8.56 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/bin/sh
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
# usage:
# build.sh <generate-compressed-module | generate-deployable-zip>
#
# generate-compressed-module : create a compressed module, used for importing in another module as dependency
# generate-deployable-zip : generate a deployable zip used for lambda deployment
#
# Note: the two cli options are mutually exclusive.
# Note: Change the module_name and module_deps accordingly per project requirement.
## Update following variables as necessary
module_name="step-up-auth-sample-api-lambda"
module_deps="auth-utils" # this could list of project in same monorepo separated by spaces
## DO NOT MODIFY
module_build="build"
module_source_root="src" # there are only two supported locations, "src" or "lib"
# use "src" when building a deployable lambda
# use "lib" when creating a library module
module_test_root="test"
module_dist="${module_build}/dist"
module_zip="${module_build}/compressed"
module_build_js="${module_build}/out"
WORKING_DIR="`pwd`"
## preflight checks
if [ $# -ne 1 ] || [ "$1" != "generate-compressed-module" -a "$1" != "generate-deployable-zip" ]; then
echo "usage: build.sh <generate-compressed-module | generate-deployable-zip>"
exit 1
fi
## step 1: show some stats
echo "1 displaying stats"
echo " WORKING_DIR: ${WORKING_DIR}"
# make sure that the working directory is the root project folder
if [ "${module_source_root}" = "src" ]; then
if [ ! -f "${WORKING_DIR}/package.json" -o \
! -d "${WORKING_DIR}/${module_source_root}" -o \
! -d "${WORKING_DIR}/${module_test_root}" ]; then
echo "unable to locate required files and folders. missing one of the following:"
echo " ${WORKING_DIR}/package.json"
echo " ${WORKING_DIR}/${module_source_root}"
echo " ${WORKING_DIR}/${module_test_root}"
exit 1
fi
elif [ "${module_source_root}" = "lib" ]; then
if [ ! -f "${WORKING_DIR}/package.json" -o \
! -f "${WORKING_DIR}/index.ts" -o \
! -d "${WORKING_DIR}/${module_source_root}" -o \
! -d "${WORKING_DIR}/${module_test_root}" ]; then
echo "unable to locate required files and folders. missing one of the following:"
echo " ${WORKING_DIR}/package.json"
echo " ${WORKING_DIR}/index.ts"
echo " ${WORKING_DIR}/${module_source_root}"
echo " ${WORKING_DIR}/${module_test_root}"
exit 1
fi
else
echo "unsupported module_source_root: ${module_source_root}"
exit 1
fi
if [ ! -d "node_modules" ]; then
echo "warning: run 'npm install' before running the build to ensure local build is successful"
echo "warning: run 'npm link <dep modules>' after npm install to maintain local dev env"
exit 2
fi
## step 2: re-initialize build folders and installation
echo "2 initializing"
# clean up build and module_dist
rm -rf "${module_build}"
mkdir -p "${module_build}"
mkdir -p "${module_dist}"
mkdir -p "${module_zip}"
mkdir -p "${module_build_js}"
# copy package.json before alteration
cd "${WORKING_DIR}"
cp package.json "${module_dist}" > /tmp/$$.cp.log 2>&1
if [ $? -ne 0 ]; then
echo "cp failed: $?"
echo "cp logs:"
cat /tmp/$$.tar.log
rm -rf /tmp/$$.*
exit 2
fi
## step 3: verify tsc installation
echo "3 verifying tsc compiler"
# check tsc is installed
(
tsc --version > /tmp/$$.tsc.1.log 2>&1
if [ $? -ne 0 ]; then
echo "typescript compiler, tsc, is not installed: $?"
echo "installing tsc"
npm install -g typescript > /tmp/$$.tsc-npm-install.log 2>&1
if [ $? -ne 0 ]; then
echo "npm install tsc failed: $?"
echo "npm install tsc logs:"
cat /tmp/$$.tsc-npm-install.log
rm -rf /tmp/$$.*
exit 2
fi
fi
)
if [ $? -ne 0 ]; then
exit 2
fi
# verify tsc installation
(
tsc --version > /tmp/$$.tsc.2.log 2>&1
if [ $? -ne 0 ]; then
echo "unable to verify tsc installation: $?"
echo "tsc error logs:"
cat /tmp/$$.tsc.2.log
rm -rf /tmp/$$.*
exit 2
fi
)
if [ $? -ne 0 ]; then
exit 2
fi
## step 4: build and copy all module dependencies
echo "4 building module dependencies"
if [ ! "$module_deps" = "" ]; then
for module_dep in ${module_deps}; do
echo " $module_dep"
(
# build module dependency
cd "${WORKING_DIR}/../${module_dep}"
if [ ! -f ./build.sh ]; then
echo "unable to load ${module_dep}/build.sh"
rm -rf /tmp/$$.*
exit 3
fi
./build.sh "generate-compressed-module" > "/tmp/$$.${module_dep}-build.log" 2>&1
if [ $? -ne 0 ]; then
echo "${module_dep} build failed: $?"
echo "${module_dep} build logs:"
cat "/tmp/$$.${module_dep}-build.log"
rm -rf /tmp/$$.*
exit 2
fi
# copy built module dependency into home module_zip
cp "${module_zip}"/* "${WORKING_DIR}/${module_zip}" > "/tmp/$$.${module_dep}-cp.log" 2>&1
if [ $? -ne 0 ]; then
echo "copy failed: $?"
echo "copy logs:"
cat "/tmp/$$.${module_dep}-cp.log"
rm -rf /tmp/$$.*
exit 2
fi
)
if [ $? -ne 0 ]; then
exit 2
fi
done
if [ $? -ne 0 ]; then
exit 2
fi
fi
## step 5: build the source code (transpile from TypeScript to ES6)
echo "5 transpiling source"
# transpile code
(
npm run build-ts-prod > /tmp/$$.npm1.log 2>&1
if [ $? -ne 0 ]; then
echo "transpilation failed: $?"
echo "transpilation logs:"
cat /tmp/$$.npm1.log
rm -rf /tmp/$$.*
exit 2
fi
)
## step 6: copy module transpiled source into module_dist and run npm install in module_dist
module_tgzs="`ls ${module_zip}`"
echo "6 copying transpiled source to ${module_dist}"
(
cd "${WORKING_DIR}/${module_build_js}"
# $module_build_js into the root of $module_dist
tar cf - . | (cd "${WORKING_DIR}/${module_dist}"; tar xf - ) > "/tmp/$$.tar-${module_source_root}.log" 2>&1
if [ $? -ne 0 ]; then
echo "tar failed: $?"
echo "tar logs:"
cat "/tmp/$$.tar-${module_source_root}.log"
rm -rf /tmp/$$.*
exit 2
fi
# install compressed deps into dist folder
if [ ! "$module_tgzs" = "" ]; then
for module_tgz in ${module_tgzs}; do
(
cd "${WORKING_DIR}/${module_dist}"
npm install --production "${WORKING_DIR}/${module_zip}/${module_tgz}" > "/tmp/$$.${module_tgz}-npm-install.log" 2>&1
if [ $? -ne 0 ]; then
echo "npm install failed for ${module_tgz}: $?"
echo "npm install logs:"
cat "/tmp/$$.${module_tgz}-npm-install.log"
rm -rf /tmp/$$.*
exit 2
fi
)
if [ $? -ne 0 ]; then
exit 2
fi
done
if [ $? -ne 0 ]; then
exit 2
fi
fi
# change to dist folder and start the npm build
cd "${WORKING_DIR}/${module_dist}"
npm install --production > /tmp/$$.npm-install-1.log 2>&1
if [ $? -ne 0 ]; then
echo "npm install failed: $?"
echo "npm install logs:"
cat /tmp/$$.npm-install-1.log
rm -rf /tmp/$$.*
exit 3
fi
)
if [ $? -ne 0 ]; then
exit 2
fi
## step 7: generate compressed module
if [ $# -eq 1 -a "$1" == "generate-compressed-module" ]; then
echo "7 generating compressed module"
(
# echo "${WORKING_DIR}/${module_dist}"
cd "${WORKING_DIR}/${module_dist}"
npm pack > /tmp/$$.npm-pack.log 2>&1
if [ $? -ne 0 ]; then
echo "npm pack failed: $?"
echo "npm pack logs:"
cat /tmp/$$.npm-pack.log
rm -rf /tmp/$$.*
exit 2
fi
)
if [ $? -ne 0 ]; then
exit 2
fi
# move built module to module_zip folder
echo " moving built module to ${WORKING_DIR}/${module_zip}"
export module_tgz=`ls "${WORKING_DIR}/${module_dist}"/*tgz`
if [ "${module_tgz}" = "" ]; then
echo "unable to find built module with .tgz extension"
exit 3
fi
(
mv "${module_tgz}" "${WORKING_DIR}/${module_zip}" > /tmp/$$.mv.log 2>&1
if [ $? -ne 0 ]; then
echo "mv failed: $?"
echo "mv logs:"
cat /tmp/$$.mv.log
rm -rf /tmp/$$.*
exit 2
fi
)
if [ $? -ne 0 ]; then
exit 2
fi
else
echo "7 [skipped] generating compressed module"
fi
## step 8: generate deployable zip
if [ $# -eq 1 -a "$1" == "generate-deployable-zip" ]; then
echo "8 generating deployable zip"
(
# echo "${WORKING_DIR}/${module_dist}"
cd "${WORKING_DIR}/${module_dist}"
zip -9r "${WORKING_DIR}/${module_zip}/${module_name}.zip" . > /tmp/$$.zip.log 2>&1
if [ $? -ne 0 ]; then
echo "zip failed: $?"
echo "zip logs:"
cat /tmp/$$.zip.log
rm -rf /tmp/$$.*
exit 2
fi
)
if [ $? -ne 0 ]; then
exit 2
fi
else
echo "8 [skipped] generating deployable zip"
fi
## step 9: clean up
rm -rf /tmp/$$.*
echo "done"