@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/bin/vblocks.sh CHANGED
@@ -1,71 +1,164 @@
1
- #!/usr/bin/env sh
2
-
3
- # =============================================================================
4
- # VBLOCKS - Webpack with opinionated configs
5
- # =============================================================================
6
- # This script helps JavaScript developers run common Webpack tasks without
7
- # needing to remember complex command line arguments.
8
- #
9
- # Available Commands:
10
- # ---
11
- # init: Creates a new project from the template
12
- # build: Creates a production-ready build of your project
13
- # serve: Starts a development server with live reload
14
- # eject: Prints a default config, save it in your project to customize it
15
- #
16
- # Usage Examples:
17
- # ---
18
- # npx vblocks init my-app # Create a project in ./my-app
19
- # npx vblocks init my-flow --template xyflow # Create a project with a specific template
20
- # npx vblocks build # Build for production
21
- # npx vblocks serve # Start dev server
22
- # npx vblocks eject webpack > webpack.config.js # Customize a config
23
- # =============================================================================
24
-
25
- # Directory where this script is located and
26
- # default configuration file paths (from this package).
27
- export VB_DIR="$(dirname "$(dirname "$(realpath "$0")")")"
28
-
29
- # Project directory (where you run the command from) and
30
- # project-specific config file paths.
31
- export PROJECT_DIR="$(pwd)"
32
-
33
- # Choose between project-specific and default configs. Project files take precedence.
34
- select_config() {
35
- project_path="$PROJECT_DIR/$1"
36
- default_path=$(realpath "$VB_DIR/src/$1")
37
- [ -f "$project_path" ] && echo "$project_path" || echo "$default_path"
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
- # Determine config files location.
41
- export VB_WEBPACK_CONFIG="$(select_config "webpack.config.js")"
42
- export VB_POSTCSS_CONFIG="$(select_config "postcss.config.js")"
43
- export VB_SWC_CONFIG="$(select_config ".swcrc")"
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
- # Wrap webpack command with custom configs
46
- run_webpack() {
47
- action=$1
48
- shift 1
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
- robots_path="$PROJECT_DIR/src/public/robots.txt"
51
- if [ ! -f "$robots_path" ]; then
52
- robots_path="$VB_DIR/src/public/robots.txt"
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
- favicon_path="$PROJECT_DIR/src/public/favicon.svg"
56
- if [ ! -f "$favicon_path" ]; then
57
- favicon_path="$VB_DIR/src/public/favicon.svg"
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
- npx webpack "$action" --config "$VB_WEBPACK_CONFIG" \
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
- # Copy `template/_common`, then one template over it, into a directory that
82
- # is missing or empty
83
- init_project() {
84
- init_usage="Usage: vblocks init <directory> [--template <name>]"
85
- init_dir=""
86
- init_template="plain"
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 [ "$#" -gt 0 ]; do
89
- case "$1" in
90
- --template)
91
- if [ "$#" -lt 2 ]; then
92
- echo "$init_usage" >&2
93
- exit 1
94
- fi
95
- init_template=$2
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
- if [ -z "$init_dir" ]; then
114
- echo "$init_usage" >&2
115
- exit 1
116
- fi
197
+ return 1
198
+ }
117
199
 
118
- init_templates=$(list_templates)
119
- if ! printf '%s\n' "$init_templates" | grep -qxF -- "$init_template"; then
120
- printf 'Unknown template "%s". Available: %s\n' "$init_template" \
121
- "$(printf '%s\n' "$init_templates" | paste -sd, - | sed 's/,/, /g')" >&2
122
- exit 1
123
- fi
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
- if [ -e "$init_dir" ] && [ -n "$(ls -A -- "$init_dir")" ]; then
126
- printf '%s exists and is not empty\n' "$init_dir" >&2
127
- exit 1
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
- mkdir -p -- "$init_dir" || exit 1
131
- cp -R -- "$VB_DIR/template/_common/." "$init_dir" || exit 1
132
- cp -R -- "$VB_DIR/template/$init_template/." "$init_dir" || exit 1
216
+ cp -R -- "$init_source/$init_part/." "$init_dir" \
217
+ || fail 3 "Could not copy $init_source/$init_part"
218
+ done
133
219
 
134
- # npm leaves every `.gitignore` out of the package, so the template ships
135
- # `_gitignore`
136
- mv -- "$init_dir/_gitignore" "$init_dir/.gitignore" || exit 1
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
- init_name=$(basename "$(realpath "$init_dir")")
139
- init_version=$(node -p "require('$VB_DIR/package.json').version")
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
- cd -- "$init_dir" \
143
- && npm pkg set "name=$init_name" \
144
- "devDependencies.@asd14/vblocks=^$init_version"
145
- ) || exit 1
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
- printf 'Created %s. Next:\n\n' "$init_name"
148
- printf ' cd %s\n' "$init_dir"
149
- printf ' npm install\n'
150
- printf ' npm start\n'
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
- print_hero() {
154
- version=$(node -p "require('$VB_DIR/package.json').version")
155
- bold='\033[1m'
156
- green='\033[32m'
157
- blue='\033[34m'
158
- gray='\033[90m'
159
- reset='\033[0m'
160
-
161
- bold() { echo "${bold}$1${reset}"; }
162
- blue() { echo "${blue}$1${reset}"; }
163
- green() { echo "${green}$1${reset}"; }
164
- gray() { echo "${gray}$1${reset}"; }
165
- replace_home() { echo "$1" | sed "s|$HOME|~|g"; }
166
- replace_config_base() { echo "$1" | sed "s|$PROJECT_DIR|\$PROJECT_DIR|g" | sed "s|$VB_DIR|\$VBLOCKS_DIR|g"; }
167
- format_config_path() {
168
- if echo "$1" | grep -q "^$VB_DIR"; then
169
- gray "$(replace_config_base "$1")"
170
- else
171
- replace_config_base "$1"
172
- fi
173
- }
174
-
175
- printf "╭───────────┤ %b\n" "$(green " VBlocks v${version}")"
176
- printf "│ %b\n" "$(bold "󱆃 POSIX +  Webpack, an unlikely story!")"
177
- printf "│\n"
178
- printf "│ %b\n" "\$PROJECT_DIR: $(replace_home "$PROJECT_DIR")"
179
- printf "│ %b\n" "\$VBLOCKS_DIR: $(gray "$(replace_home "$VB_DIR")")"
180
- printf "│\n"
181
- printf "│ %b\n" " Webpack: $(format_config_path "$VB_WEBPACK_CONFIG")"
182
- printf "│ %b\n" " PostCSS: $(format_config_path "$VB_POSTCSS_CONFIG")"
183
- printf "│ %b\n" "󱡮 SWC: $(format_config_path "$VB_SWC_CONFIG")"
184
- printf "╰──┤\n\n"
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
- # │ Main
189
- # ╰────────
296
+ # ╭───
297
+ # │ Bootstrap
298
+ # ╰───
190
299
 
191
- # stdout carries the config `eject` prints, keep the hero out of it
192
- print_hero >&2
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
- case "$1" in
328
+ COMMAND=$1
329
+ shift
330
+
331
+ case "$COMMAND" in
195
332
  init)
196
- shift
197
- init_project "$@"
198
- ;;
199
- build)
200
- shift
201
- # Without a mode Webpack falls back to `production`, also for `serve`.
202
- # A `--mode` in "$@" comes later, so it wins.
203
- run_webpack build --mode production "$@"
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
- shift
207
- run_webpack serve --mode development "$@"
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
- shift
211
- file=$1
409
+ [ "$#" -ne 1 ] && fail 2 "eject needs one config: webpack, postcss or swc"
212
410
 
213
- case "$file" in
214
- webpack)
215
- cat "$VB_DIR/src/webpack.config.js"
216
- ;;
217
- postcss)
218
- cat "$VB_DIR/src/postcss.config.js"
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
- swc)
221
- cat "$VB_DIR/src/.swcrc"
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
- echo "Unknown config file to eject. Available: webpack, postcss and swc" >&2
225
- exit 1
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
- echo "Usage: $0 {init|build|serve|eject}" >&2
231
- exit 1
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