@asd14/vblocks 5.4.0 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -56
- package/bin/vblocks +71 -0
- package/bin/vblocks.d/build +193 -0
- package/bin/vblocks.d/eject +69 -0
- package/bin/vblocks.d/init +337 -0
- package/bin/vblocks.d/lib.sh +569 -0
- package/bin/vblocks.d/serve +149 -0
- package/bin/vblocks.d/vblocks.sh +95 -0
- package/bin/vblocks.d/webpack.sh +120 -0
- package/defaults/postcss.config.js +14 -0
- package/{src → defaults}/webpack.config.js +40 -21
- package/package.json +18 -14
- package/template/xyflow/app/postcss.config.js +9 -8
- package/template/xyflow/app/src/diagram.tsx +0 -2
- package/template/xyflow/post-init-app.sh +1 -0
- package/template/xyflow-tabs/app/postcss.config.js +9 -8
- package/template/xyflow-tabs/post-init-app.sh +1 -0
- package/{src/types → types}/client.d.ts +20 -1
- package/bin/vblocks.sh +0 -685
- package/src/postcss.config.js +0 -13
- package/template/xyflow/app/src/ui/zoom-level.tsx +0 -29
- /package/{src → defaults}/.swcrc +0 -0
- /package/{src → defaults/src}/public/favicon.svg +0 -0
- /package/{src → defaults/src}/public/robots.txt +0 -0
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
|
|
3
|
+
# NAME
|
|
4
|
+
# lib.sh - Shared primitives for posix-utils scripts
|
|
5
|
+
#
|
|
6
|
+
# SYNOPSIS
|
|
7
|
+
# SCRIPT_DIR=$(dirname "$(realpath "$0")")
|
|
8
|
+
# . "$SCRIPT_DIR/../lib.sh"
|
|
9
|
+
#
|
|
10
|
+
# export LOG_NAMESPACE="my-script"
|
|
11
|
+
# export LOG_LEVEL="${LOG_LEVEL:-info}"
|
|
12
|
+
#
|
|
13
|
+
# DESCRIPTION
|
|
14
|
+
# Domain-agnostic primitives: logging, predicates, guards, files, text,
|
|
15
|
+
# terminal and help. A script sources it, never executes it.
|
|
16
|
+
#
|
|
17
|
+
# A primitive prints its result to stdout. A predicate answers with its
|
|
18
|
+
# status and prints nothing. Everything else goes to stderr.
|
|
19
|
+
#
|
|
20
|
+
# The comment above each function lists its statuses under RETURNS and its
|
|
21
|
+
# exit codes under EXIT.
|
|
22
|
+
#
|
|
23
|
+
# Only _is checks its positional count. Under set -u, a missing positional
|
|
24
|
+
# ends the script with the shell's own code.
|
|
25
|
+
#
|
|
26
|
+
# ENVIRONMENT
|
|
27
|
+
# LOG_LEVEL Last level to print, in the order of _log (default: info).
|
|
28
|
+
# An unknown level ends the script with 2 at the first log
|
|
29
|
+
# line
|
|
30
|
+
# LOG_NAMESPACE Label printed before the message
|
|
31
|
+
# NO_LOG_TIME Hides the time when set and non-empty
|
|
32
|
+
# NO_COLOR Disables color when set and non-empty
|
|
33
|
+
# TERM Disables color when unset, empty or "dumb"
|
|
34
|
+
#
|
|
35
|
+
# SEE ALSO
|
|
36
|
+
# curl(1)
|
|
37
|
+
|
|
38
|
+
# ╭───
|
|
39
|
+
# │ Logging
|
|
40
|
+
# ╰───
|
|
41
|
+
|
|
42
|
+
# _level_rank <level>
|
|
43
|
+
#
|
|
44
|
+
# Print the rank of <level>, from 0 for error to 4 for debug.
|
|
45
|
+
#
|
|
46
|
+
# RETURNS
|
|
47
|
+
# 1 <level> is not a level or a short form, the reason is on stderr
|
|
48
|
+
_level_rank() {
|
|
49
|
+
case "$1" in
|
|
50
|
+
error | err) printf '0' ;;
|
|
51
|
+
warning | wrn | warn) printf '1' ;;
|
|
52
|
+
success | suc | succ) printf '2' ;;
|
|
53
|
+
info | inf) printf '3' ;;
|
|
54
|
+
debug | dbg) printf '4' ;;
|
|
55
|
+
*)
|
|
56
|
+
# Not through _log, which calls back here
|
|
57
|
+
printf 'lib.sh/_level_rank: Unknown log level "%s". Use error, warning, success, info or debug\n' \
|
|
58
|
+
"$1" >&2
|
|
59
|
+
return 1
|
|
60
|
+
;;
|
|
61
|
+
esac
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
# _has_color [fd]
|
|
65
|
+
#
|
|
66
|
+
# Tell if <fd> can show color. <fd> defaults to 2, stderr.
|
|
67
|
+
#
|
|
68
|
+
# RETURNS
|
|
69
|
+
# 0 <fd> is a terminal, NO_COLOR is unset or empty, TERM is set and not
|
|
70
|
+
# "dumb"
|
|
71
|
+
# 1 One of the three does not hold
|
|
72
|
+
# 2 <fd> is not a number
|
|
73
|
+
_has_color() {
|
|
74
|
+
[ -t "${1:-2}" ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-dumb}" != "dumb" ]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
# _log <level> [-v <key> <val>]... [--] <message>...
|
|
78
|
+
#
|
|
79
|
+
# Print one line to stderr at <level>. Print nothing when <level> comes after
|
|
80
|
+
# LOG_LEVEL in this order: error < warning < success < info < debug.
|
|
81
|
+
#
|
|
82
|
+
# <level> also has short forms: err, wrn, warn, suc, succ, inf, dbg. -v has the
|
|
83
|
+
# long form --var. -- ends the pairs, so a message that starts with -v prints as
|
|
84
|
+
# written.
|
|
85
|
+
#
|
|
86
|
+
# Data always goes through %s. Only the format string holds escapes, so a
|
|
87
|
+
# value containing a backslash prints as written.
|
|
88
|
+
#
|
|
89
|
+
# Newlines inside a <val> print as ",", so a list with one item per line
|
|
90
|
+
# reads "a,b,c". <message> prints as written.
|
|
91
|
+
#
|
|
92
|
+
# EXIT
|
|
93
|
+
# 2 <level> or LOG_LEVEL is not a level or a short form
|
|
94
|
+
_log() {
|
|
95
|
+
lib_l_level="$1"
|
|
96
|
+
shift
|
|
97
|
+
|
|
98
|
+
lib_l_rank=$(_level_rank "$lib_l_level") || exit 2
|
|
99
|
+
lib_l_cutoff=$(_level_rank "${LOG_LEVEL:-info}") || exit 2
|
|
100
|
+
|
|
101
|
+
[ "$lib_l_rank" -le "$lib_l_cutoff" ] || return 0
|
|
102
|
+
|
|
103
|
+
case "$lib_l_level" in
|
|
104
|
+
error | err) lib_l_label="ERR" lib_l_color=31 ;;
|
|
105
|
+
warning | wrn | warn) lib_l_label="WRN" lib_l_color=33 ;;
|
|
106
|
+
success | suc | succ) lib_l_label="SUC" lib_l_color=32 ;;
|
|
107
|
+
info | inf) lib_l_label="INF" lib_l_color=34 ;;
|
|
108
|
+
debug | dbg) lib_l_label="DBG" lib_l_color=90 ;;
|
|
109
|
+
esac
|
|
110
|
+
|
|
111
|
+
if _has_color; then
|
|
112
|
+
[ -z "${NO_LOG_TIME:-}" ] && printf '\033[90m%s\033[0m ' "$(date +%T)" >&2
|
|
113
|
+
printf '\033[1;%sm%s\033[0m ' "$lib_l_color" "$lib_l_label" >&2
|
|
114
|
+
[ -n "${LOG_NAMESPACE:-}" ] \
|
|
115
|
+
&& printf '\033[90m%s:\033[0m ' "$LOG_NAMESPACE" >&2
|
|
116
|
+
else
|
|
117
|
+
[ -z "${NO_LOG_TIME:-}" ] && printf '%s ' "$(date +%T)" >&2
|
|
118
|
+
printf '%s ' "$lib_l_label" >&2
|
|
119
|
+
[ -n "${LOG_NAMESPACE:-}" ] && printf '%s: ' "$LOG_NAMESPACE" >&2
|
|
120
|
+
fi
|
|
121
|
+
|
|
122
|
+
# Collect the pairs here and print them after the message.
|
|
123
|
+
lib_l_pairs=""
|
|
124
|
+
while [ "$#" -gt 1 ]; do
|
|
125
|
+
case "$1" in
|
|
126
|
+
-v | --var)
|
|
127
|
+
[ "$#" -lt 3 ] && break
|
|
128
|
+
|
|
129
|
+
lib_l_value=$(printf '%s' "$3" | tr '\n' ',')
|
|
130
|
+
if _has_color; then
|
|
131
|
+
lib_l_pairs="${lib_l_pairs}$(printf ' \033[90m%s=\033[0m%s' "$2" "$lib_l_value")"
|
|
132
|
+
else
|
|
133
|
+
lib_l_pairs="${lib_l_pairs}$(printf ' %s=%s' "$2" "$lib_l_value")"
|
|
134
|
+
fi
|
|
135
|
+
shift 3
|
|
136
|
+
;;
|
|
137
|
+
--)
|
|
138
|
+
shift
|
|
139
|
+
break
|
|
140
|
+
;;
|
|
141
|
+
*) break ;;
|
|
142
|
+
esac
|
|
143
|
+
done
|
|
144
|
+
|
|
145
|
+
# The positionals left are the message. Join them with a space, as echo
|
|
146
|
+
# does.
|
|
147
|
+
printf '%s' "$*" >&2
|
|
148
|
+
[ -n "$lib_l_pairs" ] && printf '%s' "$lib_l_pairs" >&2
|
|
149
|
+
printf '\n' >&2
|
|
150
|
+
|
|
151
|
+
return 0
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
# _debug, _info, _ok, _warn, _error [-v <key> <val>]... [--] <message>...
|
|
155
|
+
#
|
|
156
|
+
# Call _log at one level: debug, info, success, warning, error.
|
|
157
|
+
#
|
|
158
|
+
# EXIT
|
|
159
|
+
# 2 LOG_LEVEL is not a level or a short form
|
|
160
|
+
_debug() { _log debug "$@"; }
|
|
161
|
+
_info() { _log info "$@"; }
|
|
162
|
+
_ok() { _log success "$@"; }
|
|
163
|
+
_warn() { _log warning "$@"; }
|
|
164
|
+
_error() { _log error "$@"; }
|
|
165
|
+
|
|
166
|
+
# _fail <code> [-v <key> <val>]... [--] <message>...
|
|
167
|
+
#
|
|
168
|
+
# Print at error level, then exit with <code>.
|
|
169
|
+
#
|
|
170
|
+
# EXIT
|
|
171
|
+
# <code> <code> is 1 to 125
|
|
172
|
+
# 2 <code> is not 1 to 125, or LOG_LEVEL is not a level or a short form
|
|
173
|
+
_fail() {
|
|
174
|
+
lib_f_code=$(printf '%d' "${1:-}" 2>/dev/null) || lib_f_code=0
|
|
175
|
+
|
|
176
|
+
if [ "$lib_f_code" -lt 1 ] || [ "$lib_f_code" -gt 125 ]; then
|
|
177
|
+
_log error -v got "${1:-}" "_fail needs an exit code of 1 to 125"
|
|
178
|
+
exit 2
|
|
179
|
+
fi
|
|
180
|
+
|
|
181
|
+
shift
|
|
182
|
+
_log error "$@"
|
|
183
|
+
exit "$lib_f_code"
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
# ╭───
|
|
187
|
+
# │ Predicates & Guards
|
|
188
|
+
# ╰───
|
|
189
|
+
|
|
190
|
+
# _is <type> <value>
|
|
191
|
+
#
|
|
192
|
+
# Tell if <value> has the shape of <type>.
|
|
193
|
+
#
|
|
194
|
+
# positive-integer 0-9, no leading zero
|
|
195
|
+
# version 0-9 in groups, one dot between two groups, eg. 1.13.4
|
|
196
|
+
# slug A-Za-z0-9_-, starts with a letter or a digit
|
|
197
|
+
# identifier A-Za-z0-9_, cannot start with a digit
|
|
198
|
+
# folgezettel a-z0-9, starts with 1-9, a run of digits never starts
|
|
199
|
+
# with 0, eg. 1b3. 1b03 and 1b3 would name one place
|
|
200
|
+
#
|
|
201
|
+
# RETURNS
|
|
202
|
+
# 0 <value> has the shape
|
|
203
|
+
# 1 <value> does not have it, or is empty
|
|
204
|
+
#
|
|
205
|
+
# EXIT
|
|
206
|
+
# 3 <type> is not one of the five and <value> is not empty, or the call
|
|
207
|
+
# does not have two positionals
|
|
208
|
+
_is() {
|
|
209
|
+
[ "$#" -eq 2 ] || _fail 3 "_is needs <type> <value>"
|
|
210
|
+
[ -n "$2" ] || return 1
|
|
211
|
+
|
|
212
|
+
# Reject with a negated class, which checks every character.
|
|
213
|
+
# [A-Za-z_][A-Za-z0-9_]* checks two characters and accepts "ab; rm -rf /".
|
|
214
|
+
# Inside a class, put "-" last and "]" first.
|
|
215
|
+
case "$1" in
|
|
216
|
+
positive-integer)
|
|
217
|
+
# 0* rejects zero and every leading zero with it, so no arithmetic runs
|
|
218
|
+
# and a 20-digit value cannot abort the shell with "Illegal number".
|
|
219
|
+
case "$2" in
|
|
220
|
+
*[!0-9]* | 0*) return 1 ;;
|
|
221
|
+
esac
|
|
222
|
+
;;
|
|
223
|
+
version)
|
|
224
|
+
case "$2" in
|
|
225
|
+
*[!0-9.]* | .* | *. | *..*) return 1 ;;
|
|
226
|
+
esac
|
|
227
|
+
;;
|
|
228
|
+
slug)
|
|
229
|
+
case "$2" in
|
|
230
|
+
*[!A-Za-z0-9_-]* | [_-]*) return 1 ;;
|
|
231
|
+
esac
|
|
232
|
+
;;
|
|
233
|
+
identifier)
|
|
234
|
+
case "$2" in
|
|
235
|
+
*[!A-Za-z0-9_]* | [0-9]*) return 1 ;;
|
|
236
|
+
esac
|
|
237
|
+
;;
|
|
238
|
+
folgezettel)
|
|
239
|
+
case "$2" in
|
|
240
|
+
*[!0-9a-z]* | [!1-9]* | *[a-z]0*) return 1 ;;
|
|
241
|
+
esac
|
|
242
|
+
;;
|
|
243
|
+
*)
|
|
244
|
+
_fail 3 -v type "$1" "Unknown type"
|
|
245
|
+
;;
|
|
246
|
+
esac
|
|
247
|
+
|
|
248
|
+
return 0
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
# _require_cmd <cmd>...
|
|
252
|
+
#
|
|
253
|
+
# End the script unless the shell finds every <cmd>.
|
|
254
|
+
#
|
|
255
|
+
# EXIT
|
|
256
|
+
# 3 The shell cannot find a <cmd>
|
|
257
|
+
_require_cmd() {
|
|
258
|
+
lib_rc_missing=""
|
|
259
|
+
for lib_rc_cmd in "$@"; do
|
|
260
|
+
command -v "$lib_rc_cmd" >/dev/null 2>&1 \
|
|
261
|
+
|| lib_rc_missing="${lib_rc_missing:+$lib_rc_missing, }$lib_rc_cmd"
|
|
262
|
+
done
|
|
263
|
+
|
|
264
|
+
[ -n "$lib_rc_missing" ] \
|
|
265
|
+
&& _fail 3 -v missing "$lib_rc_missing" "Required commands not found"
|
|
266
|
+
|
|
267
|
+
return 0
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
# _require_var <NAME>...
|
|
271
|
+
#
|
|
272
|
+
# End the script unless every <NAME> is set and non-empty.
|
|
273
|
+
#
|
|
274
|
+
# EXIT
|
|
275
|
+
# 2 A <NAME> is unset or empty
|
|
276
|
+
# 3 A <NAME> is not an identifier
|
|
277
|
+
_require_var() {
|
|
278
|
+
lib_rv_missing=""
|
|
279
|
+
for lib_rv_name in "$@"; do
|
|
280
|
+
_is identifier "$lib_rv_name" \
|
|
281
|
+
|| _fail 3 -v name "$lib_rv_name" "_require_var takes variable names"
|
|
282
|
+
|
|
283
|
+
eval "lib_rv_value=\${$lib_rv_name:-}"
|
|
284
|
+
[ -z "$lib_rv_value" ] \
|
|
285
|
+
&& lib_rv_missing="${lib_rv_missing:+$lib_rv_missing, }$lib_rv_name"
|
|
286
|
+
done
|
|
287
|
+
|
|
288
|
+
[ -n "$lib_rv_missing" ] \
|
|
289
|
+
&& _fail 2 -v missing "$lib_rv_missing" "Required variables not set"
|
|
290
|
+
|
|
291
|
+
return 0
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
# ╭───
|
|
295
|
+
# │ Files
|
|
296
|
+
# ╰───
|
|
297
|
+
|
|
298
|
+
# _download_file <url> <output-file>
|
|
299
|
+
#
|
|
300
|
+
# Download <url> into <output-file>.
|
|
301
|
+
#
|
|
302
|
+
# EXIT
|
|
303
|
+
# 3 curl is not on PATH, or the download fails
|
|
304
|
+
_download_file() {
|
|
305
|
+
_require_cmd curl
|
|
306
|
+
curl --silent --show-error --fail --location --output "$2" "$1" \
|
|
307
|
+
|| _fail 3 -v url "$1" "Download failed"
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
# _resolve_bin_dir
|
|
311
|
+
#
|
|
312
|
+
# Print the first writable bin dir of /usr/local/bin and ~/.local/bin.
|
|
313
|
+
# ~/.local/bin is the user bin dir in the "XDG Base Directory Specification":
|
|
314
|
+
# https://specifications.freedesktop.org/basedir/latest/
|
|
315
|
+
#
|
|
316
|
+
# EXIT
|
|
317
|
+
# 3 Neither /usr/local/bin nor ~/.local/bin is a writable directory
|
|
318
|
+
_resolve_bin_dir() {
|
|
319
|
+
for lib_rbd_dir in /usr/local/bin "${HOME:-}/.local/bin"; do
|
|
320
|
+
[ -d "$lib_rbd_dir" ] && [ -w "$lib_rbd_dir" ] && {
|
|
321
|
+
printf '%s' "$lib_rbd_dir"
|
|
322
|
+
return 0
|
|
323
|
+
}
|
|
324
|
+
done
|
|
325
|
+
|
|
326
|
+
_fail 3 -v current_user "$(whoami)" \
|
|
327
|
+
"No writable bin dir (/usr/local/bin, ~/.local/bin)"
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
# ╭───
|
|
331
|
+
# │ Text
|
|
332
|
+
# ╰───
|
|
333
|
+
|
|
334
|
+
# _end_with_newline <text>
|
|
335
|
+
#
|
|
336
|
+
# Print <text> with a newline at its end, the one $(...) strips. Print nothing
|
|
337
|
+
# when <text> is empty, where a plain printf would print one empty line.
|
|
338
|
+
_end_with_newline() {
|
|
339
|
+
if [ -n "$1" ]; then
|
|
340
|
+
printf '%s\n' "$1"
|
|
341
|
+
fi
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
# A block is the lines between two marker lines, <begin> and <end>, in a text
|
|
345
|
+
# that otherwise belongs to someone else: a crontab, a shell profile. A marker
|
|
346
|
+
# is a whole line, compared as written. A text must hold no block, or exactly
|
|
347
|
+
# one: one <begin>, and one <end> below it.
|
|
348
|
+
#
|
|
349
|
+
# awk reads the markers and the block lines from its environment, because
|
|
350
|
+
# awk -v rewrites a backslash in them. awk compares two values that look like
|
|
351
|
+
# numbers as numbers, so a marker "10" would match a line "10.0". "" after a
|
|
352
|
+
# value makes it a string.
|
|
353
|
+
|
|
354
|
+
# _has_valid_markers <begin> <end> <text>
|
|
355
|
+
#
|
|
356
|
+
# Tell if the markers in <text> make no block, or exactly one.
|
|
357
|
+
#
|
|
358
|
+
# RETURNS
|
|
359
|
+
# 0 <text> has neither marker, or each marker once with <begin> above <end>
|
|
360
|
+
# 1 A marker is there more than once or alone, or <begin> is not above
|
|
361
|
+
# <end>
|
|
362
|
+
#
|
|
363
|
+
# EXIT
|
|
364
|
+
# 3 A marker is empty or holds a newline
|
|
365
|
+
_has_valid_markers() {
|
|
366
|
+
# An empty marker matches every empty line, one with a newline matches none
|
|
367
|
+
case "$1$2" in
|
|
368
|
+
*"
|
|
369
|
+
"*) _fail 3 "A marker is one line" ;;
|
|
370
|
+
esac
|
|
371
|
+
|
|
372
|
+
if [ -z "$1" ] || [ -z "$2" ]; then
|
|
373
|
+
_fail 3 "A marker cannot be empty"
|
|
374
|
+
fi
|
|
375
|
+
|
|
376
|
+
# The line number of each marker. -F -x is the whole line, as written
|
|
377
|
+
lib_hvm_begin=$(printf '%s\n' "$3" | grep -n -F -x -e "$1" | cut -d ':' -f 1)
|
|
378
|
+
lib_hvm_end=$(printf '%s\n' "$3" | grep -n -F -x -e "$2" | cut -d ':' -f 1)
|
|
379
|
+
|
|
380
|
+
# Each is one number, when its marker is there once
|
|
381
|
+
case "$lib_hvm_begin:$lib_hvm_end" in
|
|
382
|
+
:) return 0 ;; # neither marker
|
|
383
|
+
*[!0-9:]* | :* | *:) return 1 ;; # one is there twice, or one is missing
|
|
384
|
+
esac
|
|
385
|
+
|
|
386
|
+
[ "$lib_hvm_begin" -lt "$lib_hvm_end" ] || return 1
|
|
387
|
+
|
|
388
|
+
return 0
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
# _find_block <begin> <end>
|
|
392
|
+
#
|
|
393
|
+
# Read a text from stdin, print the lines of its block without the markers.
|
|
394
|
+
# Print nothing when the text has no block.
|
|
395
|
+
#
|
|
396
|
+
# RETURNS
|
|
397
|
+
# 1 A marker is there more than once or alone, or <begin> is not above
|
|
398
|
+
# <end>. Prints nothing
|
|
399
|
+
#
|
|
400
|
+
# EXIT
|
|
401
|
+
# 3 A marker is empty or holds a newline
|
|
402
|
+
_find_block() {
|
|
403
|
+
lib_fb_text=$(cat)
|
|
404
|
+
_has_valid_markers "$1" "$2" "$lib_fb_text" || return 1
|
|
405
|
+
|
|
406
|
+
printf '%s\n' "$lib_fb_text" \
|
|
407
|
+
| BLOCK_BEGIN_MARKER="$1" BLOCK_END_MARKER="$2" awk '
|
|
408
|
+
BEGIN {
|
|
409
|
+
begin = ENVIRON["BLOCK_BEGIN_MARKER"] ""
|
|
410
|
+
end = ENVIRON["BLOCK_END_MARKER"] ""
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
$0 == begin { is_inside = 1; next }
|
|
414
|
+
$0 == end { is_inside = 0 }
|
|
415
|
+
is_inside { print }'
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
# _replace_block <begin> <end> <lines>
|
|
419
|
+
#
|
|
420
|
+
# Read a text from stdin, print it with the lines of its block replaced by
|
|
421
|
+
# <lines>. Add the block and its markers at the end when the text has none.
|
|
422
|
+
# Drop both when <lines> is empty.
|
|
423
|
+
#
|
|
424
|
+
# Take the result into a variable and read the status before writing it
|
|
425
|
+
# anywhere. In a pipe, the next command reads an empty text when
|
|
426
|
+
# _replace_block returns 1 or 2.
|
|
427
|
+
#
|
|
428
|
+
# RETURNS
|
|
429
|
+
# 1 A marker is there more than once or alone, or <begin> is not above
|
|
430
|
+
# <end>. Prints nothing
|
|
431
|
+
# 2 One of <lines> is a marker. Prints nothing
|
|
432
|
+
#
|
|
433
|
+
# EXIT
|
|
434
|
+
# 3 A marker is empty or holds a newline
|
|
435
|
+
_replace_block() {
|
|
436
|
+
lib_rb_text=$(cat)
|
|
437
|
+
_has_valid_markers "$1" "$2" "$lib_rb_text" || return 1
|
|
438
|
+
|
|
439
|
+
# A marker inside the block would make two blocks of it
|
|
440
|
+
if printf '%s\n' "$3" | grep -q -F -x -e "$1" -e "$2"; then
|
|
441
|
+
return 2
|
|
442
|
+
fi
|
|
443
|
+
|
|
444
|
+
_end_with_newline "$lib_rb_text" | BLOCK_BEGIN_MARKER="$1" BLOCK_END_MARKER="$2" BLOCK_LINES="$3" awk '
|
|
445
|
+
function put_block() {
|
|
446
|
+
if (lines != "") {
|
|
447
|
+
print begin
|
|
448
|
+
print lines
|
|
449
|
+
print end
|
|
450
|
+
}
|
|
451
|
+
is_put = 1
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
BEGIN {
|
|
455
|
+
begin = ENVIRON["BLOCK_BEGIN_MARKER"] ""
|
|
456
|
+
end = ENVIRON["BLOCK_END_MARKER"] ""
|
|
457
|
+
lines = ENVIRON["BLOCK_LINES"] ""
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
$0 == begin { is_inside = 1; next }
|
|
461
|
+
$0 == end { is_inside = 0; put_block(); next }
|
|
462
|
+
!is_inside { print }
|
|
463
|
+
|
|
464
|
+
END { if (!is_put) put_block() }'
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
# _diff_lines <old-text> <new-text>
|
|
468
|
+
#
|
|
469
|
+
# Print what changed between two sets of lines: "- <line>" for a line only in
|
|
470
|
+
# <old-text>, then "+ <line>" for a line only in <new-text>. The order of the
|
|
471
|
+
# lines, repeats and empty lines do not count.
|
|
472
|
+
#
|
|
473
|
+
# Print the "-" lines red and the "+" lines green when stdout can show color,
|
|
474
|
+
# see _has_color.
|
|
475
|
+
_diff_lines() {
|
|
476
|
+
lib_dl_color=""
|
|
477
|
+
if _has_color 1; then
|
|
478
|
+
lib_dl_color="true"
|
|
479
|
+
fi
|
|
480
|
+
|
|
481
|
+
DIFF_OLD_TEXT="$1" DIFF_NEW_TEXT="$2" awk -v color="$lib_dl_color" '
|
|
482
|
+
BEGIN {
|
|
483
|
+
old_count = split(ENVIRON["DIFF_OLD_TEXT"], old_lines, "\n")
|
|
484
|
+
new_count = split(ENVIRON["DIFF_NEW_TEXT"], new_lines, "\n")
|
|
485
|
+
|
|
486
|
+
minus = color ? "\033[31m- %s\033[0m\n" : "- %s\n"
|
|
487
|
+
plus = color ? "\033[32m+ %s\033[0m\n" : "+ %s\n"
|
|
488
|
+
|
|
489
|
+
for (i = 1; i <= old_count; i++) is_old[old_lines[i]] = 1
|
|
490
|
+
for (i = 1; i <= new_count; i++) is_new[new_lines[i]] = 1
|
|
491
|
+
|
|
492
|
+
for (i = 1; i <= old_count; i++) {
|
|
493
|
+
line = old_lines[i]
|
|
494
|
+
if (line != "" && !(line in is_new) && !(line in is_printed)) {
|
|
495
|
+
printf minus, line
|
|
496
|
+
is_printed[line] = 1
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
for (i = 1; i <= new_count; i++) {
|
|
501
|
+
line = new_lines[i]
|
|
502
|
+
if (line != "" && !(line in is_old) && !(line in is_printed)) {
|
|
503
|
+
printf plus, line
|
|
504
|
+
is_printed[line] = 1
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}'
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
# ╭───
|
|
511
|
+
# │ Terminal
|
|
512
|
+
# ╰───
|
|
513
|
+
|
|
514
|
+
_HAS_TITLE="" # "true" once _set_title changed the window title
|
|
515
|
+
|
|
516
|
+
# _set_title <title>
|
|
517
|
+
#
|
|
518
|
+
# Name the terminal window, when stderr is a terminal. The escape codes are
|
|
519
|
+
# xterm's and most terminals read them, a flag such as --title differs per
|
|
520
|
+
# terminal. The first code saves the old title, _restore_title restores it.
|
|
521
|
+
_set_title() {
|
|
522
|
+
[ -t 2 ] || return 0
|
|
523
|
+
|
|
524
|
+
printf '\033[22;0t\033]0;%s\007' "$1" >&2
|
|
525
|
+
_HAS_TITLE="true"
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
# _restore_title
|
|
529
|
+
#
|
|
530
|
+
# Restore the title that _set_title saved. The window may be gone, and a
|
|
531
|
+
# failed write must not stop the rest.
|
|
532
|
+
_restore_title() {
|
|
533
|
+
if [ -n "$_HAS_TITLE" ]; then
|
|
534
|
+
printf '\033[23;0t' >&2 2>/dev/null || true
|
|
535
|
+
_HAS_TITLE=""
|
|
536
|
+
fi
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
# _wait_for_enter
|
|
540
|
+
#
|
|
541
|
+
# Stop until the user presses Enter. A window opened only for the calling
|
|
542
|
+
# command closes with it, before the user reads what it shows. Without a
|
|
543
|
+
# terminal on stdin, read takes the next line or finds none, and the wait
|
|
544
|
+
# ends at once.
|
|
545
|
+
_wait_for_enter() {
|
|
546
|
+
printf 'Press Enter to continue ' >&2
|
|
547
|
+
# shellcheck disable=SC2034 # read needs a name, nobody reads it
|
|
548
|
+
read -r lib_wfe_reply || true
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
# ╭───
|
|
552
|
+
# │ Help
|
|
553
|
+
# ╰───
|
|
554
|
+
|
|
555
|
+
# _show_help <script-file>
|
|
556
|
+
#
|
|
557
|
+
# Print the man page of <script-file>: the comment block that starts at its
|
|
558
|
+
# NAME line.
|
|
559
|
+
#
|
|
560
|
+
# EXIT
|
|
561
|
+
# 2 <script-file> is not a file
|
|
562
|
+
_show_help() {
|
|
563
|
+
if [ ! -f "$1" ]; then
|
|
564
|
+
_fail 2 -v path "$1" "Script not found"
|
|
565
|
+
fi
|
|
566
|
+
|
|
567
|
+
sed -n '/^# NAME$/,/^$/p' "$1" \
|
|
568
|
+
| sed -e 's/^# \{0,1\}//'
|
|
569
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
|
|
3
|
+
# NAME
|
|
4
|
+
# vblocks serve - Start the development server of an app
|
|
5
|
+
#
|
|
6
|
+
# SYNOPSIS
|
|
7
|
+
# vblocks serve
|
|
8
|
+
# [--mode <development|production>]
|
|
9
|
+
# [--prefix <directory>]
|
|
10
|
+
# [--analyze]
|
|
11
|
+
# [--log-level <error|warn|info|verbose>]
|
|
12
|
+
# [-- <webpack option>...]
|
|
13
|
+
#
|
|
14
|
+
# DESCRIPTION
|
|
15
|
+
# The app folder holds src/index.tsx and src/index.html. A config in it,
|
|
16
|
+
# webpack.config.js, postcss.config.js or .swcrc, replaces the VBlocks
|
|
17
|
+
# default of the same name. So do src/public/robots.txt and
|
|
18
|
+
# src/public/favicon.svg.
|
|
19
|
+
#
|
|
20
|
+
# Print the app folder, the VBlocks folder and the files Webpack runs with,
|
|
21
|
+
# then start the server.
|
|
22
|
+
#
|
|
23
|
+
# OPTIONS
|
|
24
|
+
# --mode <mode> The Webpack mode, development or production
|
|
25
|
+
# (default: development)
|
|
26
|
+
# --prefix <directory> The app folder, which must exist (default: the
|
|
27
|
+
# current folder)
|
|
28
|
+
# --analyze Run the bundle analyzer, on a free port. An
|
|
29
|
+
# ejected webpack.config.js must read
|
|
30
|
+
# env.analyze
|
|
31
|
+
# --log-level <level> How much Webpack and its development server
|
|
32
|
+
# print: error, warn, info or verbose (default:
|
|
33
|
+
# warn)
|
|
34
|
+
# -- <webpack option>... Pass each option to Webpack, unchanged
|
|
35
|
+
# -h, --help Print this help
|
|
36
|
+
#
|
|
37
|
+
# ENVIRONMENT
|
|
38
|
+
# NO_COLOR When non-empty, print without color, the icons or the header
|
|
39
|
+
#
|
|
40
|
+
# EXAMPLE
|
|
41
|
+
# npx vblocks serve
|
|
42
|
+
# npx vblocks serve --prefix flow -- --host 0.0.0.0
|
|
43
|
+
#
|
|
44
|
+
# EXIT CODES
|
|
45
|
+
# 0 Done
|
|
46
|
+
# 2 Invalid usage, or Webpack rejected an option or the config
|
|
47
|
+
# 3 Could not run: a missing tool, or a failed Webpack run
|
|
48
|
+
#
|
|
49
|
+
# SEE ALSO
|
|
50
|
+
# vblocks, vblocks build, vblocks eject, webpack
|
|
51
|
+
|
|
52
|
+
# vblocks runs this file. Never call it directly.
|
|
53
|
+
|
|
54
|
+
set -eu
|
|
55
|
+
|
|
56
|
+
_SCRIPT_DIR=$(dirname "$0")
|
|
57
|
+
# shellcheck source=vblocks.sh
|
|
58
|
+
. "$_SCRIPT_DIR/vblocks.sh"
|
|
59
|
+
# shellcheck source=webpack.sh
|
|
60
|
+
. "$_SCRIPT_DIR/webpack.sh"
|
|
61
|
+
|
|
62
|
+
# ╭───
|
|
63
|
+
# │ Settings
|
|
64
|
+
# ╰───
|
|
65
|
+
|
|
66
|
+
_PREFIX_DIR="" # --prefix, the app folder
|
|
67
|
+
_MODE="development" # --mode: development or production
|
|
68
|
+
_LOG_LEVEL="warn" # --log-level: error, warn, info or verbose
|
|
69
|
+
_SHOULD_ANALYZE="" # --analyze, "true" or ""
|
|
70
|
+
|
|
71
|
+
# ╭───
|
|
72
|
+
# │ Flags and positionals
|
|
73
|
+
# ╰───
|
|
74
|
+
|
|
75
|
+
while [ "$#" -gt 0 ]; do
|
|
76
|
+
case "$1" in
|
|
77
|
+
-h | --help)
|
|
78
|
+
_show_help "$0"
|
|
79
|
+
exit 0
|
|
80
|
+
;;
|
|
81
|
+
--prefix)
|
|
82
|
+
[ "$#" -lt 2 ] && _fail 2 "--prefix needs a folder"
|
|
83
|
+
_PREFIX_DIR=$2
|
|
84
|
+
shift 2
|
|
85
|
+
;;
|
|
86
|
+
--mode)
|
|
87
|
+
_guard_mode "${2:-}"
|
|
88
|
+
_MODE=$2
|
|
89
|
+
shift 2
|
|
90
|
+
;;
|
|
91
|
+
--log-level)
|
|
92
|
+
_guard_log_level "${2:-}"
|
|
93
|
+
_LOG_LEVEL=$2
|
|
94
|
+
shift 2
|
|
95
|
+
;;
|
|
96
|
+
--analyze)
|
|
97
|
+
_SHOULD_ANALYZE="true"
|
|
98
|
+
shift
|
|
99
|
+
;;
|
|
100
|
+
--single-file | --single)
|
|
101
|
+
_fail 2 "--single-file works only with build"
|
|
102
|
+
;;
|
|
103
|
+
--)
|
|
104
|
+
shift
|
|
105
|
+
break
|
|
106
|
+
;;
|
|
107
|
+
-?*)
|
|
108
|
+
_fail 2 -v flag "$1" "Unknown flag. For Webpack options, place them after --"
|
|
109
|
+
;;
|
|
110
|
+
*)
|
|
111
|
+
_fail 2 -v argument "$1" "serve takes no positional"
|
|
112
|
+
;;
|
|
113
|
+
esac
|
|
114
|
+
done
|
|
115
|
+
|
|
116
|
+
if [ -n "$_PREFIX_DIR" ] && [ ! -d "$_PREFIX_DIR" ]; then
|
|
117
|
+
_fail 2 -v prefix "$_PREFIX_DIR" "--prefix is not a folder"
|
|
118
|
+
fi
|
|
119
|
+
|
|
120
|
+
# node runs webpack-cli
|
|
121
|
+
_require_cmd node
|
|
122
|
+
_export_version
|
|
123
|
+
|
|
124
|
+
# ╭───
|
|
125
|
+
# │ Main
|
|
126
|
+
# ╰───
|
|
127
|
+
|
|
128
|
+
_APP_DIR=$(cd -- "${_PREFIX_DIR:-.}" && pwd)
|
|
129
|
+
_WEBPACK_CONFIG=$(_pick_file "$_APP_DIR" webpack.config.js)
|
|
130
|
+
_POSTCSS_CONFIG=$(_pick_file "$_APP_DIR" postcss.config.js)
|
|
131
|
+
_SWC_CONFIG=$(_pick_file "$_APP_DIR" .swcrc)
|
|
132
|
+
_ROBOTS_FILE=$(_pick_file "$_APP_DIR" src/public/robots.txt)
|
|
133
|
+
_FAVICON_FILE=$(_pick_file "$_APP_DIR" src/public/favicon.svg)
|
|
134
|
+
|
|
135
|
+
_print_header "$VB_VERSION" "${_BOLD}serve${_RESET}: $_MODE${_SHOULD_ANALYZE:+, analyze}" >&2
|
|
136
|
+
_print_summary "$_APP_DIR" "$_WEBPACK_CONFIG" "$_POSTCSS_CONFIG" "$_SWC_CONFIG" \
|
|
137
|
+
"$_ROBOTS_FILE" "$_FAVICON_FILE"
|
|
138
|
+
|
|
139
|
+
# The default Webpack config reads env.analyze. An ejected config supports
|
|
140
|
+
# --analyze only if it reads env.analyze too
|
|
141
|
+
_run_webpack serve "$_WEBPACK_CONFIG" \
|
|
142
|
+
--env "entryDir=$_APP_DIR" \
|
|
143
|
+
--env "postcssConfigPath=$_POSTCSS_CONFIG" \
|
|
144
|
+
--env "swcConfigPath=$_SWC_CONFIG" \
|
|
145
|
+
--env "robotsPath=$_ROBOTS_FILE" \
|
|
146
|
+
--env "faviconPath=$_FAVICON_FILE" \
|
|
147
|
+
--env "logLevel=$_LOG_LEVEL" \
|
|
148
|
+
--mode "$_MODE" ${_SHOULD_ANALYZE:+--env analyze} "$@"
|
|
149
|
+
exit 0
|