@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/bin/vblocks.sh CHANGED
@@ -1,71 +1,162 @@
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>] [<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
- # 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")"
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
- # Wrap webpack command with custom configs
46
- run_webpack() {
47
- action=$1
48
- shift 1
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
- robots_path="$PROJECT_DIR/src/public/robots.txt"
51
- if [ ! -f "$robots_path" ]; then
52
- robots_path="$VB_DIR/src/public/robots.txt"
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
- favicon_path="$PROJECT_DIR/src/public/favicon.svg"
56
- if [ ! -f "$favicon_path" ]; then
57
- favicon_path="$VB_DIR/src/public/favicon.svg"
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
- npx webpack "$action" --config "$VB_WEBPACK_CONFIG" \
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
- # 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"
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 [ "$#" -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
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
- if [ -z "$init_dir" ]; then
114
- echo "$init_usage" >&2
115
- exit 1
116
- fi
195
+ return 1
196
+ }
117
197
 
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
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
- 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
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
- 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
214
+ cp -R -- "$init_source/$init_part/." "$init_dir" \
215
+ || fail 3 "Could not copy $init_source/$init_part"
216
+ done
133
217
 
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
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
- init_name=$(basename "$(realpath "$init_dir")")
139
- init_version=$(node -p "require('$VB_DIR/package.json').version")
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
- cd -- "$init_dir" \
143
- && npm pkg set "name=$init_name" \
144
- "devDependencies.@asd14/vblocks=^$init_version"
145
- ) || exit 1
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
- 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'
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
- 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"
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
- # │ Main
189
- # ╰────────
294
+ # ╭───
295
+ # │ Bootstrap
296
+ # ╰───
190
297
 
191
- # stdout carries the config `eject` prints, keep the hero out of it
192
- print_hero >&2
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
- case "$1" in
326
+ COMMAND=$1
327
+ shift
328
+
329
+ case "$COMMAND" in
195
330
  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 "$@"
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
- shift
207
- run_webpack serve --mode development "$@"
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
- shift
211
- file=$1
402
+ [ "$#" -ne 1 ] && fail 2 "eject needs one config: webpack, postcss or swc"
212
403
 
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"
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
- swc)
221
- cat "$VB_DIR/src/.swcrc"
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
- echo "Unknown config file to eject. Available: webpack, postcss and swc" >&2
225
- exit 1
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
- echo "Usage: $0 {init|build|serve|eject}" >&2
231
- exit 1
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
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@asd14/vblocks",
4
- "version": "2.1.0",
4
+ "version": "3.1.0",
5
5
  "description": "󱆃 POSIX +  Webpack, an unlikely story!",
6
6
  "license": "MIT",
7
7
  "sideEffects": false,