@elumixor/devkit 0.12.1 → 0.12.2

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 CHANGED
@@ -82,7 +82,7 @@ at the top, the merged log below.
82
82
  ── all ─────────────────────────────────────────────────────────────────────────────
83
83
  web:frontend │ VITE v8.3.0 ready in 1055 ms
84
84
  web:backend │ [nitro] ✔ Nitro Server built in 521ms
85
- 1-2 focus a all o open r restart c clear jk ud gg G scroll v select y copy q quit
85
+ 1-2 focus a all o open r restart c clear jk ud gg G scroll v select y copy e errors q quit
86
86
  ```
87
87
 
88
88
  The log scrolls like `less`: `j`/`k` or arrows by a line, `u`/`d` by half a screen, PgUp/PgDn by a
@@ -91,6 +91,10 @@ row; move it with the same keys and `y` copies it to the clipboard, `y` alone co
91
91
  Dragging with the mouse selects too, scrolling on its own while the pointer is above or below the log,
92
92
  and copies on release; the wheel scrolls. Hold Option (macOS) or Shift for the terminal's own selection.
93
93
 
94
+ A printed error with a stack folds into one red summary row (its message, plus a database `detail`
95
+ when there is one). Click `[copy]` to copy the whole error, or the `[▸ N lines]` button to expand it;
96
+ `e` expands or folds every error.
97
+
94
98
  A crashed process stays on the board until you restart it with `r`; the rest keep running. Anywhere
95
99
  else (an agent's shell, CI, a pipe, or `--plain`) it prints prefixed lines instead: the URLs first,
96
100
  then a `devkit` line for every ready, crash and exit, and one `all ready:` line listing every URL.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elumixor/devkit",
3
- "version": "0.12.1",
3
+ "version": "0.12.2",
4
4
  "description": "Dev orchestrator for bun monorepos — free ports, run apps side-by-side, open the browser when ready",
5
5
  "author": "elumixor",
6
6
  "license": "ISC",
package/src/dev/board.tsx CHANGED
@@ -5,8 +5,9 @@ import { useEffect, useMemo, useRef, useState, useSyncExternalStore } from "reac
5
5
  import wrapAnsi from "wrap-ansi";
6
6
  import { stripColor } from "../ansi.ts";
7
7
  import { copyToClipboard } from "./clipboard.ts";
8
+ import { type ErrorBlock, foldErrors, type ShownEntry } from "./errors.ts";
8
9
  import type { Service, ServiceState } from "./service.ts";
9
- import { DEVKIT, type LogEntry, type Supervisor } from "./supervisor.ts";
10
+ import { DEVKIT, type Supervisor } from "./supervisor.ts";
10
11
 
11
12
  const SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
12
13
  const FRAME_MS = 80;
@@ -28,6 +29,10 @@ const WHEEL_DOWN = 65;
28
29
  /** Button presses, drags and releases, reported as SGR coordinates. */
29
30
  const MOUSE_ON = "\x1b[?1002h\x1b[?1006h";
30
31
  const MOUSE_OFF = "\x1b[?1002l\x1b[?1006l";
32
+ /** The buttons ahead of an error's summary, and the columns they take after the log's gutter. */
33
+ const COPY_BUTTON = "[copy]";
34
+ const toggleButton = (error: ErrorBlock & { expanded: boolean }) =>
35
+ `[${error.expanded ? "▾" : "▸"} ${error.lines} lines]`;
31
36
 
32
37
  const MARK: Record<ServiceState, { glyph: string; color: string }> = {
33
38
  starting: { glyph: "", color: "yellow" },
@@ -60,6 +65,8 @@ function Board({ supervisor, title }: { readonly supervisor: Supervisor; readonl
60
65
  const frame = useFrame(supervisor.services.some((s) => s.state === "starting" || s.state === "stopping"));
61
66
 
62
67
  const [focus, setFocus] = useState<number | null>(null);
68
+ /** `seq`s of the errors shown whole instead of as a summary. */
69
+ const [expanded, setExpanded] = useState<ReadonlySet<number>>(new Set());
63
70
  /** `seq` the view ends before while scrolled back; `null` follows the newest line. */
64
71
  const [end, setEnd] = useState<number | null>(null);
65
72
  const [quitting, setQuitting] = useState(false);
@@ -95,8 +102,9 @@ function Board({ supervisor, title }: { readonly supervisor: Supervisor; readonl
95
102
  const textWidth = Math.max(MIN_LOG_WIDTH, cols - labelWidth - 3);
96
103
  const halfPage = Math.max(1, Math.floor(logRows / 2));
97
104
 
98
- const shown = supervisor.entries.filter(
99
- (entry) => focus == null || entry.source === focus || entry.source === DEVKIT,
105
+ const shown = foldErrors(
106
+ supervisor.entries.filter((entry) => focus == null || entry.source === focus || entry.source === DEVKIT),
107
+ expanded,
100
108
  );
101
109
  const endIndex = end == null ? shown.length : countBefore(shown, end);
102
110
  const who = (source: number) =>
@@ -144,10 +152,27 @@ function Board({ supervisor, title }: { readonly supervisor: Supervisor; readonl
144
152
  if (resumeFollow.current) setEnd(null);
145
153
  };
146
154
 
147
- const yank = (entries: readonly LogEntry[]) => {
148
- if (entries.length === 0) return;
149
- copyToClipboard(entries.map((entry) => stripColor(entry.text)).join("\n"), stdout);
150
- setFlash(`copied ${entries.length} line${entries.length === 1 ? "" : "s"}`);
155
+ const yank = (entries: readonly ShownEntry[]) => {
156
+ // An expanded error's lines follow its summary, so the summary itself adds nothing.
157
+ const texts = entries.flatMap((entry) =>
158
+ entry.error ? (entry.error.expanded ? [] : [entry.error.text]) : [stripColor(entry.text)],
159
+ );
160
+ if (texts.length === 0) return;
161
+ copyToClipboard(texts.join("\n"), stdout);
162
+ const count = texts.join("\n").split("\n").length;
163
+ setFlash(`copied ${count} line${count === 1 ? "" : "s"}`);
164
+ };
165
+
166
+ const toggleError = (seq: number) => {
167
+ const next = new Set(expanded);
168
+ if (!next.delete(seq)) next.add(seq);
169
+ setExpanded(next);
170
+ };
171
+
172
+ /** Expands every error unless all already are, then folds them all. */
173
+ const toggleErrors = () => {
174
+ const errors = shown.flatMap((entry) => (entry.error ? [entry.error] : []));
175
+ setExpanded(errors.every((error) => error.expanded) ? new Set() : new Set(errors.map((error) => error.seq)));
151
176
  };
152
177
 
153
178
  const yankSelection = () => {
@@ -166,7 +191,7 @@ function Board({ supervisor, title }: { readonly supervisor: Supervisor; readonl
166
191
  void supervisor.stop().then(() => exit());
167
192
  };
168
193
 
169
- const onMouse = ({ button, row, release }: MouseEvent) => {
194
+ const onMouse = ({ button, row, col, release }: MouseEvent) => {
170
195
  const at = row - logTop;
171
196
  if (button === WHEEL_UP || button === WHEEL_DOWN) {
172
197
  const by = button === WHEEL_UP ? -WHEEL_LINES : WHEEL_LINES;
@@ -179,6 +204,15 @@ function Board({ supervisor, title }: { readonly supervisor: Supervisor; readonl
179
204
  if (button === LEFT) {
180
205
  const line = lines[at];
181
206
  if (!line) return;
207
+ const x = col - 1 - (labelWidth + 3);
208
+ if (line.error && !visual) {
209
+ if (x >= 0 && x < COPY_BUTTON.length) {
210
+ copyToClipboard(line.error.text, stdout);
211
+ return setFlash(`copied error (${line.error.lines} lines)`);
212
+ }
213
+ const toggleAt = COPY_BUTTON.length + 1;
214
+ if (x >= toggleAt && x < toggleAt + toggleButton(line.error).length) return toggleError(line.error.seq);
215
+ }
182
216
  startVisual(line.seq);
183
217
  return setDragging(true);
184
218
  }
@@ -223,6 +257,7 @@ function Board({ supervisor, title }: { readonly supervisor: Supervisor; readonl
223
257
  return last && startVisual(last.seq);
224
258
  }
225
259
  if (input === "y") return yank(shown.slice(firstVisible(shown, endIndex, logRows, textWidth), endIndex));
260
+ if (input === "e") return toggleErrors();
226
261
  const digit = Number(input);
227
262
  if (Number.isInteger(digit) && digit >= 1 && digit <= services.length) {
228
263
  setFocus(focus === digit - 1 ? null : digit - 1);
@@ -287,10 +322,21 @@ function Board({ supervisor, title }: { readonly supervisor: Supervisor; readonl
287
322
  <Text dimColor> │ </Text>
288
323
  )}
289
324
  {/* Inverse on the plain text: a child's own colour resets would cut the highlight short. */}
290
- {mark ? (
325
+ {line.error && !mark ? (
326
+ <Text wrap="truncate-end">
327
+ <Text inverse>{COPY_BUTTON}</Text> <Text inverse>{toggleButton(line.error)}</Text>{" "}
328
+ <Text color="red" bold>
329
+ {line.error.summary}
330
+ </Text>
331
+ </Text>
332
+ ) : mark ? (
291
333
  <Text inverse wrap="truncate-end">
292
334
  {stripColor(line.text) || " "}
293
335
  </Text>
336
+ ) : line.failed ? (
337
+ <Text color="red" wrap="truncate-end">
338
+ {stripColor(line.text)}
339
+ </Text>
294
340
  ) : (
295
341
  <Text wrap="truncate-end">{line.text}</Text>
296
342
  )}
@@ -389,6 +435,7 @@ function Footer({
389
435
  ["jk ud gg G", "scroll"],
390
436
  ["v", "select"],
391
437
  ["y", "copy"],
438
+ ["e", "errors"],
392
439
  ["q", "quit"],
393
440
  ]
394
441
  : [
@@ -420,11 +467,13 @@ interface LogLine {
420
467
  readonly text: string;
421
468
  /** Only an entry's first wrapped row carries its label. */
422
469
  readonly first: boolean;
470
+ readonly error?: ShownEntry["error"];
471
+ readonly failed?: boolean;
423
472
  }
424
473
 
425
474
  /** The last `count` screen rows of `entries[0, endIndex)`, wrapped to `width`. */
426
475
  function tail(
427
- entries: readonly LogEntry[],
476
+ entries: readonly ShownEntry[],
428
477
  endIndex: number,
429
478
  count: number,
430
479
  width: number,
@@ -432,57 +481,60 @@ function tail(
432
481
  ): LogLine[] {
433
482
  const out: LogLine[] = [];
434
483
  for (let i = endIndex - 1; i >= 0 && out.length < count; i--) {
435
- const entry = entries[i] as LogEntry;
436
- const rows = wrapAnsi(entry.text, width, { hard: true, trim: false }).split("\n");
484
+ const entry = entries[i] as ShownEntry;
485
+ const rows = entry.error ? [entry.text] : wrapAnsi(entry.text, width, { hard: true, trim: false }).split("\n");
437
486
  for (let row = rows.length - 1; row >= 0 && out.length < count; row--)
438
487
  out.push({
439
488
  key: `${entry.seq}:${row}`,
440
489
  seq: entry.seq,
441
490
  text: rows[row] ?? "",
442
491
  first: row === 0,
492
+ error: entry.error,
493
+ failed: entry.failed,
443
494
  ...who(entry.source),
444
495
  });
445
496
  }
446
497
  return out.reverse();
447
498
  }
448
499
 
449
- const heightOf = (entry: LogEntry, width: number) =>
450
- wrapAnsi(entry.text, width, { hard: true, trim: false }).split("\n").length;
500
+ /** An error's summary is one row, cut short rather than wrapped. */
501
+ const heightOf = (entry: ShownEntry, width: number) =>
502
+ entry.error ? 1 : wrapAnsi(entry.text, width, { hard: true, trim: false }).split("\n").length;
451
503
 
452
504
  /** The fewest leading entries that fill the log — scrolling back further would only show blank rows. */
453
- function firstFullScreen(entries: readonly LogEntry[], rows: number, width: number) {
505
+ function firstFullScreen(entries: readonly ShownEntry[], rows: number, width: number) {
454
506
  let filled = 0;
455
507
  for (let i = 0; i < entries.length; i++) {
456
- filled += heightOf(entries[i] as LogEntry, width);
508
+ filled += heightOf(entries[i] as ShownEntry, width);
457
509
  if (filled >= rows) return i + 1;
458
510
  }
459
511
  return entries.length;
460
512
  }
461
513
 
462
514
  /** The first entry `entries[0, endIndex)` shows whole, so a cursor there needs no scrolling. */
463
- function firstVisible(entries: readonly LogEntry[], endIndex: number, rows: number, width: number) {
515
+ function firstVisible(entries: readonly ShownEntry[], endIndex: number, rows: number, width: number) {
464
516
  let filled = 0;
465
517
  let i = endIndex;
466
- while (i > 0 && filled < rows) filled += heightOf(entries[--i] as LogEntry, width);
518
+ while (i > 0 && filled < rows) filled += heightOf(entries[--i] as ShownEntry, width);
467
519
  return Math.min(Math.max(0, endIndex - 1), filled > rows ? i + 1 : i);
468
520
  }
469
521
 
470
522
  /** Where a view must end to show `entries[start]` on its top row. */
471
- function endFor(entries: readonly LogEntry[], start: number, rows: number, width: number) {
523
+ function endFor(entries: readonly ShownEntry[], start: number, rows: number, width: number) {
472
524
  let filled = 0;
473
525
  let i = start;
474
- while (i < entries.length && filled + heightOf(entries[i] as LogEntry, width) <= rows)
475
- filled += heightOf(entries[i++] as LogEntry, width);
526
+ while (i < entries.length && filled + heightOf(entries[i] as ShownEntry, width) <= rows)
527
+ filled += heightOf(entries[i++] as ShownEntry, width);
476
528
  return Math.max(i, start + 1);
477
529
  }
478
530
 
479
531
  /** How many entries come before `seq` — entries are in `seq` order. */
480
- function countBefore(entries: readonly LogEntry[], seq: number) {
532
+ function countBefore(entries: readonly ShownEntry[], seq: number) {
481
533
  let low = 0;
482
534
  let high = entries.length;
483
535
  while (low < high) {
484
536
  const mid = (low + high) >> 1;
485
- if ((entries[mid] as LogEntry).seq < seq) low = mid + 1;
537
+ if ((entries[mid] as ShownEntry).seq < seq) low = mid + 1;
486
538
  else high = mid;
487
539
  }
488
540
  return low;
@@ -497,16 +549,17 @@ function openTarget(services: readonly Service[], focus: number | null) {
497
549
 
498
550
  interface MouseEvent {
499
551
  readonly button: number;
500
- /** 1-based screen row. */
552
+ /** 1-based screen row and column. */
501
553
  readonly row: number;
554
+ readonly col: number;
502
555
  readonly release: boolean;
503
556
  }
504
557
 
505
558
  /** Ink hands an SGR mouse report to `useInput` whole, minus its escape. */
506
559
  function parseMouse(input: string): MouseEvent | undefined {
507
- const match = /^\[<(\d+);\d+;(\d+)([Mm])$/.exec(input);
560
+ const match = /^\[<(\d+);(\d+);(\d+)([Mm])$/.exec(input);
508
561
  if (!match) return undefined;
509
- return { button: Number(match[1]), row: Number(match[2]), release: match[3] === "m" };
562
+ return { button: Number(match[1]), col: Number(match[2]), row: Number(match[3]), release: match[4] === "m" };
510
563
  }
511
564
 
512
565
  /**
@@ -0,0 +1,87 @@
1
+ import { stripColor } from "../ansi.ts";
2
+ import type { LogEntry } from "./supervisor.ts";
3
+
4
+ const FRAME = /^\s+at\s/;
5
+ const MORE = /^\s+\.\.\. \d+ (?:lines|more)/;
6
+ /** A line that reports a failure on its own, without a stack: `error: …`, `TypeError: …`. */
7
+ const FAILURE = /^\s*(?:\[[^\]]*\]\s*)?(?:error|\w*Error|\w*Exception)\b:?\s/i;
8
+ const DETAIL = /^\s*detail:\s*(['"`])(.*)\1,?$/;
9
+
10
+ /** A thrown error a service printed: its first line, its stack, and the properties Node dumps after it. */
11
+ export interface ErrorBlock {
12
+ /** `seq` of the error's first line. */
13
+ readonly seq: number;
14
+ /** The first line, plus a database driver's `detail` when it says what clashed. */
15
+ readonly summary: string;
16
+ readonly text: string;
17
+ readonly lines: number;
18
+ }
19
+
20
+ /** A log row: a printed line, or the one-line summary standing in for an error. */
21
+ export interface ShownEntry extends LogEntry {
22
+ readonly error?: ErrorBlock & { readonly expanded: boolean };
23
+ /** A line of an expanded error, or one that reports a failure by itself. */
24
+ readonly failed?: boolean;
25
+ }
26
+
27
+ /**
28
+ * Folds every printed error into a summary row ahead of its lines, which stay only when `expanded`
29
+ * holds its `seq`. A line followed by a stack frame starts an error; frames, `... 3 more` and the
30
+ * `{ … }` property dump continue it. Each service's lines are folded apart, so interleaving is fine.
31
+ */
32
+ export function foldErrors(entries: readonly LogEntry[], expanded: ReadonlySet<number>): ShownEntry[] {
33
+ const members = new Map<number, LogEntry[]>();
34
+ const open = new Map<number, { lines: LogEntry[]; depth: number }>();
35
+ const last = new Map<number, LogEntry>();
36
+ for (const entry of entries) {
37
+ const plain = stripColor(entry.text);
38
+ const block = open.get(entry.source);
39
+ if (block && (block.depth > 0 || FRAME.test(plain) || MORE.test(plain))) {
40
+ block.lines.push(entry);
41
+ block.depth = Math.max(0, block.depth + depthChange(plain));
42
+ } else {
43
+ open.delete(entry.source);
44
+ const before = last.get(entry.source);
45
+ const beforePlain = before && stripColor(before.text);
46
+ if (before && beforePlain?.trim() && !FRAME.test(beforePlain) && FRAME.test(plain)) {
47
+ const lines = [before, entry];
48
+ members.set(before.seq, lines);
49
+ open.set(entry.source, { lines, depth: Math.max(0, depthChange(plain)) });
50
+ }
51
+ }
52
+ last.set(entry.source, entry);
53
+ }
54
+
55
+ const hidden = new Set<number>();
56
+ const member = new Set<number>();
57
+ const starts = new Map<number, ShownEntry>();
58
+ for (const [seq, lines] of members) {
59
+ const text = lines.map((line) => stripColor(line.text)).join("\n");
60
+ const detail = lines.map((line) => DETAIL.exec(stripColor(line.text))?.[2]).find(Boolean);
61
+ const head = stripColor(lines[0]?.text ?? "").trim();
62
+ const isOpen = expanded.has(seq);
63
+ starts.set(seq, {
64
+ seq: seq - 0.5,
65
+ source: lines[0]?.source ?? 0,
66
+ text: head,
67
+ error: { seq, summary: detail ? `${head} — ${detail}` : head, text, lines: lines.length, expanded: isOpen },
68
+ });
69
+ for (const line of lines) (isOpen ? member : hidden).add(line.seq);
70
+ }
71
+
72
+ const out: ShownEntry[] = [];
73
+ for (const entry of entries) {
74
+ const start = starts.get(entry.seq);
75
+ if (start) out.push(start);
76
+ if (hidden.has(entry.seq)) continue;
77
+ const failed = member.has(entry.seq) || FAILURE.test(stripColor(entry.text));
78
+ out.push(failed ? { ...entry, failed } : entry);
79
+ }
80
+ return out;
81
+ }
82
+
83
+ /** Braces opened minus closed, outside quoted strings. */
84
+ function depthChange(line: string) {
85
+ const bare = line.replace(/'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"/g, "");
86
+ return (bare.match(/[{[]/g)?.length ?? 0) - (bare.match(/[}\]]/g)?.length ?? 0);
87
+ }