-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev.sh
executable file
·438 lines (390 loc) · 10.4 KB
/
dev.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#!/bin/sh
script_dir="$(
cd "$(dirname "$0")"
pwd -P
)"
cd "$script_dir"
if [ ! -f shellutil/shellutil.sh ]; then
git submodule update --init
fi
# shellcheck source=shellutil/mainutil.sh
. shellutil/mainutil.sh
# shellcheck source=shellutil/shellutil.sh
. shellutil/shellutil.sh
# shellcheck source=shellutil/updateutil.sh
. shellutil/updateutil.sh
# set -o xtrace
docker_image=utiljs-dev:0.41.3
npm_dev_globals='[email protected]
'
npm_globals='@babel/[email protected]
@babel/[email protected]
'
audit() {
npm audit
# We cannot run audit in subpackages without a package-lock.json.
npx lerna exec "npm install --package-lock-only --save-exact"
npx @util.js/node-lerna
# shellcheck disable=SC2046
rm $(find packages -name package-lock.json)
}
build() {
clean
execute_babel
}
clean() {
git clean -fdx --exclude "node_modules"
}
execute_babel() {
if [ -n "${2:-}" ] && [ -d "packages/$2" ]; then
printf "\033[1m%s\033[0m ... " "$2"
babel \
"packages/$2/lib" \
--out-dir "packages/$2/dist"
return $?
fi
# shellcheck disable=SC2039
local packages
packages='arrays
emails
errors
jquery
numbers
objects
privates
promises
strings
'
for package in $packages; do
printf "\033[1m%s\033[0m ... " "utiljs-$package"
babel \
"packages/utiljs-$package/lib" \
--out-dir "packages/utiljs-$package/dist"
done
}
execute_docker() {
# https://stackoverflow.com/a/30543453
if [ "$(docker images -q $docker_image 2>/dev/null)" = "" ]; then
cp dev.sh docker
cp -r shellutil docker/shellutil
cd docker
if ! docker build --platform linux/arm64/v8 --tag $docker_image .; then
exit $?
fi
if ! docker build --platform linux/amd64 --tag $docker_image-amd64 .; then
exit $?
fi
rm -rf shellutil
rm -rf dev.sh
fi
# We mount /tmp because of the following error:
# ~/utiljs $ ./scripts/jsdoc2md.sh
# /home/node/utiljs/node_modules/mkdirp2/index.js:87
# throw err0
# ^
#
# Error: EROFS: read-only file system, mkdir '/tmp/jsdoc-api'
# at Object.mkdirSync (fs.js:753:3)
# ...
# /home/node/.gnupg cannot be read-only because of the following error:
# ~/utiljs $ git commit
# error: gpg failed to sign the data
# fatal: failed to write commit object
# IP, DISPLAY, and /tmp/.X11-unix are for gitk. See shellutil/git.sh.
export IP
IP="$(ifconfig en0 | grep inet | awk '$1=="inet" {print $2}')"
xhost + "$IP"
# shellcheck disable=SC2068
docker run \
--cap-drop=ALL \
--cpu-shares=1024 \
--env DISPLAY="$IP":0 \
--interactive \
--memory 1.5G \
--name utiljs-dev \
--pids-limit 100 \
--read-only \
--restart=no \
--rm \
--security-opt=no-new-privileges:true \
--tty \
--volume /home/node \
--volume /tmp/.X11-unix:/tmp/.X11-unix \
--volume ~/.gnupg:/home/node/.gnupg \
--volume ~/.ssh:/home/node/.ssh:ro \
--volume "$script_dir":/home/node/utiljs \
--volume /tmp \
--workdir /home/node/utiljs \
"$docker_image" "$@"
}
execute_eslint() {
# shellcheck disable=SC2039
local path
path="."
# shellcheck disable=SC2039
local prefix
prefix=""
if [ -n "${2:-}" ] && [ -d "packages/$2" ]; then
path="./packages/$2"
prefix="\./packages/$2/"
fi
mkdir -p target
find "$path" -type f |
grep -E "^$prefix.*\.js$" |
grep -E -v "^.*/(dist|node_modules|target)/.*$" \
>target/eslint.txt
sort target/eslint.txt -o target/eslint.txt
while read -r in; do
printf '%s\n' "$in"
eslint --fix "$in" || true
done <target/eslint.txt
rm -rf target/eslint.txt
}
execute_jsdoc() {
if [ -d "target/jsdoc" ]; then
rm -rf target/jsdoc
fi
# shellcheck disable=SC2039
local path
path="."
# shellcheck disable=SC2039
local prefix
prefix=""
if [ -n "${2:-}" ] && [ -d "packages/$2" ]; then
path="./packages/$2"
prefix="\./packages/$2/"
fi
# shellcheck disable=SC2046
jsdoc \
--destination target/jsdoc \
$(find "$path" -type f |
grep -E "^$prefix.*\.js$" |
grep -E -v "^.*/(dist|node_modules|target)/.*$")
}
execute_jsdoc2md() {
if [ -d "target/jsdoc2md" ]; then
rm -rf target/jsdoc2md
fi
mkdir -p target/jsdoc2md
# shellcheck disable=SC2039
local path
path="."
# shellcheck disable=SC2039
local prefix
prefix=""
# shellcheck disable=SC2039
local filename
filename="jsdoc2md.md"
if [ -n "${2:-}" ] && [ -d "packages/$2" ]; then
path="./packages/$2"
prefix="\./packages/$2/"
filename="$2.md"
fi
# shellcheck disable=SC2046
jsdoc2md \
$(find "$path" -type f |
grep -E "^$prefix.*\.js$" |
grep -E -v "^.*/(dist|node_modules|target)/.*$") \
>"target/jsdoc2md/$filename"
}
execute_mocha() {
if [ -n "${2:-}" ] && [ -d "packages/$2" ]; then
cd "packages/$2"
nyc \
--reporter html \
--reporter text \
--report-dir target/coverage \
mocha
else
if [ -n "${1:-}" ]; then
shift
fi
cd packages
for package in */; do
cd "$package"
if [ -d "test" ]; then
printf '%s\n' "Visting $package"
nyc \
--reporter html \
--reporter text \
--report-dir target/coverage \
mocha "$@"
else
printf '%s\n' "Skipping $package"
echo
fi
cd ..
done
fi
}
execute_prettier() {
# shellcheck disable=SC2039
local path
path="."
# shellcheck disable=SC2039
local prefix
prefix=""
if [ -n "${2:-}" ] && [ -d "packages/$2" ]; then
path="./packages/$2"
prefix="\./packages/$2/"
fi
mkdir -p target
find "$path" -type f |
grep -E "^(\./\.babelrc|$prefix.*\.(css|js|json|jsx|md|scss))$" |
grep -E -v "^.*/(.nyc_output|dist|node_modules|target)/.*$" |
grep -E -v "^.*/package-lock\.json$" |
grep -E -v "^\./lerna\.json$" \
>target/prettier.txt
sort target/prettier.txt -o target/prettier.txt
while read -r in; do prettier --write "$in"; done <target/prettier.txt
rm -rf target/prettier.txt
}
install() {
lerna bootstrap --force-local --hoist -- --save-exact
}
install_dev_globals() {
# We do not need these global packages to run in GitHub Actions.
# shellcheck disable=SC2086
npm install --global $npm_dev_globals
}
install_globals() {
# Including @babel/core prevents the following warning:
# npm WARN @babel/cli@ 7.2.3 requires a peer of @babel/core@^7.0.0-0 but
# none is installed. You must install peer dependencies yourself.
# shellcheck disable=SC2086
npm install --global $npm_global
# shellcheck disable=SC2086
npm install --global $npm_globals
}
main() {
# shellcheck disable=SC2039
local command_help
command_help='audit - Run audit in all packages.
babel - Run babel on certain (mostly non-node) packages.
build - Run clean and babel.
clean - git clean -fdx --exclude "node_modules"
docker - Develop inside a Docker container.
docker-update - Run update using the latest creemama/shellutil-dev:lts-alpine Docker image.
docker-update-dockerfile - Run Docker to update the shellutil-dev image used in docker/Dockerfile.
eslint - Run eslint in all packages.
git - Run git setting GPG_TTY if not already set for signing commits.
gitk - Run gitk.
install - Run install in all packages.
install-dev-globals - Install Node.js globals not needed by CI.
install-globals - Install Node.js globals needed by CI.
jsdoc - Run jsdoc in all packages.
jsdoc2md - Run jsdoc2md in all packages.
mocha - Run mocha/nyc in all packages.
prettier - Run prettier in all packages.
publish - Bump the version number and publish all packages to npm.
shell-format - Format shell scripts and run shellcheck.
test - Run build and mocha for CI.
update - Check and update project dependencies.
update-dockerfile - Update the shellutil-dev image used in docker/Dockerfile.'
# shellcheck disable=SC2039
local commands
commands="$(main_extract_commands "$command_help")"
# shellcheck disable=SC2086
if [ -z "${1:-}" ]; then
main_exit_with_no_command_error "$command_help"
elif [ "$1" = "$(arg 0 $commands)" ]; then
audit
elif [ "$1" = "$(arg 1 $commands)" ]; then
execute_babel "$@"
elif [ "$1" = "$(arg 2 $commands)" ]; then
build
elif [ "$1" = "$(arg 3 $commands)" ]; then
clean
elif [ "$1" = "$(arg 4 $commands)" ]; then
shift
execute_docker "$@"
elif [ "$1" = "$(arg 5 $commands)" ]; then
run_docker_update
elif [ "$1" = "$(arg 6 $commands)" ]; then
run_docker_update_dockerfile
elif [ "$1" = "$(arg 7 $commands)" ]; then
execute_eslint "$@"
elif [ "$1" = "$(arg 8 $commands)" ]; then
shift
shellutil/git.sh git "$@"
elif [ "$1" = "$(arg 9 $commands)" ]; then
shift
shellutil/git.sh gitk "$@"
elif [ "$1" = "$(arg 10 $commands)" ]; then
install
elif [ "$1" = "$(arg 11 $commands)" ]; then
install_dev_globals
elif [ "$1" = "$(arg 12 $commands)" ]; then
install_globals
elif [ "$1" = "$(arg 13 $commands)" ]; then
execute_jsdoc "$@"
elif [ "$1" = "$(arg 14 $commands)" ]; then
execute_jsdoc2md "$@"
elif [ "$1" = "$(arg 15 $commands)" ]; then
execute_mocha "$@"
elif [ "$1" = "$(arg 16 $commands)" ]; then
execute_prettier "$@"
elif [ "$1" = "$(arg 17 $commands)" ]; then
publish
elif [ "$1" = "$(arg 18 $commands)" ]; then
shellutil/format.sh shell-format
elif [ "$1" = "$(arg 19 $commands)" ]; then
run_test
elif [ "$1" = "$(arg 20 $commands)" ]; then
update
elif [ "$1" = "$(arg 21 $commands)" ]; then
update_dockerfile
else
main_exit_with_invalid_command_error "$1" "$command_help"
fi
}
publish() {
# Log into https://www.npmjs.com .
# To create scoped packages, create an organization at https://www.npmjs.com.
git clean -f
clean
build
npm login [email protected]
lerna publish --exact # This command bumps the version number.
}
run_docker_update() {
execute_docker sh -c './dev.sh update'
}
run_docker_update_dockerfile() {
docker pull --quiet node:lts-alpine >/dev/null 2>&1
docker run -it --rm --volume "$(pwd)":/tmp node:lts-alpine /tmp/dev.sh update-dockerfile
}
run_test() {
execute_mocha
}
update() {
for package in $npm_dev_globals $npm_global $npm_globals; do
npm_update_package_version "$(printf %s "$package" | sed -E 's/(.+)@.*/\1/')"
done
update_npm_deps
printf '\n%sDelete the utiljs-dev Docker image or update docker_image= if dev.sh dependencies change.\n\n%s' "$(tbold)" "$(treset)"
}
update_dockerfile() {
apk_update_node_image_version docker/Dockerfile 's#(FROM creemama/shellutil-dev:).*#\\1%s-alpine%s#'
printf '\n%sDelete the utiljs-dev Docker image or update docker_image= if docker/Dockerfile changes.\n\n%s' "$(tbold)" "$(treset)"
}
update_npm_deps() {
npx ncu -u
cd packages
for package in */; do
(
cd "$package"
printf '%s\n' "Visting $package"
npx ncu -u
)
done
}
main "$@"