deictic 0.7.0 → 0.8.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 (60) hide show
  1. package/API.md +21 -2
  2. package/README.md +2 -1
  3. package/dist/bun.js +1 -1
  4. package/dist/client/index.d.ts +1 -1
  5. package/dist/client/index.d.ts.map +1 -1
  6. package/dist/client/index.js +77 -51
  7. package/dist/client/index.js.map +1 -1
  8. package/dist/index.d.ts +2 -2
  9. package/dist/next.js +1 -1
  10. package/dist/recommended/index.d.ts +1 -1
  11. package/dist/recordings/client.js +8 -2
  12. package/dist/recordings/client.js.map +1 -1
  13. package/dist/shared/endpoints.js +26 -6
  14. package/dist/shared/endpoints.js.map +1 -1
  15. package/dist/shared/index.d.ts +35 -3
  16. package/dist/shared/index.d.ts.map +1 -1
  17. package/dist/shared/index7.d.ts +22 -2
  18. package/dist/shared/index7.d.ts.map +1 -1
  19. package/dist/shared/index9.d.ts +1 -1
  20. package/dist/shared/integration.d.ts +1 -1
  21. package/dist/shared/integration.js +59 -5
  22. package/dist/shared/integration.js.map +1 -1
  23. package/dist/shared/package.js +23 -0
  24. package/dist/shared/package.js.map +1 -0
  25. package/dist/shared/packages.js +14 -23
  26. package/dist/shared/packages.js.map +1 -1
  27. package/dist/shared/playwright.js +11 -3
  28. package/dist/shared/playwright.js.map +1 -1
  29. package/dist/shared/src.js.map +1 -1
  30. package/dist/shared/task.js +12 -3
  31. package/dist/shared/task.js.map +1 -1
  32. package/dist/shared/tasks.js +52 -4
  33. package/dist/shared/tasks.js.map +1 -1
  34. package/dist/shared/watch.js +2 -1
  35. package/dist/shared/watch.js.map +1 -1
  36. package/dist/tailwind/index.js +7 -2
  37. package/dist/tailwind/index.js.map +1 -1
  38. package/dist/tasks/client.d.ts.map +1 -1
  39. package/dist/tasks/client.js +50 -9
  40. package/dist/tasks/client.js.map +1 -1
  41. package/dist/tasks/index.d.ts +2 -2
  42. package/dist/webpack.js +1 -1
  43. package/package.json +2 -1
  44. package/src/cli/task.ts +20 -4
  45. package/src/cli/watch.ts +3 -1
  46. package/src/client/index.ts +76 -43
  47. package/src/index.ts +35 -2
  48. package/src/plugins/recordings/client.ts +8 -2
  49. package/src/plugins/screenshots/playwright.ts +8 -3
  50. package/src/plugins/tailwind/index.ts +3 -2
  51. package/src/plugins/tasks/client.ts +13 -3
  52. package/src/plugins/tasks/composer.ts +1 -1
  53. package/src/plugins/tasks/drawer.ts +3 -3
  54. package/src/plugins/tasks/index.ts +81 -3
  55. package/src/plugins/tasks/pins.ts +12 -7
  56. package/src/plugins/tasks/which.ts +34 -0
  57. package/src/tools/endpoints.ts +31 -5
  58. package/src/tools/packages.ts +12 -4
  59. package/src/tools/plugins.ts +18 -4
  60. package/src/tools/problems.ts +60 -0
@@ -17,7 +17,17 @@ import { join, relative } from 'node:path';
17
17
 
18
18
  import type { Board } from '@vercel/board';
19
19
 
20
- import { definePlugin, type Point, Refusal, type ServerContext, type Shortcut, type Source, type Target } from '#src/index.ts';
20
+ import manifest from '#package.json' with { type: 'json' };
21
+ import {
22
+ definePlugin,
23
+ type Point,
24
+ type Problem,
25
+ Refusal,
26
+ type ServerContext,
27
+ type Shortcut,
28
+ type Source,
29
+ type Target,
30
+ } from '#src/index.ts';
21
31
  // For `use('screenshots')`'s type: the plugin may not be loaded, and nothing of it runs here.
22
32
  import type { Recordings } from '#src/plugins/recordings/index.ts';
23
33
  import type { Box } from '#src/plugins/screenshots/index.ts';
@@ -65,6 +75,12 @@ export interface TasksOptions {
65
75
  html?: boolean;
66
76
  /** What opens its tab directly, beside deictic's own shortcut, which opens any: none by default. */
67
77
  shortcut?: Shortcut;
78
+ /**
79
+ * Whether deictic's own problems become tasks, for whoever works the board to fix: a package the project hasn't
80
+ * installed, a plugin that failed, a request that broke. One task per problem while it's open (`deictic.problem`
81
+ * is its key), about the project, not an element. True by default.
82
+ */
83
+ problems?: boolean;
68
84
  }
69
85
 
70
86
  export interface Task {
@@ -84,12 +100,23 @@ export interface Task {
84
100
  dropped?: string;
85
101
  /** The spot in the element it's about, where it's about one: CSS pixels from the element's top left. */
86
102
  point?: Point;
103
+ /** Which of the elements from its place it's about, where the page had more than one. */
104
+ which?: Which;
87
105
  /**
88
106
  * Where it was added, where that isn't the element's place: where the element was before it moved.
89
107
  */
90
108
  at?: Place;
91
109
  }
92
110
 
111
+ /**
112
+ * One of several elements from the same place in the source (a list's items): its place among them on the page, from 1,
113
+ * and its text where it has any, which still finds it once the list is in another order.
114
+ */
115
+ export interface Which {
116
+ nth: number;
117
+ text?: string;
118
+ }
119
+
93
120
  /** A place in the element's file: a line and a column, from 1, and the tag there as the task saw it. */
94
121
  export interface Place {
95
122
  line: number;
@@ -110,6 +137,8 @@ export interface Listed extends Task {
110
137
  last?: string;
111
138
  /** What the board says of where it stands: why it's blocked, what it came to, why it failed. */
112
139
  said?: string;
140
+ /** A problem of deictic's it tells of, not a person's ask: the project's setup to fix, or a bug of deictic's. */
141
+ problem?: Problem['kind'];
113
142
  }
114
143
 
115
144
  /** A task with a place, as its mark has it: what the page pins to the element there. */
@@ -194,6 +223,8 @@ export interface Op {
194
223
  pointed?: { path: string; html: string };
195
224
  /** How many elements on the page come from the same place in the source. */
196
225
  count: number;
226
+ /** Which of them it is, where there's more than one: what the page tells it from the others by. */
227
+ which?: Which;
197
228
  /**
198
229
  * The other elements it's about, after the one it's sent on, in the order they were attached: each one's place,
199
230
  * its tag, and its HTML as it rendered, where that's attached.
@@ -301,9 +332,14 @@ const OVER = 10;
301
332
  /** How many tasks a page is asked about at most: past that it has more pins than anyone reads. */
302
333
  const PINNED = 500;
303
334
  const AT = 'deictic.at';
335
+ /** A problem's key, on the task that tells of it: one task per problem while it's open. */
336
+ const PROBLEM = 'deictic.problem';
304
337
  const FILE = 'deictic.file';
305
338
  /** `x,y`: the spot in the element a task is about, in CSS pixels from the element's top left. */
306
339
  const POINT = 'deictic.point';
340
+ /** Which of several elements from one place it's about: `deictic.nth` from 1, `deictic.text` its text. */
341
+ const NTH = 'deictic.nth';
342
+ const TEXT = 'deictic.text';
307
343
  /** What parts what was typed from the rest of a task's text. */
308
344
  const REST = '\n---\n';
309
345
  /** A file for the board, as the plugins that keep them read it. */
@@ -363,7 +399,8 @@ export function tasks(options: TasksOptions = {}) {
363
399
  * nothing else on the board is its to change, nor a task someone has taken up.
364
400
  */
365
401
  /** One of deictic's: it has a place, or the page it's about where no element was attached. */
366
- const ours = (message: Message) => message.labels[AT] !== undefined || message.labels['deictic.page'] !== undefined;
402
+ const ours = (message: Message) =>
403
+ message.labels[AT] !== undefined || message.labels['deictic.page'] !== undefined || message.labels[PROBLEM] !== undefined;
367
404
  const openTask = async (team: TaskBoard, id: string, verb: string) => {
368
405
  const { message } = await team.thread(id);
369
406
  if (message.channel !== channel || !ours(message))
@@ -413,6 +450,9 @@ export function tasks(options: TasksOptions = {}) {
413
450
  const dropped = missing.length === 0 ? undefined : listOf(missing);
414
451
  const [, x, y] = /^(-?\d+),(-?\d+)$/.exec(message.labels[POINT] ?? '') ?? [];
415
452
  const point = x === undefined || y === undefined ? undefined : { x: Number(x), y: Number(y) };
453
+ const nth = Number(message.labels[NTH]);
454
+ const said = message.labels[TEXT];
455
+ const which = Number.isInteger(nth) && nth > 0 ? { nth, ...(said === undefined ? {} : { text: said }) } : undefined;
416
456
  return {
417
457
  id: message.id,
418
458
  title: text.split('\n', 1)[0]!,
@@ -423,6 +463,7 @@ export function tasks(options: TasksOptions = {}) {
423
463
  ...(to === undefined ? {} : { link: to }),
424
464
  ...(dropped === undefined ? {} : { dropped }),
425
465
  ...(point === undefined ? {} : { point }),
466
+ ...(which === undefined ? {} : { which }),
426
467
  };
427
468
  };
428
469
  const listedOf = (
@@ -432,6 +473,7 @@ export function tasks(options: TasksOptions = {}) {
432
473
  ): Listed => {
433
474
  const { task, labels } = message;
434
475
  const said = task?.blocked?.reason ?? task?.result ?? task?.reason ?? undefined;
476
+ const problem = labels[`${PROBLEM}.kind`];
435
477
  return {
436
478
  ...taskOf(message, actors, page),
437
479
  ...(labels[AT] === undefined ? {} : { place: labels[AT] }),
@@ -440,6 +482,7 @@ export function tasks(options: TasksOptions = {}) {
440
482
  replies,
441
483
  ...(last === null ? {} : { last: last.id }),
442
484
  ...(said === undefined || said === '' ? {} : { said: clip(said, 2000) }),
485
+ ...(problem === 'setup' || problem === 'bug' ? { problem } : {}),
443
486
  };
444
487
  };
445
488
  /**
@@ -532,7 +575,7 @@ export function tasks(options: TasksOptions = {}) {
532
575
  });
533
576
  };
534
577
 
535
- // Kept from `setup`, to `use` another plugin when an op asks for it. // Kept from `setup`, to `use` another plugin when an op asks for it.
578
+ // Kept from `setup`, to `use` another plugin when an op asks for it.
536
579
  let context: ServerContext | undefined;
537
580
  let project = options.project;
538
581
  /**
@@ -741,9 +784,42 @@ export function tasks(options: TasksOptions = {}) {
741
784
  return record;
742
785
  }),
743
786
  describe: (op) => `task "${clip(wordsOf(op).split('\n', 1)[0]!, 60)}"`,
787
+ // A task, unless one is open for it already: a dev server started again finds the same problem again.
788
+ problem: async (problem) => {
789
+ if (options.problems === false) return;
790
+ const key = clip(problem.key, LABEL);
791
+ const team = await boardOf();
792
+ if ((await team.tasks.list({ channel, label: { [PROBLEM]: key }, limit: 1 })).tasks.length > 0) return;
793
+ await create({
794
+ channel,
795
+ text: clip(problemText(problem), 10_000),
796
+ labels: {
797
+ ...options.labels,
798
+ ...(project === undefined ? {} : { 'deictic.project': clip(project, LABEL) }),
799
+ [PROBLEM]: key,
800
+ [`${PROBLEM}.kind`]: problem.kind,
801
+ ...(problem.plugin === undefined ? {} : { [`${PROBLEM}.plugin`]: clip(problem.plugin, LABEL) }),
802
+ 'deictic.version': manifest.version,
803
+ },
804
+ });
805
+ },
744
806
  });
745
807
  }
746
808
 
809
+ /**
810
+ * A problem as a task says it: what went wrong and what to do, and after the `---` its details, which `deictic task`
811
+ * prints and the drawer leaves out.
812
+ */
813
+ function problemText({ kind, message, details, plugin }: Problem): string {
814
+ const whose = plugin === undefined ? 'deictic' : `deictic's ${plugin} plugin`;
815
+ const todo =
816
+ kind === 'setup'
817
+ ? `Reported by ${whose}, about the project's setup: fix it as it says, then restart the dev server and check it's gone.`
818
+ : `A bug in ${whose} (${manifest.version}), not in this project: don't change it where it's installed. Tell the person, with the details, so they can report it (deictic's own at ${manifest.bugs.url}), and complete the task.`;
819
+ const more = details === undefined ? '' : `${REST}\`\`\`\n${details}\n\`\`\``;
820
+ return `${message[0]!.toUpperCase()}${message.slice(1)}\n\n${todo}${more}`;
821
+ }
822
+
747
823
  /**
748
824
  * The channel's page on a board `vb serve` runs on this machine, which serves its pages beside its API:
749
825
  * `/api/board` there, `/c/<channel>` beside it. Nothing for any other board.
@@ -872,6 +948,8 @@ function labelsOf(source: Source | Refusal, target: Target, op: Op): Record<stri
872
948
  ...page,
873
949
  'deictic.count': String(op.count),
874
950
  ...(op.point === undefined ? {} : { [POINT]: `${Math.round(op.point.x)},${Math.round(op.point.y)}` }),
951
+ ...(op.which === undefined ? {} : { [NTH]: String(op.which.nth) }),
952
+ ...(op.which?.text === undefined ? {} : { [TEXT]: clip(op.which.text, LABEL) }),
875
953
  ...sourceOf(op.chain ?? []),
876
954
  };
877
955
  }
@@ -8,12 +8,13 @@
8
8
  * In a layer of the overlay's root, which the tab's own styles don't reach once it's closed: so its styles are its own.
9
9
  */
10
10
  import { named, tell } from '#src/client/component.ts';
11
- import { div, place, reduced } from '#src/client/dom.ts';
11
+ import { div, place, reduced, sameAs } from '#src/client/dom.ts';
12
12
  import type { ClientContext, Point } from '#src/index.ts';
13
13
 
14
14
  import { drawerOf } from './drawer.ts';
15
15
  import type { Placed } from './index.ts';
16
16
  import { OVER, storeOf } from './store.ts';
17
+ import { elementOf } from './which.ts';
17
18
 
18
19
  const STYLE = `
19
20
  /* No box of its own, which its pins would stack in: each is under the popup, and the one pointed at over it, with its card. */
@@ -136,8 +137,6 @@ export function pinsIn(context: ClientContext): Pins {
136
137
  const pins = new Map<string, Pin>();
137
138
  let frames = 0;
138
139
 
139
- const elementAt = (at: string) => document.querySelector(`[data-deictic="${CSS.escape(at)}"]`);
140
-
141
140
  const pinOf = (at: string, el: Element, spot: Point | undefined): Pin => {
142
141
  const box = div('pin');
143
142
  const badge = Object.assign(document.createElement('button'), { type: 'button', className: 'pin-badge' });
@@ -181,7 +180,12 @@ export function pinsIn(context: ClientContext): Pins {
181
180
  if (layer === undefined) return;
182
181
  const tasks = store.tasks().filter((t): t is Placed => t.place !== undefined && !OVER.has(t.state));
183
182
  // One for an element's tasks, and one of its own for each spot a task is about.
184
- const keyOf = (t: Placed) => (t.point === undefined ? t.place : `${t.place}@${t.point.x},${t.point.y}`);
183
+ // And one for each of several elements from one place, as the page has them now.
184
+ const keyOf = (t: Placed) => {
185
+ const el = elementOf(t);
186
+ const of = el === null ? t.place : `${t.place}#${sameAs(el).indexOf(el)}`;
187
+ return t.point === undefined ? of : `${of}@${t.point.x},${t.point.y}`;
188
+ };
185
189
  const byPlace = new Map<string, Placed[]>();
186
190
  for (const t of tasks) byPlace.set(keyOf(t), [...(byPlace.get(keyOf(t)) ?? []), t]);
187
191
  for (const [at, pin] of pins) {
@@ -192,8 +196,9 @@ export function pinsIn(context: ClientContext): Pins {
192
196
  for (const [at, list] of byPlace) {
193
197
  let pin = pins.get(at);
194
198
  if (pin === undefined) {
195
- const [{ place: where, point }] = list as [Placed, ...Placed[]];
196
- const el = elementAt(where);
199
+ const [first] = list as [Placed, ...Placed[]];
200
+ const { place: where, point } = first;
201
+ const el = elementOf(first);
197
202
  if (el === null) continue;
198
203
  pin = pinOf(where, el, point);
199
204
  pins.set(at, pin);
@@ -211,7 +216,7 @@ export function pinsIn(context: ClientContext): Pins {
211
216
  // An element rendered again is another: looked for now and then, not every frame.
212
217
  const look = frames++ % 20 === 0;
213
218
  for (const pin of pins.values()) {
214
- if (!pin.el.isConnected && look) pin.el = elementAt(pin.place) ?? pin.el;
219
+ if (!pin.el.isConnected && look && pin.tasks[0] !== undefined) pin.el = elementOf(pin.tasks[0]) ?? pin.el;
215
220
  const whole = pin.el.getBoundingClientRect();
216
221
  // A spot's is a box of no size at the spot: nothing to outline, and the badge's point is on it.
217
222
  const r = pin.at === undefined ? whole : new DOMRect(whole.left + pin.at.x, whole.top + pin.at.y, 0, 0);
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Which element a task is about, where one place in the source renders several (a list's items, a `map`): the mark
3
+ * alone finds the first of them, so a task keeps its place among them and its text, and is found again by those.
4
+ */
5
+ import { ATTRIBUTE } from '#src/client/dom.ts';
6
+
7
+ import type { Task, Which } from './index.ts';
8
+
9
+ /** As much of an element's text as tells it from its siblings, and as a label holds. */
10
+ const SAID = 120;
11
+
12
+ const textOf = (el: Element) => (el.textContent ?? '').replace(/\s+/g, ' ').trim().slice(0, SAID);
13
+
14
+ /** Which of `alike` (the elements with its mark, in the page's order) `el` is; its text as `said` has it, which may be masked. */
15
+ export function whichOf(el: Element, alike: Element[], said: Element = el): Which {
16
+ const text = textOf(said);
17
+ return { nth: alike.indexOf(el) + 1, ...(text === '' ? {} : { text }) };
18
+ }
19
+
20
+ /**
21
+ * The element a task is about on this page: the only one with its mark, or the one of several with its text, else the
22
+ * one at its place among them. None where it was one of more than the page has now and its text is on none of them.
23
+ */
24
+ export function elementOf(task: Pick<Task, 'which'> & { place?: string }): Element | null {
25
+ if (task.place === undefined) return null;
26
+ const alike = [...document.querySelectorAll(`[${ATTRIBUTE}="${CSS.escape(task.place)}"]`)];
27
+ const { which } = task;
28
+ if (which === undefined || alike.length <= 1) return alike[0] ?? null;
29
+ const at = alike[which.nth - 1];
30
+ const said = which.text === undefined ? [] : alike.filter((el) => textOf(el) === which.text);
31
+ // Text that's on several (`Learn more`) is told apart by the place among them, where that's one of them.
32
+ if (said.length > 0) return at !== undefined && said.includes(at) ? at : said[0]!;
33
+ return at ?? null;
34
+ }
@@ -8,6 +8,8 @@
8
8
  *
9
9
  * Everything but `open` and `history` writes or may, so only the page itself may send it: a POST of
10
10
  * JSON from the same origin, which a form or a script on another site can't send without asking first.
11
+ * `problem` is the page's way to report one (`Problem`): it and the server's own, and any request that fails with
12
+ * something other than a `Refusal`, are said once and handed to the plugins that hear of them (`problems.ts`).
11
13
  *
12
14
  * Each plugin's own `routes` are under `plugins/<name>/`. A `GET` there reaches the plugin as it
13
15
  * comes; anything else only from the page's own origin, in whatever type it sends (an image, say).
@@ -18,8 +20,9 @@ import type { IncomingMessage, ServerResponse } from 'node:http';
18
20
  import { type Loc, Refusal, type ServerPlugin, type Source, type Target } from '#src/index.ts';
19
21
 
20
22
  import { History, type Scope } from './history.ts';
21
- import { missingMessage } from './packages.ts';
23
+ import { missingProblem } from './packages.ts';
22
24
  import { setUp } from './plugins.ts';
25
+ import { messageOf, problemIn, reporter, stackOf } from './problems.ts';
23
26
  import { applyEdits, resolveFile, sourceAt } from './source.ts';
24
27
 
25
28
  export interface EndpointOptions {
@@ -47,10 +50,12 @@ const scopeIn = ({ plugin, target }: Partial<Body>) =>
47
50
  export function endpoints({ root, plugins, base = '/', open, history = new History(root, { plugins }) }: EndpointOptions) {
48
51
  const byName = new Map(plugins.map((p) => [p.name, p]));
49
52
  const prefix = `${base}__deictic/`;
53
+ // What goes wrong that nothing in the page caused: said, and handed to the plugins that hear of it once set up.
54
+ const report = reporter(plugins, () => ready);
50
55
  // Started now, awaited by every request: a plugin's API is there before anything asks for it.
51
- const ready = setUp(root, plugins);
52
- const gone = missingMessage(root, plugins);
53
- if (gone !== undefined) console.warn(gone);
56
+ const ready = setUp(root, plugins, report);
57
+ const gone = missingProblem(root, plugins);
58
+ if (gone !== undefined) report(gone);
54
59
 
55
60
  const pluginOf = (name: string) => {
56
61
  const plugin = byName.get(name);
@@ -105,10 +110,15 @@ export function endpoints({ root, plugins, base = '/', open, history = new Histo
105
110
  const route = url.pathname.slice(prefix.length);
106
111
  const origin = req.headers.get('origin');
107
112
  const sameOrigin = origin !== null && new URL(origin).host === url.host;
113
+ // What failed, for a problem's key: a plugin's route by its first segment, not the IDs after it.
114
+ let where = route;
115
+ let plugin: string | undefined;
108
116
  try {
109
117
  const context = await ready;
110
118
  if (route.startsWith('plugins/')) {
111
119
  const [name = '', first = ''] = route.slice('plugins/'.length).split('/');
120
+ where = `plugins/${name}/${first}`;
121
+ plugin = name;
112
122
  const handle = byName.get(name)?.routes?.[first];
113
123
  if (handle === undefined) return new Response(`no endpoint ${url.pathname}`, { status: 404 });
114
124
  if (req.method !== 'GET' && req.method !== 'HEAD' && !sameOrigin)
@@ -126,13 +136,29 @@ export function endpoints({ root, plugins, base = '/', open, history = new Histo
126
136
  }
127
137
  if (route === 'open') return await open(resolveFile(root, loc.file), loc);
128
138
  const handler = routes[route];
129
- if (handler === undefined) return new Response(`no endpoint ${url.pathname}`, { status: 404 });
139
+ if (handler === undefined && route !== 'problem') return new Response(`no endpoint ${url.pathname}`, { status: 404 });
130
140
  if (req.method !== 'POST' || !sameOrigin || req.headers.get('content-type')?.startsWith('application/json') !== true)
131
141
  return Response.json({ problem: 'edits come from the page, as JSON' }, { status: 403 });
142
+ if (handler === undefined) {
143
+ const problem = problemIn(await req.json());
144
+ if (problem === undefined) return Response.json({ problem: 'a problem has a key, a kind and a message' }, { status: 400 });
145
+ report(problem);
146
+ return Response.json({});
147
+ }
132
148
  const body = (await req.json()) as Body;
149
+ plugin = body.plugin;
133
150
  return Response.json(await (route === 'read' ? handler(body) : serially(() => handler(body))));
134
151
  } catch (e) {
135
152
  if (e instanceof Refusal) return Response.json({ problem: e.message }, { status: 422 });
153
+ // Not a refusal, which says what to do instead: something broke, which no one in the page can fix.
154
+ const message = messageOf(e);
155
+ report({
156
+ key: `error:${where}:${message.split('\n', 1)[0]!.slice(0, 200)}`,
157
+ kind: 'bug',
158
+ ...(plugin === undefined ? {} : { plugin }),
159
+ message: `${prefix}${where} failed: ${message}`,
160
+ ...stackOf(e),
161
+ });
136
162
  return Response.json({ problem: e instanceof Error ? (e.stack ?? e.message) : String(e) }, { status: 500 });
137
163
  }
138
164
  };
@@ -8,7 +8,7 @@ import { createRequire } from 'node:module';
8
8
  import { dirname, join } from 'node:path';
9
9
 
10
10
  import manifest from '#package.json' with { type: 'json' };
11
- import type { ServerPlugin } from '#src/index.ts';
11
+ import type { Problem, ServerPlugin } from '#src/index.ts';
12
12
 
13
13
  export type PackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun';
14
14
 
@@ -99,12 +99,20 @@ export function missing(root: string, plugins: readonly ServerPlugin[]): { plugi
99
99
  });
100
100
  }
101
101
 
102
- /** What to say as the dev server starts when plugins' packages are missing: which, and the command that installs them. */
103
- export function missingMessage(root: string, plugins: readonly ServerPlugin[]): string | undefined {
102
+ /**
103
+ * What to report as the dev server starts when plugins' packages are missing: which, and the command that installs
104
+ * them. Its key names them, so a problem fixed and then another missing is a new one.
105
+ */
106
+ export function missingProblem(root: string, plugins: readonly ServerPlugin[]): Problem | undefined {
104
107
  const gone = missing(root, plugins);
105
108
  if (gone.length === 0) return undefined;
106
109
  const names = [...new Set(gone.flatMap((g) => g.packages))];
107
110
  const which = gone.map((g) => g.plugin);
108
111
  const who = which.length === 1 ? which[0] : `${which.slice(0, -1).join(', ')} and ${which.at(-1)}`;
109
- return `deictic: install what ${who} ${which.length === 1 ? 'needs' : 'need'}: ${installCommand(packageManager(root), names, root)}`;
112
+ const need = which.length === 1 ? 'needs' : 'need';
113
+ return {
114
+ key: `packages:${[...names].sort().join(',')}`,
115
+ kind: 'setup',
116
+ message: `install what ${who} ${need}, then restart the dev server: ${installCommand(packageManager(root), names, root)}`,
117
+ };
110
118
  }
@@ -5,6 +5,8 @@
5
5
  import type { ServerContext, ServerPlugin } from '#src/index.ts';
6
6
  import { checkShortcuts, OWN, ownShortcut, type Shortcut } from '#src/shortcut.ts';
7
7
 
8
+ import { messageOf, type Report, stackOf } from './problems.ts';
9
+
8
10
  const NAME = /^[a-z][a-z0-9-]*$/;
9
11
 
10
12
  /** Throws what to fix in a set of plugins: names, what they need, and their shortcuts, beside deictic's own. */
@@ -27,14 +29,26 @@ export function checkPlugins(plugins: readonly ServerPlugin[], own?: Shortcut |
27
29
  /**
28
30
  * Runs every plugin's `setup`, all at once, and answers the context they share: `use` reads an API
29
31
  * when it's asked for, so setups may `use` each other's only after they've all run, from a route or an
30
- * op, never in `setup` itself.
32
+ * op, never in `setup` itself. One that fails is reported and has no API: the others go on, so one of them
33
+ * can tell of it (tasks, on the board).
31
34
  */
32
- export async function setUp(root: string, plugins: readonly ServerPlugin[]): Promise<ServerContext> {
35
+ export async function setUp(root: string, plugins: readonly ServerPlugin[], report: Report): Promise<ServerContext> {
33
36
  const apis = new Map<string, unknown>();
34
- const context: ServerContext = { root, use: (name) => apis.get(name) as never };
37
+ const context: ServerContext = { root, use: (name) => apis.get(name) as never, report };
35
38
  await Promise.all(
36
39
  plugins.map(async (p) => {
37
- if (p.setup !== undefined) apis.set(p.name, await p.setup(context));
40
+ if (p.setup === undefined) return;
41
+ try {
42
+ apis.set(p.name, await p.setup(context));
43
+ } catch (e) {
44
+ report({
45
+ key: `setup:${p.name}`,
46
+ kind: 'bug',
47
+ plugin: p.name,
48
+ message: `${p.name} failed to set up, so it does nothing until the dev server restarts: ${messageOf(e)}`,
49
+ ...stackOf(e),
50
+ });
51
+ }
38
52
  }),
39
53
  );
40
54
  return context;
@@ -0,0 +1,60 @@
1
+ /**
2
+ * deictic's problems on the server, and those the page sends (`/__deictic/problem`): each said once in the dev server's
3
+ * output, and handed to every plugin with a `problem` once they're all set up, which the tasks plugin posts to the
4
+ * board. Once per key while the dev server runs: a request that fails on every try is still one problem.
5
+ */
6
+ import type { Problem, ServerPlugin } from '#src/index.ts';
7
+
8
+ /** How much of a problem is kept: its words, and its details (a stack is shorter). */
9
+ const WORDS = 2000;
10
+ const DETAILS = 20_000;
11
+
12
+ export type Report = (problem: Problem) => void;
13
+
14
+ /** What reports a problem: `ready` is when the plugins that hear of them are set up to. */
15
+ export function reporter(plugins: readonly ServerPlugin[], ready: () => Promise<unknown>): Report {
16
+ const said = new Set<string>();
17
+ return (problem) => {
18
+ if (said.has(problem.key)) return;
19
+ said.add(problem.key);
20
+ console.warn(`deictic${problem.plugin === undefined ? '' : `/${problem.plugin}`}: ${problem.message}`);
21
+ const hearing = plugins.filter((p) => p.problem !== undefined);
22
+ if (hearing.length === 0) return;
23
+ // Its own failure is said, not reported: a board that can't be reached would report itself for ever.
24
+ const tell = async (plugin: ServerPlugin) => {
25
+ try {
26
+ await plugin.problem?.(problem);
27
+ } catch (e) {
28
+ console.warn(`deictic: ${plugin.name} couldn't take that problem, as ${messageOf(e)}`);
29
+ }
30
+ };
31
+ // Later, never now: a setup that fails at once reports before what it's part of is ready to be waited for.
32
+ void Promise.resolve()
33
+ .then(ready)
34
+ .then(
35
+ () => Promise.all(hearing.map(tell)),
36
+ () => {},
37
+ );
38
+ };
39
+ }
40
+
41
+ /** What went wrong, as a problem's words say it: an error's message. */
42
+ export const messageOf = (e: unknown) => (e instanceof Error ? e.message : String(e));
43
+
44
+ /** An error's stack, where it has one, for a problem's details. */
45
+ export const stackOf = (e: unknown) => (e instanceof Error && e.stack !== undefined ? { details: e.stack } : {});
46
+
47
+ /** A problem as the page sent it, checked and cut short, or nothing where it isn't one. */
48
+ export function problemIn(value: unknown): Problem | undefined {
49
+ if (typeof value !== 'object' || value === null) return undefined;
50
+ const { key, kind, message, details, plugin } = value as Record<string, unknown>;
51
+ if (typeof key !== 'string' || key === '' || typeof message !== 'string' || message === '') return undefined;
52
+ if (kind !== 'setup' && kind !== 'bug') return undefined;
53
+ return {
54
+ key: key.slice(0, WORDS),
55
+ kind,
56
+ message: message.slice(0, WORDS),
57
+ ...(typeof details === 'string' && details !== '' ? { details: details.slice(0, DETAILS) } : {}),
58
+ ...(typeof plugin === 'string' && plugin !== '' ? { plugin: plugin.slice(0, 100) } : {}),
59
+ };
60
+ }