@asd14/vblocks 2.1.0 → 3.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 +412 -253
- package/bin/vblocks.sh +430 -181
- package/package.json +1 -1
- package/template/{plain → _common/plumbing}/package.json +1 -1
- package/template/_common/post-init-plumbing.sh +14 -0
- package/template/xyflow/app/src/index.css +3 -0
- package/template/xyflow/post-init-app.sh +11 -0
- package/template/xyflow/package.json +0 -26
- package/template/xyflow/src/index.css +0 -1
- /package/template/_common/{src → app/src}/index.css +0 -0
- /package/template/_common/{src → app/src}/index.html +0 -0
- /package/template/_common/{tsconfig.json → app/tsconfig.json} +0 -0
- /package/template/_common/{_gitignore → plumbing/_gitignore} +0 -0
- /package/template/plain/{src → app/src}/index.tsx +0 -0
- /package/template/xyflow/{postcss.config.js → app/postcss.config.js} +0 -0
- /package/template/xyflow/{src → app/src}/index.tsx +0 -0
package/bin/vblocks.sh
CHANGED
|
@@ -1,71 +1,162 @@
|
|
|
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
|
-
|
|
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> [--template <name>] [--only <app|plumbing>]
|
|
10
|
+
# vblocks serve [--mode <mode>] [--prefix <directory>] [<webpack option>...]
|
|
11
|
+
# vblocks build [--mode <mode>] [--prefix <directory>] [--single-file]
|
|
12
|
+
# [<webpack option>...]
|
|
13
|
+
# vblocks eject <webpack|postcss|swc>
|
|
14
|
+
# vblocks -h | --help
|
|
15
|
+
#
|
|
16
|
+
# DESCRIPTION
|
|
17
|
+
# The app folder holds src/index.tsx and src/index.html. A config in it,
|
|
18
|
+
# webpack.config.js, postcss.config.js or .swcrc, replaces the default of
|
|
19
|
+
# VBlocks with the same name.
|
|
20
|
+
#
|
|
21
|
+
# init Write a template into <directory>, which must be missing or
|
|
22
|
+
# empty. A template has two parts: the plumbing, the files of
|
|
23
|
+
# the project folder, and the app. Without --only, write both.
|
|
24
|
+
# After each part, run its post-init hooks in <directory>
|
|
25
|
+
# serve Start the development server
|
|
26
|
+
# build Build the app into dist/ in the app folder
|
|
27
|
+
# eject Print a default config, to save in the app folder
|
|
28
|
+
#
|
|
29
|
+
# serve and build pass every option not listed under OPTIONS to Webpack.
|
|
30
|
+
#
|
|
31
|
+
# OPTIONS
|
|
32
|
+
# --template <name> init: plain or xyflow (default: plain)
|
|
33
|
+
# --only <app|plumbing> init: write only the app or only the plumbing.
|
|
34
|
+
# The app adds its dependencies to the project
|
|
35
|
+
# folder: the nearest folder above <directory>
|
|
36
|
+
# with a package.json
|
|
37
|
+
# --mode <mode> serve, build: the Webpack mode, development or
|
|
38
|
+
# production (default: development for serve,
|
|
39
|
+
# production for build)
|
|
40
|
+
# --prefix <directory> serve, build: the app folder (default: the
|
|
41
|
+
# current folder)
|
|
42
|
+
# --single-file build: one standalone dist/index.html, every
|
|
43
|
+
# asset inlined
|
|
44
|
+
# -h, --help Print this help
|
|
45
|
+
#
|
|
46
|
+
# ENVIRONMENT
|
|
47
|
+
# NO_COLOR Disables color in the summary of serve and build when set and
|
|
48
|
+
# non-empty
|
|
49
|
+
#
|
|
50
|
+
# EXAMPLE
|
|
51
|
+
# npx @asd14/vblocks init my-app
|
|
52
|
+
# npx @asd14/vblocks init demos --only plumbing
|
|
53
|
+
# npx @asd14/vblocks init demos/flow --template xyflow --only app
|
|
54
|
+
# npx vblocks build --prefix flow --single-file
|
|
55
|
+
# npx vblocks eject postcss > postcss.config.js
|
|
56
|
+
#
|
|
57
|
+
# EXIT CODES
|
|
58
|
+
# 0 Done
|
|
59
|
+
# 2 Invalid usage, or Webpack rejected an option or the config
|
|
60
|
+
# 3 Could not run: a missing tool or project, a failed copy, hook or
|
|
61
|
+
# build
|
|
62
|
+
#
|
|
63
|
+
# SEE ALSO
|
|
64
|
+
# webpack, npm
|
|
65
|
+
|
|
66
|
+
# npm links the command to this file, so "$0" names the link. realpath follows
|
|
67
|
+
# it to the package
|
|
68
|
+
VB_DIR=$(dirname "$(dirname "$(realpath "$0")")")
|
|
69
|
+
|
|
70
|
+
# `cd` follows an exported CDPATH and prints the folder it picked, which
|
|
71
|
+
# `$(cd ... && pwd)` would capture
|
|
72
|
+
unset CDPATH
|
|
73
|
+
|
|
74
|
+
# ╭───
|
|
75
|
+
# │ Settings
|
|
76
|
+
# ╰───
|
|
77
|
+
|
|
78
|
+
COMMAND="" # init, serve, build or eject
|
|
79
|
+
INIT_DIR="" # init <directory>
|
|
80
|
+
TEMPLATE="plain" # init --template
|
|
81
|
+
PARTS="plumbing app" # init --only, "plumbing" or "app"
|
|
82
|
+
PREFIX="" # serve/build --prefix, the app folder
|
|
83
|
+
EJECT_CONFIG="" # eject <config>
|
|
84
|
+
|
|
85
|
+
# ╭───
|
|
86
|
+
# │ Primitives
|
|
87
|
+
# ╰───
|
|
88
|
+
|
|
89
|
+
# fail <code> <message>
|
|
90
|
+
#
|
|
91
|
+
# Print <message> to stderr, then exit with <code>.
|
|
92
|
+
fail() {
|
|
93
|
+
printf 'vblocks: %s\n' "$2" >&2
|
|
94
|
+
exit "$1"
|
|
38
95
|
}
|
|
39
96
|
|
|
40
|
-
#
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
97
|
+
# show_help
|
|
98
|
+
#
|
|
99
|
+
# Print the man page: the comment block that starts at the NAME line.
|
|
100
|
+
show_help() {
|
|
101
|
+
sed -n '/^# NAME$/,/^$/p' "$0" | sed 's/^# \{0,1\}//'
|
|
102
|
+
}
|
|
44
103
|
|
|
45
|
-
#
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
104
|
+
# require_cmd <cmd>...
|
|
105
|
+
#
|
|
106
|
+
# EXIT
|
|
107
|
+
# 3 The shell finds no <cmd>
|
|
108
|
+
require_cmd() {
|
|
109
|
+
for require_name in "$@"; do
|
|
110
|
+
command -v "$require_name" >/dev/null 2>&1 \
|
|
111
|
+
|| fail 3 "$require_name is not installed"
|
|
112
|
+
done
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
# has_color
|
|
116
|
+
#
|
|
117
|
+
# RETURNS
|
|
118
|
+
# 0 stderr is a terminal that shows color
|
|
119
|
+
# 1 It is not, or NO_COLOR is set
|
|
120
|
+
has_color() {
|
|
121
|
+
[ -t 2 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-dumb}" != "dumb" ]
|
|
122
|
+
}
|
|
49
123
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
124
|
+
# pick_file <file> <default-file>
|
|
125
|
+
#
|
|
126
|
+
# Print <file> when it exists, otherwise <default-file>.
|
|
127
|
+
pick_file() {
|
|
128
|
+
if [ -f "$1" ]; then
|
|
129
|
+
printf '%s\n' "$1"
|
|
130
|
+
else
|
|
131
|
+
printf '%s\n' "$2"
|
|
53
132
|
fi
|
|
133
|
+
}
|
|
54
134
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
135
|
+
# shorten_home <path>
|
|
136
|
+
#
|
|
137
|
+
# Print <path> with the home folder as "~".
|
|
138
|
+
shorten_home() {
|
|
139
|
+
if [ -n "${HOME:-}" ]; then
|
|
140
|
+
case "$1" in
|
|
141
|
+
"$HOME"/*)
|
|
142
|
+
# shellcheck disable=SC2088 # a literal "~", for display
|
|
143
|
+
printf '~/%s\n' "${1#"$HOME"/}"
|
|
144
|
+
return 0
|
|
145
|
+
;;
|
|
146
|
+
esac
|
|
58
147
|
fi
|
|
59
148
|
|
|
60
|
-
|
|
61
|
-
--env "entryDir=$PROJECT_DIR" \
|
|
62
|
-
--env "robotsPath=$robots_path" \
|
|
63
|
-
--env "faviconPath=$favicon_path" \
|
|
64
|
-
"$@"
|
|
149
|
+
printf '%s\n' "$1"
|
|
65
150
|
}
|
|
66
151
|
|
|
152
|
+
# ╭───
|
|
153
|
+
# │ Domain primitives
|
|
154
|
+
# ╰───
|
|
155
|
+
|
|
156
|
+
# list_templates
|
|
157
|
+
#
|
|
67
158
|
# Print the template names, one per line. A template is a folder in
|
|
68
|
-
# `template/` whose name has no `_` prefix
|
|
159
|
+
# `template/` whose name has no `_` prefix.
|
|
69
160
|
list_templates() {
|
|
70
161
|
for list_dir in "$VB_DIR"/template/*/; do
|
|
71
162
|
[ -d "$list_dir" ] || continue
|
|
@@ -78,156 +169,314 @@ list_templates() {
|
|
|
78
169
|
done
|
|
79
170
|
}
|
|
80
171
|
|
|
81
|
-
#
|
|
82
|
-
#
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
172
|
+
# find_project_root <directory>
|
|
173
|
+
#
|
|
174
|
+
# Print the nearest folder above <directory> that holds a package.json. The
|
|
175
|
+
# folders of <directory> need not exist yet.
|
|
176
|
+
#
|
|
177
|
+
# RETURNS
|
|
178
|
+
# 1 No folder above <directory> holds one
|
|
179
|
+
find_project_root() {
|
|
180
|
+
case "$1" in
|
|
181
|
+
/*) root_dir=$1 ;;
|
|
182
|
+
*) root_dir="$PWD/$1" ;;
|
|
183
|
+
esac
|
|
87
184
|
|
|
88
|
-
while [ "
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
shift 2
|
|
97
|
-
;;
|
|
98
|
-
-*)
|
|
99
|
-
echo "$init_usage" >&2
|
|
100
|
-
exit 1
|
|
101
|
-
;;
|
|
102
|
-
*)
|
|
103
|
-
if [ -n "$init_dir" ]; then
|
|
104
|
-
echo "$init_usage" >&2
|
|
105
|
-
exit 1
|
|
106
|
-
fi
|
|
107
|
-
init_dir=$1
|
|
108
|
-
shift
|
|
109
|
-
;;
|
|
110
|
-
esac
|
|
185
|
+
while [ "$root_dir" != "/" ]; do
|
|
186
|
+
root_dir=$(dirname "$root_dir")
|
|
187
|
+
|
|
188
|
+
if [ -f "$root_dir/package.json" ]; then
|
|
189
|
+
# Drop "." and ".." from the path, the caller compares it with `pwd`
|
|
190
|
+
(cd -- "$root_dir" && pwd)
|
|
191
|
+
return 0
|
|
192
|
+
fi
|
|
111
193
|
done
|
|
112
194
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
exit 1
|
|
116
|
-
fi
|
|
195
|
+
return 1
|
|
196
|
+
}
|
|
117
197
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
198
|
+
# init_project <directory> <template> <parts>
|
|
199
|
+
#
|
|
200
|
+
# Copy the <parts> of <template> into <directory>, then run the hooks of each
|
|
201
|
+
# part in it. <parts> is "plumbing", "app" or "plumbing app". A template's file
|
|
202
|
+
# replaces the `_common` file of the same path.
|
|
203
|
+
#
|
|
204
|
+
# EXIT
|
|
205
|
+
# 3 A copy or a hook failed
|
|
206
|
+
init_project() {
|
|
207
|
+
init_dir=$1
|
|
208
|
+
init_template=$2
|
|
124
209
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
fi
|
|
210
|
+
for init_part in $3; do
|
|
211
|
+
for init_source in "$VB_DIR/template/_common" "$VB_DIR/template/$init_template"; do
|
|
212
|
+
[ -d "$init_source/$init_part" ] || continue
|
|
129
213
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
214
|
+
cp -R -- "$init_source/$init_part/." "$init_dir" \
|
|
215
|
+
|| fail 3 "Could not copy $init_source/$init_part"
|
|
216
|
+
done
|
|
133
217
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
218
|
+
for init_source in "$VB_DIR/template/_common" "$VB_DIR/template/$init_template"; do
|
|
219
|
+
init_hook="$init_source/post-init-$init_part.sh"
|
|
220
|
+
[ -f "$init_hook" ] || continue
|
|
137
221
|
|
|
138
|
-
|
|
139
|
-
|
|
222
|
+
# A hook reads VB_VERSION, and nothing else of this script
|
|
223
|
+
(cd -- "$init_dir" && VB_VERSION="$VB_VERSION" sh "$init_hook") \
|
|
224
|
+
|| fail 3 "The hook $init_hook failed"
|
|
225
|
+
done
|
|
226
|
+
done
|
|
227
|
+
}
|
|
140
228
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
229
|
+
# show_config_file <config-file> <app-dir>
|
|
230
|
+
#
|
|
231
|
+
# Print <config-file> with its folder as $APP_DIR, or as $VBLOCKS_DIR in
|
|
232
|
+
# gray for a default config.
|
|
233
|
+
show_config_file() {
|
|
234
|
+
case "$1" in
|
|
235
|
+
"$2"/*) printf '%s\n' "\$APP_DIR/${1#"$2"/}" ;;
|
|
236
|
+
"$VB_DIR"/*) printf '%s\n' "${GRAY}\$VBLOCKS_DIR/${1#"$VB_DIR"/}${RESET}" ;;
|
|
237
|
+
*) printf '%s\n' "$1" ;;
|
|
238
|
+
esac
|
|
239
|
+
}
|
|
146
240
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
241
|
+
# print_summary <app-dir> <webpack-config> <postcss-config> <swc-config>
|
|
242
|
+
#
|
|
243
|
+
# Print the version, the app folder and the configs Webpack runs with, to
|
|
244
|
+
# stderr.
|
|
245
|
+
print_summary() {
|
|
246
|
+
{
|
|
247
|
+
printf '╭───────────┤ %s\n' "${GREEN} VBlocks v${VB_VERSION}${RESET}"
|
|
248
|
+
printf '│ %s\n' "${BOLD} POSIX + Webpack, an unlikely story!${RESET}"
|
|
249
|
+
printf '│\n'
|
|
250
|
+
printf '│ %s\n' "\$APP_DIR: $(shorten_home "$1")"
|
|
251
|
+
printf '│ %s\n' "\$VBLOCKS_DIR: ${GRAY}$(shorten_home "$VB_DIR")${RESET}"
|
|
252
|
+
printf '│\n'
|
|
253
|
+
printf '│ %s\n' " Webpack: $(show_config_file "$2" "$1")"
|
|
254
|
+
printf '│ %s\n' " PostCSS: $(show_config_file "$3" "$1")"
|
|
255
|
+
printf '│ %s\n' " SWC: $(show_config_file "$4" "$1")"
|
|
256
|
+
printf '╰──┤\n\n'
|
|
257
|
+
} >&2
|
|
151
258
|
}
|
|
152
259
|
|
|
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
|
-
|
|
260
|
+
# run_webpack <action> <app-dir> <webpack-config> [<webpack option>...]
|
|
261
|
+
#
|
|
262
|
+
# Run `webpack <action>` for the app in <app-dir>.
|
|
263
|
+
#
|
|
264
|
+
# EXIT
|
|
265
|
+
# 2 Webpack rejected an option or the config
|
|
266
|
+
# 3 Webpack failed
|
|
267
|
+
run_webpack() {
|
|
268
|
+
webpack_action=$1
|
|
269
|
+
webpack_app_dir=$2
|
|
270
|
+
webpack_config=$3
|
|
271
|
+
shift 3
|
|
272
|
+
|
|
273
|
+
webpack_robots_file=$(pick_file \
|
|
274
|
+
"$webpack_app_dir/src/public/robots.txt" "$VB_DIR/src/public/robots.txt")
|
|
275
|
+
webpack_favicon_file=$(pick_file \
|
|
276
|
+
"$webpack_app_dir/src/public/favicon.svg" "$VB_DIR/src/public/favicon.svg")
|
|
277
|
+
|
|
278
|
+
webpack_status=0
|
|
279
|
+
npx webpack "$webpack_action" --config "$webpack_config" \
|
|
280
|
+
--env "entryDir=$webpack_app_dir" \
|
|
281
|
+
--env "robotsPath=$webpack_robots_file" \
|
|
282
|
+
--env "faviconPath=$webpack_favicon_file" \
|
|
283
|
+
"$@" || webpack_status=$?
|
|
284
|
+
|
|
285
|
+
# webpack-cli exits 1 when the build has errors, 2 when an option or the
|
|
286
|
+
# config is wrong
|
|
287
|
+
case "$webpack_status" in
|
|
288
|
+
0) return 0 ;;
|
|
289
|
+
2) fail 2 "Webpack rejected an option or the config" ;;
|
|
290
|
+
*) fail 3 "Webpack failed" ;;
|
|
291
|
+
esac
|
|
185
292
|
}
|
|
186
293
|
|
|
187
|
-
#
|
|
188
|
-
# │
|
|
189
|
-
#
|
|
294
|
+
# ╭───
|
|
295
|
+
# │ Bootstrap
|
|
296
|
+
# ╰───
|
|
190
297
|
|
|
191
|
-
|
|
192
|
-
|
|
298
|
+
case "${1:-}" in
|
|
299
|
+
-h | --help)
|
|
300
|
+
show_help
|
|
301
|
+
exit 0
|
|
302
|
+
;;
|
|
303
|
+
"")
|
|
304
|
+
show_help
|
|
305
|
+
exit 2
|
|
306
|
+
;;
|
|
307
|
+
esac
|
|
308
|
+
|
|
309
|
+
require_cmd node npx
|
|
310
|
+
|
|
311
|
+
VB_VERSION=$(node -p 'require(process.argv[1]).version' "$VB_DIR/package.json")
|
|
312
|
+
|
|
313
|
+
# Color codes for stderr, empty when it shows no color
|
|
314
|
+
BOLD="" GREEN="" GRAY="" RESET=""
|
|
315
|
+
if has_color; then
|
|
316
|
+
BOLD=$(printf '\033[1m')
|
|
317
|
+
GREEN=$(printf '\033[32m')
|
|
318
|
+
GRAY=$(printf '\033[90m')
|
|
319
|
+
RESET=$(printf '\033[0m')
|
|
320
|
+
fi
|
|
321
|
+
|
|
322
|
+
# ╭───
|
|
323
|
+
# │ Flags and positionals
|
|
324
|
+
# ╰───
|
|
193
325
|
|
|
194
|
-
|
|
326
|
+
COMMAND=$1
|
|
327
|
+
shift
|
|
328
|
+
|
|
329
|
+
case "$COMMAND" in
|
|
195
330
|
init)
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
331
|
+
while [ "$#" -gt 0 ]; do
|
|
332
|
+
case "$1" in
|
|
333
|
+
--template)
|
|
334
|
+
[ "$#" -lt 2 ] && fail 2 "--template needs a name"
|
|
335
|
+
TEMPLATE=$2
|
|
336
|
+
shift 2
|
|
337
|
+
;;
|
|
338
|
+
--only)
|
|
339
|
+
[ "$#" -lt 2 ] && fail 2 "--only needs a part, app or plumbing"
|
|
340
|
+
case "$2" in
|
|
341
|
+
app | plumbing) PARTS=$2 ;;
|
|
342
|
+
*) fail 2 "Unknown part \"$2\", use app or plumbing" ;;
|
|
343
|
+
esac
|
|
344
|
+
shift 2
|
|
345
|
+
;;
|
|
346
|
+
--)
|
|
347
|
+
shift
|
|
348
|
+
if [ "$#" -ne 1 ] || [ -n "$INIT_DIR" ]; then
|
|
349
|
+
fail 2 "init takes one directory"
|
|
350
|
+
fi
|
|
351
|
+
INIT_DIR=$1
|
|
352
|
+
shift
|
|
353
|
+
;;
|
|
354
|
+
-?*)
|
|
355
|
+
fail 2 "Unknown flag $1"
|
|
356
|
+
;;
|
|
357
|
+
*)
|
|
358
|
+
[ -n "$INIT_DIR" ] && fail 2 "init takes one directory"
|
|
359
|
+
INIT_DIR=$1
|
|
360
|
+
shift
|
|
361
|
+
;;
|
|
362
|
+
esac
|
|
363
|
+
done
|
|
364
|
+
|
|
365
|
+
[ -z "$INIT_DIR" ] && fail 2 "init needs a directory"
|
|
366
|
+
|
|
367
|
+
if ! list_templates | grep -qxF -- "$TEMPLATE"; then
|
|
368
|
+
fail 2 "Unknown template \"$TEMPLATE\". Available: $(list_templates | paste -sd ' ' -)"
|
|
369
|
+
fi
|
|
204
370
|
;;
|
|
205
|
-
serve)
|
|
206
|
-
|
|
207
|
-
|
|
371
|
+
serve | build)
|
|
372
|
+
# Take the flags of VBlocks out of the arguments, Webpack gets the rest
|
|
373
|
+
ARG_COUNT=$#
|
|
374
|
+
while [ "$ARG_COUNT" -gt 0 ]; do
|
|
375
|
+
ARG=$1
|
|
376
|
+
shift
|
|
377
|
+
ARG_COUNT=$((ARG_COUNT - 1))
|
|
378
|
+
|
|
379
|
+
case "$ARG" in
|
|
380
|
+
--prefix)
|
|
381
|
+
[ "$ARG_COUNT" -eq 0 ] && fail 2 "--prefix needs a directory"
|
|
382
|
+
PREFIX=$1
|
|
383
|
+
shift
|
|
384
|
+
ARG_COUNT=$((ARG_COUNT - 1))
|
|
385
|
+
;;
|
|
386
|
+
--single-file)
|
|
387
|
+
[ "$COMMAND" = "build" ] || fail 2 "--single-file works only with build"
|
|
388
|
+
# webpack.config.js reads env.single, an ejected one too
|
|
389
|
+
set -- "$@" --env single
|
|
390
|
+
;;
|
|
391
|
+
*)
|
|
392
|
+
set -- "$@" "$ARG"
|
|
393
|
+
;;
|
|
394
|
+
esac
|
|
395
|
+
done
|
|
396
|
+
|
|
397
|
+
if [ -n "$PREFIX" ] && [ ! -d "$PREFIX" ]; then
|
|
398
|
+
fail 2 "$PREFIX is not a directory"
|
|
399
|
+
fi
|
|
208
400
|
;;
|
|
209
401
|
eject)
|
|
210
|
-
|
|
211
|
-
file=$1
|
|
402
|
+
[ "$#" -ne 1 ] && fail 2 "eject needs one config: webpack, postcss or swc"
|
|
212
403
|
|
|
213
|
-
case "$
|
|
214
|
-
webpack)
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
postcss
|
|
218
|
-
|
|
404
|
+
case "$1" in
|
|
405
|
+
webpack) EJECT_CONFIG="webpack.config.js" ;;
|
|
406
|
+
postcss) EJECT_CONFIG="postcss.config.js" ;;
|
|
407
|
+
swc) EJECT_CONFIG=".swcrc" ;;
|
|
408
|
+
*) fail 2 "Unknown config \"$1\", use webpack, postcss or swc" ;;
|
|
409
|
+
esac
|
|
410
|
+
;;
|
|
411
|
+
*)
|
|
412
|
+
fail 2 "Unknown command \"$COMMAND\", use init, serve, build or eject"
|
|
413
|
+
;;
|
|
414
|
+
esac
|
|
415
|
+
|
|
416
|
+
# ╭───
|
|
417
|
+
# │ Main
|
|
418
|
+
# ╰───
|
|
419
|
+
|
|
420
|
+
case "$COMMAND" in
|
|
421
|
+
init)
|
|
422
|
+
if [ -e "$INIT_DIR" ]; then
|
|
423
|
+
if [ ! -d "$INIT_DIR" ] || [ -n "$(ls -A -- "$INIT_DIR")" ]; then
|
|
424
|
+
fail 2 "$INIT_DIR exists and is not an empty folder"
|
|
425
|
+
fi
|
|
426
|
+
fi
|
|
427
|
+
|
|
428
|
+
# An app joins a project, the app hooks write its package.json
|
|
429
|
+
PROJECT_ROOT=""
|
|
430
|
+
if [ "$PARTS" = "app" ]; then
|
|
431
|
+
PROJECT_ROOT=$(find_project_root "$INIT_DIR") \
|
|
432
|
+
|| fail 3 "No package.json above $INIT_DIR. Write the project first: npx @asd14/vblocks init <directory> --only plumbing"
|
|
433
|
+
fi
|
|
434
|
+
|
|
435
|
+
mkdir -p -- "$INIT_DIR" || fail 3 "Could not create $INIT_DIR"
|
|
436
|
+
init_project "$INIT_DIR" "$TEMPLATE" "$PARTS"
|
|
437
|
+
|
|
438
|
+
case "$PARTS" in
|
|
439
|
+
plumbing)
|
|
440
|
+
printf 'Wrote %s. Add an app:\n\n' "$INIT_DIR" >&2
|
|
441
|
+
printf ' npx @asd14/vblocks init %s/<app> --only app\n' "$INIT_DIR" >&2
|
|
219
442
|
;;
|
|
220
|
-
|
|
221
|
-
|
|
443
|
+
app)
|
|
444
|
+
APP_DIR=$(cd -- "$INIT_DIR" && pwd)
|
|
445
|
+
printf 'Wrote %s. Next, in %s:\n\n' "$INIT_DIR" "$PROJECT_ROOT" >&2
|
|
446
|
+
printf ' npm install\n' >&2
|
|
447
|
+
printf ' npm start -- --prefix %s\n' "${APP_DIR#"$PROJECT_ROOT"/}" >&2
|
|
222
448
|
;;
|
|
223
449
|
*)
|
|
224
|
-
|
|
225
|
-
|
|
450
|
+
printf 'Wrote %s. Next:\n\n' "$INIT_DIR" >&2
|
|
451
|
+
printf ' cd %s\n' "$INIT_DIR" >&2
|
|
452
|
+
printf ' npm install\n' >&2
|
|
453
|
+
printf ' npm start\n' >&2
|
|
226
454
|
;;
|
|
227
455
|
esac
|
|
456
|
+
exit 0
|
|
228
457
|
;;
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
458
|
+
serve | build)
|
|
459
|
+
APP_DIR=$(cd -- "${PREFIX:-.}" && pwd)
|
|
460
|
+
WEBPACK_CONFIG=$(pick_file "$APP_DIR/webpack.config.js" "$VB_DIR/src/webpack.config.js")
|
|
461
|
+
|
|
462
|
+
# webpack.config.js reads them
|
|
463
|
+
VB_POSTCSS_CONFIG=$(pick_file "$APP_DIR/postcss.config.js" "$VB_DIR/src/postcss.config.js")
|
|
464
|
+
VB_SWC_CONFIG=$(pick_file "$APP_DIR/.swcrc" "$VB_DIR/src/.swcrc")
|
|
465
|
+
export VB_POSTCSS_CONFIG VB_SWC_CONFIG
|
|
466
|
+
|
|
467
|
+
print_summary "$APP_DIR" "$WEBPACK_CONFIG" "$VB_POSTCSS_CONFIG" "$VB_SWC_CONFIG"
|
|
468
|
+
|
|
469
|
+
# Without a mode, Webpack takes `production`, also for `serve`. A --mode in
|
|
470
|
+
# "$@" comes later, so it wins
|
|
471
|
+
if [ "$COMMAND" = "build" ]; then
|
|
472
|
+
run_webpack build "$APP_DIR" "$WEBPACK_CONFIG" --mode production "$@"
|
|
473
|
+
else
|
|
474
|
+
run_webpack serve "$APP_DIR" "$WEBPACK_CONFIG" --mode development "$@"
|
|
475
|
+
fi
|
|
476
|
+
exit 0
|
|
477
|
+
;;
|
|
478
|
+
eject)
|
|
479
|
+
cat -- "$VB_DIR/src/$EJECT_CONFIG"
|
|
480
|
+
exit 0
|
|
232
481
|
;;
|
|
233
482
|
esac
|