@asd14/vblocks 6.2.0 → 6.4.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.
Files changed (34) hide show
  1. package/README.md +101 -43
  2. package/bin/vblocks +4 -0
  3. package/bin/vblocks.d/build +9 -0
  4. package/bin/vblocks.d/eject +6 -1
  5. package/bin/vblocks.d/init +9 -0
  6. package/bin/vblocks.d/lib/guard.sh +64 -0
  7. package/bin/vblocks.d/lib/help.sh +26 -0
  8. package/bin/vblocks.d/lib/log.sh +178 -0
  9. package/bin/vblocks.d/lib/validation.sh +101 -0
  10. package/bin/vblocks.d/serve +9 -0
  11. package/bin/vblocks.d/vblocks.sh +9 -9
  12. package/bin/vblocks.d/webpack.sh +2 -0
  13. package/defaults/webpack.config.js +19 -0
  14. package/package.json +12 -10
  15. package/template/_common/app/.env.example +5 -0
  16. package/template/_common/app/src/index.tsx +27 -0
  17. package/template/_common/plumbing/package.json +1 -1
  18. package/template/_common/post-init-app.sh +9 -0
  19. package/template/plain/app/src/index.app.tsx +3 -0
  20. package/template/xyflow/app/src/details.tsx +1 -1
  21. package/template/xyflow/app/src/diagram.tsx +1 -1
  22. package/template/xyflow/app/src/{index.tsx → index.app.tsx} +6 -14
  23. package/template/xyflow/app/src/index.css +1 -0
  24. package/template/xyflow/post-init-app.sh +2 -2
  25. package/template/xyflow-tabs/app/src/{index.tsx → index.app.tsx} +5 -15
  26. package/template/xyflow-tabs/app/src/index.css +1 -0
  27. package/template/xyflow-tabs/app/src/pages/build/details.tsx +1 -1
  28. package/template/xyflow-tabs/app/src/pages/build/diagram.tsx +2 -2
  29. package/template/xyflow-tabs/app/src/pages/serve/details.tsx +1 -1
  30. package/template/xyflow-tabs/app/src/pages/serve/diagram.tsx +2 -2
  31. package/template/xyflow-tabs/post-init-app.sh +2 -2
  32. package/types/client.d.ts +16 -0
  33. package/bin/vblocks.d/lib.sh +0 -569
  34. package/template/plain/app/src/index.tsx +0 -11
package/README.md CHANGED
@@ -5,15 +5,15 @@
5
5
 
6
6
  > Zero-config React builds, as a static site or a single HTML file
7
7
 
8
- **✂️ Zero config**
8
+ **✂️ Zero config**\
9
9
  Needs only `src/index.tsx` and `src/index.html`
10
10
 
11
11
  <!--
12
- Using "Maximal support" to answer "Zero config" above: nothing to set up, and
12
+ Using "Maximal support" to pair with "Zero config" above: nothing to set up, and
13
13
  everything set up.
14
14
  -->
15
15
 
16
- **🔋 Maximal support**
16
+ **🔋 Maximal support**\
17
17
  SWC, PostCSS, CSS Modules, MDX, React Refresh, Dotenv
18
18
 
19
19
  <!--
@@ -23,15 +23,21 @@ PARKED until SSG is finished, then name both outputs: SPA and SSG.
23
23
  Builds to `dist/`, for any static host
24
24
  -->
25
25
 
26
- **📦 Several apps, one install**
26
+ **📦 Several apps, one install**\
27
27
  Apps share one `package.json` and `node_modules/`. A new app reuses the
28
28
  installed packages
29
29
 
30
- **🤖 Optimized for AI agents**
31
- The agent's context holds only the app: no Webpack, PostCSS or TypeScript config
32
- to write, read or fix
30
+ **🤖 Optimized for AI agents**\
31
+ The agent's context contains only the app: no Webpack, PostCSS or TypeScript
32
+ config to write, read or fix
33
33
 
34
- > [!IMPORTANT]
34
+ - [Agentation](https://www.agentation.com) in development: click an element and
35
+ write what to change. The agent gets the note with the element's selector,
36
+ text and React components
37
+ - A terminal gets color and a header. An agent reads through a pipe, and gets
38
+ only the facts
39
+
40
+ > [!IMPORTANT]\
35
41
  > VBlocks needs Node `^22.22.3`, `^24.15.0` or `>=26.0`.
36
42
 
37
43
  ## Table of contents
@@ -45,6 +51,7 @@ to write, read or fix
45
51
  - [Use environment variables](#use-environment-variables)
46
52
  - [Stop using `../../../` everywhere](#stop-using--everywhere)
47
53
  - [Use CSS Modules](#use-css-modules)
54
+ - [Import a file as text](#import-a-file-as-text)
48
55
  - [Replace a default config](#replace-a-default-config)
49
56
  - [Style with Tailwind](#style-with-tailwind)
50
57
  - [Add VBlocks to an existing project](#add-vblocks-to-an-existing-project)
@@ -52,6 +59,7 @@ to write, read or fix
52
59
  - [Commands](#commands)
53
60
  - [`vblocks init`](#vblocks-init)
54
61
  - [`vblocks serve`](#vblocks-serve)
62
+ - [Agentation](#agentation)
55
63
  - [`vblocks build`](#vblocks-build)
56
64
  - [`vblocks eject`](#vblocks-eject)
57
65
  - [Templates](#templates)
@@ -100,12 +108,14 @@ scaffolders spots the folder at a glance, as with `vite-project` and
100
108
  ```text
101
109
  my-vblocks-app/
102
110
  ├─ src/
103
- │ ├─ index.tsx
111
+ │ ├─ index.tsx # mounts App, and the Agentation toolbar in development
112
+ │ ├─ index.app.tsx # App, your code starts here
104
113
  │ ├─ index.html
105
114
  │ └─ index.css
115
+ ├─ .env.example
106
116
  ├─ .gitignore
107
117
  ├─ tsconfig.json
108
- └─ package.json # react, react-dom and the vblocks scripts
118
+ └─ package.json # react, react-dom, agentation and the vblocks scripts
109
119
  ```
110
120
 
111
121
  2. **Start a development server**
@@ -150,10 +160,12 @@ Several apps, eg. demos or MVP sketches, can share one `package.json` and one
150
160
  sketches/
151
161
  + ├─ flow-diagram/
152
162
  + │ ├─ src/ # the files of the xyflow template
163
+ + │ ├─ .env.example
153
164
  + │ ├─ postcss.config.js
154
165
  + │ └─ tsconfig.json
155
166
  ├─ .gitignore
156
- ! └─ package.json # + @xyflow/react, tailwindcss, @tailwindcss/postcss, cssnano
167
+ ! └─ package.json # + @xyflow/react, tailwindcss, @tailwindcss/postcss,
168
+ ! # cssnano, agentation
157
169
  ```
158
170
 
159
171
  3. **Start a development server for one app**
@@ -174,7 +186,6 @@ the definitions in.
174
186
  | **Plumbing** | The project files `init` writes: `package.json`, `.gitignore` |
175
187
  | **Project folder** | The folder with `package.json` |
176
188
  | **App folder** | The folder with `src/index.tsx` and `src/index.html` |
177
- | **Bundle** | The files `build` writes to `dist/` |
178
189
 
179
190
  ## How to
180
191
 
@@ -185,7 +196,7 @@ early the Quick Start reader has the pain.
185
196
 
186
197
  ### Use environment variables
187
198
 
188
- 1. **Add a `.env` file to your app folder**
199
+ 1. **Create a `.env` file in your app folder**
189
200
 
190
201
  ```env
191
202
  API_URL=https://api.example.com
@@ -201,11 +212,11 @@ Webpack replaces each `process.env["NAME"]` with the value of `NAME`, and
201
212
  `undefined` when `.env` does not set it. Webpack leaves out of the bundle every
202
213
  variable your code does not read.
203
214
 
204
- > [!WARNING]
215
+ > [!WARNING]\
205
216
  > Never expose a secret, eg. an API key, through `process.env`. Anyone who loads
206
217
  > the page can read it.
207
218
 
208
- > [!NOTE]
219
+ > [!NOTE]\
209
220
  > The [client types](types/client.d.ts) declare `process.env` and nothing else
210
221
  > from Node, because Webpack replaces nothing else.
211
222
 
@@ -214,11 +225,11 @@ variable your code does not read.
214
225
  VBlocks resolves `@self` to the `src/` of your app folder:
215
226
 
216
227
  ```tsx
217
- // Instead of: import { Component } from "../../../components"
218
- import { Component } from "@self/components"
228
+ // Instead of: import { Button } from "../../../components/button.tsx"
229
+ import { Button } from "@self/components/button.tsx"
219
230
  ```
220
231
 
221
- > [!NOTE]
232
+ > [!NOTE]\
222
233
  > VBlocks compiles TypeScript with SWC and does not typecheck. Run `tsc`
223
234
  > yourself. Your `tsconfig.json` needs the same alias as Webpack.
224
235
  > The `tsconfig.json` that [`vblocks init`](#vblocks-init) writes already has
@@ -256,31 +267,43 @@ import { Component } from "@self/components"
256
267
  - **Class names** - a bare hash in `production` mode, `[local]__[hash]` in
257
268
  `development` mode
258
269
 
259
- > [!NOTE]
270
+ > [!NOTE]\
260
271
  > The [client types](types/client.d.ts) declare `.css`, image and font imports.
261
272
 
273
+ ### Import a file as text
274
+
275
+ Add `?raw` to the import path. The import returns the file's contents as a
276
+ string, as in Vite:
277
+
278
+ ```tsx
279
+ import greeting from "./greeting.txt?raw"
280
+ // greeting => "Hello from a text file\n"
281
+ ```
282
+
283
+ > [!NOTE]\
284
+ > The [client types](types/client.d.ts) type each `?raw` import as a `string`.
285
+
262
286
  ### Replace a default config
263
287
 
264
288
  Each config in your app folder replaces the default of the same name:
265
289
 
266
290
  - `webpack.config.js`
267
- - `postcss.config.js`
291
+ - `postcss.config.js`, the default runs:
268
292
  - `postcss-preset-env`, which compiles nesting, `oklch()` and `color-mix()`
269
293
  for older browsers and adds vendor prefixes
270
294
  - `cssnano` in `production`
271
- - `.swcrc`
272
- - browser targets `> 0.25%, not dead`
295
+ - `.swcrc`, the default targets `> 0.25%, not dead`
273
296
 
274
297
  1. **[Eject](#vblocks-eject) the default into your app folder**
275
298
 
276
299
  ```sh
277
- cd flow-diagram
300
+ cd my-vblocks-app
278
301
  npx vblocks eject swc > .swcrc
279
302
  ```
280
303
 
281
304
  2. **Update the ejected file**
282
305
 
283
- > [!NOTE]
306
+ > [!NOTE]\
284
307
  > VBlocks does not merge your config with the default, with two exceptions:
285
308
  >
286
309
  > - `.swcrc -> env.targets`: VBlocks adds the default when yours is missing
@@ -292,12 +315,12 @@ Each config in your app folder replaces the default of the same name:
292
315
  VBlocks does not include Tailwind. Add it through a `postcss.config.js` in your
293
316
  app folder:
294
317
 
295
- > [!NOTE]
318
+ > [!NOTE]\
296
319
  > The `xyflow` and `xyflow-tabs` templates of [`vblocks init`](#vblocks-init)
297
320
  > have this setup.
298
321
 
299
- 1. **Install Tailwind, the Tailwind plugin for PostCSS and cssnano, which the
300
- ejected config imports**
322
+ 1. **Install Tailwind, its PostCSS plugin and cssnano, which the ejected config
323
+ imports**
301
324
 
302
325
  ```sh
303
326
  npm install --save-dev tailwindcss @tailwindcss/postcss cssnano
@@ -325,7 +348,7 @@ app folder:
325
348
  })
326
349
  ```
327
350
 
328
- > [!IMPORTANT]
351
+ > [!IMPORTANT]\
329
352
  > Tailwind 4
330
353
  > [compiles nesting and adds vendor prefixes](https://tailwindcss.com/docs/compatibility)
331
354
  > itself. With both plugins, `postcss-preset-env` rewrites Tailwind's `@layer`
@@ -340,10 +363,9 @@ app folder:
340
363
  `source(".")` limits Tailwind's class scan to the folder of the CSS file,
341
364
  `src/`.
342
365
 
343
- > [!NOTE]
344
- > Without `source(".")`, Tailwind scans the current folder. With
345
- > [`--prefix`](#vblocks-build), you run VBlocks from the project folder, which
346
- > holds every app. Each app's CSS then also gets the classes of the other apps.
366
+ - Without it, Tailwind scans the folder you run VBlocks from. With
367
+ [`--prefix`](#vblocks-build), that is the project folder, which contains
368
+ every app. Each app's CSS then also gets the classes of the other apps
347
369
 
348
370
  ### Add VBlocks to an existing project
349
371
 
@@ -365,11 +387,16 @@ app folder:
365
387
  }
366
388
  ```
367
389
 
368
- 3. **Ensure `index.tsx` and `index.html` are in `src/`**
390
+ > [!NOTE]\
391
+ > An import may name the file tsc emits, eg. `./button.js` for `button.tsx`, as
392
+ > TypeScript's `nodenext` resolution requires. VBlocks resolves it to
393
+ > `button.tsx`.
394
+
395
+ 3. **Move `index.tsx` and `index.html` into `src/`**
369
396
 
370
- > [!NOTE]
397
+ > [!NOTE]\
371
398
  > VBlocks builds `dist/index.html` from `src/index.html` and adds the script and
372
- > stylesheet tags to it. Drop the tag that loads your entry, eg. Vite's
399
+ > stylesheet tags to it. Remove the tag that loads your entry, eg. Vite's
373
400
  > `<script type="module" src="/src/main.tsx"></script>`.
374
401
 
375
402
  4. **Start a development server**
@@ -401,8 +428,8 @@ vblocks init <directory>
401
428
  - `--only` (default: both)
402
429
  - Write one part of the template
403
430
  - `plumbing`: `package.json`, `.gitignore`
404
- - `app`: `src/`, `tsconfig.json` and the template's configs. `init` adds the
405
- template's dependencies to the `package.json` of the
431
+ - `app`: `src/`, `.env.example`, `tsconfig.json` and the template's configs.
432
+ `init` adds the template's dependencies to the `package.json` of the
406
433
  [project folder](#vocabulary), the parent of `<directory>`. Without a
407
434
  `package.json` there, `init` fails
408
435
 
@@ -411,6 +438,8 @@ vblocks init <directory>
411
438
  Start the development server.
412
439
 
413
440
  - Runs at `http://localhost:8080`, or the next free port
441
+ - Shows the [Agentation](#agentation) toolbar (`vblocks init` writes it into
442
+ `src/index.tsx`)
414
443
  - Listens on `localhost`, so another device on your network, eg. a phone, cannot
415
444
  open the page
416
445
  - Opens the page in the browser
@@ -426,7 +455,7 @@ vblocks serve
426
455
 
427
456
  - `--mode` (default: `development`)
428
457
  - The Webpack mode
429
- - React Refresh runs only in `development`
458
+ - React Refresh and the Agentation toolbar run only in `development`
430
459
  - `--prefix` (default: the current folder)
431
460
  - The [app folder](#vocabulary), which must exist
432
461
  - `--analyze`
@@ -438,6 +467,20 @@ vblocks serve
438
467
  - `serve` passes each one to Webpack unchanged
439
468
  - eg. `-- --host 0.0.0.0`, to listen on the network
440
469
 
470
+ ##### Agentation
471
+
472
+ [Agentation](https://www.agentation.com) is a toolbar for UI feedback to a
473
+ coding agent.
474
+
475
+ - Configure the agentation-mcp server, see
476
+ [Agentation's docs](https://www.agentation.com/mcp)
477
+ - Set `AGENTATION_ENDPOINT` in the `.env` of the app folder, as in
478
+ `.env.example`
479
+
480
+ > [!NOTE]\
481
+ > Agentation's license is PolyForm Shield 1.0.0, not MIT. To remove Agentation,
482
+ > remove its lines from `src/index.tsx` and run `npm uninstall agentation`.
483
+
441
484
  #### `vblocks build`
442
485
 
443
486
  Build the app into a bundle in the `dist/` of the app folder.
@@ -507,13 +550,28 @@ config.
507
550
 
508
551
  ### Templates
509
552
 
510
- All templates scaffold `src/index.html`, `src/index.css` and `tsconfig.json`.
553
+ Every template scaffolds:
554
+
555
+ ```text
556
+ src/
557
+ ├── index.tsx # mounts App, and the Agentation toolbar in development
558
+ ├── index.app.tsx # App, one per template
559
+ ├── index.html
560
+ └── index.css # the global CSS and its @imports, a Webpack entry of its
561
+ # own, so no .tsx needs to import it
562
+ .env.example # AGENTATION_ENDPOINT
563
+ tsconfig.json
564
+ ```
565
+
566
+ - Adds `agentation` to the `devDependencies` of the project folder
567
+ - Each `src/index.<concern>.tsx` defines one concern of `src/index.tsx`, eg.
568
+ `index.app.tsx` defines App
511
569
 
512
570
  #### `plain`
513
571
 
514
- "Hello World!" in `src/index.tsx`.
572
+ "Hello World!" in `src/index.app.tsx`.
515
573
 
516
- - Adds no packages
574
+ - Adds no other packages
517
575
 
518
576
  #### `xyflow`
519
577
 
@@ -525,7 +583,7 @@ One diagram, and a details panel for the selected node or edge.
525
583
 
526
584
  ```text
527
585
  src/
528
- ├── index.tsx # the layout: header, diagram, details panel, the selected id
586
+ ├── index.app.tsx # the layout: header, diagram, details panel, the selected id
529
587
  ├── diagram.tsx # the nodes and edges, placed and sized in grid cells
530
588
  ├── details.tsx # the panel for the selected node or edge
531
589
  └── utils/grid.ts # CELL_SIZE and toPixels
@@ -539,7 +597,7 @@ One tab per diagram, each with its own details panel.
539
597
 
540
598
  ```text
541
599
  src/
542
- ├── index.tsx # the layout: header, tab bar, details panel, the selected id
600
+ ├── index.app.tsx # the layout: header, tab bar, details panel, the selected id
543
601
  ├── pages/
544
602
  │ ├── build/
545
603
  │ │ ├── diagram.tsx # the nodes and edges of one tab, in grid cells
package/bin/vblocks CHANGED
@@ -37,6 +37,10 @@ set -eu
37
37
  # npm links the command to this file, so "$0" names the link. realpath follows
38
38
  # it to the package, where vblocks.d/ holds the commands
39
39
  _SCRIPT_DIR="$(dirname "$(realpath "$0")")/vblocks.d"
40
+ # shellcheck source=vblocks.d/lib/log.sh
41
+ . "$_SCRIPT_DIR/lib/log.sh"
42
+ # shellcheck source=vblocks.d/lib/help.sh
43
+ . "$_SCRIPT_DIR/lib/help.sh"
40
44
  # shellcheck source=vblocks.d/vblocks.sh
41
45
  . "$_SCRIPT_DIR/vblocks.sh"
42
46
 
@@ -54,6 +54,15 @@
54
54
  set -eu
55
55
 
56
56
  _SCRIPT_DIR=$(dirname "$0")
57
+ # tools/update-lib downloads lib/ from posix-utils, never edit it here
58
+ # shellcheck source=lib/log.sh
59
+ . "$_SCRIPT_DIR/lib/log.sh"
60
+ # shellcheck source=lib/validation.sh
61
+ . "$_SCRIPT_DIR/lib/validation.sh"
62
+ # shellcheck source=lib/guard.sh
63
+ . "$_SCRIPT_DIR/lib/guard.sh"
64
+ # shellcheck source=lib/help.sh
65
+ . "$_SCRIPT_DIR/lib/help.sh"
57
66
  # shellcheck source=vblocks.sh
58
67
  . "$_SCRIPT_DIR/vblocks.sh"
59
68
  # shellcheck source=webpack.sh
@@ -35,6 +35,11 @@
35
35
  set -eu
36
36
 
37
37
  _SCRIPT_DIR=$(dirname "$0")
38
+ # tools/update-lib downloads lib/ from posix-utils, never edit it here
39
+ # shellcheck source=lib/log.sh
40
+ . "$_SCRIPT_DIR/lib/log.sh"
41
+ # shellcheck source=lib/help.sh
42
+ . "$_SCRIPT_DIR/lib/help.sh"
38
43
  # shellcheck source=vblocks.sh
39
44
  . "$_SCRIPT_DIR/vblocks.sh"
40
45
 
@@ -42,7 +47,7 @@ _SCRIPT_DIR=$(dirname "$0")
42
47
  # │ Settings
43
48
  # ╰───
44
49
 
45
- _EJECT_CONFIG="" # eject <config>
50
+ _EJECT_CONFIG="" # eject <config>
46
51
 
47
52
  # ╭───
48
53
  # │ Flags and positionals
@@ -52,6 +52,15 @@
52
52
  set -eu
53
53
 
54
54
  _SCRIPT_DIR=$(dirname "$0")
55
+ # tools/update-lib downloads lib/ from posix-utils, never edit it here
56
+ # shellcheck source=lib/log.sh
57
+ . "$_SCRIPT_DIR/lib/log.sh"
58
+ # shellcheck source=lib/validation.sh
59
+ . "$_SCRIPT_DIR/lib/validation.sh"
60
+ # shellcheck source=lib/guard.sh
61
+ . "$_SCRIPT_DIR/lib/guard.sh"
62
+ # shellcheck source=lib/help.sh
63
+ . "$_SCRIPT_DIR/lib/help.sh"
55
64
  # shellcheck source=vblocks.sh
56
65
  . "$_SCRIPT_DIR/vblocks.sh"
57
66
 
@@ -0,0 +1,64 @@
1
+ #!/bin/sh
2
+
3
+ # NAME
4
+ # guard.sh - End the script when a command, a variable or a display is missing
5
+ #
6
+ # SYNOPSIS
7
+ # . "$_SCRIPT_DIR/../lib/guard.sh"
8
+ #
9
+ # DESCRIPTION
10
+ # Needs log.sh, validation.sh. A script sources them before this file.
11
+
12
+ # _require_cmd <cmd>...
13
+ #
14
+ # End the script unless the shell finds every <cmd>.
15
+ #
16
+ # EXIT
17
+ # 3 The shell cannot find a <cmd>
18
+ _require_cmd() {
19
+ lib_rc_missing=""
20
+ for lib_rc_cmd in "$@"; do
21
+ command -v "$lib_rc_cmd" >/dev/null 2>&1 \
22
+ || lib_rc_missing="${lib_rc_missing:+$lib_rc_missing, }$lib_rc_cmd"
23
+ done
24
+
25
+ [ -n "$lib_rc_missing" ] \
26
+ && _fail 3 -v missing "$lib_rc_missing" "Required commands not found"
27
+
28
+ return 0
29
+ }
30
+
31
+ # _require_var <NAME>...
32
+ #
33
+ # End the script unless every <NAME> is set and non-empty.
34
+ #
35
+ # EXIT
36
+ # 2 A <NAME> is unset or empty
37
+ # 3 A <NAME> is not an identifier
38
+ _require_var() {
39
+ lib_rv_missing=""
40
+ for lib_rv_name in "$@"; do
41
+ _is identifier "$lib_rv_name" \
42
+ || _fail 3 -v name "$lib_rv_name" "_require_var takes variable names"
43
+
44
+ eval "lib_rv_value=\${$lib_rv_name:-}"
45
+ [ -z "$lib_rv_value" ] \
46
+ && lib_rv_missing="${lib_rv_missing:+$lib_rv_missing, }$lib_rv_name"
47
+ done
48
+
49
+ [ -n "$lib_rv_missing" ] \
50
+ && _fail 2 -v missing "$lib_rv_missing" "Required variables not set"
51
+
52
+ return 0
53
+ }
54
+
55
+ # _require_display
56
+ #
57
+ # End the script unless WAYLAND_DISPLAY or DISPLAY is set. Without a
58
+ # display, rofi exits 1, the same status as on Esc.
59
+ #
60
+ # EXIT
61
+ # 3 WAYLAND_DISPLAY and DISPLAY are unset or empty
62
+ _require_display() {
63
+ [ -n "${WAYLAND_DISPLAY:-}${DISPLAY:-}" ] || _fail 3 "No Wayland or X display"
64
+ }
@@ -0,0 +1,26 @@
1
+ #!/bin/sh
2
+
3
+ # NAME
4
+ # help.sh - Print the man page of a script
5
+ #
6
+ # SYNOPSIS
7
+ # . "$_SCRIPT_DIR/../lib/help.sh"
8
+ #
9
+ # DESCRIPTION
10
+ # Needs log.sh. A script sources it before this file.
11
+
12
+ # _show_help <script-file>
13
+ #
14
+ # Print the man page of <script-file>: the comment block that starts at its
15
+ # NAME line.
16
+ #
17
+ # EXIT
18
+ # 2 <script-file> is not a file
19
+ _show_help() {
20
+ if [ ! -f "$1" ]; then
21
+ _fail 2 -v path "$1" "Script not found"
22
+ fi
23
+
24
+ sed -n '/^# NAME$/,/^$/p' "$1" \
25
+ | sed -e 's/^# \{0,1\}//'
26
+ }
@@ -0,0 +1,178 @@
1
+ #!/bin/sh
2
+
3
+ # NAME
4
+ # log.sh - Print log lines to stderr, and end the script on a failure
5
+ #
6
+ # SYNOPSIS
7
+ # . "$_SCRIPT_DIR/../lib/log.sh"
8
+ #
9
+ # export LOG_NAMESPACE="my-script"
10
+ # export LOG_LEVEL="${LOG_LEVEL:-info}"
11
+ #
12
+ # DESCRIPTION
13
+ # Needs no other file of bin/lib.
14
+ #
15
+ # ENVIRONMENT
16
+ # LOG_LEVEL Last level to print, in the order error < warning < info
17
+ # < debug, in any case or as a short form: err, warn, wrn,
18
+ # inf, dbg (default: info). An unknown level ends the script
19
+ # with 2 at its first _log call
20
+ # LOG_NAMESPACE Label printed before the message
21
+ # NO_LOG_TIME Hides the time when set and non-empty
22
+ # NO_COLOR Disables color when set and non-empty
23
+ # TERM Disables color when unset, empty or "dumb"
24
+
25
+ # _level_rank <level>
26
+ #
27
+ # Print the rank of <level>, from 0 for error to 3 for debug. Read <level> in
28
+ # any case, as a name or a short form: error/err, warning/warn/wrn, info/inf,
29
+ # debug/dbg.
30
+ #
31
+ # RETURNS
32
+ # 1 <level> is not a level or a short form. Prints the reason to stderr
33
+ _level_rank() {
34
+ case $(printf '%s' "$1" | tr '[:upper:]' '[:lower:]') in
35
+ error | err) printf '0' ;;
36
+ warning | warn | wrn) printf '1' ;;
37
+ info | inf) printf '2' ;;
38
+ debug | dbg) printf '3' ;;
39
+ *)
40
+ # Print with printf: _log calls _level_rank, so an unknown LOG_LEVEL
41
+ # would loop
42
+ printf 'log.sh/_level_rank: Unknown log level "%s". Use error, warning, info or debug\n' \
43
+ "$1" >&2
44
+ return 1
45
+ ;;
46
+ esac
47
+ }
48
+
49
+ # _has_color [fd]
50
+ #
51
+ # Tell if <fd> can show color. <fd> defaults to 2, stderr.
52
+ #
53
+ # RETURNS
54
+ # 0 <fd> is a terminal, NO_COLOR is unset or empty, TERM is set and not
55
+ # "dumb"
56
+ # 1 One of the three does not hold
57
+ # 2 <fd> is not a number
58
+ _has_color() {
59
+ [ -t "${1:-2}" ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-dumb}" != "dumb" ]
60
+ }
61
+
62
+ # _log <type> [-v <key> <val>]... [--] <message>...
63
+ #
64
+ # Print one line of <type> to stderr: error, warning, success, info or debug.
65
+ # Print nothing when the level of <type> comes after LOG_LEVEL in this order:
66
+ # error < warning < info < debug. success has no level, so LOG_LEVEL never
67
+ # hides a success line.
68
+ #
69
+ # Read <type> in any case, as a name or a short form: err, warn, wrn, suc,
70
+ # succ, inf, dbg. -v has the long form --var. -- ends the pairs, so a message
71
+ # that starts with -v prints as written.
72
+ #
73
+ # Data always goes through %s. Only the format string holds escapes, so a
74
+ # value containing a backslash prints as written.
75
+ #
76
+ # Newlines inside a <val> print as ",", so a list with one item per line
77
+ # reads "a,b,c". <message> prints as written.
78
+ #
79
+ # EXIT
80
+ # 2 <type> is not a type or a short form, or LOG_LEVEL is not a level or a
81
+ # short form
82
+ _log() {
83
+ case $(printf '%s' "$1" | tr '[:upper:]' '[:lower:]') in
84
+ error | err) lib_l_level=error lib_l_label="ERR" lib_l_color=31 ;;
85
+ warning | warn | wrn) lib_l_level=warning lib_l_label="WRN" lib_l_color=33 ;;
86
+ success | suc | succ) lib_l_level="" lib_l_label="SUC" lib_l_color=32 ;;
87
+ info | inf) lib_l_level=info lib_l_label="INF" lib_l_color=34 ;;
88
+ debug | dbg) lib_l_level=debug lib_l_label="DBG" lib_l_color=90 ;;
89
+ *)
90
+ printf 'log.sh/_log: Unknown log type "%s". Use error, warning, success, info or debug\n' \
91
+ "$1" >&2
92
+ exit 2
93
+ ;;
94
+ esac
95
+ shift
96
+
97
+ lib_l_cutoff=$(_level_rank "${LOG_LEVEL:-info}") || exit 2
98
+
99
+ # The success type has no level, so LOG_LEVEL never hides a success line
100
+ if [ -n "$lib_l_level" ]; then
101
+ lib_l_rank=$(_level_rank "$lib_l_level")
102
+ [ "$lib_l_rank" -le "$lib_l_cutoff" ] || return 0
103
+ fi
104
+
105
+ if _has_color; then
106
+ [ -z "${NO_LOG_TIME:-}" ] && printf '\033[90m%s\033[0m ' "$(date +%T)" >&2
107
+ printf '\033[1;%sm%s\033[0m ' "$lib_l_color" "$lib_l_label" >&2
108
+ [ -n "${LOG_NAMESPACE:-}" ] \
109
+ && printf '\033[90m%s:\033[0m ' "$LOG_NAMESPACE" >&2
110
+ else
111
+ [ -z "${NO_LOG_TIME:-}" ] && printf '%s ' "$(date +%T)" >&2
112
+ printf '%s ' "$lib_l_label" >&2
113
+ [ -n "${LOG_NAMESPACE:-}" ] && printf '%s: ' "$LOG_NAMESPACE" >&2
114
+ fi
115
+
116
+ # Collect the pairs here and print them after the message.
117
+ lib_l_pairs=""
118
+ while [ "$#" -gt 1 ]; do
119
+ case "$1" in
120
+ -v | --var)
121
+ [ "$#" -lt 3 ] && break
122
+
123
+ lib_l_value=$(printf '%s' "$3" | tr '\n' ',')
124
+ if _has_color; then
125
+ lib_l_pairs="${lib_l_pairs}$(printf ' \033[90m%s=\033[0m%s' "$2" "$lib_l_value")"
126
+ else
127
+ lib_l_pairs="${lib_l_pairs}$(printf ' %s=%s' "$2" "$lib_l_value")"
128
+ fi
129
+ shift 3
130
+ ;;
131
+ --)
132
+ shift
133
+ break
134
+ ;;
135
+ *) break ;;
136
+ esac
137
+ done
138
+
139
+ # The positionals left are the message. Join them with a space, as echo
140
+ # does.
141
+ printf '%s' "$*" >&2
142
+ [ -n "$lib_l_pairs" ] && printf '%s' "$lib_l_pairs" >&2
143
+ printf '\n' >&2
144
+
145
+ return 0
146
+ }
147
+
148
+ # _debug, _info, _ok, _warn, _error [-v <key> <val>]... [--] <message>...
149
+ #
150
+ # Call _log with one type: debug, info, success, warning, error.
151
+ #
152
+ # EXIT
153
+ # 2 LOG_LEVEL is not a level or a short form
154
+ _debug() { _log debug "$@"; }
155
+ _info() { _log info "$@"; }
156
+ _ok() { _log success "$@"; }
157
+ _warn() { _log warning "$@"; }
158
+ _error() { _log error "$@"; }
159
+
160
+ # _fail <code> [-v <key> <val>]... [--] <message>...
161
+ #
162
+ # Print an error line, then exit with <code>.
163
+ #
164
+ # EXIT
165
+ # <code> <code> is 1 to 125
166
+ # 2 <code> is not 1 to 125, or LOG_LEVEL is not a level or a short form
167
+ _fail() {
168
+ lib_f_code=$(printf '%d' "${1:-}" 2>/dev/null) || lib_f_code=0
169
+
170
+ if [ "$lib_f_code" -lt 1 ] || [ "$lib_f_code" -gt 125 ]; then
171
+ _log error -v got "${1:-}" "_fail needs an exit code of 1 to 125"
172
+ exit 2
173
+ fi
174
+
175
+ shift
176
+ _log error "$@"
177
+ exit "$lib_f_code"
178
+ }