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