@asd14/vblocks 6.3.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.
- package/README.md +88 -48
- package/bin/vblocks +4 -0
- package/bin/vblocks.d/build +9 -0
- package/bin/vblocks.d/eject +6 -1
- package/bin/vblocks.d/init +9 -0
- package/bin/vblocks.d/lib/guard.sh +64 -0
- package/bin/vblocks.d/lib/help.sh +26 -0
- package/bin/vblocks.d/lib/log.sh +178 -0
- package/bin/vblocks.d/lib/validation.sh +101 -0
- package/bin/vblocks.d/serve +9 -0
- package/bin/vblocks.d/vblocks.sh +9 -9
- package/bin/vblocks.d/webpack.sh +2 -0
- package/defaults/webpack.config.js +1 -1
- package/package.json +3 -1
- package/template/_common/app/.env.example +5 -0
- package/template/_common/app/src/index.tsx +27 -0
- package/template/_common/post-init-app.sh +9 -0
- package/template/plain/app/src/index.app.tsx +3 -0
- package/template/xyflow/app/src/details.tsx +1 -1
- package/template/xyflow/app/src/diagram.tsx +1 -1
- package/template/xyflow/app/src/{index.tsx → index.app.tsx} +6 -14
- package/template/xyflow/app/src/index.css +1 -0
- package/template/xyflow/post-init-app.sh +2 -2
- package/template/xyflow-tabs/app/src/{index.tsx → index.app.tsx} +5 -15
- package/template/xyflow-tabs/app/src/index.css +1 -0
- package/template/xyflow-tabs/app/src/pages/build/details.tsx +1 -1
- package/template/xyflow-tabs/app/src/pages/build/diagram.tsx +2 -2
- package/template/xyflow-tabs/app/src/pages/serve/details.tsx +1 -1
- package/template/xyflow-tabs/app/src/pages/serve/diagram.tsx +2 -2
- package/template/xyflow-tabs/post-init-app.sh +2 -2
- package/bin/vblocks.d/lib.sh +0 -569
- 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
|
|
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
|
|
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
|
-
|
|
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
|
|
@@ -53,6 +59,7 @@ to write, read or fix
|
|
|
53
59
|
- [Commands](#commands)
|
|
54
60
|
- [`vblocks init`](#vblocks-init)
|
|
55
61
|
- [`vblocks serve`](#vblocks-serve)
|
|
62
|
+
- [Agentation](#agentation)
|
|
56
63
|
- [`vblocks build`](#vblocks-build)
|
|
57
64
|
- [`vblocks eject`](#vblocks-eject)
|
|
58
65
|
- [Templates](#templates)
|
|
@@ -101,12 +108,14 @@ scaffolders spots the folder at a glance, as with `vite-project` and
|
|
|
101
108
|
```text
|
|
102
109
|
my-vblocks-app/
|
|
103
110
|
├─ src/
|
|
104
|
-
│ ├─ index.tsx
|
|
111
|
+
│ ├─ index.tsx # mounts App, and the Agentation toolbar in development
|
|
112
|
+
│ ├─ index.app.tsx # App, your code starts here
|
|
105
113
|
│ ├─ index.html
|
|
106
114
|
│ └─ index.css
|
|
115
|
+
├─ .env.example
|
|
107
116
|
├─ .gitignore
|
|
108
117
|
├─ tsconfig.json
|
|
109
|
-
└─ package.json # react, react-dom and the vblocks scripts
|
|
118
|
+
└─ package.json # react, react-dom, agentation and the vblocks scripts
|
|
110
119
|
```
|
|
111
120
|
|
|
112
121
|
2. **Start a development server**
|
|
@@ -151,10 +160,12 @@ Several apps, eg. demos or MVP sketches, can share one `package.json` and one
|
|
|
151
160
|
sketches/
|
|
152
161
|
+ ├─ flow-diagram/
|
|
153
162
|
+ │ ├─ src/ # the files of the xyflow template
|
|
163
|
+
+ │ ├─ .env.example
|
|
154
164
|
+ │ ├─ postcss.config.js
|
|
155
165
|
+ │ └─ tsconfig.json
|
|
156
166
|
├─ .gitignore
|
|
157
|
-
! └─ package.json # + @xyflow/react, tailwindcss, @tailwindcss/postcss,
|
|
167
|
+
! └─ package.json # + @xyflow/react, tailwindcss, @tailwindcss/postcss,
|
|
168
|
+
! # cssnano, agentation
|
|
158
169
|
```
|
|
159
170
|
|
|
160
171
|
3. **Start a development server for one app**
|
|
@@ -175,7 +186,6 @@ the definitions in.
|
|
|
175
186
|
| **Plumbing** | The project files `init` writes: `package.json`, `.gitignore` |
|
|
176
187
|
| **Project folder** | The folder with `package.json` |
|
|
177
188
|
| **App folder** | The folder with `src/index.tsx` and `src/index.html` |
|
|
178
|
-
| **Bundle** | The files `build` writes to `dist/` |
|
|
179
189
|
|
|
180
190
|
## How to
|
|
181
191
|
|
|
@@ -186,7 +196,7 @@ early the Quick Start reader has the pain.
|
|
|
186
196
|
|
|
187
197
|
### Use environment variables
|
|
188
198
|
|
|
189
|
-
1. **
|
|
199
|
+
1. **Create a `.env` file in your app folder**
|
|
190
200
|
|
|
191
201
|
```env
|
|
192
202
|
API_URL=https://api.example.com
|
|
@@ -202,11 +212,11 @@ Webpack replaces each `process.env["NAME"]` with the value of `NAME`, and
|
|
|
202
212
|
`undefined` when `.env` does not set it. Webpack leaves out of the bundle every
|
|
203
213
|
variable your code does not read.
|
|
204
214
|
|
|
205
|
-
> [!WARNING]
|
|
215
|
+
> [!WARNING]\
|
|
206
216
|
> Never expose a secret, eg. an API key, through `process.env`. Anyone who loads
|
|
207
217
|
> the page can read it.
|
|
208
218
|
|
|
209
|
-
> [!NOTE]
|
|
219
|
+
> [!NOTE]\
|
|
210
220
|
> The [client types](types/client.d.ts) declare `process.env` and nothing else
|
|
211
221
|
> from Node, because Webpack replaces nothing else.
|
|
212
222
|
|
|
@@ -215,11 +225,11 @@ variable your code does not read.
|
|
|
215
225
|
VBlocks resolves `@self` to the `src/` of your app folder:
|
|
216
226
|
|
|
217
227
|
```tsx
|
|
218
|
-
// Instead of: import {
|
|
219
|
-
import {
|
|
228
|
+
// Instead of: import { Button } from "../../../components/button.tsx"
|
|
229
|
+
import { Button } from "@self/components/button.tsx"
|
|
220
230
|
```
|
|
221
231
|
|
|
222
|
-
> [!NOTE]
|
|
232
|
+
> [!NOTE]\
|
|
223
233
|
> VBlocks compiles TypeScript with SWC and does not typecheck. Run `tsc`
|
|
224
234
|
> yourself. Your `tsconfig.json` needs the same alias as Webpack.
|
|
225
235
|
> The `tsconfig.json` that [`vblocks init`](#vblocks-init) writes already has
|
|
@@ -257,20 +267,20 @@ import { Component } from "@self/components"
|
|
|
257
267
|
- **Class names** - a bare hash in `production` mode, `[local]__[hash]` in
|
|
258
268
|
`development` mode
|
|
259
269
|
|
|
260
|
-
> [!NOTE]
|
|
270
|
+
> [!NOTE]\
|
|
261
271
|
> The [client types](types/client.d.ts) declare `.css`, image and font imports.
|
|
262
272
|
|
|
263
273
|
### Import a file as text
|
|
264
274
|
|
|
265
|
-
Add `?raw` to the import path.
|
|
266
|
-
as Vite
|
|
275
|
+
Add `?raw` to the import path. The import returns the file's contents as a
|
|
276
|
+
string, as in Vite:
|
|
267
277
|
|
|
268
278
|
```tsx
|
|
269
279
|
import greeting from "./greeting.txt?raw"
|
|
270
280
|
// greeting => "Hello from a text file\n"
|
|
271
281
|
```
|
|
272
282
|
|
|
273
|
-
> [!NOTE]
|
|
283
|
+
> [!NOTE]\
|
|
274
284
|
> The [client types](types/client.d.ts) type each `?raw` import as a `string`.
|
|
275
285
|
|
|
276
286
|
### Replace a default config
|
|
@@ -278,23 +288,22 @@ import greeting from "./greeting.txt?raw"
|
|
|
278
288
|
Each config in your app folder replaces the default of the same name:
|
|
279
289
|
|
|
280
290
|
- `webpack.config.js`
|
|
281
|
-
- `postcss.config.js
|
|
291
|
+
- `postcss.config.js`, the default runs:
|
|
282
292
|
- `postcss-preset-env`, which compiles nesting, `oklch()` and `color-mix()`
|
|
283
293
|
for older browsers and adds vendor prefixes
|
|
284
294
|
- `cssnano` in `production`
|
|
285
|
-
- `.swcrc`
|
|
286
|
-
- browser targets `> 0.25%, not dead`
|
|
295
|
+
- `.swcrc`, the default targets `> 0.25%, not dead`
|
|
287
296
|
|
|
288
297
|
1. **[Eject](#vblocks-eject) the default into your app folder**
|
|
289
298
|
|
|
290
299
|
```sh
|
|
291
|
-
cd
|
|
300
|
+
cd my-vblocks-app
|
|
292
301
|
npx vblocks eject swc > .swcrc
|
|
293
302
|
```
|
|
294
303
|
|
|
295
304
|
2. **Update the ejected file**
|
|
296
305
|
|
|
297
|
-
> [!NOTE]
|
|
306
|
+
> [!NOTE]\
|
|
298
307
|
> VBlocks does not merge your config with the default, with two exceptions:
|
|
299
308
|
>
|
|
300
309
|
> - `.swcrc -> env.targets`: VBlocks adds the default when yours is missing
|
|
@@ -306,12 +315,12 @@ Each config in your app folder replaces the default of the same name:
|
|
|
306
315
|
VBlocks does not include Tailwind. Add it through a `postcss.config.js` in your
|
|
307
316
|
app folder:
|
|
308
317
|
|
|
309
|
-
> [!NOTE]
|
|
318
|
+
> [!NOTE]\
|
|
310
319
|
> The `xyflow` and `xyflow-tabs` templates of [`vblocks init`](#vblocks-init)
|
|
311
320
|
> have this setup.
|
|
312
321
|
|
|
313
|
-
1. **Install Tailwind,
|
|
314
|
-
|
|
322
|
+
1. **Install Tailwind, its PostCSS plugin and cssnano, which the ejected config
|
|
323
|
+
imports**
|
|
315
324
|
|
|
316
325
|
```sh
|
|
317
326
|
npm install --save-dev tailwindcss @tailwindcss/postcss cssnano
|
|
@@ -339,7 +348,7 @@ app folder:
|
|
|
339
348
|
})
|
|
340
349
|
```
|
|
341
350
|
|
|
342
|
-
> [!IMPORTANT]
|
|
351
|
+
> [!IMPORTANT]\
|
|
343
352
|
> Tailwind 4
|
|
344
353
|
> [compiles nesting and adds vendor prefixes](https://tailwindcss.com/docs/compatibility)
|
|
345
354
|
> itself. With both plugins, `postcss-preset-env` rewrites Tailwind's `@layer`
|
|
@@ -354,10 +363,9 @@ app folder:
|
|
|
354
363
|
`source(".")` limits Tailwind's class scan to the folder of the CSS file,
|
|
355
364
|
`src/`.
|
|
356
365
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
> 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
|
|
361
369
|
|
|
362
370
|
### Add VBlocks to an existing project
|
|
363
371
|
|
|
@@ -379,15 +387,16 @@ app folder:
|
|
|
379
387
|
}
|
|
380
388
|
```
|
|
381
389
|
|
|
382
|
-
> [!NOTE]
|
|
390
|
+
> [!NOTE]\
|
|
383
391
|
> An import may name the file tsc emits, eg. `./button.js` for `button.tsx`, as
|
|
384
|
-
> TypeScript's `nodenext` resolution
|
|
392
|
+
> TypeScript's `nodenext` resolution requires. VBlocks resolves it to
|
|
393
|
+
> `button.tsx`.
|
|
385
394
|
|
|
386
|
-
3. **
|
|
395
|
+
3. **Move `index.tsx` and `index.html` into `src/`**
|
|
387
396
|
|
|
388
|
-
> [!NOTE]
|
|
397
|
+
> [!NOTE]\
|
|
389
398
|
> VBlocks builds `dist/index.html` from `src/index.html` and adds the script and
|
|
390
|
-
> stylesheet tags to it.
|
|
399
|
+
> stylesheet tags to it. Remove the tag that loads your entry, eg. Vite's
|
|
391
400
|
> `<script type="module" src="/src/main.tsx"></script>`.
|
|
392
401
|
|
|
393
402
|
4. **Start a development server**
|
|
@@ -419,8 +428,8 @@ vblocks init <directory>
|
|
|
419
428
|
- `--only` (default: both)
|
|
420
429
|
- Write one part of the template
|
|
421
430
|
- `plumbing`: `package.json`, `.gitignore`
|
|
422
|
-
- `app`: `src/`, `tsconfig.json` and the template's configs.
|
|
423
|
-
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
|
|
424
433
|
[project folder](#vocabulary), the parent of `<directory>`. Without a
|
|
425
434
|
`package.json` there, `init` fails
|
|
426
435
|
|
|
@@ -429,6 +438,8 @@ vblocks init <directory>
|
|
|
429
438
|
Start the development server.
|
|
430
439
|
|
|
431
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`)
|
|
432
443
|
- Listens on `localhost`, so another device on your network, eg. a phone, cannot
|
|
433
444
|
open the page
|
|
434
445
|
- Opens the page in the browser
|
|
@@ -444,7 +455,7 @@ vblocks serve
|
|
|
444
455
|
|
|
445
456
|
- `--mode` (default: `development`)
|
|
446
457
|
- The Webpack mode
|
|
447
|
-
- React Refresh
|
|
458
|
+
- React Refresh and the Agentation toolbar run only in `development`
|
|
448
459
|
- `--prefix` (default: the current folder)
|
|
449
460
|
- The [app folder](#vocabulary), which must exist
|
|
450
461
|
- `--analyze`
|
|
@@ -456,6 +467,20 @@ vblocks serve
|
|
|
456
467
|
- `serve` passes each one to Webpack unchanged
|
|
457
468
|
- eg. `-- --host 0.0.0.0`, to listen on the network
|
|
458
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
|
+
|
|
459
484
|
#### `vblocks build`
|
|
460
485
|
|
|
461
486
|
Build the app into a bundle in the `dist/` of the app folder.
|
|
@@ -525,13 +550,28 @@ config.
|
|
|
525
550
|
|
|
526
551
|
### Templates
|
|
527
552
|
|
|
528
|
-
|
|
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
|
|
529
569
|
|
|
530
570
|
#### `plain`
|
|
531
571
|
|
|
532
|
-
"Hello World!" in `src/index.tsx`.
|
|
572
|
+
"Hello World!" in `src/index.app.tsx`.
|
|
533
573
|
|
|
534
|
-
- Adds no packages
|
|
574
|
+
- Adds no other packages
|
|
535
575
|
|
|
536
576
|
#### `xyflow`
|
|
537
577
|
|
|
@@ -543,7 +583,7 @@ One diagram, and a details panel for the selected node or edge.
|
|
|
543
583
|
|
|
544
584
|
```text
|
|
545
585
|
src/
|
|
546
|
-
├── index.tsx
|
|
586
|
+
├── index.app.tsx # the layout: header, diagram, details panel, the selected id
|
|
547
587
|
├── diagram.tsx # the nodes and edges, placed and sized in grid cells
|
|
548
588
|
├── details.tsx # the panel for the selected node or edge
|
|
549
589
|
└── utils/grid.ts # CELL_SIZE and toPixels
|
|
@@ -557,7 +597,7 @@ One tab per diagram, each with its own details panel.
|
|
|
557
597
|
|
|
558
598
|
```text
|
|
559
599
|
src/
|
|
560
|
-
├── index.tsx
|
|
600
|
+
├── index.app.tsx # the layout: header, tab bar, details panel, the selected id
|
|
561
601
|
├── pages/
|
|
562
602
|
│ ├── build/
|
|
563
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
|
|
package/bin/vblocks.d/build
CHANGED
|
@@ -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
|
package/bin/vblocks.d/eject
CHANGED
|
@@ -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=""
|
|
50
|
+
_EJECT_CONFIG="" # eject <config>
|
|
46
51
|
|
|
47
52
|
# ╭───
|
|
48
53
|
# │ Flags and positionals
|
package/bin/vblocks.d/init
CHANGED
|
@@ -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
|
+
}
|