@asd14/vblocks 6.4.0 → 7.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.
@@ -6,35 +6,66 @@
6
6
  # SYNOPSIS
7
7
  # vblocks build
8
8
  # [--mode <development|production>]
9
- # [--prefix <directory>]
9
+ # [--prefix <folder>]
10
10
  # [--single-file|--single]
11
11
  # [--log-level <error|warn|info|verbose>]
12
12
  # [-- <webpack option>...]
13
13
  #
14
14
  # DESCRIPTION
15
- # The app folder holds src/index.tsx and src/index.html. A config in it,
16
- # webpack.config.js, postcss.config.js or .swcrc, replaces the VBlocks
17
- # default of the same name. So do src/public/robots.txt and
15
+ # The app folder contains src/index.tsx and src/index.html. A config in it,
16
+ # webpack.config.js, postcss.config.js, .swcrc or .browserslistrc, replaces
17
+ # the VBlocks default of the same name. So do src/public/robots.txt and
18
18
  # src/public/favicon.svg.
19
19
  #
20
- # Print the app folder, the VBlocks folder and the files Webpack runs with,
21
- # then build. Print each file in dist/, its size and the total.
20
+ # Print the app folder, the VBlocks folder, the files Webpack runs with and
21
+ # the source of the browsers, then build. Print each file in dist/, its size
22
+ # and the total.
23
+ #
24
+ # SWC, Webpack and postcss-preset-env compile for the browsers of the app's
25
+ # browserslist config. browserslist reads BROWSERSLIST, BROWSERSLIST_CONFIG,
26
+ # then a .browserslistrc or the browserslist field of package.json, from the
27
+ # app folder up. Without any of them, browserslist reads the VBlocks default,
28
+ # defaults/.browserslistrc.
29
+ #
30
+ # Without --single-file, dist/ contains:
31
+ #
32
+ # index.html
33
+ # favicon.svg
34
+ # robots.txt
35
+ # js/main.<hash>.js
36
+ # js/react.<hash>.js react and react-dom
37
+ # js/vendors.<hash>.js the rest of node_modules
38
+ # js/*.LICENSE.txt the license comments of each chunk
39
+ # css/main.<hash>.css
40
+ # fonts/<name>.<ext> each font
41
+ # <hash>.<ext> each image over 10 KiB
42
+ #
43
+ # Webpack inlines an image up to 10 KiB. It splits react and vendors out of
44
+ # main, so the browser keeps them cached across builds.
22
45
  #
23
46
  # OPTIONS
24
47
  # --mode <mode> The Webpack mode, development or production
25
48
  # (default: production)
26
- # --prefix <directory> The app folder, which must exist (default: the
49
+ # --prefix <folder> The app folder, which must exist (default: the
27
50
  # current folder)
28
51
  # --single-file, --single Build one standalone dist/index.html, every
29
- # asset inlined, with no robots.txt. An ejected
30
- # webpack.config.js must read env.single
52
+ # asset inlined, with no robots.txt. A browser
53
+ # opens it from disk. There, history.pushState
54
+ # to another path throws, so keep the route in
55
+ # the URL hash. An ejected webpack.config.js
56
+ # builds one file only if it reads env.single
31
57
  # --log-level <level> How much Webpack prints: error, warn, info or
32
58
  # verbose (default: warn)
33
59
  # -- <webpack option>... Pass each option to Webpack, unchanged
34
60
  # -h, --help Print this help
35
61
  #
36
62
  # ENVIRONMENT
37
- # NO_COLOR When non-empty, print without color, the icons or the header
63
+ # NO_COLOR When non-empty, print without color, the icons or
64
+ # the header
65
+ # BROWSERSLIST The browsers, as a browserslist query. It overrides
66
+ # every config file
67
+ # BROWSERSLIST_CONFIG The path of a browserslist config, in place of the
68
+ # one browserslist finds from the app folder up
38
69
  #
39
70
  # EXAMPLE
40
71
  # npx vblocks build
@@ -43,7 +74,8 @@
43
74
  #
44
75
  # EXIT CODES
45
76
  # 0 Done
46
- # 2 Invalid usage, or Webpack rejected an option or the config
77
+ # 2 Invalid usage, Webpack rejected an option or the config, or
78
+ # browserslist could not read a config
47
79
  # 3 Could not run: a missing tool, or a failed Webpack run
48
80
  #
49
81
  # SEE ALSO
@@ -63,6 +95,10 @@ _SCRIPT_DIR=$(dirname "$0")
63
95
  . "$_SCRIPT_DIR/lib/guard.sh"
64
96
  # shellcheck source=lib/help.sh
65
97
  . "$_SCRIPT_DIR/lib/help.sh"
98
+ # shellcheck source=lib/os.sh
99
+ . "$_SCRIPT_DIR/lib/os.sh"
100
+ # shellcheck source=lib/terminal.sh
101
+ . "$_SCRIPT_DIR/lib/terminal.sh"
66
102
  # shellcheck source=vblocks.sh
67
103
  . "$_SCRIPT_DIR/vblocks.sh"
68
104
  # shellcheck source=webpack.sh
@@ -177,6 +213,9 @@ if [ -z "$_IS_SINGLE_FILE" ]; then
177
213
  _ROBOTS_FILE=$(_pick_file "$_APP_DIR" src/public/robots.txt)
178
214
  fi
179
215
  _FAVICON_FILE=$(_pick_file "$_APP_DIR" src/public/favicon.svg)
216
+ _pick_browserslist "$_APP_DIR"
217
+ _BROWSERSLIST_SOURCE=$(_find_browserslist "$_APP_DIR")
218
+ _set_command_title build "$(basename -- "$_APP_DIR")"
180
219
 
181
220
  _ARTIFACT="SPA bundle"
182
221
  if [ -n "$_IS_SINGLE_FILE" ]; then
@@ -185,7 +224,7 @@ fi
185
224
 
186
225
  _print_header "$VB_VERSION" "${_BOLD}build${_RESET}: $_MODE, $_ARTIFACT" >&2
187
226
  _print_summary "$_APP_DIR" "$_WEBPACK_CONFIG" "$_POSTCSS_CONFIG" "$_SWC_CONFIG" \
188
- "$_ROBOTS_FILE" "$_FAVICON_FILE"
227
+ "$_ROBOTS_FILE" "$_FAVICON_FILE" "$_BROWSERSLIST_SOURCE"
189
228
 
190
229
  # The default Webpack config reads env.single. An ejected config supports
191
230
  # --single-file only if it reads env.single too
@@ -7,15 +7,18 @@
7
7
  # vblocks eject <config>
8
8
  #
9
9
  # DESCRIPTION
10
- # Print the config to stdout. Save it in the app folder, and serve and
11
- # build run with it in place of the VBlocks default.
10
+ # eject prints the config to stdout. Save it in the app folder. serve and
11
+ # build then run with it in place of the VBlocks default.
12
12
  #
13
13
  # An ejected config loads its plugins and loaders from the project folder,
14
- # not from VBlocks. Install them there.
14
+ # not from VBlocks. Install them there. serve and build still run the
15
+ # webpack-cli, webpack and webpack-dev-server of VBlocks.
15
16
  #
16
17
  # ARGUMENTS
17
- # config webpack, postcss or swc. Save each as webpack.config.js,
18
- # postcss.config.js or .swcrc
18
+ # config webpack Save it as webpack.config.js
19
+ # postcss Save it as postcss.config.js
20
+ # swc Save it as .swcrc
21
+ # browserslist Save it as .browserslistrc
19
22
  #
20
23
  # OPTIONS
21
24
  # -h, --help Print this help
@@ -40,6 +43,10 @@ _SCRIPT_DIR=$(dirname "$0")
40
43
  . "$_SCRIPT_DIR/lib/log.sh"
41
44
  # shellcheck source=lib/help.sh
42
45
  . "$_SCRIPT_DIR/lib/help.sh"
46
+ # shellcheck source=lib/os.sh
47
+ . "$_SCRIPT_DIR/lib/os.sh"
48
+ # shellcheck source=lib/terminal.sh
49
+ . "$_SCRIPT_DIR/lib/terminal.sh"
43
50
  # shellcheck source=vblocks.sh
44
51
  . "$_SCRIPT_DIR/vblocks.sh"
45
52
 
@@ -53,7 +60,7 @@ _EJECT_CONFIG="" # eject <config>
53
60
  # │ Flags and positionals
54
61
  # ╰───
55
62
 
56
- [ "$#" -ne 1 ] && _fail 2 -v available_configs "webpack,postcss,swc" "eject needs one config"
63
+ [ "$#" -ne 1 ] && _fail 2 -v available_configs "webpack,postcss,swc,browserslist" "eject needs one config"
57
64
 
58
65
  case "$1" in
59
66
  -h | --help)
@@ -63,12 +70,15 @@ case "$1" in
63
70
  webpack) _EJECT_CONFIG="webpack.config.js" ;;
64
71
  postcss) _EJECT_CONFIG="postcss.config.js" ;;
65
72
  swc) _EJECT_CONFIG=".swcrc" ;;
66
- *) _fail 2 -v config "$1" -v available_configs "webpack,postcss,swc" "Unknown config" ;;
73
+ browserslist) _EJECT_CONFIG=".browserslistrc" ;;
74
+ *) _fail 2 -v config "$1" -v available_configs "webpack,postcss,swc,browserslist" "Unknown config" ;;
67
75
  esac
68
76
 
69
77
  # ╭───
70
78
  # │ Main
71
79
  # ╰───
72
80
 
81
+ _set_command_title eject "$1"
82
+
73
83
  cat -- "$_VB_DIR/defaults/$_EJECT_CONFIG"
74
84
  exit 0
@@ -4,28 +4,36 @@
4
4
  # vblocks init - Write a template into a folder
5
5
  #
6
6
  # SYNOPSIS
7
- # vblocks init <directory>
8
- # [--template <plain|xyflow|xyflow-tabs>]
7
+ # vblocks init <folder>
8
+ # [--template <name>]
9
9
  # [--only <app|plumbing>]
10
+ # vblocks init --list
10
11
  #
11
12
  # 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>.
13
+ # A template has two parts:
15
14
  #
16
- # Print the project folder and each file init adds, updates or drops, then
17
- # the next steps.
15
+ # plumbing package.json and .gitignore, for the project folder
16
+ # app src/, .env.example, tsconfig.json and the template's
17
+ # configs, for the app folder
18
+ #
19
+ # init writes each part into its folder, then runs the part's post-init
20
+ # hooks in <folder>. template/<name>/README.md, in the VBlocks folder,
21
+ # lists the files and hooks of each template.
22
+ #
23
+ # Print the project folder, each file init creates, updates or deletes, then
24
+ # the next step.
18
25
  #
19
26
  # ARGUMENTS
20
- # directory The folder to write into, missing or empty
27
+ # folder The folder to write into, missing or empty
21
28
  #
22
29
  # OPTIONS
23
- # --template <name> The template, plain, xyflow or xyflow-tabs (default:
24
- # plain)
30
+ # --template <name> The template, one of the names --list prints
31
+ # (default: plain)
32
+ # --list Print the templates, one per line
25
33
  # --only <part> Write only the app or only the plumbing (default:
26
34
  # both). With app, init adds the template's
27
35
  # dependencies to the package.json of the project
28
- # folder, the parent of <directory>. That package.json
36
+ # folder, the parent of <folder>. That package.json
29
37
  # must exist
30
38
  # -h, --help Print this help
31
39
  #
@@ -34,15 +42,16 @@
34
42
  #
35
43
  # EXAMPLE
36
44
  # npx @asd14/vblocks init my-app
45
+ # npx @asd14/vblocks init --list
37
46
  # npx @asd14/vblocks init demos --only plumbing
38
47
  # npx @asd14/vblocks init demos/flow --template xyflow --only app
39
48
  #
40
49
  # EXIT CODES
41
50
  # 0 Done
42
- # 2 Invalid usage, an unknown template, or a <directory> that is not empty
43
- # or not a folder
51
+ # 2 Invalid usage, an unknown template, or a <folder> that is a file or
52
+ # not empty
44
53
  # 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
54
+ # a folder init could not create, or a failed copy or hook
46
55
  #
47
56
  # SEE ALSO
48
57
  # vblocks, vblocks serve, node, npm
@@ -61,6 +70,10 @@ _SCRIPT_DIR=$(dirname "$0")
61
70
  . "$_SCRIPT_DIR/lib/guard.sh"
62
71
  # shellcheck source=lib/help.sh
63
72
  . "$_SCRIPT_DIR/lib/help.sh"
73
+ # shellcheck source=lib/os.sh
74
+ . "$_SCRIPT_DIR/lib/os.sh"
75
+ # shellcheck source=lib/terminal.sh
76
+ . "$_SCRIPT_DIR/lib/terminal.sh"
64
77
  # shellcheck source=vblocks.sh
65
78
  . "$_SCRIPT_DIR/vblocks.sh"
66
79
 
@@ -68,7 +81,7 @@ _SCRIPT_DIR=$(dirname "$0")
68
81
  # │ Settings
69
82
  # ╰───
70
83
 
71
- _INIT_DIR="" # init <directory>
84
+ _INIT_DIR="" # init <folder>
72
85
  _TEMPLATE="plain" # init --template
73
86
  _PARTS="plumbing app" # init --only: "plumbing", "app" or "plumbing app"
74
87
 
@@ -112,7 +125,9 @@ _quote_path() {
112
125
  # Print the template names, one per line. A template is a folder in
113
126
  # `template/` whose name has no `_` prefix.
114
127
  _list_templates() {
115
- for lt_dir in "$_VB_DIR"/template/*/; do
128
+ # No trailing "/" in the glob, so "xyflow" sorts before "xyflow-tabs" in
129
+ # every locale
130
+ for lt_dir in "$_VB_DIR"/template/*; do
116
131
  [ -d "$lt_dir" ] || continue
117
132
  lt_name=$(basename "$lt_dir")
118
133
 
@@ -123,9 +138,9 @@ _list_templates() {
123
138
  done
124
139
  }
125
140
 
126
- # _init_project <directory> <template> <parts>
141
+ # _init_project <folder> <template> <parts>
127
142
  #
128
- # For each of <parts>, copy it from <template> into <directory>, then run its
143
+ # For each of <parts>, copy it from <template> into <folder>, then run its
129
144
  # hooks there. <parts> is "plumbing", "app" or "plumbing app". A template's file
130
145
  # replaces the `_common` file of the same path.
131
146
  #
@@ -171,7 +186,7 @@ _snapshot_folder() {
171
186
 
172
187
  # _print_snapshot_diff <before-snapshot> <after-snapshot>
173
188
  #
174
- # Print "add", "update" or "drop" for each file that differs between two
189
+ # Print "create", "update" or "delete" for each file that differs between two
175
190
  # snapshots of the project folder, with its path relative to that folder.
176
191
  _print_snapshot_diff() {
177
192
  # Every snapshot line has a tab. The AFTER line has none, so it marks where
@@ -181,9 +196,9 @@ _print_snapshot_diff() {
181
196
  printf 'AFTER\n'
182
197
  printf '%s\n' "$2"
183
198
  } | awk -F '\t' \
184
- -v add="${_GREEN}add${_RESET}" \
185
- -v update="${_YELLOW}update${_RESET}" \
186
- -v drop="${_RED}drop${_RESET}" '
199
+ -v create_word="${_GREEN}create${_RESET}" \
200
+ -v update_word="${_YELLOW}update${_RESET}" \
201
+ -v delete_word="${_RED}delete${_RESET}" '
187
202
  # The color codes take no column, so pad by the plain verb
188
203
  function verb(styled, plain) {
189
204
  return styled substr(" ", 1, 8 - length(plain))
@@ -193,12 +208,12 @@ _print_snapshot_diff() {
193
208
  !is_after { before[$2] = $1; next }
194
209
  {
195
210
  seen[$2] = 1
196
- if (!($2 in before)) printf "%s%s\n", verb(add, "add"), $2
197
- else if (before[$2] != $1) printf "%s%s\n", verb(update, "update"), $2
211
+ if (!($2 in before)) printf "%s%s\n", verb(create_word, "create"), $2
212
+ else if (before[$2] != $1) printf "%s%s\n", verb(update_word, "update"), $2
198
213
  }
199
214
  END {
200
215
  for (path in before) {
201
- if (!(path in seen)) printf "%s%s\n", verb(drop, "drop"), path
216
+ if (!(path in seen)) printf "%s%s\n", verb(delete_word, "delete"), path
202
217
  }
203
218
  }
204
219
  '
@@ -223,13 +238,13 @@ _print_init_summary() {
223
238
  printf '\n'
224
239
  }
225
240
 
226
- # _print_next_steps <parts> <init-dir> <project-dir> <init-abs-dir>
241
+ # _print_next_step <parts> <init-dir> <project-dir> <init-abs-dir>
227
242
  #
228
243
  # Print the next step and its command, in one line: for the plumbing, the init
229
244
  # of an app, otherwise the install and the serve of the app. <init-dir> is
230
- # <directory> as init prints it. <project-dir> and <init-abs-dir> are the
231
- # absolute paths of the project folder and of <directory>.
232
- _print_next_steps() {
245
+ # <folder> as init prints it. <project-dir> and <init-abs-dir> are the
246
+ # absolute paths of the project folder and of <folder>.
247
+ _print_next_step() {
233
248
  case "$1" in
234
249
  plumbing)
235
250
  _info -v add_cmd "npx @asd14/vblocks init $(_quote_path "$2")/your-app-name --only app" \
@@ -260,6 +275,10 @@ while [ "$#" -gt 0 ]; do
260
275
  _show_help "$0"
261
276
  exit 0
262
277
  ;;
278
+ --list)
279
+ _list_templates
280
+ exit 0
281
+ ;;
263
282
  --template)
264
283
  [ "$#" -lt 2 ] && _fail 2 -v available_templates "$(_list_templates)" "--template needs a name"
265
284
  _TEMPLATE=$2
@@ -327,11 +346,12 @@ _PROJECT_DIR=""
327
346
  if [ "$_PARTS" = "app" ]; then
328
347
  _PARENT_DIR=$(dirname -- "$_INIT_DIR")
329
348
  [ -f "$_PARENT_DIR/package.json" ] \
330
- || _fail 3 -v folder "$_PARENT_DIR" -v next "npx @asd14/vblocks init <directory> --only plumbing" "No package.json in the parent folder"
349
+ || _fail 3 -v folder "$_PARENT_DIR" -v next "npx @asd14/vblocks init <folder> --only plumbing" "No package.json in the parent folder"
331
350
  _PROJECT_DIR=$(cd -- "$_PARENT_DIR" && pwd)
332
351
  fi
333
352
 
334
- mkdir -p -- "$_INIT_DIR" || _fail 3 -v folder "$_INIT_DIR" "Could not add the folder"
353
+ _set_command_title init "$(basename -- "$_INIT_DIR")"
354
+ mkdir -p -- "$_INIT_DIR" || _fail 3 -v folder "$_INIT_DIR" "Could not create the folder"
335
355
  _INIT_ABS_DIR=$(cd -- "$_INIT_DIR" && pwd)
336
356
  _PROJECT_DIR=${_PROJECT_DIR:-$_INIT_ABS_DIR}
337
357
 
@@ -342,5 +362,5 @@ _AFTER_SNAPSHOT=$(_snapshot_folder "$_PROJECT_DIR")
342
362
  _print_header "$VB_VERSION" "${_BOLD}init${_RESET}: $(_name_parts "$_PARTS" "$_TEMPLATE")" >&2
343
363
  _print_init_summary "$_PROJECT_DIR"
344
364
  _print_snapshot_diff "$_BEFORE_SNAPSHOT" "$_AFTER_SNAPSHOT"
345
- _print_next_steps "$_PARTS" "$_INIT_DIR" "$_PROJECT_DIR" "$_INIT_ABS_DIR"
365
+ _print_next_step "$_PARTS" "$_INIT_DIR" "$_PROJECT_DIR" "$_INIT_ABS_DIR"
346
366
  exit 0
@@ -0,0 +1,188 @@
1
+ #!/bin/sh
2
+
3
+ # NAME
4
+ # os.sh - Run functions on exit, create a temporary folder and delete it on
5
+ # exit, find OS folders
6
+ #
7
+ # SYNOPSIS
8
+ # . "$_SCRIPT_DIR/../lib/os.sh"
9
+ #
10
+ # DESCRIPTION
11
+ # Needs log.sh. A script sources it before this file.
12
+
13
+ # _run_if_found <cmd> [arg...]
14
+ #
15
+ # Run <cmd> with its args when the shell finds it. Do nothing when it does
16
+ # not. Use it for an optional command.
17
+ #
18
+ # RETURNS
19
+ # 0 The shell cannot find <cmd>, or <cmd> exits 0
20
+ # other The status of <cmd>
21
+ _run_if_found() {
22
+ command -v "$1" >/dev/null 2>&1 || return 0
23
+ "$@"
24
+ }
25
+
26
+ # _get_user_dir <NAME> <fallback-dir>
27
+ #
28
+ # Print the XDG user folder <NAME>, eg. PICTURES. Print <fallback-dir> when
29
+ # xdg-user-dir is not installed.
30
+ _get_user_dir() {
31
+ if command -v xdg-user-dir >/dev/null 2>&1; then
32
+ xdg-user-dir "$1"
33
+ else
34
+ printf '%s\n' "$2"
35
+ fi
36
+ }
37
+
38
+ # _get_runtime_dir
39
+ #
40
+ # Print the runtime folder of the current user: XDG_RUNTIME_DIR, else
41
+ # /run/user/<uid>. The system clears it at boot, so nothing in it outlasts a
42
+ # reboot.
43
+ #
44
+ # EXIT
45
+ # 3 XDG_RUNTIME_DIR is unset or empty, and /run/user/<uid> is not a
46
+ # folder
47
+ _get_runtime_dir() {
48
+ if [ -n "${XDG_RUNTIME_DIR:-}" ]; then
49
+ printf '%s\n' "$XDG_RUNTIME_DIR"
50
+ return 0
51
+ fi
52
+
53
+ lib_grd_dir="/run/user/$(id -u)"
54
+ [ -d "$lib_grd_dir" ] \
55
+ || _fail 3 -v dir "$lib_grd_dir" "No runtime folder, XDG_RUNTIME_DIR is not set"
56
+
57
+ printf '%s\n' "$lib_grd_dir"
58
+ }
59
+
60
+ # _resolve_bin_dir
61
+ #
62
+ # Print the first writable bin dir of /usr/local/bin and ~/.local/bin.
63
+ # ~/.local/bin is the user bin dir in the "XDG Base Directory Specification":
64
+ # https://specifications.freedesktop.org/basedir/latest/
65
+ #
66
+ # EXIT
67
+ # 3 Neither /usr/local/bin nor ~/.local/bin is a writable directory
68
+ _resolve_bin_dir() {
69
+ for lib_rbd_dir in /usr/local/bin "${HOME:-}/.local/bin"; do
70
+ [ -d "$lib_rbd_dir" ] && [ -w "$lib_rbd_dir" ] && {
71
+ printf '%s' "$lib_rbd_dir"
72
+ return 0
73
+ }
74
+ done
75
+
76
+ _fail 3 -v current_user "$(whoami)" \
77
+ "No writable bin dir (/usr/local/bin, ~/.local/bin)"
78
+ }
79
+
80
+ # _create_temp_dir <name>
81
+ #
82
+ # Create a folder named after <name> under TMPDIR, eg. /tmp/t212.Ab12Cd, and
83
+ # print its path. Pass the path to _delete_on_exit on the next line: a signal
84
+ # between the two leaves the empty folder behind.
85
+ #
86
+ # EXIT
87
+ # 3 mktemp failed
88
+ _create_temp_dir() {
89
+ mktemp -d "${TMPDIR:-/tmp}/$1.XXXXXX" \
90
+ || _fail 3 "Could not create a temporary folder"
91
+ }
92
+
93
+ _EXIT_FUNCTIONS="" # space-separated, the last registered first
94
+ _DELETE_DIR="" # the folder to delete on exit
95
+
96
+ # _on_exit <function>
97
+ #
98
+ # Register <function> to run when the script exits, or when INT, TERM or HUP
99
+ # ends it. <function> gets the exit code as $1: the code of the script on
100
+ # EXIT, 130 on INT, 143 on TERM, 129 on HUP. The last function registered runs
101
+ # first, and each runs once. A failed one does not stop the rest.
102
+ #
103
+ # _on_exit owns the traps of EXIT, INT, TERM and HUP. Its first call replaces
104
+ # any the script set, and a later `trap` on one of them replaces its own.
105
+ #
106
+ # Call it in the main shell. A subshell, eg. $(...), starts with no traps but
107
+ # keeps _EXIT_FUNCTIONS. If the list is empty, _on_exit sets the traps of the
108
+ # subshell, and the function runs when the subshell exits. If not, it sets no
109
+ # trap, and the function never runs.
110
+ #
111
+ # EXIT
112
+ # 3 <function> is not a defined function
113
+ _on_exit() {
114
+ # command -v prints the name of a function, a builtin or a reserved word, the
115
+ # path of a program, or nothing. A builtin passes, eg. `true`, and does no
116
+ # harm
117
+ [ "$(command -v -- "$1")" = "$1" ] \
118
+ || _fail 3 -v function "$1" "_on_exit takes a defined function"
119
+
120
+ # The traps read _EXIT_FUNCTIONS when they run, so only the first call sets
121
+ # the traps
122
+ if [ -z "$_EXIT_FUNCTIONS" ]; then
123
+ trap '_run_exit_functions $?' EXIT
124
+ trap '_end_by_signal INT 130' INT # Ctrl-C
125
+ trap '_end_by_signal TERM 143' TERM # kill, timeout, cancelled CI job
126
+ trap '_end_by_signal HUP 129' HUP # lost SSH connection, closed window
127
+ fi
128
+
129
+ _EXIT_FUNCTIONS="$1 $_EXIT_FUNCTIONS"
130
+ }
131
+
132
+ # _run_exit_functions <exit-code>
133
+ #
134
+ # Take each function off _EXIT_FUNCTIONS, then run it with <exit-code>. A
135
+ # function that exits during a signal trap makes the shell run the EXIT trap,
136
+ # which runs only the functions left.
137
+ _run_exit_functions() {
138
+ while [ -n "$_EXIT_FUNCTIONS" ]; do
139
+ lib_ref_function=${_EXIT_FUNCTIONS%% *}
140
+ _EXIT_FUNCTIONS=${_EXIT_FUNCTIONS#* }
141
+ "$lib_ref_function" "$1" || true
142
+ done
143
+ }
144
+
145
+ # _end_by_signal <signal> <exit-code>
146
+ #
147
+ # Run the functions _on_exit registered with <exit-code>, then end the script
148
+ # by <signal>, so its caller sees the signal. A trap runs in place of the
149
+ # default action of <signal>, so _end_by_signal clears the traps of EXIT and
150
+ # <signal>, then resends <signal>.
151
+ _end_by_signal() {
152
+ _run_exit_functions "$2"
153
+ trap - EXIT "$1"
154
+ kill -s "$1" $$
155
+ }
156
+
157
+ # _delete_on_exit <dir>
158
+ #
159
+ # Delete the absolute folder <dir> when the script exits, or when INT, TERM or
160
+ # HUP ends it. A function that a later _on_exit call registers runs first.
161
+ #
162
+ # A script calls it once. Create any other temporary folder inside <dir>, eg.
163
+ # `mktemp -d "$_TEMP_DIR/part.XXXXXX"`.
164
+ #
165
+ # EXIT
166
+ # 3 A second call, or a <dir> that is not an absolute path
167
+ _delete_on_exit() {
168
+ [ -z "$_DELETE_DIR" ] \
169
+ || _fail 3 -v dir "$1" "_delete_on_exit takes one folder per script. Create this one inside the first"
170
+
171
+ # The script can cd before it exits, and a relative path then names
172
+ # another folder
173
+ case "$1" in
174
+ /*) ;;
175
+ *) _fail 3 -v dir "$1" "_delete_on_exit takes an absolute path" ;;
176
+ esac
177
+
178
+ _DELETE_DIR=$1
179
+ _on_exit _delete_dir
180
+ }
181
+
182
+ # _delete_dir
183
+ #
184
+ # Delete _DELETE_DIR.
185
+ # shellcheck disable=SC2329 # _delete_on_exit registers it with _on_exit
186
+ _delete_dir() {
187
+ rm -rf -- "$_DELETE_DIR"
188
+ }
@@ -0,0 +1,57 @@
1
+ #!/bin/sh
2
+
3
+ # NAME
4
+ # terminal.sh - Set and restore the title of the terminal, wait for Enter
5
+ #
6
+ # SYNOPSIS
7
+ # . "$_SCRIPT_DIR/../lib/terminal.sh"
8
+ #
9
+ # DESCRIPTION
10
+ # Needs log.sh, os.sh. A script sources them before this file.
11
+
12
+ _HAS_TITLE="" # "true" once _set_title changed the window title
13
+
14
+ # _set_title <title>
15
+ #
16
+ # Name the terminal window, when stderr is a terminal. The escape codes are
17
+ # xterm's and most terminals read them, a flag such as --title differs per
18
+ # terminal. The first code saves the old title, _restore_title restores it.
19
+ _set_title() {
20
+ [ -t 2 ] || return 0
21
+
22
+ printf '\033[22;0t\033]0;%s\007' "$1" >&2
23
+ _HAS_TITLE="true"
24
+ }
25
+
26
+ # _restore_title
27
+ #
28
+ # Restore the title that _set_title saved. The window may be gone, and a
29
+ # failed write must not stop the rest.
30
+ _restore_title() {
31
+ if [ -n "$_HAS_TITLE" ]; then
32
+ printf '\033[23;0t' >&2 2>/dev/null || true
33
+ _HAS_TITLE=""
34
+ fi
35
+ }
36
+
37
+ # _set_title_until_exit <title>
38
+ #
39
+ # Name the terminal window <title> until the script exits, or until INT, TERM
40
+ # or HUP ends it, then restore the old title. It registers _restore_title
41
+ # with _on_exit.
42
+ _set_title_until_exit() {
43
+ _on_exit _restore_title
44
+ _set_title "$1"
45
+ }
46
+
47
+ # _wait_for_enter
48
+ #
49
+ # Stop until the user presses Enter. A window opened only for the calling
50
+ # command closes with it, before the user reads what it shows. Without a
51
+ # terminal on stdin, read takes the next line or finds none, and the wait
52
+ # ends at once.
53
+ _wait_for_enter() {
54
+ printf 'Press Enter to continue ' >&2
55
+ # shellcheck disable=SC2034 # read needs a name, nobody reads it
56
+ read -r lib_wfe_reply || true
57
+ }