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