-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.sh
237 lines (200 loc) · 6.04 KB
/
commands.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
# Utility commands for working with TypeScript projects that have esbuild installed.
## The directory to output compiled files to.
export TS_UTILITY_DIR="$HOME/.ts_util"
## Creates the $TS_UTILITY_DIR folder if it doesn't exist and symlinks the
## current `node_modules` directory into it.
##
## ensure_tmp_exists
ensure_tmp_exists() {
mkdir -p "$TS_UTILITY_DIR"
rm "$TS_UTILITY_DIR/node_modules" 2> "/dev/null"
mkdir -p "$TS_UTILITY_DIR"
ln -s "$PWD/node_modules" "$TS_UTILITY_DIR/node_modules"
}
## Builds a file into $TS_UTILITY_DIR using esbuild, then echoes the name of the
## output file.
##
## esbuild [options] <entry>
## esbuild src/index.ts
## esbuild --bundle --format esm src/index.ts
esbuild() {
if [[ -z $1 ]]; then
echo "usage: esbuild [options] <entry>"
echo "example: esbuild src/index.ts"
return
fi
ensure_tmp_exists
if [[ "$@" == *"--servedir"* ]]; then
npx esbuild $@ --outfile="$TS_UTILITY_DIR/${@[-1]:t:r}.mjs"
else
npx esbuild $@ --outfile="$TS_UTILITY_DIR/${@[-1]:t:r}.mjs" 2> /dev/null &&
echo "$TS_UTILITY_DIR/${@[-1]:t:r}.mjs" ||
npx esbuild $@ --outfile="$TS_UTILITY_DIR/${@[-1]:t:r}.mjs"
fi
}
## Conditionally echoes a build flag in esbuild style (--flag=value). Note that
## the echoed output contains "=".
##
## maybe $USER_SPECIFIED_FLAGS --flag default_value
## maybe $USER_SPECIFIED_FLAGS --format esm
## maybe $USER_SPECIFIED_FLAGS --platform node
maybe() {
if [[ "$1" != *"$2"* ]]; then
echo "$2=$3"
fi
}
## Conditionally echoes a build flag in tsc style (--flag value). Note that the
## echoed output contains " ".
##
## maybe_space $USER_SPECIFIED_FLAGS --flag default_value
## maybe_space $USER_SPECIFIED_FLAGS --format esm
## maybe_space $USER_SPECIFIED_FLAGS --platform node
maybe_space() {
if [[ "$1" != *"$2"* ]]; then
echo "$2 $3"
fi
}
## Bundles an input file into an ES module using esbuild.
##
## bundle [options] <entry>
## bundle src/index.ts
bundle() {
if [[ -z $1 ]]; then
echo "usage: bundle [options] <entry>"
echo "example: bundle src/index.ts"
return
fi
args="$@"
esbuild --bundle \
$(maybe $args --format esm ) \
$(maybe $args --tree-shaking false ) \
$(maybe $args --platform node ) \
$@
}
## Bundles an input file into an ES module using esbuild, then copies the
## result.
##
## copy [options] <entry>
## copy src/index.ts
copy() {
if [[ -z $1 ]]; then
echo "usage: copy [options] <entry>"
echo "example: copy src/index.ts"
return
fi
cat $(bundle $@) | pbcopy
}
## Minifies an input file into an ES module using esbuild.
##
## minify [options] <entry>
## minify src/index.ts
minify() {
if [[ -z $1 ]]; then
echo "usage: minify [options] <entry>"
echo "example: minify src/index.ts"
return
fi
bundle --minify --tree-shaking=true $@
}
## Bundles an input file into an ES module using esbuild, then starts a server
## that serves an HTML page which runs the bundled file.
##
## serve [options] <entry>
## serve src/index.ts
serve() {
if [[ -z $1 ]]; then
echo "usage: serve [options] <entry>"
echo "example: serve src/index.ts"
return
fi
ensure_tmp_exists
echo "<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"utf-8\" />
<title>${@[-1]:t}</title>
<style>
*,
::before,
::after {
box-sizing: border-box;
}
</style>
<link rel=\"stylesheet\" href=\"/${@[-1]:t:r}.css\" />
</head>
<body>
<script type=\"module\" src=\"/${@[-1]:t:r}.mjs\"></script>
</body>
</html>" > "$TS_UTILITY_DIR/index.html"
bundle "--servedir=$TS_UTILITY_DIR" $@
}
## Minifies an input file into an ES module using esbuild, then opens NodeJS
## and puts the module's exports into the global "M" variable.
##
## use [options] <entry>
## use src/index.ts
use() {
if [[ -z $1 ]]; then
echo "usage: use [options] <entry>"
echo "example: use src/index.ts"
return
fi
NAME=$(bundle $@)
node -i -e "import('$NAME').then(module => global.M = module)"
}
## Minifies an input file into an ES module using esbuild, then opens NodeJS
## and imports the module.
##
## run [options] <entry>
## run src/index.ts
run() {
if [[ -z $1 ]]; then
echo "usage: run [options] <entry>"
echo "example: run src/index.ts"
return
fi
NAME=$(bundle $@)
node -e "import('$NAME')"
}
## Bundles an input file using TSC, then puts the output file into a given
## esbuild function, such as bundle, copy, minify, serve, or use.
## Alternatively, run `tsc pure` to bundle the input files and output the names
## of the output files without passing them into esbuild.
## This can be useful when one wants to use relatively new TSC features, such
## as proper ES decorators, which were released in the TSC 5.0 beta long before
## esbuild added them.
##
## tsc (bundle|copy|minify|pure|run|serve|use) [tsc-options] <entry>
## tsc bundle src/index.ts
## tsc pure src/index.ts
tsc() {
if [[ -z "$2" ]]; then
echo "usage: tsc (bundle|copy|minify|pure|run|serve|use) [tsc-options] <file>"
echo "example: tsc bundle src/index.ts"
return
fi
args="${@:2}"
rm -rf "$TS_UTILITY_DIR"
mkdir -p "$TS_UTILITY_DIR"
npx tsc \
$(maybe_space $args --allowJs true) \
$(maybe_space $args --allowSyntheticDefaultImports true) \
$(maybe_space $args --checkJs true) \
$(maybe_space $args --moduleResolution node) \
$(maybe_space $args --jsx react) \
$(maybe_space $args --jsxFactory h) \
$(maybe_space $args --jsxFragmentFactory f) \
$(maybe_space $args --module esnext) \
$(maybe_space $args --noFallthroughCasesInSwitch true) \
$(maybe_space $args --noUncheckedIndexedAccess true) \
$(maybe_space $args --skipLibCheck true) \
$(maybe_space $args --strict true) \
$(maybe_space $args --strictFunctionTypes true) \
$(maybe_space $args --target es2022) \
--outDir "$TS_UTILITY_DIR" ${@:2}
if [[ $1 == "pure" ]]; then
echo $TS_UTILITY_DIR/**/*
else
$1 "$TS_UTILITY_DIR/${@[-1]:t:r}.mjs"
fi
}