@asd14/vblocks 5.4.0 → 6.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.
@@ -0,0 +1,337 @@
1
+ #!/bin/sh
2
+
3
+ # NAME
4
+ # vblocks init - Write a template into a folder
5
+ #
6
+ # SYNOPSIS
7
+ # vblocks init <directory>
8
+ # [--template <plain|xyflow|xyflow-tabs>]
9
+ # [--only <app|plumbing>]
10
+ #
11
+ # DESCRIPTION
12
+ # A template has two parts: the plumbing, which holds the files of the
13
+ # project folder, and the app, which holds the files of the app folder.
14
+ # After each part, run its post-init hooks in <directory>.
15
+ #
16
+ # Print the project folder and each file init adds, updates or drops, then
17
+ # the next steps.
18
+ #
19
+ # ARGUMENTS
20
+ # directory The folder to write into, missing or empty
21
+ #
22
+ # OPTIONS
23
+ # --template <name> The template, plain, xyflow or xyflow-tabs (default:
24
+ # plain)
25
+ # --only <part> Write only the app or only the plumbing (default:
26
+ # both). With app, init adds the template's
27
+ # dependencies to the package.json of the project
28
+ # folder, the parent of <directory>. That package.json
29
+ # must exist
30
+ # -h, --help Print this help
31
+ #
32
+ # ENVIRONMENT
33
+ # NO_COLOR When non-empty, print without color or the header
34
+ #
35
+ # EXAMPLE
36
+ # npx @asd14/vblocks init my-app
37
+ # npx @asd14/vblocks init demos --only plumbing
38
+ # npx @asd14/vblocks init demos/flow --template xyflow --only app
39
+ #
40
+ # EXIT CODES
41
+ # 0 Done
42
+ # 2 Invalid usage, an unknown template, or a <directory> that is not empty
43
+ # or not a folder
44
+ # 3 Could not run: a missing tool, no package.json in the project folder,
45
+ # a folder init could not add, or a failed copy or hook
46
+ #
47
+ # SEE ALSO
48
+ # vblocks, vblocks serve, node, npm
49
+
50
+ # vblocks runs this file. Never call it directly.
51
+
52
+ set -eu
53
+
54
+ _SCRIPT_DIR=$(dirname "$0")
55
+ # shellcheck source=vblocks.sh
56
+ . "$_SCRIPT_DIR/vblocks.sh"
57
+
58
+ # ╭───
59
+ # │ Settings
60
+ # ╰───
61
+
62
+ _INIT_DIR="" # init <directory>
63
+ _TEMPLATE="plain" # init --template
64
+ _PARTS="plumbing app" # init --only: "plumbing", "app" or "plumbing app"
65
+
66
+ # ╭───
67
+ # │ Primitives
68
+ # ╰───
69
+
70
+ # _shorten_path <absolute-path>
71
+ #
72
+ # Print <absolute-path> relative to the current folder when the path is inside
73
+ # that folder, otherwise with the home folder as "~".
74
+ _shorten_path() {
75
+ case "$1" in
76
+ "$PWD") printf '.\n' ;;
77
+ "$PWD"/*) printf '%s\n' "${1#"$PWD"/}" ;;
78
+ *) _shorten_home "$1" ;;
79
+ esac
80
+ }
81
+
82
+ # _quote_path <path>
83
+ #
84
+ # Print <path> in single quotes, so a shell reads it back as one word. A
85
+ # leading "~/" stays out of the quotes, so the shell still expands it.
86
+ _quote_path() {
87
+ # shellcheck disable=SC2088 # a literal "~", for display
88
+ case "$1" in
89
+ "~/"*) qp_home="~/" qp_path=${1#"~/"} ;;
90
+ *) qp_home="" qp_path=$1 ;;
91
+ esac
92
+
93
+ # End the quotes, add an escaped quote, open them again: ' becomes '\''
94
+ printf "%s'%s'\n" "$qp_home" "$(printf '%s' "$qp_path" | sed "s/'/'\\\\''/g")"
95
+ }
96
+
97
+ # ╭───
98
+ # │ Domain primitives
99
+ # ╰───
100
+
101
+ # _list_templates
102
+ #
103
+ # Print the template names, one per line. A template is a folder in
104
+ # `template/` whose name has no `_` prefix.
105
+ _list_templates() {
106
+ for lt_dir in "$_VB_DIR"/template/*/; do
107
+ [ -d "$lt_dir" ] || continue
108
+ lt_name=$(basename "$lt_dir")
109
+
110
+ case "$lt_name" in
111
+ _*) ;;
112
+ *) printf '%s\n' "$lt_name" ;;
113
+ esac
114
+ done
115
+ }
116
+
117
+ # _init_project <directory> <template> <parts>
118
+ #
119
+ # For each of <parts>, copy it from <template> into <directory>, then run its
120
+ # hooks there. <parts> is "plumbing", "app" or "plumbing app". A template's file
121
+ # replaces the `_common` file of the same path.
122
+ #
123
+ # EXIT
124
+ # 3 A copy or a hook failed
125
+ _init_project() {
126
+ ip_dir=$1
127
+ ip_template=$2
128
+
129
+ for ip_part in $3; do
130
+ for ip_source in "$_VB_DIR/template/_common" "$_VB_DIR/template/$ip_template"; do
131
+ [ -d "$ip_source/$ip_part" ] || continue
132
+
133
+ cp -R -- "$ip_source/$ip_part/." "$ip_dir" \
134
+ || _fail 3 -v file "$ip_source/$ip_part" "Could not copy"
135
+ done
136
+
137
+ for ip_source in "$_VB_DIR/template/_common" "$_VB_DIR/template/$ip_template"; do
138
+ ip_hook="$ip_source/post-init-$ip_part.sh"
139
+ [ -f "$ip_hook" ] || continue
140
+
141
+ # A hook reads the exported VB_VERSION, and nothing else of this script
142
+ (cd -- "$ip_dir" && sh "$ip_hook") \
143
+ || _fail 3 -v hook "$ip_hook" "The hook failed"
144
+ done
145
+ done
146
+ }
147
+
148
+ # _snapshot_folder <dir>
149
+ #
150
+ # Print one line per file in <dir>: its checksum and size, a tab, its path
151
+ # relative to <dir>. Skip node_modules/, dist/ and .git/. init never writes there. They
152
+ # can hold thousands of files.
153
+ _snapshot_folder() {
154
+ (cd -- "$1" && find . \( -name node_modules -o -name dist -o -name .git \) \
155
+ -prune -o -type f -print) \
156
+ | sed 's|^\./||' \
157
+ | LC_ALL=C sort \
158
+ | while IFS= read -r sf_file; do
159
+ printf '%s\t%s\n' "$(cksum <"$1/$sf_file")" "$sf_file"
160
+ done
161
+ }
162
+
163
+ # _print_snapshot_diff <before-snapshot> <after-snapshot>
164
+ #
165
+ # Print "add", "update" or "drop" for each file that differs between two
166
+ # snapshots of the project folder, with its path relative to that folder.
167
+ _print_snapshot_diff() {
168
+ # Every snapshot line has a tab. The AFTER line has none, so it marks where
169
+ # the second snapshot starts
170
+ {
171
+ printf '%s\n' "$1"
172
+ printf 'AFTER\n'
173
+ printf '%s\n' "$2"
174
+ } | awk -F '\t' \
175
+ -v add="${_GREEN}add${_RESET}" \
176
+ -v update="${_YELLOW}update${_RESET}" \
177
+ -v drop="${_RED}drop${_RESET}" '
178
+ # The color codes take no column, so pad by the plain verb
179
+ function verb(styled, plain) {
180
+ return styled substr(" ", 1, 8 - length(plain))
181
+ }
182
+
183
+ NF < 2 { is_after = 1; next }
184
+ !is_after { before[$2] = $1; next }
185
+ {
186
+ seen[$2] = 1
187
+ if (!($2 in before)) printf "%s%s\n", verb(add, "add"), $2
188
+ else if (before[$2] != $1) printf "%s%s\n", verb(update, "update"), $2
189
+ }
190
+ END {
191
+ for (path in before) {
192
+ if (!(path in seen)) printf "%s%s\n", verb(drop, "drop"), path
193
+ }
194
+ }
195
+ '
196
+ }
197
+
198
+ # _name_parts <parts> <template>
199
+ #
200
+ # Print <parts> as the header names them, eg. "app, xyflow".
201
+ _name_parts() {
202
+ case "$1" in
203
+ plumbing) printf 'plumbing\n' ;;
204
+ app) printf 'app, %s\n' "$2" ;;
205
+ *) printf 'plumbing and app, %s\n' "$2" ;;
206
+ esac
207
+ }
208
+
209
+ # _print_init_summary <project-dir>
210
+ #
211
+ # Print the project folder. The paths that follow are relative to it.
212
+ _print_init_summary() {
213
+ printf '%s\t%s\n' "${_BOLD}\$PROJECT_DIR${_RESET}" "$(_shorten_home "$1")"
214
+ printf '\n'
215
+ }
216
+
217
+ # _print_next_steps <parts> <init-dir> <project-dir> <init-abs-dir>
218
+ #
219
+ # Print the next step and its command, in one line: for the plumbing, the init
220
+ # of an app, otherwise the install and the serve of the app. <init-dir> is
221
+ # <directory> as init prints it. <project-dir> and <init-abs-dir> are the
222
+ # absolute paths of the project folder and of <directory>.
223
+ _print_next_steps() {
224
+ case "$1" in
225
+ plumbing)
226
+ _info -v add_cmd "npx @asd14/vblocks init $(_quote_path "$2")/your-app-name --only app" \
227
+ "Next, add an app"
228
+ ;;
229
+ app)
230
+ pns_cd=""
231
+ if [ "$3" != "$PWD" ]; then
232
+ pns_cd="cd $(_quote_path "$(_shorten_path "$3")") && "
233
+ fi
234
+ _info -v serve_cmd "${pns_cd}npm install && npx vblocks serve --prefix $(_quote_path "${4#"$3"/}")" \
235
+ "Next, serve the app"
236
+ ;;
237
+ *)
238
+ _info -v serve_cmd "cd $(_quote_path "$2") && npm install && npx vblocks serve" \
239
+ "Next, serve the app"
240
+ ;;
241
+ esac
242
+ }
243
+
244
+ # ╭───
245
+ # │ Flags and positionals
246
+ # ╰───
247
+
248
+ while [ "$#" -gt 0 ]; do
249
+ case "$1" in
250
+ -h | --help)
251
+ _show_help "$0"
252
+ exit 0
253
+ ;;
254
+ --template)
255
+ [ "$#" -lt 2 ] && _fail 2 -v available_templates "$(_list_templates)" "--template needs a name"
256
+ _TEMPLATE=$2
257
+ shift 2
258
+ ;;
259
+ --only)
260
+ [ "$#" -lt 2 ] && _fail 2 -v available_parts "app,plumbing" "--only needs a part"
261
+ case "$2" in
262
+ app | plumbing) _PARTS=$2 ;;
263
+ *) _fail 2 -v part "$2" -v available_parts "app,plumbing" "Unknown part" ;;
264
+ esac
265
+ shift 2
266
+ ;;
267
+ --)
268
+ shift
269
+ if [ "$#" -ne 1 ] || [ -n "$_INIT_DIR" ]; then
270
+ _fail 2 "init takes one folder"
271
+ fi
272
+ _INIT_DIR=$1
273
+ shift
274
+ ;;
275
+ -?*)
276
+ _fail 2 -v flag "$1" "Unknown flag"
277
+ ;;
278
+ *)
279
+ [ -n "$_INIT_DIR" ] && _fail 2 "init takes one folder"
280
+ _INIT_DIR=$1
281
+ shift
282
+ ;;
283
+ esac
284
+ done
285
+
286
+ [ -z "$_INIT_DIR" ] && _fail 2 "init needs a folder"
287
+
288
+ # One shape for the printed paths: no trailing "/", and no leading "-" that
289
+ # a command would read as a flag
290
+ while [ "$_INIT_DIR" != "/" ] && [ "${_INIT_DIR%/}" != "$_INIT_DIR" ]; do
291
+ _INIT_DIR=${_INIT_DIR%/}
292
+ done
293
+ case "$_INIT_DIR" in
294
+ -*) _INIT_DIR="./$_INIT_DIR" ;;
295
+ esac
296
+
297
+ # A template is a folder in template/. A slug rules out a path, eg. `../x`
298
+ if ! _is slug "$_TEMPLATE" || [ ! -d "$_VB_DIR/template/$_TEMPLATE" ]; then
299
+ _fail 2 -v template "$_TEMPLATE" -v available_templates "$(_list_templates)" "Unknown template"
300
+ fi
301
+
302
+ # node reads the version. The post-init hooks run npm
303
+ _require_cmd node npm
304
+ _export_version
305
+
306
+ # ╭───
307
+ # │ Main
308
+ # ╰───
309
+
310
+ if [ -e "$_INIT_DIR" ]; then
311
+ if [ ! -d "$_INIT_DIR" ] || [ -n "$(ls -A -- "$_INIT_DIR")" ]; then
312
+ _fail 2 -v folder "$_INIT_DIR" "The folder exists and is not empty"
313
+ fi
314
+ fi
315
+ # An app lives in the project folder, its parent. The app hooks write to
316
+ # the package.json there
317
+ _PROJECT_DIR=""
318
+ if [ "$_PARTS" = "app" ]; then
319
+ _PARENT_DIR=$(dirname -- "$_INIT_DIR")
320
+ [ -f "$_PARENT_DIR/package.json" ] \
321
+ || _fail 3 -v folder "$_PARENT_DIR" -v next "npx @asd14/vblocks init <directory> --only plumbing" "No package.json in the parent folder"
322
+ _PROJECT_DIR=$(cd -- "$_PARENT_DIR" && pwd)
323
+ fi
324
+
325
+ mkdir -p -- "$_INIT_DIR" || _fail 3 -v folder "$_INIT_DIR" "Could not add the folder"
326
+ _INIT_ABS_DIR=$(cd -- "$_INIT_DIR" && pwd)
327
+ _PROJECT_DIR=${_PROJECT_DIR:-$_INIT_ABS_DIR}
328
+
329
+ _BEFORE_SNAPSHOT=$(_snapshot_folder "$_PROJECT_DIR")
330
+ _init_project "$_INIT_DIR" "$_TEMPLATE" "$_PARTS"
331
+ _AFTER_SNAPSHOT=$(_snapshot_folder "$_PROJECT_DIR")
332
+
333
+ _print_header "$VB_VERSION" "${_BOLD}init${_RESET}: $(_name_parts "$_PARTS" "$_TEMPLATE")" >&2
334
+ _print_init_summary "$_PROJECT_DIR"
335
+ _print_snapshot_diff "$_BEFORE_SNAPSHOT" "$_AFTER_SNAPSHOT"
336
+ _print_next_steps "$_PARTS" "$_INIT_DIR" "$_PROJECT_DIR" "$_INIT_ABS_DIR"
337
+ exit 0