@delurio/cli 0.9.0 → 0.11.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 (86) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/NOTICE +7 -0
  3. package/README.md +16 -15
  4. package/SPEC.md +37 -11
  5. package/js/changes.d.ts +17 -0
  6. package/js/cli.js +6 -0
  7. package/{dist → js}/edges.d.ts +2 -2
  8. package/js/edges.js +40 -0
  9. package/js/expr.js +25 -0
  10. package/js/graph.js +59 -0
  11. package/{dist → js}/html.d.ts +6 -6
  12. package/js/html.js +13 -0
  13. package/js/internal.js +408 -0
  14. package/js/kit.js +54 -0
  15. package/{dist → js}/model.d.ts +2 -2
  16. package/js/model.js +136 -0
  17. package/js/native.cjs +46 -0
  18. package/js/native.d.cts +322 -0
  19. package/{dist → js}/nav.d.ts +1 -1
  20. package/js/nav.js +70 -0
  21. package/{dist → js}/needs.d.ts +3 -2
  22. package/js/needs.js +30 -0
  23. package/{dist → js}/spine.d.ts +3 -3
  24. package/js/spine.js +22 -0
  25. package/{dist → js}/starts.d.ts +1 -3
  26. package/{dist → js}/story.d.ts +2 -2
  27. package/js/story.js +22 -0
  28. package/{dist → js}/wireframe.d.ts +4 -2
  29. package/js/wireframe.js +29 -0
  30. package/js/yaml.d.ts +20 -0
  31. package/js/yaml.js +40 -0
  32. package/package.json +41 -25
  33. package/schemas/domain.schema.json +3 -0
  34. package/dist/changes.d.ts +0 -25
  35. package/dist/changes.js +0 -101
  36. package/dist/check.d.ts +0 -23
  37. package/dist/check.js +0 -574
  38. package/dist/cli.d.ts +0 -2
  39. package/dist/cli.js +0 -194
  40. package/dist/compare.d.ts +0 -34
  41. package/dist/compare.js +0 -143
  42. package/dist/diff-flow.d.ts +0 -33
  43. package/dist/diff-flow.js +0 -547
  44. package/dist/diff-nav.d.ts +0 -20
  45. package/dist/diff-nav.js +0 -401
  46. package/dist/diff.d.ts +0 -25
  47. package/dist/diff.js +0 -331
  48. package/dist/domain-names.d.ts +0 -18
  49. package/dist/domain-names.js +0 -124
  50. package/dist/domain.d.ts +0 -25
  51. package/dist/domain.js +0 -110
  52. package/dist/edges.js +0 -120
  53. package/dist/expr.js +0 -157
  54. package/dist/graph.js +0 -236
  55. package/dist/html.js +0 -311
  56. package/dist/index-page.d.ts +0 -12
  57. package/dist/index-page.js +0 -50
  58. package/dist/invokes.d.ts +0 -10
  59. package/dist/invokes.js +0 -67
  60. package/dist/kit.js +0 -710
  61. package/dist/mermaid.d.ts +0 -3
  62. package/dist/mermaid.js +0 -50
  63. package/dist/model.js +0 -608
  64. package/dist/nav-page.d.ts +0 -15
  65. package/dist/nav-page.js +0 -283
  66. package/dist/nav.js +0 -578
  67. package/dist/needs.js +0 -311
  68. package/dist/pan.d.ts +0 -2
  69. package/dist/pan.js +0 -252
  70. package/dist/schemas.d.ts +0 -10
  71. package/dist/schemas.js +0 -266
  72. package/dist/spine.js +0 -156
  73. package/dist/starts.js +0 -88
  74. package/dist/story.js +0 -109
  75. package/dist/tutorial-check.d.ts +0 -9
  76. package/dist/tutorial-check.js +0 -297
  77. package/dist/ui-check.d.ts +0 -8
  78. package/dist/ui-check.js +0 -136
  79. package/dist/watch.d.ts +0 -5
  80. package/dist/watch.js +0 -114
  81. package/dist/wireframe.js +0 -273
  82. package/dist/yaml.d.ts +0 -19
  83. package/dist/yaml.js +0 -134
  84. /package/{dist → js}/expr.d.ts +0 -0
  85. /package/{dist → js}/graph.d.ts +0 -0
  86. /package/{dist → js}/kit.d.ts +0 -0
@@ -1,297 +0,0 @@
1
- // A flow's tutorial.yaml held to the flow and its ui (SPEC.md §7). First every name a tutorial uses: the flow it is
2
- // written against, the variables and values of its `scenario` and of each step's `then`, the states of `show`, `cut`
3
- // and `end`, the nodes of `press`, `reveal` and `focus`, the events of `after`, and each `{word}` its words fill. Then,
4
- // when every name is one, its steps are replayed on the flow from the scenario as the video plays them, and the first
5
- // step that no longer holds is where it stops: a tutorial that no longer matches the graph stops at that step.
6
- import { kindOf } from './edges.js';
7
- import { shownIn } from './graph.js';
8
- import { readYaml } from './yaml.js';
9
- const isMap = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
10
- const names = (v) => (Array.isArray(v) ? v : v === undefined ? [] : [v]);
11
- const shown = (v) => JSON.stringify(v);
12
- class Stop extends Error {
13
- path;
14
- constructor(path, message) {
15
- super(message);
16
- this.path = path;
17
- }
18
- }
19
- export function checkTutorial(g, m, ex, text) {
20
- let t;
21
- try {
22
- t = readYaml(text);
23
- }
24
- catch (err) {
25
- const at = err.linePos?.[0];
26
- return [{ path: [], line: at?.line ?? 1, column: at?.col ?? 1, message: `tutorial.yaml cannot be read: ${err.message.split('\n')[0]}` }];
27
- }
28
- if (!isMap(t) || t.kind !== 'tutorial')
29
- return [{ path: isMap(t) ? ['kind'] : [], message: 'tutorial.yaml is not a tutorial: it says kind: tutorial (SPEC.md §1, §7)' }];
30
- if (t.flow !== g.flow.id)
31
- return [{ path: ['flow'], message: `the tutorial is written against ${String(t.flow)}, and the flow beside it is ${g.flow.id} (SPEC.md §7)` }];
32
- const stage = t.stage ?? 'screen';
33
- if (stage !== 'screen' && stage !== 'diagram')
34
- return [{ path: ['stage'], message: `stage is screen or diagram, not ${String(stage)} (SPEC.md §7)` }];
35
- const steps = Array.isArray(t.steps) ? t.steps : [];
36
- const out = [];
37
- const flow = g.flow;
38
- const nodes = g.ui.nodes;
39
- const events = new Set(Object.values(flow.states).flatMap((st) => Object.keys(st.on ?? {})));
40
- const samples = new Set([...Object.keys(g.ui.samples), ...(isMap(t.samples) ? Object.keys(t.samples) : [])]);
41
- const problem = (path, message) => {
42
- out.push({ path, message });
43
- };
44
- const values = (where, given, what) => {
45
- if (given === undefined)
46
- return;
47
- if (!isMap(given))
48
- return problem(where, `${what} is a map of variables to values`);
49
- for (const [name, v] of Object.entries(given)) {
50
- const cv = flow.context[name];
51
- if (cv === undefined)
52
- problem([...where, name], `${what} names ${name}, which is not a variable of ${flow.id}`);
53
- else if (!cv.values.includes(v))
54
- problem([...where, name], `${what}: ${shown(v)} is not a value of ${name} (${cv.values.map(shown).join(', ')})`);
55
- }
56
- };
57
- // the start: a value of the scenario's for each variable that starts from any value, and the fixed one of the others
58
- values(['scenario'], t.scenario, 'scenario');
59
- const scenario = isMap(t.scenario) ? t.scenario : {};
60
- for (const v of m.vars) {
61
- const cv = flow.context[v];
62
- const given = scenario[v];
63
- if (cv.initial === 'any' && given === undefined)
64
- problem(['scenario'], `scenario gives no value for ${v}, and the flow starts from any value of it`);
65
- else if (cv.initial !== 'any' && given !== undefined && given !== cv.initial)
66
- problem(['scenario', v], `scenario gives ${v} = ${shown(given)}, and ${flow.id} always starts with ${v} = ${shown(cv.initial)}`);
67
- }
68
- const words = (where, s, step) => {
69
- if (typeof s !== 'string')
70
- return;
71
- for (const [, name] of s.matchAll(/\{(\w+)\}/g)) {
72
- if (samples.has(name))
73
- continue;
74
- const n = nodes[name];
75
- if (n === undefined)
76
- problem(where, `${step}{${name}} is neither a sample value nor a node of ui.yaml`);
77
- else if (n.text === undefined)
78
- problem(where, `${step}{${name}} names the node ${name}, which has no visible text to say`);
79
- }
80
- };
81
- words(['title'], t.title, 'title: ');
82
- steps.forEach((s0, i) => {
83
- if (!isMap(s0))
84
- return;
85
- const s = s0;
86
- const at = (...rest) => ['steps', String(i), ...rest];
87
- const step = `step ${i + 1}`;
88
- for (const key of ['show', 'cut', 'end']) {
89
- const v = s[key];
90
- if (typeof v === 'string' && flow.states[v] === undefined)
91
- problem(at(key), `${step}: ${key} ${v}, which is not a state of ${flow.id}`);
92
- }
93
- for (const key of ['press', 'reveal', 'focus']) {
94
- names(s[key]).forEach((id, j) => {
95
- const where = Array.isArray(s[key]) ? at(key, String(j)) : at(key);
96
- if (stage === 'diagram')
97
- problem(where, `${step}: ${key} points at a node, and a tutorial on the diagram has none (SPEC.md §7)`);
98
- else if (typeof id !== 'string' || nodes[id] === undefined)
99
- problem(where, `${step}: ${key} ${String(id)}, which is not a node of ui.yaml`);
100
- });
101
- }
102
- names(s.after).forEach((e, j) => {
103
- if (typeof e !== 'string' || !events.has(e))
104
- problem(Array.isArray(s.after) ? at('after', String(j)) : at('after'), `${step}: after ${String(e)}, which no state of ${flow.id} handles`);
105
- });
106
- values(at('then'), s.then, `${step}: then`);
107
- words(at('say'), s.say, `${step}: `);
108
- words(at('chapter'), s.chapter, `${step}: `);
109
- if (isMap(s.card)) {
110
- words(at('card', 'title'), s.card.title, `${step}: `);
111
- words(at('card', 'body'), s.card.body, `${step}: `);
112
- }
113
- });
114
- if (out.length > 0)
115
- return out;
116
- try {
117
- if (stage === 'diagram')
118
- replayDiagram(g, m, ex, steps, scenario);
119
- else
120
- replayScreens(g, m, ex, steps, scenario);
121
- }
122
- catch (err) {
123
- if (!(err instanceof Stop))
124
- throw err;
125
- return [{ path: err.path, message: err.message }];
126
- }
127
- return [];
128
- }
129
- function start(g, m, scenario) {
130
- return { state: g.flow.initial, ctx: m.vars.map((v) => (g.flow.context[v].initial === 'any' ? scenario[v] : g.flow.context[v].initial)) };
131
- }
132
- // the step's `then`: of the cases its transitions can take, the one with these values; without it, the first
133
- function thenOf(m, s) {
134
- return isMap(s.then) ? Object.entries(s.then).map(([name, v]) => [m.vars.indexOf(name), v]) : [];
135
- }
136
- const pick = (cases, then) => cases.find((c) => then.every(([i, v]) => c.ctx[i] === v)) ?? cases[0];
137
- function thenHolds(m, s, cur, path, step) {
138
- for (const [i, v] of thenOf(m, s)) {
139
- if (cur.ctx[i] !== v)
140
- throw new Stop([...path, 'then', m.vars[i]], `${step}: no case of this step has ${m.vars[i]} = ${shown(v)}`);
141
- }
142
- }
143
- function replayScreens(g, m, ex, steps, scenario) {
144
- const flow = g.flow;
145
- const nodes = g.ui.nodes;
146
- const hasScreen = (state) => Object.values(nodes).some((n) => shownIn(n, state));
147
- let then = [];
148
- const take = (cur, event, path, step) => {
149
- const hit = pick(m.steps(cur.state, cur.ctx).filter((x) => x.event === event), then);
150
- if (hit === undefined)
151
- throw new Stop(path, `${step}: the flow is in ${cur.state}, which does not handle ${event}`);
152
- return settle({ state: hit.target, ctx: hit.ctx }, path, step);
153
- };
154
- // a step with no screen passes on its own along its normal way (a transition with no event, then DONE); one with no
155
- // such way waits for an event the tutorial gives with `after`
156
- const settle = (cur, path, step) => {
157
- let c = cur;
158
- for (let hops = 0; hops < 50 && kindOf(g, c.state) === 'server'; hops++) {
159
- const ways = m.steps(c.state, c.ctx);
160
- const next = pick(ways.filter((x) => x.event === null || x.event === 'DONE'), then);
161
- if (next === undefined && ways.length > 0)
162
- return c;
163
- if (next === undefined)
164
- throw new Stop(path, `${step}: the flow stops in ${c.state}, which has no screen and no way on`);
165
- c = { state: next.target, ctx: next.ctx };
166
- }
167
- return c;
168
- };
169
- let cur = settle(start(g, m, scenario), ['scenario'], 'the start');
170
- steps.forEach((s0, i) => {
171
- if (!isMap(s0))
172
- return;
173
- const s = s0;
174
- const path = ['steps', String(i)];
175
- const step = `step ${i + 1}`;
176
- then = thenOf(m, s);
177
- if (s.intro === true) {
178
- names(s.after).forEach((e, j) => {
179
- cur = take(cur, e, Array.isArray(s.after) ? [...path, 'after', String(j)] : [...path, 'after'], step);
180
- });
181
- thenHolds(m, s, cur, path, step);
182
- return;
183
- }
184
- const reveal = [...names(s.reveal)];
185
- if (typeof s.press === 'string' && nodes[s.press].when !== undefined && !reveal.includes(s.press))
186
- reveal.push(s.press);
187
- let state = cur.state; // the state whose screen the step shows; null for a card
188
- if (typeof s.cut === 'string') {
189
- const wanted = s.cut;
190
- let hit;
191
- try {
192
- hit = ex.configs.find((c) => c.state === wanted && reveal.every((id) => nodes[id].when === undefined || m.test(nodes[id].when, c.state, c.ctx)));
193
- }
194
- catch (err) {
195
- throw new Stop([...path, 'reveal'], `${step}: ${err.message}`);
196
- }
197
- if (hit === undefined)
198
- throw new Stop([...path, 'cut'], `${step}: no situation the flow reaches is in ${wanted}${reveal.length > 0 ? ` and shows ${reveal.join(', ')}` : ''}`);
199
- cur = { state: hit.state, ctx: hit.ctx };
200
- state = cur.state;
201
- }
202
- else if (s.end !== undefined) {
203
- if (typeof s.end === 'string' && s.end !== cur.state)
204
- throw new Stop([...path, 'end'], `${step}: the tutorial ends in ${s.end}, and the flow is in ${cur.state} here`);
205
- if (flow.states[cur.state].type === 'final')
206
- state = null;
207
- else if (typeof s.end !== 'string')
208
- throw new Stop([...path, 'end'], `${step}: the flow is in ${cur.state} here, which is no exit`);
209
- if (isMap(s.card))
210
- state = null;
211
- }
212
- else if (typeof s.show === 'string') {
213
- if (s.show !== cur.state)
214
- throw new Stop([...path, 'show'], `${step}: shows ${s.show}, and the flow is in ${cur.state} here`);
215
- }
216
- else if (s.card !== undefined) {
217
- state = null;
218
- }
219
- if (state !== null && !hasScreen(state))
220
- throw new Stop([...path, typeof s.cut === 'string' ? 'cut' : typeof s.show === 'string' ? 'show' : 'end'], `${step}: ${state} has no screen to show`);
221
- for (const key of ['reveal', 'focus']) {
222
- names(s[key]).forEach((id, j) => {
223
- const where = Array.isArray(s[key]) ? [...path, key, String(j)] : [...path, key];
224
- const n = nodes[id];
225
- if (state === null)
226
- throw new Stop(where, `${step}: ${key} ${String(id)} on a card, which shows no node`);
227
- if (!shownIn(n, state))
228
- throw new Stop(where, `${step}: ${key} ${String(id)}, which ${state} does not show`);
229
- if (key === 'focus' && n.when !== undefined && !reveal.includes(id))
230
- throw new Stop(where, `${step}: ${String(id)} is shown only when ${n.when}: list it under reveal`);
231
- });
232
- }
233
- if (typeof s.press === 'string') {
234
- const n = nodes[s.press];
235
- const where = [...path, 'press'];
236
- if (!shownIn(n, cur.state))
237
- throw new Stop(where, `${step}: press ${s.press}, which ${cur.state} does not show`);
238
- if ((n.disabledIn ?? []).includes(cur.state))
239
- throw new Stop(where, `${step}: press ${s.press}, which is disabled in ${cur.state}`);
240
- if (n.emits === undefined)
241
- throw new Stop(where, `${step}: press ${s.press}, which sends no event`);
242
- cur = take(cur, n.emits, where, step);
243
- }
244
- names(s.after).forEach((e, j) => {
245
- cur = take(cur, e, Array.isArray(s.after) ? [...path, 'after', String(j)] : [...path, 'after'], step);
246
- });
247
- thenHolds(m, s, cur, path, step);
248
- });
249
- }
250
- // On the diagram no state passes on its own: `show` and `end` walk the flow to their state along its normal way (a
251
- // transition with no event, then DONE), and every other event is given with `after`
252
- function replayDiagram(g, m, ex, steps, scenario) {
253
- let cur = start(g, m, scenario);
254
- let then = [];
255
- const walkTo = (wanted, path, step) => {
256
- const from = cur.state;
257
- for (let hops = 0; cur.state !== wanted; hops++) {
258
- const next = hops < 50 ? pick(m.steps(cur.state, cur.ctx).filter((x) => x.event === null || x.event === 'DONE'), then) : undefined;
259
- if (next === undefined)
260
- throw new Stop(path, `${step}: ${wanted} is not reached from ${from} without an event${cur.state === from ? '' : ` (the way stops in ${cur.state})`}`);
261
- cur = { state: next.target, ctx: next.ctx };
262
- }
263
- };
264
- steps.forEach((s0, i) => {
265
- if (!isMap(s0))
266
- return;
267
- const s = s0;
268
- const path = ['steps', String(i)];
269
- const step = `step ${i + 1}`;
270
- then = thenOf(m, s);
271
- if (s.intro === true)
272
- return;
273
- if (typeof s.cut === 'string') {
274
- const wanted = s.cut;
275
- const hit = ex.configs.find((c) => c.state === wanted);
276
- if (hit === undefined)
277
- throw new Stop([...path, 'cut'], `${step}: no situation the flow reaches is in ${wanted}`);
278
- cur = { state: hit.state, ctx: hit.ctx };
279
- }
280
- else if (typeof s.end === 'string') {
281
- walkTo(s.end, [...path, 'end'], step);
282
- if (g.flow.states[s.end].type !== 'final')
283
- throw new Stop([...path, 'end'], `${step}: ends in ${s.end}, which is no exit`);
284
- }
285
- else if (typeof s.show === 'string') {
286
- walkTo(s.show, [...path, 'show'], step);
287
- }
288
- names(s.after).forEach((e, j) => {
289
- const where = Array.isArray(s.after) ? [...path, 'after', String(j)] : [...path, 'after'];
290
- const hit = pick(m.steps(cur.state, cur.ctx).filter((x) => x.event === e), then);
291
- if (hit === undefined)
292
- throw new Stop(where, `${step}: the flow is in ${cur.state}, which does not handle ${String(e)}`);
293
- cur = { state: hit.target, ctx: hit.ctx };
294
- });
295
- thenHolds(m, s, cur, path, step);
296
- });
297
- }
@@ -1,8 +0,0 @@
1
- import type { Graph } from './graph.ts';
2
- export interface UiProblem {
3
- rule: 'focus-not-shown' | 'returns-focus-not-dialog' | 'task-locale';
4
- path: string[];
5
- message: string;
6
- element: string;
7
- }
8
- export declare function checkUi(g: Graph, warn: (message: string) => void): UiProblem[];
package/dist/ui-check.js DELETED
@@ -1,136 +0,0 @@
1
- // What `check` holds in ui.yaml beyond its schema (SPEC.md §4): `focus` and `returnsFocus` give the focus to a node
2
- // that is shown where the focus goes, `returnsFocus` starts at a dialog, the words a field's `value` takes from the
3
- // catalog are there, as every other node's are, and the flow's name for its users (`task`) is in every locale. That `value`, `checked` and `script` sit only on the nodes they
4
- // are for is the schema's; that a `focus` state exists and that `checked` reads the context is the loader's.
5
- import { readFileSync } from 'node:fs';
6
- import { basename, dirname, join } from 'node:path';
7
- import { alternatives, isDialog, OPENER, shownIn } from './graph.js';
8
- const listed = (xs) => (xs.length <= 1 ? xs.join('') : `${xs.slice(0, -1).join(', ')} and ${xs[xs.length - 1]}`);
9
- // the states the flow stops in after a transition out of `state` (on `event` only, when it is given): its targets, and
10
- // past a state with `always` the targets of that state, as far as they go
11
- function stopsAfter(g, state, event) {
12
- const out = [];
13
- const seen = new Set();
14
- const ways = (s) => {
15
- const st = g.flow.states[s];
16
- return [...(st.always ?? []).map((a) => a.target), ...Object.values(st.on ?? {}).flatMap((spec) => alternatives(spec).map((a) => a.target))];
17
- };
18
- const first = g.flow.states[state].on?.[event ?? ''];
19
- const todo = event === undefined ? ways(state) : first === undefined ? [] : alternatives(first).map((a) => a.target);
20
- while (todo.length > 0) {
21
- const t = todo.shift();
22
- if (seen.has(t))
23
- continue;
24
- seen.add(t);
25
- if (g.flow.states[t].always !== undefined)
26
- todo.push(...ways(t));
27
- else
28
- out.push(t);
29
- }
30
- return out;
31
- }
32
- // the states a dialog closes into: those the flow stops in after a state that shows it, and that do not show it
33
- function closesInto(g, dialog) {
34
- const d = g.ui.nodes[dialog];
35
- const out = new Set();
36
- for (const s of Object.keys(g.flow.states)) {
37
- if (!shownIn(d, s))
38
- continue;
39
- for (const t of stopsAfter(g, s))
40
- if (!shownIn(d, t))
41
- out.add(t);
42
- }
43
- return [...out];
44
- }
45
- // the nodes that open a dialog: each is shown in a state that does not show the dialog, and sends an event that leads
46
- // from there to a state that shows it (SPEC.md §4, the node that opened a dialog)
47
- function openersOf(g, dialog) {
48
- const d = g.ui.nodes[dialog];
49
- const out = [];
50
- for (const [id, n] of Object.entries(g.ui.nodes)) {
51
- if (n.emits === undefined)
52
- continue;
53
- const opens = Object.keys(g.flow.states).some((s) => shownIn(n, s) && !shownIn(d, s) && stopsAfter(g, s, n.emits).some((t) => shownIn(d, t)));
54
- if (opens)
55
- out.push(id);
56
- }
57
- return out;
58
- }
59
- export function checkUi(g, warn) {
60
- const nodes = g.ui.nodes;
61
- const out = [];
62
- for (const [state, node] of Object.entries(g.ui.focus ?? {})) {
63
- const path = ['focus', state];
64
- const element = `focus/${state}`;
65
- if (nodes[node] === undefined)
66
- out.push({ rule: 'focus-not-shown', path, element, message: `focus.${state} names ${node}, which is no node of ui.yaml` });
67
- else if (!shownIn(nodes[node], state))
68
- out.push({ rule: 'focus-not-shown', path, element, message: `focus.${state} gives the focus to ${node}, which ${state} does not show: a node has focus only where it is shown (its in and unless, SPEC.md §4)` });
69
- }
70
- (g.ui.edges ?? []).forEach((e, i) => {
71
- if (e.returnsFocus === undefined)
72
- return;
73
- const [dialog, node] = e.returnsFocus;
74
- const path = ['edges', String(i)];
75
- const element = `edges/returnsFocus/${dialog}`;
76
- if (!isDialog(nodes[dialog])) {
77
- const what = nodes[dialog] === undefined ? 'no node of ui.yaml' : 'not a dialog';
78
- out.push({ rule: 'returns-focus-not-dialog', path, element, message: `returnsFocus starts at ${dialog}, which is ${what}: it starts at a page node whose role is dialog or alertdialog (SPEC.md §4)` });
79
- return;
80
- }
81
- // one node, the nodes that open the dialog, or OPENER: whichever node opens it
82
- const several = Array.isArray(node) || node === OPENER;
83
- const targets = node === OPENER ? openersOf(g, dialog) : Array.isArray(node) ? node : [node];
84
- if (node === OPENER && targets.length === 0) {
85
- out.push({ rule: 'focus-not-shown', path, element, message: `returnsFocus gives the focus of ${dialog} back to the node that opened it, but no node opens it: none sends an event that leads from a state without the dialog to one that shows it (SPEC.md §4)` });
86
- return;
87
- }
88
- const into = closesInto(g, dialog);
89
- for (const t of targets) {
90
- const at = several ? `${element}/${t}` : element;
91
- if (nodes[t] === undefined) {
92
- out.push({ rule: 'focus-not-shown', path, element: at, message: `returnsFocus gives the focus of ${dialog} to ${t}, which is no node of ui.yaml` });
93
- continue;
94
- }
95
- if (into.some((s) => shownIn(nodes[t], s)))
96
- continue;
97
- const where = into.length === 0 ? `the dialog ${dialog} never closes into a state the flow stops in` : `no state ${dialog} closes into shows it (${listed(into)})`;
98
- const who = node === OPENER ? `back to ${t}, which opens it,` : `to ${t},`;
99
- out.push({ rule: 'focus-not-shown', path, element: at, message: `returnsFocus gives the focus of ${dialog} ${who} but ${where} (SPEC.md §4)` });
100
- }
101
- });
102
- // a field's value in words of the catalog, held as every other node's words are
103
- const words = (v) => {
104
- for (const key of [...(v.key === undefined ? [] : [v.key]), ...(v.oneOf ?? [])]) {
105
- const message = g.catalog[key];
106
- if (message === undefined) {
107
- warn(`catalog key ${key} is missing`);
108
- continue;
109
- }
110
- for (const [, p] of message.matchAll(/\{(\w+)\}/g))
111
- if (g.ui.samples[p] === undefined)
112
- warn(`no sample value for {${p}} in ${key}`);
113
- }
114
- };
115
- for (const n of Object.values(nodes))
116
- if (n.value !== undefined)
117
- words(n.value);
118
- // the flow's name for its users, in the catalog of every locale, as the changes note is
119
- const task = g.ui.task?.key;
120
- if (g.ui.absent !== true && typeof task === 'string') {
121
- for (const locale of g.ui.locales ?? [basename(g.ui.catalog, '.json')]) {
122
- let catalog = null;
123
- try {
124
- catalog = JSON.parse(readFileSync(join(g.dir, dirname(g.ui.catalog), `${locale}.json`), 'utf8'));
125
- }
126
- catch {
127
- catalog = null;
128
- }
129
- const name = catalog?.[task];
130
- if (typeof name === 'string' && name.trim() !== '')
131
- continue;
132
- out.push({ rule: 'task-locale', path: ['task'], element: `task/${locale}`, message: `the flow's name for its users, ${task}, is not in the ${locale} catalog (SPEC.md §4)` });
133
- }
134
- }
135
- return out;
136
- }
package/dist/watch.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export interface Rendered {
2
- ok: boolean;
3
- message: string;
4
- }
5
- export declare function watchPages(root: string, out: string, render: () => Promise<Rendered>, port: number | null): Promise<never>;
package/dist/watch.js DELETED
@@ -1,114 +0,0 @@
1
- // `page --watch` writes the pages again whenever a graph file under the folder changes; with `--serve` it also shows
2
- // them at http://127.0.0.1:<port>/ and reloads the open page after each write, or shows why the write failed. Local
3
- // only: the server listens on the loopback address and serves the written pages and nothing else.
4
- import { existsSync, readFileSync, statSync, watch } from 'node:fs';
5
- import { createServer } from 'node:http';
6
- import { extname, join, normalize, resolve, sep } from 'node:path';
7
- const WATCHED = new Set(['.yaml', '.yml', '.json']);
8
- const TYPES = {
9
- '.html': 'text/html; charset=utf-8',
10
- '.css': 'text/css; charset=utf-8',
11
- '.js': 'text/javascript; charset=utf-8',
12
- '.svg': 'image/svg+xml',
13
- '.json': 'application/json',
14
- '.mmd': 'text/plain; charset=utf-8',
15
- };
16
- // added to every page the server sends, never to the files on disk
17
- const RELOAD = `<script>(() => {
18
- const events = new EventSource('/__delurio/events');
19
- events.addEventListener('reload', () => location.reload());
20
- events.addEventListener('failed', (e) => {
21
- let box = document.getElementById('__delurio');
22
- if (!box) {
23
- box = document.createElement('pre');
24
- box.id = '__delurio';
25
- box.setAttribute('role', 'alert');
26
- box.style.cssText = 'position:fixed;inset:auto 0 0 0;margin:0;padding:12px 16px;background:#7f1d1d;color:#fff;font:13px/1.5 ui-monospace,Menlo,Consolas,monospace;white-space:pre-wrap;z-index:2147483647';
27
- document.body.append(box);
28
- }
29
- box.textContent = 'delurio page: ' + e.data;
30
- });
31
- })();</script>`;
32
- // whether a changed file can change a page: a graph file, not one of the pages being written
33
- function counts(root, out, name) {
34
- const abs = resolve(root, name);
35
- if (abs === out || abs.startsWith(out + sep))
36
- return false;
37
- if (abs.split(sep).includes('viz'))
38
- return false;
39
- return WATCHED.has(extname(abs));
40
- }
41
- function serve(out, port, clients) {
42
- const server = createServer((req, res) => {
43
- const url = new URL(req.url ?? '/', 'http://127.0.0.1');
44
- if (url.pathname === '/__delurio/events') {
45
- res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-store', Connection: 'keep-alive' });
46
- res.write(': delurio\n\n');
47
- clients.add(res);
48
- req.on('close', () => clients.delete(res));
49
- return;
50
- }
51
- let wanted;
52
- try {
53
- wanted = decodeURIComponent(url.pathname);
54
- }
55
- catch {
56
- res.writeHead(400).end();
57
- return;
58
- }
59
- let path = join(out, normalize(wanted));
60
- if (path !== out && !path.startsWith(out + sep)) {
61
- res.writeHead(403).end();
62
- return;
63
- }
64
- if (existsSync(path) && statSync(path).isDirectory())
65
- path = join(path, 'index.html');
66
- if (!existsSync(path)) {
67
- res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' }).end('not found');
68
- return;
69
- }
70
- const type = TYPES[extname(path)] ?? 'application/octet-stream';
71
- let body = readFileSync(path);
72
- if (type.startsWith('text/html')) {
73
- const html = body.toString('utf8');
74
- body = html.includes('</body>') ? html.replace('</body>', `${RELOAD}</body>`) : html + RELOAD;
75
- }
76
- res.writeHead(200, { 'Content-Type': type, 'Cache-Control': 'no-store' }).end(body);
77
- });
78
- server.listen(port, '127.0.0.1', () => console.log(`serving http://127.0.0.1:${port}/ (Ctrl-C stops)`));
79
- }
80
- // Runs until stopped. `render` writes every page once and says whether it could.
81
- export function watchPages(root, out, render, port) {
82
- const clients = new Set();
83
- const tell = (event, data) => {
84
- for (const c of clients)
85
- c.write(`event: ${event}\ndata: ${data.replace(/\n/g, ' ')}\n\n`);
86
- };
87
- if (port !== null)
88
- serve(out, port, clients);
89
- let timer = null;
90
- let running = false;
91
- let again = false;
92
- const run = async () => {
93
- if (running) {
94
- again = true;
95
- return;
96
- }
97
- running = true;
98
- do {
99
- again = false;
100
- const done = await render();
101
- tell(done.ok ? 'reload' : 'failed', done.ok ? '' : done.message);
102
- } while (again);
103
- running = false;
104
- };
105
- watch(root, { recursive: true }, (_event, name) => {
106
- if (name === null || !counts(resolve(root), out, String(name)))
107
- return;
108
- if (timer !== null)
109
- clearTimeout(timer);
110
- timer = setTimeout(() => void run(), 150);
111
- });
112
- console.log(`watching ${root} for changes`);
113
- return new Promise(() => { });
114
- }