-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·118 lines (102 loc) · 2.26 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
#!/bin/bash -e
cd "$(dirname "$0")"
BUILD_DIR=.build
OPT_HELP=false
OPT_OPT=false
OPT_WATCH=false
# parse args
while [[ $# -gt 0 ]]; do
case "$1" in
-h*|--h*)
OPT_HELP=true
shift
;;
-w*|--w*)
OPT_WATCH=true
shift
;;
-O)
OPT_OPT=true
shift
;;
*)
echo "$0: Unknown option $1" >&2
OPT_HELP=true
shift
;;
esac
done
if $OPT_HELP; then
echo "Usage: $0 [options]"
echo "Options:"
echo " -h Show help."
echo " -O Build optimized product."
echo " -w Watch source files for changes and rebuild incrementally."
exit 1
fi
# check esbuild
if ! (which esbuild); then
esbuild_suffix=wasm
if [[ "$OSTYPE" == "darwin"* ]]; then
esbuild_suffix=darwin-64
elif [[ "$OSTYPE" == "linux"* ]]; then
esbuild_suffix=linux-64
elif [[ "$OSTYPE" == "cygwin" ]] || \
[[ "$OSTYPE" == "msys" ]] || \
[[ "$OSTYPE" == "win32" ]] || \
[[ "$OSTYPE" == "win64" ]]
then
esbuild_suffix=windows-64
fi
echo "esbuild not found in PATH. Please install with:" >&2
echo "npm install -g esbuild-${esbuild_suffix}" >&2
exit 1
fi
mkdir -p "$BUILD_DIR"
pushd "$BUILD_DIR" >/dev/null
BUILD_DIR=$PWD
popd >/dev/null
WATCHFILE=$BUILD_DIR/.build.sh.watch
function fn_build_appjs {
if $OPT_OPT; then
esbuild --define:DEBUG=false --bundle --sourcemap --minify \
"--outfile=docs/app.js" src/main.js
else
esbuild --define:DEBUG=true --bundle --sourcemap \
"--outfile=docs/app.js" src/main.js
fi
}
fn_build_appjs &
if $OPT_WATCH; then
echo y > "$WATCHFILE"
# make sure we can ctrl-c in the while loop
function fn_stop {
echo n > "$WATCHFILE"
exit
}
trap fn_stop SIGINT
# make sure background processes are killed when this script is stopped
pids=()
function fn_cleanup {
set +e
for pid in "${pids[@]}"; do
kill $pid 2>/dev/null
wait $pid
kill -9 $pid 2>/dev/null
echo n > "$WATCHFILE"
done
set -e
}
trap fn_cleanup EXIT
# wait for initial build
wait
# fn_watch_other &
# pids+=( $! )
while true; do
fswatch -1 -l 0.2 -r -E --exclude='.+' --include='\.js$' src >/dev/null
if ! [ -f "$WATCHFILE" ] || [ "$(cat "$WATCHFILE")" != "y" ]; then break; fi
set +e ; fn_build_appjs ; set -e
done
else
wait
fi