@asd14/vblocks 5.4.0 → 6.0.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 +28 -36
- package/bin/vblocks +120 -0
- package/bin/vblocks.d/build +145 -0
- package/bin/vblocks.d/eject +38 -0
- package/bin/vblocks.d/init +274 -0
- package/bin/vblocks.d/lib.sh +569 -0
- package/bin/vblocks.d/serve +100 -0
- package/bin/vblocks.d/vblocks.sh +92 -0
- package/bin/vblocks.d/webpack.sh +130 -0
- package/package.json +2 -2
- package/src/types/client.d.ts +19 -0
- package/src/webpack.config.js +32 -21
- package/template/xyflow/app/src/diagram.tsx +0 -2
- package/bin/vblocks.sh +0 -685
- package/template/xyflow/app/src/ui/zoom-level.tsx +0 -29
package/bin/vblocks.sh
DELETED
|
@@ -1,685 +0,0 @@
|
|
|
1
|
-
#!/bin/sh
|
|
2
|
-
|
|
3
|
-
set -eu
|
|
4
|
-
|
|
5
|
-
# NAME
|
|
6
|
-
# vblocks - Run Webpack with the configs of VBlocks, write a project or an app
|
|
7
|
-
#
|
|
8
|
-
# SYNOPSIS
|
|
9
|
-
# vblocks init <directory>
|
|
10
|
-
# [--template <plain|xyflow|xyflow-tabs>]
|
|
11
|
-
# [--only <app|plumbing>]
|
|
12
|
-
# vblocks serve
|
|
13
|
-
# [--mode <development|production>]
|
|
14
|
-
# [--prefix <directory>]
|
|
15
|
-
# [--analyze]
|
|
16
|
-
# [--log-level <error|warn|info|verbose>]
|
|
17
|
-
# [<webpack option>...]
|
|
18
|
-
# vblocks build
|
|
19
|
-
# [--mode <development|production>]
|
|
20
|
-
# [--prefix <directory>]
|
|
21
|
-
# [--single-file]
|
|
22
|
-
# [--log-level <error|warn|info|verbose>]
|
|
23
|
-
# [<webpack option>...]
|
|
24
|
-
# vblocks eject <webpack|postcss|swc>
|
|
25
|
-
# vblocks -h | --help
|
|
26
|
-
#
|
|
27
|
-
# DESCRIPTION
|
|
28
|
-
# The app folder holds src/index.tsx and src/index.html. A config in it,
|
|
29
|
-
# webpack.config.js, postcss.config.js or .swcrc, replaces the default of
|
|
30
|
-
# VBlocks with the same name.
|
|
31
|
-
#
|
|
32
|
-
# init Write a template into <directory>, which must be missing or
|
|
33
|
-
# empty. A template has two parts: the plumbing, which holds
|
|
34
|
-
# the files of the project folder, and the app. Without --only,
|
|
35
|
-
# write both.
|
|
36
|
-
# After each part, run its post-init hooks in <directory>.
|
|
37
|
-
# Print each file it adds or updates, then the next steps
|
|
38
|
-
# serve Start the development server
|
|
39
|
-
# build Build the app into dist/ in the app folder, then print each
|
|
40
|
-
# file in it
|
|
41
|
-
# eject Print a default config, to save in the app folder
|
|
42
|
-
#
|
|
43
|
-
# serve and build pass every option not listed under OPTIONS to Webpack.
|
|
44
|
-
#
|
|
45
|
-
# OPTIONS
|
|
46
|
-
# --template <name> init: plain, xyflow or xyflow-tabs (default: plain)
|
|
47
|
-
# --only <app|plumbing> init: write only the app or only the plumbing.
|
|
48
|
-
# init adds the app's dependencies to the
|
|
49
|
-
# project folder: the nearest folder above
|
|
50
|
-
# <directory> with a package.json
|
|
51
|
-
# --mode <mode> serve, build: the Webpack mode, development or
|
|
52
|
-
# production (default: development for serve,
|
|
53
|
-
# production for build)
|
|
54
|
-
# --prefix <directory> serve, build: the app folder (default: the
|
|
55
|
-
# current folder)
|
|
56
|
-
# --single-file build: one standalone dist/index.html, every
|
|
57
|
-
# asset inlined
|
|
58
|
-
# --analyze serve: run the bundle analyzer, on a free port
|
|
59
|
-
# --log-level <level> serve, build: how much Webpack, its dev server and
|
|
60
|
-
# npm print: error, warn, info or verbose (default:
|
|
61
|
-
# warn)
|
|
62
|
-
# -h, --help Print this help
|
|
63
|
-
#
|
|
64
|
-
# ENVIRONMENT
|
|
65
|
-
# NO_COLOR When set and non-empty, print every summary without color
|
|
66
|
-
#
|
|
67
|
-
# EXAMPLE
|
|
68
|
-
# npx @asd14/vblocks init my-app
|
|
69
|
-
# npx @asd14/vblocks init demos --only plumbing
|
|
70
|
-
# npx @asd14/vblocks init demos/flow --template xyflow --only app
|
|
71
|
-
# npx vblocks build --prefix flow --single-file
|
|
72
|
-
# npx vblocks eject postcss > postcss.config.js
|
|
73
|
-
#
|
|
74
|
-
# EXIT CODES
|
|
75
|
-
# 0 Done
|
|
76
|
-
# 2 Invalid usage, or Webpack rejected an option or the config
|
|
77
|
-
# 3 Could not run: a missing tool or project, or a failed folder, copy,
|
|
78
|
-
# hook or build
|
|
79
|
-
#
|
|
80
|
-
# SEE ALSO
|
|
81
|
-
# webpack, npm
|
|
82
|
-
|
|
83
|
-
# npm links the command to this file, so "$0" names the link. realpath follows
|
|
84
|
-
# it to the package
|
|
85
|
-
VB_DIR=$(dirname "$(dirname "$(realpath "$0")")")
|
|
86
|
-
|
|
87
|
-
# `cd` follows an exported CDPATH and prints the folder it picked, which
|
|
88
|
-
# `$(cd ... && pwd)` would capture
|
|
89
|
-
unset CDPATH
|
|
90
|
-
|
|
91
|
-
# ╭───
|
|
92
|
-
# │ Settings
|
|
93
|
-
# ╰───
|
|
94
|
-
|
|
95
|
-
COMMAND="" # init, serve, build or eject
|
|
96
|
-
INIT_DIR="" # init <directory>
|
|
97
|
-
TEMPLATE="plain" # init --template
|
|
98
|
-
PARTS="plumbing app" # init --only, "plumbing" or "app"
|
|
99
|
-
PREFIX="" # serve/build --prefix, the app folder
|
|
100
|
-
LOG_LEVEL="warn" # serve/build --log-level, error, warn, info or verbose
|
|
101
|
-
EJECT_CONFIG="" # eject <config>
|
|
102
|
-
|
|
103
|
-
# ╭───
|
|
104
|
-
# │ Primitives
|
|
105
|
-
# ╰───
|
|
106
|
-
|
|
107
|
-
# fail <code> <message>
|
|
108
|
-
#
|
|
109
|
-
# Print <message> to stderr, then exit with <code>.
|
|
110
|
-
fail() {
|
|
111
|
-
printf 'vblocks: %s\n' "$2" >&2
|
|
112
|
-
exit "$1"
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
# show_help
|
|
116
|
-
#
|
|
117
|
-
# Print the man page: the comment block that starts at the NAME line.
|
|
118
|
-
show_help() {
|
|
119
|
-
sed -n '/^# NAME$/,/^$/p' "$0" | sed 's/^# \{0,1\}//'
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
# require_cmd <cmd>...
|
|
123
|
-
#
|
|
124
|
-
# EXIT
|
|
125
|
-
# 3 The shell finds no <cmd>
|
|
126
|
-
require_cmd() {
|
|
127
|
-
for require_name in "$@"; do
|
|
128
|
-
command -v "$require_name" >/dev/null 2>&1 \
|
|
129
|
-
|| fail 3 "$require_name is not installed"
|
|
130
|
-
done
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
# has_color
|
|
134
|
-
#
|
|
135
|
-
# RETURNS
|
|
136
|
-
# 0 stderr is a terminal that shows color
|
|
137
|
-
# 1 It is not, NO_COLOR is non-empty, or TERM is dumb or unset
|
|
138
|
-
has_color() {
|
|
139
|
-
[ -t 2 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-dumb}" != "dumb" ]
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
# pick_file <file> <default-file>
|
|
143
|
-
#
|
|
144
|
-
# Print <file> when it exists, otherwise <default-file>.
|
|
145
|
-
pick_file() {
|
|
146
|
-
if [ -f "$1" ]; then
|
|
147
|
-
printf '%s\n' "$1"
|
|
148
|
-
else
|
|
149
|
-
printf '%s\n' "$2"
|
|
150
|
-
fi
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
# shorten_home <path>
|
|
154
|
-
#
|
|
155
|
-
# Print <path> with the home folder as "~".
|
|
156
|
-
shorten_home() {
|
|
157
|
-
if [ -n "${HOME:-}" ]; then
|
|
158
|
-
case "$1" in
|
|
159
|
-
"$HOME"/*)
|
|
160
|
-
# shellcheck disable=SC2088 # a literal "~", for display
|
|
161
|
-
printf '~/%s\n' "${1#"$HOME"/}"
|
|
162
|
-
return 0
|
|
163
|
-
;;
|
|
164
|
-
esac
|
|
165
|
-
fi
|
|
166
|
-
|
|
167
|
-
printf '%s\n' "$1"
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
# shorten_path <absolute-path>
|
|
171
|
-
#
|
|
172
|
-
# Print <absolute-path> relative to the current folder when it is inside it,
|
|
173
|
-
# otherwise with the home folder as "~".
|
|
174
|
-
shorten_path() {
|
|
175
|
-
case "$1" in
|
|
176
|
-
"$PWD") printf '.\n' ;;
|
|
177
|
-
"$PWD"/*) printf '%s\n' "${1#"$PWD"/}" ;;
|
|
178
|
-
*) shorten_home "$1" ;;
|
|
179
|
-
esac
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
# ╭───
|
|
183
|
-
# │ Domain primitives
|
|
184
|
-
# ╰───
|
|
185
|
-
|
|
186
|
-
# list_templates
|
|
187
|
-
#
|
|
188
|
-
# Print the template names, one per line. A template is a folder in
|
|
189
|
-
# `template/` whose name has no `_` prefix.
|
|
190
|
-
list_templates() {
|
|
191
|
-
for list_dir in "$VB_DIR"/template/*/; do
|
|
192
|
-
[ -d "$list_dir" ] || continue
|
|
193
|
-
list_name=$(basename "$list_dir")
|
|
194
|
-
|
|
195
|
-
case "$list_name" in
|
|
196
|
-
_*) ;;
|
|
197
|
-
*) printf '%s\n' "$list_name" ;;
|
|
198
|
-
esac
|
|
199
|
-
done
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
# find_project_root <directory>
|
|
203
|
-
#
|
|
204
|
-
# Print the nearest folder above <directory> that holds a package.json.
|
|
205
|
-
# <directory> and the folders above it need not exist yet.
|
|
206
|
-
#
|
|
207
|
-
# RETURNS
|
|
208
|
-
# 1 No folder above <directory> holds one
|
|
209
|
-
find_project_root() {
|
|
210
|
-
case "$1" in
|
|
211
|
-
/*) root_dir=$1 ;;
|
|
212
|
-
*) root_dir="$PWD/$1" ;;
|
|
213
|
-
esac
|
|
214
|
-
|
|
215
|
-
while [ "$root_dir" != "/" ]; do
|
|
216
|
-
root_dir=$(dirname "$root_dir")
|
|
217
|
-
|
|
218
|
-
if [ -f "$root_dir/package.json" ]; then
|
|
219
|
-
# Drop "." and ".." from the path. The caller compares it with `pwd`
|
|
220
|
-
(cd -- "$root_dir" && pwd)
|
|
221
|
-
return 0
|
|
222
|
-
fi
|
|
223
|
-
done
|
|
224
|
-
|
|
225
|
-
return 1
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
# init_project <directory> <template> <parts>
|
|
229
|
-
#
|
|
230
|
-
# Copy the <parts> of <template> into <directory>, then run the hooks of each
|
|
231
|
-
# part in it. <parts> is "plumbing", "app" or "plumbing app". A template's file
|
|
232
|
-
# replaces the `_common` file of the same path.
|
|
233
|
-
#
|
|
234
|
-
# EXIT
|
|
235
|
-
# 3 A copy or a hook failed
|
|
236
|
-
init_project() {
|
|
237
|
-
init_dir=$1
|
|
238
|
-
init_template=$2
|
|
239
|
-
|
|
240
|
-
for init_part in $3; do
|
|
241
|
-
for init_source in "$VB_DIR/template/_common" "$VB_DIR/template/$init_template"; do
|
|
242
|
-
[ -d "$init_source/$init_part" ] || continue
|
|
243
|
-
|
|
244
|
-
cp -R -- "$init_source/$init_part/." "$init_dir" \
|
|
245
|
-
|| fail 3 "Could not copy $init_source/$init_part"
|
|
246
|
-
done
|
|
247
|
-
|
|
248
|
-
for init_source in "$VB_DIR/template/_common" "$VB_DIR/template/$init_template"; do
|
|
249
|
-
init_hook="$init_source/post-init-$init_part.sh"
|
|
250
|
-
[ -f "$init_hook" ] || continue
|
|
251
|
-
|
|
252
|
-
# A hook reads VB_VERSION, and nothing else of this script
|
|
253
|
-
(cd -- "$init_dir" && VB_VERSION="$VB_VERSION" sh "$init_hook") \
|
|
254
|
-
|| fail 3 "The hook $init_hook failed"
|
|
255
|
-
done
|
|
256
|
-
done
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
# show_config_file <config-file> <app-dir>
|
|
260
|
-
#
|
|
261
|
-
# Print <config-file> with its folder as $APP_DIR, or as $VBLOCKS_DIR in
|
|
262
|
-
# gray for a default config.
|
|
263
|
-
show_config_file() {
|
|
264
|
-
case "$1" in
|
|
265
|
-
"$2"/*) printf '%s\n' "\$APP_DIR/${1#"$2"/}" ;;
|
|
266
|
-
"$VB_DIR"/*) printf '%s\n' "${GRAY}\$VBLOCKS_DIR/${1#"$VB_DIR"/}${RESET}" ;;
|
|
267
|
-
*) printf '%s\n' "$1" ;;
|
|
268
|
-
esac
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
# print_title
|
|
272
|
-
#
|
|
273
|
-
# Print the first line of a summary box: the name and the version.
|
|
274
|
-
print_title() {
|
|
275
|
-
printf '╭───────────┤ %s\n' "${GREEN} VBlocks v${VB_VERSION}${RESET}"
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
# snapshot_folder <dir>
|
|
279
|
-
#
|
|
280
|
-
# Print one line per file in <dir>: its checksum, a tab, its path relative to
|
|
281
|
-
# <dir>. Skip node_modules/, dist/ and .git/. init never writes there, and
|
|
282
|
-
# they can hold thousands of files.
|
|
283
|
-
snapshot_folder() {
|
|
284
|
-
(cd -- "$1" && find . \( -name node_modules -o -name dist -o -name .git \) \
|
|
285
|
-
-prune -o -type f -print) \
|
|
286
|
-
| sed 's|^\./||' \
|
|
287
|
-
| LC_ALL=C sort \
|
|
288
|
-
| while IFS= read -r snapshot_file; do
|
|
289
|
-
printf '%s\t%s\n' "$(cksum <"$1/$snapshot_file")" "$snapshot_file"
|
|
290
|
-
done
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
# print_snapshot_diff <before-snapshot> <after-snapshot> <app-subdir>
|
|
294
|
-
#
|
|
295
|
-
# Print "add", "update" or "drop" for each file that differs between two
|
|
296
|
-
# snapshots of the project folder. A path inside <app-subdir> prints as
|
|
297
|
-
# $APP_DIR/..., the rest as $PROJECT_DIR/... <app-subdir> is "." when the app
|
|
298
|
-
# is the project folder, empty when there is no app.
|
|
299
|
-
print_snapshot_diff() {
|
|
300
|
-
# Every snapshot line has a tab. The AFTER line has none, so it marks where
|
|
301
|
-
# the second snapshot starts
|
|
302
|
-
{
|
|
303
|
-
printf '%s\n' "$1"
|
|
304
|
-
printf 'AFTER\n'
|
|
305
|
-
printf '%s\n' "$2"
|
|
306
|
-
} | awk -F '\t' -v app="$3" '
|
|
307
|
-
function label(path) {
|
|
308
|
-
if (app == ".") return "$APP_DIR/" path
|
|
309
|
-
if (app != "" && index(path, app "/") == 1) {
|
|
310
|
-
return "$APP_DIR/" substr(path, length(app) + 2)
|
|
311
|
-
}
|
|
312
|
-
return "$PROJECT_DIR/" path
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
NF < 2 { is_after = 1; next }
|
|
316
|
-
!is_after { before[$2] = $1; next }
|
|
317
|
-
{
|
|
318
|
-
seen[$2] = 1
|
|
319
|
-
if (!($2 in before)) printf "%-8s%s\n", "add", label($2)
|
|
320
|
-
else if (before[$2] != $1) printf "%-8s%s\n", "update", label($2)
|
|
321
|
-
}
|
|
322
|
-
END {
|
|
323
|
-
for (path in before) {
|
|
324
|
-
if (!(path in seen)) printf "%-8s%s\n", "drop", label(path)
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
'
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
# print_build_files <app-dir>
|
|
331
|
-
#
|
|
332
|
-
# Print each file in the dist/ of <app-dir> as $APP_DIR/dist/..., a tab and its
|
|
333
|
-
# size, then the total size. Webpack empties dist/ before each build, so every
|
|
334
|
-
# file there is from the last build.
|
|
335
|
-
print_build_files() {
|
|
336
|
-
# An ejected config can write elsewhere
|
|
337
|
-
[ -d "$1/dist" ] || return 0
|
|
338
|
-
|
|
339
|
-
(cd -- "$1/dist" && find . -type f | LC_ALL=C sort) \
|
|
340
|
-
| while IFS= read -r build_file; do
|
|
341
|
-
printf '%s\t%s\n' "$(wc -c <"$1/dist/$build_file")" "${build_file#./}"
|
|
342
|
-
done \
|
|
343
|
-
| awk -F '\t' '
|
|
344
|
-
function format_size(bytes) {
|
|
345
|
-
if (bytes < 1024) return bytes " B"
|
|
346
|
-
if (bytes < 1024 * 1024) return sprintf("%.1f KiB", bytes / 1024)
|
|
347
|
-
return sprintf("%.1f MiB", bytes / 1024 / 1024)
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
{ total += $1; printf "$APP_DIR/dist/%s\t%s\n", $2, format_size($1) }
|
|
351
|
-
END { printf "\nTotal\t%s\n", format_size(total) }
|
|
352
|
-
'
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
# print_init_summary <parts> <template> <project-dir> <app-dir>
|
|
356
|
-
#
|
|
357
|
-
# Print the version, the parts of <template> that init writes and their
|
|
358
|
-
# folders, to stderr. <app-dir> is empty when init writes no app.
|
|
359
|
-
print_init_summary() {
|
|
360
|
-
case "$1" in
|
|
361
|
-
plumbing) summary_parts="plumbing" ;;
|
|
362
|
-
app) summary_parts="app, $2" ;;
|
|
363
|
-
*) summary_parts="plumbing and app, $2" ;;
|
|
364
|
-
esac
|
|
365
|
-
|
|
366
|
-
{
|
|
367
|
-
print_title
|
|
368
|
-
printf '│ %s\n' "init: $summary_parts"
|
|
369
|
-
printf '│\n'
|
|
370
|
-
|
|
371
|
-
# When plumbing and app share one folder, print it once, as $APP_DIR
|
|
372
|
-
if [ "$3" != "$4" ]; then
|
|
373
|
-
printf '│ %s\n' "\$PROJECT_DIR: $(shorten_home "$3")"
|
|
374
|
-
fi
|
|
375
|
-
if [ -n "$4" ]; then
|
|
376
|
-
printf '│ %s\n' "\$APP_DIR: $(shorten_home "$4")"
|
|
377
|
-
fi
|
|
378
|
-
|
|
379
|
-
printf '╰──┤\n\n'
|
|
380
|
-
} >&2
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
# print_summary <app-dir> <webpack-config> <postcss-config> <swc-config>
|
|
384
|
-
#
|
|
385
|
-
# Print the version, the app folder and the configs Webpack runs with, to
|
|
386
|
-
# stderr.
|
|
387
|
-
print_summary() {
|
|
388
|
-
{
|
|
389
|
-
print_title
|
|
390
|
-
printf '│ %s\n' "${BOLD} POSIX + Webpack, an unlikely story!${RESET}"
|
|
391
|
-
printf '│\n'
|
|
392
|
-
printf '│ %s\n' "\$APP_DIR: $(shorten_home "$1")"
|
|
393
|
-
printf '│ %s\n' "\$VBLOCKS_DIR: ${GRAY}$(shorten_home "$VB_DIR")${RESET}"
|
|
394
|
-
printf '│\n'
|
|
395
|
-
printf '│ %s\n' " Webpack: $(show_config_file "$2" "$1")"
|
|
396
|
-
printf '│ %s\n' " PostCSS: $(show_config_file "$3" "$1")"
|
|
397
|
-
printf '│ %s\n' " SWC: $(show_config_file "$4" "$1")"
|
|
398
|
-
printf '╰──┤\n\n'
|
|
399
|
-
} >&2
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
# run_webpack <action> <app-dir> <webpack-config> [<webpack option>...]
|
|
403
|
-
#
|
|
404
|
-
# Run `webpack <action>` for the app in <app-dir>.
|
|
405
|
-
#
|
|
406
|
-
# EXIT
|
|
407
|
-
# 2 Webpack rejected an option or the config
|
|
408
|
-
# 3 Webpack failed
|
|
409
|
-
run_webpack() {
|
|
410
|
-
webpack_action=$1
|
|
411
|
-
webpack_app_dir=$2
|
|
412
|
-
webpack_config=$3
|
|
413
|
-
shift 3
|
|
414
|
-
|
|
415
|
-
webpack_robots_file=$(pick_file \
|
|
416
|
-
"$webpack_app_dir/src/public/robots.txt" "$VB_DIR/src/public/robots.txt")
|
|
417
|
-
webpack_favicon_file=$(pick_file \
|
|
418
|
-
"$webpack_app_dir/src/public/favicon.svg" "$VB_DIR/src/public/favicon.svg")
|
|
419
|
-
|
|
420
|
-
# npm echoes every npx call at notice, the level between warn and info
|
|
421
|
-
case "$LOG_LEVEL" in
|
|
422
|
-
info) webpack_npm_level="notice" ;;
|
|
423
|
-
*) webpack_npm_level=$LOG_LEVEL ;;
|
|
424
|
-
esac
|
|
425
|
-
|
|
426
|
-
webpack_status=0
|
|
427
|
-
npx --loglevel="$webpack_npm_level" webpack "$webpack_action" \
|
|
428
|
-
--config "$webpack_config" \
|
|
429
|
-
--env "entryDir=$webpack_app_dir" \
|
|
430
|
-
--env "logLevel=$LOG_LEVEL" \
|
|
431
|
-
--env "robotsPath=$webpack_robots_file" \
|
|
432
|
-
--env "faviconPath=$webpack_favicon_file" \
|
|
433
|
-
"$@" || webpack_status=$?
|
|
434
|
-
|
|
435
|
-
# webpack-cli exits 1 when the build has errors, 2 when an option or the
|
|
436
|
-
# config is wrong
|
|
437
|
-
case "$webpack_status" in
|
|
438
|
-
0) return 0 ;;
|
|
439
|
-
2) fail 2 "Webpack rejected an option or the config" ;;
|
|
440
|
-
*) fail 3 "Webpack failed" ;;
|
|
441
|
-
esac
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
# ╭───
|
|
445
|
-
# │ Bootstrap
|
|
446
|
-
# ╰───
|
|
447
|
-
|
|
448
|
-
case "${1:-}" in
|
|
449
|
-
-h | --help)
|
|
450
|
-
show_help
|
|
451
|
-
exit 0
|
|
452
|
-
;;
|
|
453
|
-
"")
|
|
454
|
-
show_help
|
|
455
|
-
exit 2
|
|
456
|
-
;;
|
|
457
|
-
esac
|
|
458
|
-
|
|
459
|
-
require_cmd node npx
|
|
460
|
-
|
|
461
|
-
VB_VERSION=$(node -p 'require(process.argv[1]).version' "$VB_DIR/package.json")
|
|
462
|
-
|
|
463
|
-
# Color codes for stderr, empty when it shows no color
|
|
464
|
-
BOLD="" GREEN="" GRAY="" RESET=""
|
|
465
|
-
if has_color; then
|
|
466
|
-
BOLD=$(printf '\033[1m')
|
|
467
|
-
GREEN=$(printf '\033[32m')
|
|
468
|
-
GRAY=$(printf '\033[90m')
|
|
469
|
-
RESET=$(printf '\033[0m')
|
|
470
|
-
fi
|
|
471
|
-
|
|
472
|
-
# ╭───
|
|
473
|
-
# │ Flags and positionals
|
|
474
|
-
# ╰───
|
|
475
|
-
|
|
476
|
-
COMMAND=$1
|
|
477
|
-
shift
|
|
478
|
-
|
|
479
|
-
case "$COMMAND" in
|
|
480
|
-
init)
|
|
481
|
-
while [ "$#" -gt 0 ]; do
|
|
482
|
-
case "$1" in
|
|
483
|
-
--template)
|
|
484
|
-
[ "$#" -lt 2 ] && fail 2 "--template needs a name"
|
|
485
|
-
TEMPLATE=$2
|
|
486
|
-
shift 2
|
|
487
|
-
;;
|
|
488
|
-
--only)
|
|
489
|
-
[ "$#" -lt 2 ] && fail 2 "--only needs a part, app or plumbing"
|
|
490
|
-
case "$2" in
|
|
491
|
-
app | plumbing) PARTS=$2 ;;
|
|
492
|
-
*) fail 2 "Unknown part \"$2\", use app or plumbing" ;;
|
|
493
|
-
esac
|
|
494
|
-
shift 2
|
|
495
|
-
;;
|
|
496
|
-
--)
|
|
497
|
-
shift
|
|
498
|
-
if [ "$#" -ne 1 ] || [ -n "$INIT_DIR" ]; then
|
|
499
|
-
fail 2 "init takes one folder"
|
|
500
|
-
fi
|
|
501
|
-
INIT_DIR=$1
|
|
502
|
-
shift
|
|
503
|
-
;;
|
|
504
|
-
-?*)
|
|
505
|
-
fail 2 "Unknown flag $1"
|
|
506
|
-
;;
|
|
507
|
-
*)
|
|
508
|
-
[ -n "$INIT_DIR" ] && fail 2 "init takes one folder"
|
|
509
|
-
INIT_DIR=$1
|
|
510
|
-
shift
|
|
511
|
-
;;
|
|
512
|
-
esac
|
|
513
|
-
done
|
|
514
|
-
|
|
515
|
-
[ -z "$INIT_DIR" ] && fail 2 "init needs a folder"
|
|
516
|
-
|
|
517
|
-
# One shape for the printed paths: no trailing "/", and no leading "-" that
|
|
518
|
-
# a command would read as a flag
|
|
519
|
-
while [ "$INIT_DIR" != "/" ] && [ "${INIT_DIR%/}" != "$INIT_DIR" ]; do
|
|
520
|
-
INIT_DIR=${INIT_DIR%/}
|
|
521
|
-
done
|
|
522
|
-
case "$INIT_DIR" in
|
|
523
|
-
-*) INIT_DIR="./$INIT_DIR" ;;
|
|
524
|
-
esac
|
|
525
|
-
|
|
526
|
-
if ! list_templates | grep -qxF -- "$TEMPLATE"; then
|
|
527
|
-
fail 2 "Unknown template \"$TEMPLATE\". Available: $(list_templates | paste -sd ' ' -)"
|
|
528
|
-
fi
|
|
529
|
-
;;
|
|
530
|
-
serve | build)
|
|
531
|
-
# Take the flags of VBlocks out of the arguments. Webpack gets the rest
|
|
532
|
-
ARG_COUNT=$#
|
|
533
|
-
while [ "$ARG_COUNT" -gt 0 ]; do
|
|
534
|
-
ARG=$1
|
|
535
|
-
shift
|
|
536
|
-
ARG_COUNT=$((ARG_COUNT - 1))
|
|
537
|
-
|
|
538
|
-
case "$ARG" in
|
|
539
|
-
--prefix)
|
|
540
|
-
[ "$ARG_COUNT" -eq 0 ] && fail 2 "--prefix needs a folder"
|
|
541
|
-
PREFIX=$1
|
|
542
|
-
shift
|
|
543
|
-
ARG_COUNT=$((ARG_COUNT - 1))
|
|
544
|
-
;;
|
|
545
|
-
--log-level)
|
|
546
|
-
[ "$ARG_COUNT" -eq 0 ] && fail 2 "--log-level needs a level"
|
|
547
|
-
case "$1" in
|
|
548
|
-
error | warn | info | verbose) LOG_LEVEL=$1 ;;
|
|
549
|
-
*) fail 2 "Unknown level \"$1\", use error, warn, info or verbose" ;;
|
|
550
|
-
esac
|
|
551
|
-
shift
|
|
552
|
-
ARG_COUNT=$((ARG_COUNT - 1))
|
|
553
|
-
;;
|
|
554
|
-
--single-file)
|
|
555
|
-
[ "$COMMAND" = "build" ] || fail 2 "--single-file works only with build"
|
|
556
|
-
# The Webpack config does the work. An ejected config supports the
|
|
557
|
-
# flag only while it reads env.single
|
|
558
|
-
set -- "$@" --env single
|
|
559
|
-
;;
|
|
560
|
-
--analyze)
|
|
561
|
-
[ "$COMMAND" = "serve" ] || fail 2 "--analyze works only with serve"
|
|
562
|
-
# The Webpack config does the work. An ejected config supports the
|
|
563
|
-
# flag only while it reads env.analyze
|
|
564
|
-
set -- "$@" --env analyze
|
|
565
|
-
;;
|
|
566
|
-
*)
|
|
567
|
-
set -- "$@" "$ARG"
|
|
568
|
-
;;
|
|
569
|
-
esac
|
|
570
|
-
done
|
|
571
|
-
|
|
572
|
-
if [ -n "$PREFIX" ] && [ ! -d "$PREFIX" ]; then
|
|
573
|
-
fail 2 "$PREFIX is not a folder"
|
|
574
|
-
fi
|
|
575
|
-
;;
|
|
576
|
-
eject)
|
|
577
|
-
[ "$#" -ne 1 ] && fail 2 "eject needs one config: webpack, postcss or swc"
|
|
578
|
-
|
|
579
|
-
case "$1" in
|
|
580
|
-
webpack) EJECT_CONFIG="webpack.config.js" ;;
|
|
581
|
-
postcss) EJECT_CONFIG="postcss.config.js" ;;
|
|
582
|
-
swc) EJECT_CONFIG=".swcrc" ;;
|
|
583
|
-
*) fail 2 "Unknown config \"$1\", use webpack, postcss or swc" ;;
|
|
584
|
-
esac
|
|
585
|
-
;;
|
|
586
|
-
*)
|
|
587
|
-
fail 2 "Unknown command \"$COMMAND\", use init, serve, build or eject"
|
|
588
|
-
;;
|
|
589
|
-
esac
|
|
590
|
-
|
|
591
|
-
# ╭───
|
|
592
|
-
# │ Main
|
|
593
|
-
# ╰───
|
|
594
|
-
|
|
595
|
-
case "$COMMAND" in
|
|
596
|
-
init)
|
|
597
|
-
if [ -e "$INIT_DIR" ]; then
|
|
598
|
-
if [ ! -d "$INIT_DIR" ] || [ -n "$(ls -A -- "$INIT_DIR")" ]; then
|
|
599
|
-
fail 2 "$INIT_DIR exists and is not an empty folder"
|
|
600
|
-
fi
|
|
601
|
-
fi
|
|
602
|
-
|
|
603
|
-
# An app needs a project folder. The app hooks write to its package.json
|
|
604
|
-
PROJECT_DIR=""
|
|
605
|
-
if [ "$PARTS" = "app" ]; then
|
|
606
|
-
PROJECT_DIR=$(find_project_root "$INIT_DIR") \
|
|
607
|
-
|| fail 3 "No package.json above $INIT_DIR. Write the project first: npx @asd14/vblocks init <directory> --only plumbing"
|
|
608
|
-
fi
|
|
609
|
-
|
|
610
|
-
mkdir -p -- "$INIT_DIR" || fail 3 "Could not add $INIT_DIR"
|
|
611
|
-
INIT_ABS_DIR=$(cd -- "$INIT_DIR" && pwd)
|
|
612
|
-
|
|
613
|
-
case "$PARTS" in
|
|
614
|
-
plumbing)
|
|
615
|
-
PROJECT_DIR=$INIT_ABS_DIR
|
|
616
|
-
APP_DIR=""
|
|
617
|
-
APP_SUBDIR=""
|
|
618
|
-
;;
|
|
619
|
-
app)
|
|
620
|
-
APP_DIR=$INIT_ABS_DIR
|
|
621
|
-
APP_SUBDIR=${APP_DIR#"$PROJECT_DIR"/}
|
|
622
|
-
;;
|
|
623
|
-
*)
|
|
624
|
-
PROJECT_DIR=$INIT_ABS_DIR
|
|
625
|
-
APP_DIR=$INIT_ABS_DIR
|
|
626
|
-
APP_SUBDIR="."
|
|
627
|
-
;;
|
|
628
|
-
esac
|
|
629
|
-
|
|
630
|
-
BEFORE_SNAPSHOT=$(snapshot_folder "$PROJECT_DIR")
|
|
631
|
-
init_project "$INIT_DIR" "$TEMPLATE" "$PARTS"
|
|
632
|
-
AFTER_SNAPSHOT=$(snapshot_folder "$PROJECT_DIR")
|
|
633
|
-
|
|
634
|
-
print_init_summary "$PARTS" "$TEMPLATE" "$PROJECT_DIR" "$APP_DIR"
|
|
635
|
-
print_snapshot_diff "$BEFORE_SNAPSHOT" "$AFTER_SNAPSHOT" "$APP_SUBDIR"
|
|
636
|
-
|
|
637
|
-
case "$PARTS" in
|
|
638
|
-
plumbing)
|
|
639
|
-
printf '\nNext, add an app:\n'
|
|
640
|
-
printf ' npx @asd14/vblocks init %s/<app> --only app\n' "$INIT_DIR"
|
|
641
|
-
;;
|
|
642
|
-
app)
|
|
643
|
-
printf '\nNext:\n'
|
|
644
|
-
if [ "$PROJECT_DIR" != "$PWD" ]; then
|
|
645
|
-
printf ' cd %s\n' "$(shorten_path "$PROJECT_DIR")"
|
|
646
|
-
fi
|
|
647
|
-
printf ' npm install\n'
|
|
648
|
-
printf ' npx vblocks serve --prefix %s\n' "$APP_SUBDIR"
|
|
649
|
-
;;
|
|
650
|
-
*)
|
|
651
|
-
printf '\nNext:\n'
|
|
652
|
-
printf ' cd %s\n' "$INIT_DIR"
|
|
653
|
-
printf ' npm install\n'
|
|
654
|
-
printf ' npx vblocks serve\n'
|
|
655
|
-
;;
|
|
656
|
-
esac
|
|
657
|
-
exit 0
|
|
658
|
-
;;
|
|
659
|
-
serve | build)
|
|
660
|
-
APP_DIR=$(cd -- "${PREFIX:-.}" && pwd)
|
|
661
|
-
WEBPACK_CONFIG=$(pick_file "$APP_DIR/webpack.config.js" "$VB_DIR/src/webpack.config.js")
|
|
662
|
-
|
|
663
|
-
# webpack.config.js reads them
|
|
664
|
-
VB_POSTCSS_CONFIG=$(pick_file "$APP_DIR/postcss.config.js" "$VB_DIR/src/postcss.config.js")
|
|
665
|
-
VB_SWC_CONFIG=$(pick_file "$APP_DIR/.swcrc" "$VB_DIR/src/.swcrc")
|
|
666
|
-
export VB_POSTCSS_CONFIG VB_SWC_CONFIG
|
|
667
|
-
|
|
668
|
-
print_summary "$APP_DIR" "$WEBPACK_CONFIG" "$VB_POSTCSS_CONFIG" "$VB_SWC_CONFIG"
|
|
669
|
-
|
|
670
|
-
# Without a mode, Webpack takes `production`, also for `serve`. A --mode in
|
|
671
|
-
# "$@" comes later, so it overrides this one
|
|
672
|
-
if [ "$COMMAND" = "build" ]; then
|
|
673
|
-
run_webpack build "$APP_DIR" "$WEBPACK_CONFIG" --mode production "$@"
|
|
674
|
-
printf '\n'
|
|
675
|
-
print_build_files "$APP_DIR"
|
|
676
|
-
else
|
|
677
|
-
run_webpack serve "$APP_DIR" "$WEBPACK_CONFIG" --mode development "$@"
|
|
678
|
-
fi
|
|
679
|
-
exit 0
|
|
680
|
-
;;
|
|
681
|
-
eject)
|
|
682
|
-
cat -- "$VB_DIR/src/$EJECT_CONFIG"
|
|
683
|
-
exit 0
|
|
684
|
-
;;
|
|
685
|
-
esac
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { Panel, useReactFlow, useViewport } from "@xyflow/react"
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The zoom of the diagram, as a percentage, and a button that centers the
|
|
5
|
-
* diagram at 100%. Render it inside `<ReactFlow>`, both hooks read the viewport
|
|
6
|
-
* from there
|
|
7
|
-
*/
|
|
8
|
-
const ZoomLevel = () => {
|
|
9
|
-
const { zoom } = useViewport()
|
|
10
|
-
const { fitView } = useReactFlow()
|
|
11
|
-
|
|
12
|
-
return (
|
|
13
|
-
<Panel
|
|
14
|
-
position="top-right"
|
|
15
|
-
className="flex items-center gap-2 rounded border border-stone-200 bg-white px-2 py-1 font-mono text-xs leading-none text-stone-600">
|
|
16
|
-
<span>{Math.round(zoom * 100)}%</span>
|
|
17
|
-
<button
|
|
18
|
-
type="button"
|
|
19
|
-
onClick={() => {
|
|
20
|
-
void fitView({ minZoom: 1, maxZoom: 1, duration: 200 })
|
|
21
|
-
}}
|
|
22
|
-
className="cursor-pointer text-stone-500 hover:text-stone-900">
|
|
23
|
-
Reset
|
|
24
|
-
</button>
|
|
25
|
-
</Panel>
|
|
26
|
-
)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export { ZoomLevel }
|