@delurio/cli 0.10.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.
- package/CHANGELOG.md +9 -0
- package/NOTICE +7 -0
- package/README.md +14 -13
- package/js/changes.d.ts +17 -0
- package/js/cli.js +6 -0
- package/{dist → js}/edges.d.ts +2 -2
- package/js/edges.js +40 -0
- package/js/expr.js +25 -0
- package/js/graph.js +59 -0
- package/{dist → js}/html.d.ts +6 -6
- package/js/html.js +13 -0
- package/js/internal.js +408 -0
- package/js/kit.js +54 -0
- package/{dist → js}/model.d.ts +2 -2
- package/js/model.js +136 -0
- package/js/native.cjs +46 -0
- package/js/native.d.cts +322 -0
- package/{dist → js}/nav.d.ts +1 -1
- package/js/nav.js +70 -0
- package/js/needs.js +30 -0
- package/{dist → js}/spine.d.ts +3 -3
- package/js/spine.js +22 -0
- package/{dist → js}/starts.d.ts +1 -3
- package/{dist → js}/story.d.ts +2 -2
- package/js/story.js +22 -0
- package/{dist → js}/wireframe.d.ts +2 -2
- package/js/wireframe.js +29 -0
- package/js/yaml.d.ts +20 -0
- package/js/yaml.js +40 -0
- package/package.json +40 -24
- package/dist/anchors.d.ts +0 -29
- package/dist/anchors.js +0 -160
- package/dist/changes.d.ts +0 -25
- package/dist/changes.js +0 -101
- package/dist/check.d.ts +0 -23
- package/dist/check.js +0 -649
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -194
- package/dist/compare.d.ts +0 -34
- package/dist/compare.js +0 -143
- package/dist/diff-flow.d.ts +0 -33
- package/dist/diff-flow.js +0 -547
- package/dist/diff-nav.d.ts +0 -20
- package/dist/diff-nav.js +0 -401
- package/dist/diff.d.ts +0 -25
- package/dist/diff.js +0 -331
- package/dist/domain-names.d.ts +0 -18
- package/dist/domain-names.js +0 -124
- package/dist/domain.d.ts +0 -25
- package/dist/domain.js +0 -110
- package/dist/edges.js +0 -120
- package/dist/expr.js +0 -157
- package/dist/graph.js +0 -236
- package/dist/html.js +0 -311
- package/dist/index-page.d.ts +0 -12
- package/dist/index-page.js +0 -50
- package/dist/invokes.d.ts +0 -10
- package/dist/invokes.js +0 -67
- package/dist/kit.js +0 -711
- package/dist/mermaid.d.ts +0 -3
- package/dist/mermaid.js +0 -50
- package/dist/model.js +0 -608
- package/dist/nav-page.d.ts +0 -15
- package/dist/nav-page.js +0 -283
- package/dist/nav.js +0 -578
- package/dist/needs.js +0 -335
- package/dist/pan.d.ts +0 -2
- package/dist/pan.js +0 -252
- package/dist/schemas.d.ts +0 -15
- package/dist/schemas.js +0 -316
- package/dist/spine.js +0 -156
- package/dist/starts.js +0 -88
- package/dist/story.js +0 -109
- package/dist/tutorial-check.d.ts +0 -9
- package/dist/tutorial-check.js +0 -297
- package/dist/ui-check.d.ts +0 -8
- package/dist/ui-check.js +0 -142
- package/dist/watch.d.ts +0 -5
- package/dist/watch.js +0 -114
- package/dist/wireframe.js +0 -276
- package/dist/yaml.d.ts +0 -19
- package/dist/yaml.js +0 -134
- /package/{dist → js}/expr.d.ts +0 -0
- /package/{dist → js}/graph.d.ts +0 -0
- /package/{dist → js}/kit.d.ts +0 -0
- /package/{dist → js}/needs.d.ts +0 -0
package/dist/schemas.js
DELETED
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
// The JSON Schemas of schemas/ (one per kind of graph file, SPEC.md §12) as `check` applies them: a file is validated
|
|
2
|
-
// against its kind's schema, and each failure is placed at its line and column through the YAML document and said in
|
|
3
|
-
// words a writer can act on: the key that is not one, the key it was probably meant to be, why an if/then rule exists
|
|
4
|
-
// (its `$comment`), and the comma that cut a value inside `{ }`. The schemas are read from the package's own folder,
|
|
5
|
-
// beside src/ and dist/ alike.
|
|
6
|
-
import { existsSync, readFileSync } from 'node:fs';
|
|
7
|
-
import { basename, dirname, join } from 'node:path';
|
|
8
|
-
import { Ajv2020 } from 'ajv/dist/2020.js';
|
|
9
|
-
import addFormatsModule from 'ajv-formats';
|
|
10
|
-
import { isMap, isSeq, LineCounter, parseDocument } from 'yaml';
|
|
11
|
-
import { readYaml, unsupported } from './yaml.js';
|
|
12
|
-
import { isDomainText, mayBeDomain } from './domain.js';
|
|
13
|
-
const FORMAT = 'delurio-graph/0';
|
|
14
|
-
const KINDS = ['flow', 'ui', 'domain', 'nav', 'catalog', 'needs', 'gaps'];
|
|
15
|
-
// the kind of graph file, by its name, and a domain also by its `kind` (a domain may be split across files, SPEC.md
|
|
16
|
-
// §5): null for a file no schema covers (binding, tutorial, kit, anchors.lock)
|
|
17
|
-
export function schemaKindOf(file) {
|
|
18
|
-
const name = basename(file);
|
|
19
|
-
if (name === 'flow.yaml' || name === 'ui.yaml' || name === 'domain.yaml' || name === 'needs.yaml')
|
|
20
|
-
return name.slice(0, -'.yaml'.length);
|
|
21
|
-
if (name === 'nav.yaml' || name === 'nav.extracted.yaml')
|
|
22
|
-
return 'nav';
|
|
23
|
-
if (name === 'needs.gaps.yaml')
|
|
24
|
-
return 'gaps';
|
|
25
|
-
if (name.endsWith('.json') && basename(dirname(file)) === 'catalog')
|
|
26
|
-
return 'catalog';
|
|
27
|
-
if (mayBeDomain(name) && existsSync(file) && isDomainText(readFileSync(file, 'utf8')))
|
|
28
|
-
return 'domain';
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
let compiled = null;
|
|
32
|
-
// compiled once, on first use; strictRequired is off because an if/then names keys its branch does not declare
|
|
33
|
-
function schemas() {
|
|
34
|
-
if (compiled !== null)
|
|
35
|
-
return compiled;
|
|
36
|
-
const ajv = new Ajv2020({ allErrors: true, verbose: true, strict: true, strictRequired: false, allowUnionTypes: true });
|
|
37
|
-
addFormatsModule.default(ajv);
|
|
38
|
-
const roots = {};
|
|
39
|
-
const validators = {};
|
|
40
|
-
for (const kind of KINDS)
|
|
41
|
-
roots[kind] = JSON.parse(readFileSync(new URL(`../schemas/${kind}.schema.json`, import.meta.url), 'utf8'));
|
|
42
|
-
for (const kind of KINDS)
|
|
43
|
-
validators[kind] = ajv.compile(roots[kind]);
|
|
44
|
-
compiled = { roots, validators };
|
|
45
|
-
return compiled;
|
|
46
|
-
}
|
|
47
|
-
const steps = (pointer) => (pointer === '' ? [] : pointer.slice(1).split('/').map((t) => t.replace(/~1/g, '/').replace(/~0/g, '~')));
|
|
48
|
-
const escape = (step) => step.replace(/~/g, '~0').replace(/\//g, '~1');
|
|
49
|
-
const within = (path, base) => path === base || path.startsWith(`${base}/`);
|
|
50
|
-
const keyOf = (pair) => String(pair.key?.value ?? pair.key);
|
|
51
|
-
// the YAML node at a JSON pointer; with `name`, the key `name` of the map there, that map and the key's value
|
|
52
|
-
function place(doc, pointer, name) {
|
|
53
|
-
let node = doc.contents;
|
|
54
|
-
for (const t of steps(pointer)) {
|
|
55
|
-
const next = isMap(node) ? node.items.find((p) => keyOf(p) === t)?.value : isSeq(node) ? node.items[Number(t)] : undefined;
|
|
56
|
-
if (next === undefined || next === null)
|
|
57
|
-
break;
|
|
58
|
-
node = next;
|
|
59
|
-
}
|
|
60
|
-
if (name === undefined || !isMap(node))
|
|
61
|
-
return { at: node };
|
|
62
|
-
const pair = node.items.find((p) => keyOf(p) === name);
|
|
63
|
-
return { at: pair?.key ?? node, map: node, value: pair?.value };
|
|
64
|
-
}
|
|
65
|
-
// a schema and the ones its `$ref`s name, inside the same file
|
|
66
|
-
function chain(root, schema) {
|
|
67
|
-
const out = [];
|
|
68
|
-
for (let s = schema; s !== null && typeof s === 'object' && out.length < 8;) {
|
|
69
|
-
const here = s;
|
|
70
|
-
out.push(here);
|
|
71
|
-
const ref = here.$ref;
|
|
72
|
-
s = typeof ref === 'string' && ref.startsWith('#/') ? ref.slice(2).split('/').reduce((x, k) => x?.[k], root) : undefined;
|
|
73
|
-
}
|
|
74
|
-
return out;
|
|
75
|
-
}
|
|
76
|
-
const ANCHOR = '#/$defs/sourceAnchor';
|
|
77
|
-
const patterns = new Map();
|
|
78
|
-
const matches = (pattern, name) => {
|
|
79
|
-
let re = patterns.get(pattern);
|
|
80
|
-
if (re === undefined)
|
|
81
|
-
patterns.set(pattern, (re = new RegExp(pattern, 'u')));
|
|
82
|
-
return re.test(name);
|
|
83
|
-
};
|
|
84
|
-
const refAt = (root, ref) => ref.slice(2).split('/').reduce((x, k) => x?.[k], root);
|
|
85
|
-
// every source anchor in a graph file's value (SPEC.md §9): each `source` whose schema is the kind's `sourceAnchor`,
|
|
86
|
-
// found by walking the value along its schema, so a key `source` that the schema gives another meaning (a nav graph's
|
|
87
|
-
// `source`, a sample value or a parameter of ui.yaml, a field of a record) is none. Where the schema has alternatives,
|
|
88
|
-
// every one that the value's shape reaches is walked.
|
|
89
|
-
export function anchorsIn(kind, data) {
|
|
90
|
-
const root = schemas().roots[kind];
|
|
91
|
-
const out = new Map();
|
|
92
|
-
const visit = (schema, value, path, seen) => {
|
|
93
|
-
if (schema === null || typeof schema !== 'object' || seen.has(schema))
|
|
94
|
-
return;
|
|
95
|
-
seen.add(schema);
|
|
96
|
-
const s = schema;
|
|
97
|
-
if (s.$ref === ANCHOR) {
|
|
98
|
-
if (typeof value === 'string')
|
|
99
|
-
out.set(path.join('\0'), { path, value });
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
if (typeof s.$ref === 'string' && s.$ref.startsWith('#/'))
|
|
103
|
-
visit(refAt(root, s.$ref), value, path, seen);
|
|
104
|
-
for (const k of ['allOf', 'anyOf', 'oneOf'])
|
|
105
|
-
for (const branch of s[k] ?? [])
|
|
106
|
-
visit(branch, value, path, seen);
|
|
107
|
-
for (const k of ['then', 'else'])
|
|
108
|
-
visit(s[k], value, path, seen);
|
|
109
|
-
if (Array.isArray(value)) {
|
|
110
|
-
const prefix = s.prefixItems ?? [];
|
|
111
|
-
value.forEach((x, i) => visit(i < prefix.length ? prefix[i] : s.items, x, [...path, String(i)], new Set()));
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
if (value === null || typeof value !== 'object')
|
|
115
|
-
return;
|
|
116
|
-
const properties = (s.properties ?? {});
|
|
117
|
-
const byPattern = Object.entries((s.patternProperties ?? {}));
|
|
118
|
-
for (const [k, x] of Object.entries(value)) {
|
|
119
|
-
const sub = Object.hasOwn(properties, k) ? properties[k] : (byPattern.find(([p]) => matches(p, k))?.[1] ?? s.additionalProperties);
|
|
120
|
-
visit(sub, x, [...path, k], new Set());
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
visit(root, data, [], new Set());
|
|
124
|
-
return [...out.values()];
|
|
125
|
-
}
|
|
126
|
-
// edits between two names, a swap of two neighbours counting as one (titel, title)
|
|
127
|
-
function distance(a, b) {
|
|
128
|
-
const d = Array.from({ length: a.length + 1 }, (_, i) => [i, ...Array(b.length).fill(0)]);
|
|
129
|
-
for (let j = 1; j <= b.length; j++)
|
|
130
|
-
d[0][j] = j;
|
|
131
|
-
for (let i = 1; i <= a.length; i++) {
|
|
132
|
-
for (let j = 1; j <= b.length; j++) {
|
|
133
|
-
d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
134
|
-
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1])
|
|
135
|
-
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return d[a.length][b.length];
|
|
139
|
-
}
|
|
140
|
-
const KIND_WORDS = { object: 'an object', array: 'a list', string: 'a string', integer: 'an integer', number: 'a number', boolean: 'true or false', null: 'null' };
|
|
141
|
-
const either = (xs) => (xs.length <= 1 ? xs.join('') : `${xs.slice(0, -1).join(', ')} or ${xs[xs.length - 1]}`);
|
|
142
|
-
const quoted = (xs) => either(xs.map((x) => JSON.stringify(x)));
|
|
143
|
-
function message(e, root) {
|
|
144
|
-
const p = e.params;
|
|
145
|
-
switch (e.keyword) {
|
|
146
|
-
case 'additionalProperties':
|
|
147
|
-
case 'unevaluatedProperties': {
|
|
148
|
-
const name = String(p.additionalProperty ?? p.unevaluatedProperty);
|
|
149
|
-
const known = chain(root, e.parentSchema).flatMap((x) => Object.keys(x.properties ?? {}));
|
|
150
|
-
const near = known.filter((k) => distance(k.toLowerCase(), name.toLowerCase()) <= Math.min(2, Math.max(1, Math.floor(k.length / 3))));
|
|
151
|
-
return `"${name}" is not a key here${near.length > 0 ? `; did you mean ${quoted(near)}?` : ''}`;
|
|
152
|
-
}
|
|
153
|
-
case 'required':
|
|
154
|
-
return `${p.missingProperty.map((k) => JSON.stringify(k)).join(' or ')} is missing`;
|
|
155
|
-
case 'enum': {
|
|
156
|
-
const allowed = p.allowedValues;
|
|
157
|
-
return allowed.length <= 12 ? `must be ${quoted(allowed)}` : `${JSON.stringify(e.data)} is not one of its ${allowed.length} values`;
|
|
158
|
-
}
|
|
159
|
-
case 'const':
|
|
160
|
-
return `must be ${JSON.stringify(p.allowedValue)}`;
|
|
161
|
-
case 'type':
|
|
162
|
-
return `must be ${either(p.type.map((t) => KIND_WORDS[t] ?? t))}`;
|
|
163
|
-
case 'pattern':
|
|
164
|
-
return `${JSON.stringify(e.data)} does not match ${String(p.pattern)}`;
|
|
165
|
-
case 'propertyNames': {
|
|
166
|
-
const said = chain(root, e.parentSchema?.propertyNames).find((x) => typeof x.description === 'string')?.description;
|
|
167
|
-
return `the name "${String(p.propertyName)}" is not allowed${said === undefined ? '' : `: ${said.split('. ')[0].replace(/\.$/, '')}`}`;
|
|
168
|
-
}
|
|
169
|
-
case 'false schema':
|
|
170
|
-
return 'is not allowed here';
|
|
171
|
-
case 'oneOf':
|
|
172
|
-
return 'matches more than one of its forms';
|
|
173
|
-
case 'minItems':
|
|
174
|
-
case 'maxItems':
|
|
175
|
-
case 'minProperties':
|
|
176
|
-
case 'maxProperties': {
|
|
177
|
-
const n = Number(p.limit);
|
|
178
|
-
const what = e.keyword.endsWith('Items') ? (n === 1 ? 'item' : 'items') : n === 1 ? 'key' : 'keys';
|
|
179
|
-
return e.keyword.startsWith('min') ? `needs at least ${n} ${what}` : `has more than ${n} ${what}`;
|
|
180
|
-
}
|
|
181
|
-
default:
|
|
182
|
-
return e.message ?? e.keyword;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
// Ajv reports every alternative of an anyOf and every rule around a then; keep what the writer can act on
|
|
186
|
-
function simplify(errors) {
|
|
187
|
-
// the reason for an if/then rule, from its $comment, goes onto the errors its then or else gave (just before it)
|
|
188
|
-
errors.forEach((e, i) => {
|
|
189
|
-
const comment = e.parentSchema?.$comment;
|
|
190
|
-
if (e.keyword !== 'if' || typeof comment !== 'string')
|
|
191
|
-
return;
|
|
192
|
-
const base = e.schemaPath.slice(0, -'/if'.length);
|
|
193
|
-
for (let j = i - 1; j >= 0; j--) {
|
|
194
|
-
const x = errors[j];
|
|
195
|
-
if (!within(x.instancePath, e.instancePath) || !(x.schemaPath.startsWith(`${base}/then/`) || x.schemaPath.startsWith(`${base}/else/`)))
|
|
196
|
-
break;
|
|
197
|
-
x.because = comment;
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
const wrapper = (x) => ['if', 'anyOf', 'oneOf'].includes(x.keyword);
|
|
201
|
-
const explained = (e) => errors.some((x) => x !== e && !wrapper(x) && ((x.instancePath !== e.instancePath && within(x.instancePath, e.instancePath)) || (x.instancePath === e.instancePath && x.keyword !== 'type')));
|
|
202
|
-
const kept = errors.filter((e) => {
|
|
203
|
-
if (e.keyword === 'if')
|
|
204
|
-
return false; // its then or else said it
|
|
205
|
-
if (e.keyword === 'anyOf' || (e.keyword === 'oneOf' && e.params.passingSchemas === null)) {
|
|
206
|
-
return !explained(e) && !errors.some((x) => x !== e && x.keyword === 'type' && x.instancePath === e.instancePath);
|
|
207
|
-
}
|
|
208
|
-
if (e.keyword === 'type')
|
|
209
|
-
return !explained(e); // an alternative of the right type is there, and failed further in
|
|
210
|
-
if (e.propertyName !== undefined)
|
|
211
|
-
return false; // the name's own failure; its propertyNames error says it
|
|
212
|
-
return true;
|
|
213
|
-
});
|
|
214
|
-
// what is left at one place in one form is one line: the types the alternatives want, the keys of which one is needed
|
|
215
|
-
const out = [];
|
|
216
|
-
for (const e of kept) {
|
|
217
|
-
const alternative = e.keyword === 'required' && /\/(anyOf|oneOf)\/\d+\/required$/.test(e.schemaPath);
|
|
218
|
-
const twin = out.find((x) => x.instancePath === e.instancePath && x.keyword === e.keyword && (e.keyword === 'type' || (alternative && x.alternative === true)));
|
|
219
|
-
const p = e.params;
|
|
220
|
-
if (twin !== undefined && e.keyword === 'type')
|
|
221
|
-
twin.params.type = [...new Set([...twin.params.type, ...[p.type].flat()])];
|
|
222
|
-
else if (twin !== undefined)
|
|
223
|
-
twin.params.missingProperty.push(p.missingProperty);
|
|
224
|
-
else {
|
|
225
|
-
const params = e.keyword === 'type' ? { type: [p.type].flat() } : e.keyword === 'required' ? { missingProperty: [p.missingProperty] } : e.params;
|
|
226
|
-
out.push({ ...e, alternative, params });
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
return out;
|
|
230
|
-
}
|
|
231
|
-
// the id of the graph a file belongs to, when the file does not say it: the flow, nav or ui graph beside it
|
|
232
|
-
function graphIdBeside(dir) {
|
|
233
|
-
for (const name of ['flow.yaml', 'nav.yaml', 'ui.yaml']) {
|
|
234
|
-
const file = join(dir, name);
|
|
235
|
-
if (!existsSync(file))
|
|
236
|
-
continue;
|
|
237
|
-
try {
|
|
238
|
-
const id = readYaml(readFileSync(file, 'utf8'))?.id;
|
|
239
|
-
if (typeof id === 'string')
|
|
240
|
-
return id;
|
|
241
|
-
}
|
|
242
|
-
catch {
|
|
243
|
-
// a graph file that does not parse names no graph; its own check says why
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
return null;
|
|
247
|
-
}
|
|
248
|
-
// every place where the file does not follow its kind's schema; a file written for another format is left to the
|
|
249
|
-
// format check
|
|
250
|
-
export function validateFile(file) {
|
|
251
|
-
const kind = schemaKindOf(file);
|
|
252
|
-
if (kind === null)
|
|
253
|
-
return [];
|
|
254
|
-
const text = readFileSync(file, 'utf8');
|
|
255
|
-
const refused = kind === 'catalog' ? null : unsupported(text);
|
|
256
|
-
const lineCounter = new LineCounter();
|
|
257
|
-
const doc = parseDocument(text, { lineCounter, prettyErrors: false });
|
|
258
|
-
const at = (offset) => {
|
|
259
|
-
const { line, col } = lineCounter.linePos(offset ?? 0);
|
|
260
|
-
return { line, column: col };
|
|
261
|
-
};
|
|
262
|
-
const ownFile = kind === 'catalog' ? `catalog/${basename(file)}` : basename(file);
|
|
263
|
-
let graph = null;
|
|
264
|
-
const failure = (where, said, pointer) => {
|
|
265
|
-
graph ??= graphIdBeside(kind === 'catalog' ? dirname(dirname(file)) : dirname(file));
|
|
266
|
-
return { ...where, message: said, graph, element: `${ownFile}#${pointer}` };
|
|
267
|
-
};
|
|
268
|
-
// the first YAML error only: the ones after it mostly follow from it; one before the unsupported place comes first
|
|
269
|
-
if (refused !== null && (doc.errors.length === 0 || refused.offset < doc.errors[0].pos[0]))
|
|
270
|
-
return [failure(refused, refused.message, '')];
|
|
271
|
-
if (doc.errors.length > 0)
|
|
272
|
-
return [failure(at(doc.errors[0].pos[0]), `not YAML: ${doc.errors[0].message.split('\n')[0]}`, '')];
|
|
273
|
-
let data;
|
|
274
|
-
if (kind === 'catalog') {
|
|
275
|
-
try {
|
|
276
|
-
data = JSON.parse(text);
|
|
277
|
-
}
|
|
278
|
-
catch (err) {
|
|
279
|
-
return [failure({ line: 1, column: 1 }, `not JSON: ${err.message}`, '')];
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
else
|
|
283
|
-
data = doc.toJS();
|
|
284
|
-
const own = data ?? {};
|
|
285
|
-
if (kind !== 'catalog' && typeof own.schema === 'string' && own.schema !== FORMAT)
|
|
286
|
-
return [];
|
|
287
|
-
if (kind !== 'catalog' && typeof own.id === 'string')
|
|
288
|
-
graph = own.id;
|
|
289
|
-
const { roots, validators } = schemas();
|
|
290
|
-
const validate = validators[kind];
|
|
291
|
-
if (validate(data))
|
|
292
|
-
return [];
|
|
293
|
-
const out = [];
|
|
294
|
-
for (const e of simplify((validate.errors ?? []))) {
|
|
295
|
-
const p = e.params;
|
|
296
|
-
const named = p.additionalProperty ?? p.unevaluatedProperty ?? p.propertyName;
|
|
297
|
-
const name = named === undefined ? undefined : String(named);
|
|
298
|
-
const where = place(doc, e.instancePath, name);
|
|
299
|
-
const path = steps(e.instancePath).join('.');
|
|
300
|
-
let said = `${path === '' ? 'the file' : path}: ${message(e, roots[kind])}`;
|
|
301
|
-
const because = e.because ?? (e.keyword === 'false schema' ? undefined : e.parentSchema?.$comment);
|
|
302
|
-
if (typeof because === 'string')
|
|
303
|
-
said += ` (${because})`;
|
|
304
|
-
// `{ title: Done, and paid }` is a title and a key "and paid": YAML ends a flow mapping's value at the comma
|
|
305
|
-
const empty = where.value === null || where.value?.value === null;
|
|
306
|
-
if (name !== undefined && where.map?.flow === true && empty)
|
|
307
|
-
said += '; a comma inside { } ends the value before it: quote that value';
|
|
308
|
-
const missing = e.keyword === 'required' ? p.missingProperty.join('|') : undefined;
|
|
309
|
-
const pointer = name !== undefined ? `${e.instancePath}/${escape(name)}` : missing !== undefined ? `${e.instancePath}/${escape(missing)}` : e.instancePath;
|
|
310
|
-
const range = where.at?.range;
|
|
311
|
-
const finding = failure(at(range?.[0]), said, pointer);
|
|
312
|
-
if (!out.some((x) => x.line === finding.line && x.column === finding.column && x.message === finding.message))
|
|
313
|
-
out.push(finding);
|
|
314
|
-
}
|
|
315
|
-
return out;
|
|
316
|
-
}
|
package/dist/spine.js
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import ElkModule from 'elkjs/lib/elk.bundled.js';
|
|
2
|
-
import { collectEdges, kindOf, nodeLine, wrap } from './edges.js';
|
|
3
|
-
import { titleOf } from './graph.js';
|
|
4
|
-
// elkjs ships CommonJS: under Node ESM the default import is module.exports, which is the constructor
|
|
5
|
-
const Elk = ElkModule;
|
|
6
|
-
const START = '__start';
|
|
7
|
-
const CHAR_TITLE = 8.4; // 14px semibold sans
|
|
8
|
-
const CHAR_LINE = 6.9; // 11.5px monospace
|
|
9
|
-
const CHAR_LABEL = 7.1; // 13px sans
|
|
10
|
-
const LABEL_WRAP = { main: 26, full: 22 };
|
|
11
|
-
const esc = (s) => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
12
|
-
const r1 = (n) => Math.round(n * 10) / 10;
|
|
13
|
-
function labelLines(e, view) {
|
|
14
|
-
return e.labels.flatMap((l) => wrap(l, LABEL_WRAP[view]));
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* The flow's diagram as an SVG. Every state's group carries `data-state`, and every edge and its label `data-from` and
|
|
18
|
-
* `data-to`: one edge is drawn per pair of states, its label holding every transition between them. `story`: the
|
|
19
|
-
* states to draw as the story (`on-main`), in order.
|
|
20
|
-
*/
|
|
21
|
-
export async function renderSpine(g, ex, view, story) {
|
|
22
|
-
const all = collectEdges(g, ex);
|
|
23
|
-
// main view: the story without failure branches and without "the user closes the page"
|
|
24
|
-
const edges = view === 'full' ? all : all.filter((e) => e.kind !== 'fail' && !e.leave);
|
|
25
|
-
const visible = new Set([g.flow.initial]);
|
|
26
|
-
for (const e of edges) {
|
|
27
|
-
visible.add(e.from);
|
|
28
|
-
visible.add(e.to);
|
|
29
|
-
}
|
|
30
|
-
const names = Object.keys(g.flow.states).filter((n) => visible.has(n));
|
|
31
|
-
const mainEdges = new Set();
|
|
32
|
-
const mainNodes = new Set();
|
|
33
|
-
(story ?? []).forEach((s, k) => {
|
|
34
|
-
mainNodes.add(s.state);
|
|
35
|
-
if (k > 0)
|
|
36
|
-
mainEdges.add(`${story?.[k - 1].state}>${s.state}`);
|
|
37
|
-
});
|
|
38
|
-
const children = [{ id: START, width: 14, height: 14 }];
|
|
39
|
-
for (const name of names) {
|
|
40
|
-
const width = Math.ceil(Math.max(150, titleOf(g.flow, name).length * CHAR_TITLE, nodeLine(g, name).length * CHAR_LINE) + 36);
|
|
41
|
-
const node = { id: name, width, height: 58 };
|
|
42
|
-
if (kindOf(g, name) === 'exit')
|
|
43
|
-
node.layoutOptions = { 'elk.layered.layering.layerConstraint': 'LAST' };
|
|
44
|
-
children.push(node);
|
|
45
|
-
}
|
|
46
|
-
const elkEdges = [{ id: 'e_start', sources: [START], targets: [g.flow.initial] }];
|
|
47
|
-
edges.forEach((e, i) => {
|
|
48
|
-
const lines = labelLines(e, view);
|
|
49
|
-
elkEdges.push({
|
|
50
|
-
id: `e${i}`,
|
|
51
|
-
sources: [e.from],
|
|
52
|
-
targets: [e.to],
|
|
53
|
-
labels: lines.length === 0
|
|
54
|
-
? []
|
|
55
|
-
: [{ text: lines.join('\n'), width: Math.ceil(Math.max(...lines.map((l) => l.length)) * CHAR_LABEL + 14), height: lines.length * 17 + 6 }],
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
const root = {
|
|
59
|
-
id: 'root',
|
|
60
|
-
layoutOptions: {
|
|
61
|
-
'elk.algorithm': 'layered',
|
|
62
|
-
'elk.direction': 'DOWN',
|
|
63
|
-
'elk.edgeRouting': 'ORTHOGONAL',
|
|
64
|
-
'elk.padding': '[top=24,left=24,bottom=24,right=24]',
|
|
65
|
-
'elk.spacing.nodeNode': '36',
|
|
66
|
-
'elk.spacing.edgeNode': '22',
|
|
67
|
-
'elk.spacing.edgeEdge': '16',
|
|
68
|
-
'elk.spacing.edgeLabel': '6',
|
|
69
|
-
'elk.layered.spacing.nodeNodeBetweenLayers': '56',
|
|
70
|
-
'elk.layered.spacing.edgeNodeBetweenLayers': '22',
|
|
71
|
-
'elk.layered.considerModelOrder.strategy': 'NODES_AND_EDGES',
|
|
72
|
-
},
|
|
73
|
-
children,
|
|
74
|
-
edges: elkEdges,
|
|
75
|
-
};
|
|
76
|
-
const laid = await new Elk().layout(root);
|
|
77
|
-
const width = Math.ceil(laid.width ?? 800);
|
|
78
|
-
const height = Math.ceil(laid.height ?? 400);
|
|
79
|
-
const out = [];
|
|
80
|
-
out.push(`<svg class="spine" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}" style="${view === 'full' ? `width:${width}px;max-width:none` : `max-width:${width}px`}" role="img" aria-label="Flowchart of ${esc(g.flow.title ?? g.flow.id)}${view === 'main' ? ' without failure paths' : ' with failure paths'}. The text sections of this page describe every step.">`);
|
|
81
|
-
out.push('<defs>');
|
|
82
|
-
for (const kind of ['user', 'system', 'route', 'fail', 'main']) {
|
|
83
|
-
out.push(`<marker id="arrow-${view}-${kind}" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="8" markerHeight="8" orient="auto-start-reverse"><path class="arrow arrow-${kind}" d="M0 0 L10 5 L0 10 z"/></marker>`);
|
|
84
|
-
}
|
|
85
|
-
out.push('</defs>');
|
|
86
|
-
const byId = new Map();
|
|
87
|
-
edges.forEach((e, i) => byId.set(`e${i}`, e));
|
|
88
|
-
const drawn = (laid.edges ?? []).filter((e) => e.sections?.[0] !== undefined);
|
|
89
|
-
const isMain = (e) => e !== undefined && mainEdges.has(`${e.from}>${e.to}`);
|
|
90
|
-
// which states an edge joins, so a tool can find the drawing of a transition: one edge per pair of states
|
|
91
|
-
const ends = (e) => `data-from="${esc(e.from)}" data-to="${esc(e.to)}"`;
|
|
92
|
-
const path = (e) => {
|
|
93
|
-
const section = e.sections?.[0];
|
|
94
|
-
if (section === undefined)
|
|
95
|
-
return '';
|
|
96
|
-
const pts = [section.startPoint, ...(section.bendPoints ?? []), section.endPoint];
|
|
97
|
-
return pts.map((p, i) => `${i === 0 ? 'M' : 'L'}${r1(p.x)} ${r1(p.y)}`).join(' ');
|
|
98
|
-
};
|
|
99
|
-
for (const e of drawn) {
|
|
100
|
-
if (e.id === 'e_start')
|
|
101
|
-
out.push(`<path class="edge edge-system" d="${path(e)}" marker-end="url(#arrow-${view}-system)"/>`);
|
|
102
|
-
}
|
|
103
|
-
for (const pass of [false, true]) {
|
|
104
|
-
for (const e of drawn) {
|
|
105
|
-
const viz = byId.get(e.id);
|
|
106
|
-
if (viz === undefined || isMain(viz) !== pass)
|
|
107
|
-
continue;
|
|
108
|
-
const kind = pass ? 'main' : viz.kind;
|
|
109
|
-
out.push(`<path class="edge edge-${kind}${viz.unused ? ' edge-unused' : ''}" ${ends(viz)} d="${path(e)}" marker-end="url(#arrow-${view}-${kind})"/>`);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
for (const e of drawn) {
|
|
113
|
-
const viz = byId.get(e.id);
|
|
114
|
-
const label = e.labels?.[0];
|
|
115
|
-
if (viz === undefined || label === undefined)
|
|
116
|
-
continue;
|
|
117
|
-
const lines = labelLines(viz, view);
|
|
118
|
-
const x = label.x ?? 0;
|
|
119
|
-
const y = label.y ?? 0;
|
|
120
|
-
out.push(`<g class="elabel${isMain(viz) ? ' elabel-main' : ''}${viz.kind === 'fail' ? ' elabel-fail' : ''}" ${ends(viz)}><rect x="${r1(x)}" y="${r1(y)}" width="${label.width ?? 0}" height="${label.height ?? 0}" rx="4"/>`);
|
|
121
|
-
lines.forEach((l, i) => out.push(`<text x="${r1(x + 7)}" y="${r1(y + 17 + i * 17)}">${esc(l)}</text>`));
|
|
122
|
-
out.push('</g>');
|
|
123
|
-
}
|
|
124
|
-
for (const n of laid.children ?? []) {
|
|
125
|
-
const x = r1(n.x ?? 0);
|
|
126
|
-
const y = r1(n.y ?? 0);
|
|
127
|
-
const w = n.width ?? 0;
|
|
128
|
-
const h = n.height ?? 0;
|
|
129
|
-
if (n.id === START) {
|
|
130
|
-
out.push(`<circle class="start" cx="${r1(x + 7)}" cy="${r1(y + 7)}" r="7"><title>start</title></circle>`);
|
|
131
|
-
continue;
|
|
132
|
-
}
|
|
133
|
-
const kind = kindOf(g, n.id);
|
|
134
|
-
const st = g.flow.states[n.id];
|
|
135
|
-
const exitClass = kind === 'exit' ? ` exit-${st.leavesPage === true ? 'left' : (st.redirect ?? 'error')}` : '';
|
|
136
|
-
const unreachable = !ex.atState.has(n.id);
|
|
137
|
-
const open = kind === 'screen' ? `<a href="#screen-${esc(n.id)}">` : '';
|
|
138
|
-
const close = kind === 'screen' ? '</a>' : '';
|
|
139
|
-
const by = st.by === undefined ? '' : ` by-${st.by}`;
|
|
140
|
-
const tip = [st.description ?? n.id, ...(st.includes ?? []).map((s) => `+ ${s}`)].join('\n');
|
|
141
|
-
out.push(`${open}<g class="node node-${kind}${by}${st.goal === true ? ' goal' : ''}${exitClass}${mainNodes.has(n.id) ? ' on-main' : ''}${unreachable ? ' node-unreachable' : ''}" data-state="${esc(n.id)}"><title>${esc(tip)}</title>`);
|
|
142
|
-
const rx = kind === 'exit' || st.by === 'ai' ? 28 : st.by === 'ci' ? 0 : 8;
|
|
143
|
-
out.push(`<rect x="${x}" y="${y}" width="${w}" height="${h}" rx="${rx}"/>`);
|
|
144
|
-
if (st.by === 'agent')
|
|
145
|
-
out.push(`<rect class="inner" x="${r1(x + 4)}" y="${r1(y + 4)}" width="${w - 8}" height="${h - 8}" rx="5"/>`);
|
|
146
|
-
if ((st.includes ?? []).length > 0)
|
|
147
|
-
out.push(`<text class="nmore" x="${r1(x + w - 8)}" y="${r1(y + 14)}" text-anchor="end">+${st.includes?.length}</text>`);
|
|
148
|
-
if (kind === 'subflow')
|
|
149
|
-
out.push(`<rect class="inner" x="${r1(x + 4)}" y="${r1(y + 4)}" width="${w - 8}" height="${h - 8}" rx="5"/>`);
|
|
150
|
-
out.push(`<text class="nname" x="${r1(x + w / 2)}" y="${r1(y + 25)}" text-anchor="middle">${esc(titleOf(g.flow, n.id))}</text>`);
|
|
151
|
-
out.push(`<text class="nline" x="${r1(x + w / 2)}" y="${r1(y + 44)}" text-anchor="middle">${esc(nodeLine(g, n.id))}${unreachable ? ' · unreachable' : ''}</text>`);
|
|
152
|
-
out.push(`</g>${close}`);
|
|
153
|
-
}
|
|
154
|
-
out.push('</svg>');
|
|
155
|
-
return out.join('\n');
|
|
156
|
-
}
|
package/dist/starts.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
// `starts:` on a transition: this flow starts another one and does not wait for it (SPEC.md §2, "Several flows").
|
|
2
|
-
// The started flow is looked up by id among the graphs beside this one. When it is found, every situation in which the
|
|
3
|
-
// transition is taken must give it a beginning it accepts; when it is not written yet, that is a note, not a failure.
|
|
4
|
-
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
5
|
-
import { basename, dirname, join } from 'node:path';
|
|
6
|
-
import { readYaml } from './yaml.js';
|
|
7
|
-
import { alternatives } from './graph.js';
|
|
8
|
-
const SKIP = new Set(['viz', 'video', 'node_modules', '.git']);
|
|
9
|
-
// the root to search: the nearest folder called `graphs` above the graph, else the graph's parent
|
|
10
|
-
function rootOf(dir) {
|
|
11
|
-
for (let d = dir, i = 0; i < 5; i++, d = dirname(d))
|
|
12
|
-
if (basename(d) === 'graphs')
|
|
13
|
-
return d;
|
|
14
|
-
return dirname(dir);
|
|
15
|
-
}
|
|
16
|
-
// every flow under root by its id; a flow.yaml that cannot be read is left out, as its own check reports it
|
|
17
|
-
function flowsUnder(root, depth = 4, out = new Map()) {
|
|
18
|
-
const file = join(root, 'flow.yaml');
|
|
19
|
-
if (existsSync(file)) {
|
|
20
|
-
let f = null;
|
|
21
|
-
try {
|
|
22
|
-
f = readYaml(readFileSync(file, 'utf8'));
|
|
23
|
-
}
|
|
24
|
-
catch {
|
|
25
|
-
f = null;
|
|
26
|
-
}
|
|
27
|
-
if (typeof f?.id === 'string')
|
|
28
|
-
out.set(f.id, { ...f, context: f.context ?? {} });
|
|
29
|
-
}
|
|
30
|
-
if (depth === 0)
|
|
31
|
-
return out;
|
|
32
|
-
for (const name of readdirSync(root)) {
|
|
33
|
-
if (SKIP.has(name))
|
|
34
|
-
continue;
|
|
35
|
-
const sub = join(root, name);
|
|
36
|
-
if (statSync(sub).isDirectory())
|
|
37
|
-
flowsUnder(sub, depth - 1, out);
|
|
38
|
-
}
|
|
39
|
-
return out;
|
|
40
|
-
}
|
|
41
|
-
export function checkStarts(g, m, ex) {
|
|
42
|
-
const out = [];
|
|
43
|
-
let flows = null;
|
|
44
|
-
for (const [name, st] of Object.entries(g.flow.states)) {
|
|
45
|
-
const ways = [
|
|
46
|
-
...(st.always ?? []).map((t, i) => [null, i, t]),
|
|
47
|
-
...Object.entries(st.on ?? {}).flatMap(([event, spec]) => alternatives(spec).map((t, i) => [event, i, t])),
|
|
48
|
-
];
|
|
49
|
-
for (const [event, i, t] of ways) {
|
|
50
|
-
if (t.starts === undefined)
|
|
51
|
-
continue;
|
|
52
|
-
flows ??= flowsUnder(rootOf(g.dir));
|
|
53
|
-
const given = t.starts.given ?? {};
|
|
54
|
-
const started = flows.get(t.starts.flow);
|
|
55
|
-
const hand = { from: name, event, flow: t.starts.flow, given, found: started !== undefined, problems: [] };
|
|
56
|
-
out.push(hand);
|
|
57
|
-
if (started === undefined)
|
|
58
|
-
continue;
|
|
59
|
-
const problem = (p) => {
|
|
60
|
-
if (!hand.problems.includes(p))
|
|
61
|
-
hand.problems.push(p);
|
|
62
|
-
};
|
|
63
|
-
for (const k of Object.keys(given))
|
|
64
|
-
if (started.context[k] === undefined)
|
|
65
|
-
problem(`${k} is not a variable of ${started.id}`);
|
|
66
|
-
const taken = `${name}|${event ?? 'always'}|${i}`;
|
|
67
|
-
for (const c of ex.configs) {
|
|
68
|
-
if (c.state !== name)
|
|
69
|
-
continue;
|
|
70
|
-
for (const s of m.steps(c.state, c.ctx)) {
|
|
71
|
-
if (s.taken !== taken)
|
|
72
|
-
continue;
|
|
73
|
-
for (const [k, expr] of Object.entries(given)) {
|
|
74
|
-
const cv = started.context[k];
|
|
75
|
-
if (cv === undefined)
|
|
76
|
-
continue;
|
|
77
|
-
const v = typeof expr === 'string' ? m.value(expr, name, s.ctx) : expr;
|
|
78
|
-
if (!cv.values.includes(v))
|
|
79
|
-
problem(`${k} would be ${JSON.stringify(v)}, which is not a value of ${started.id}'s ${k}`);
|
|
80
|
-
else if (cv.initial !== 'any' && cv.initial !== v)
|
|
81
|
-
problem(`${started.id} begins with ${k} = ${String(cv.initial)}, and here it would be ${String(v)}`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return out;
|
|
88
|
-
}
|
package/dist/story.js
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { alternatives, titleOf } from './graph.js';
|
|
2
|
-
import { kindOf, pressLabel } from './edges.js';
|
|
3
|
-
export function stepsTo(m, ex, index) {
|
|
4
|
-
const chain = [];
|
|
5
|
-
for (let i = index; i >= 0; i = ex.configs[i].parent)
|
|
6
|
-
chain.push(i);
|
|
7
|
-
chain.reverse();
|
|
8
|
-
return chain.map((ci, k) => {
|
|
9
|
-
const c = ex.configs[ci];
|
|
10
|
-
let taken = null;
|
|
11
|
-
if (k > 0) {
|
|
12
|
-
const prev = ex.configs[chain[k - 1]];
|
|
13
|
-
const hit = m.steps(prev.state, prev.ctx).find((s) => s.target === c.state && s.event === c.event && s.ctx.join(',') === c.ctx.join(','));
|
|
14
|
-
taken = hit?.taken ?? null;
|
|
15
|
-
}
|
|
16
|
-
return { state: c.state, ctx: c.ctx, event: c.event, taken };
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
// The main story the graph declares: from a starting situation, send the events in order (`always` rules run by themselves).
|
|
20
|
-
export function stepsByEvents(m, main) {
|
|
21
|
-
const events = Array.isArray(main) ? main : main.events;
|
|
22
|
-
const given = Array.isArray(main) ? {} : (main.given ?? {});
|
|
23
|
-
for (const init of m.initials()) {
|
|
24
|
-
if (Object.entries(given).some(([k, v]) => init[m.vars.indexOf(k)] !== v))
|
|
25
|
-
continue;
|
|
26
|
-
const path = walk(m, [{ state: m.flow.initial, ctx: init, event: null, taken: null }], events, 0);
|
|
27
|
-
if (path !== null)
|
|
28
|
-
return path;
|
|
29
|
-
}
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
// Depth first, in document order, so the first walk that sends every event wins; when an effect leaves several
|
|
33
|
-
// situations (havoc), one that cannot go on gives way to the next.
|
|
34
|
-
function walk(m, path, events, i) {
|
|
35
|
-
if (path.length > 500)
|
|
36
|
-
return null;
|
|
37
|
-
const cur = path[path.length - 1];
|
|
38
|
-
const steps = m.steps(cur.state, cur.ctx);
|
|
39
|
-
const autos = steps.filter((s) => s.event === null);
|
|
40
|
-
const next = autos.length > 0 ? autos : i < events.length ? steps.filter((s) => s.event === events[i]) : [];
|
|
41
|
-
if (next.length === 0)
|
|
42
|
-
return i === events.length ? path : null;
|
|
43
|
-
for (const s of next) {
|
|
44
|
-
const found = walk(m, [...path, { state: s.target, ctx: s.ctx, event: s.event, taken: s.taken }], events, autos.length > 0 ? i : i + 1);
|
|
45
|
-
if (found !== null)
|
|
46
|
-
return found;
|
|
47
|
-
}
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
// The first success exit reachable through at least one screen: the story a person expects to read first.
|
|
51
|
-
export function mainStoryIndex(g, ex) {
|
|
52
|
-
const viaScreen = [];
|
|
53
|
-
ex.configs.forEach((c, i) => {
|
|
54
|
-
viaScreen[i] = kindOf(g, c.state) === 'screen' || (c.parent >= 0 && viaScreen[c.parent]);
|
|
55
|
-
});
|
|
56
|
-
for (const [name, st] of Object.entries(g.flow.states)) {
|
|
57
|
-
if (st.type !== 'final' || st.redirect !== 'success')
|
|
58
|
-
continue;
|
|
59
|
-
const i = ex.configs.findIndex((c, idx) => c.state === name && viaScreen[idx]);
|
|
60
|
-
if (i >= 0)
|
|
61
|
-
return i;
|
|
62
|
-
}
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
function sayOf(g, taken) {
|
|
66
|
-
if (taken === null)
|
|
67
|
-
return null;
|
|
68
|
-
const [state, event, idx] = taken.split('|');
|
|
69
|
-
const st = g.flow.states[state];
|
|
70
|
-
const a = event === 'always' ? st.always?.[Number(idx)] : alternatives(st.on?.[event] ?? []).at(Number(idx));
|
|
71
|
-
return a?.say ?? null;
|
|
72
|
-
}
|
|
73
|
-
const capital = (s) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
74
|
-
export function narrate(g, m, steps) {
|
|
75
|
-
const start = steps[0];
|
|
76
|
-
const parts = [];
|
|
77
|
-
m.vars.forEach((v, i) => {
|
|
78
|
-
const cv = g.flow.context[v];
|
|
79
|
-
if (cv.initial !== 'any')
|
|
80
|
-
return;
|
|
81
|
-
const said = cv.say?.[String(start.ctx[i])];
|
|
82
|
-
if (said === undefined)
|
|
83
|
-
parts.push(`${v} is ${start.ctx[i]}`);
|
|
84
|
-
else if (said !== '')
|
|
85
|
-
parts.push(said);
|
|
86
|
-
});
|
|
87
|
-
const lines = [];
|
|
88
|
-
steps.forEach((s, k) => {
|
|
89
|
-
if (g.flow.states[s.state].type === 'final')
|
|
90
|
-
return;
|
|
91
|
-
const next = steps[k + 1];
|
|
92
|
-
let line = titleOf(g.flow, s.state);
|
|
93
|
-
if (next !== undefined) {
|
|
94
|
-
const say = sayOf(g, next.taken);
|
|
95
|
-
const press = next.event === null ? null : pressLabel(g, s.state, next.event);
|
|
96
|
-
if (press !== null)
|
|
97
|
-
line += `: the user presses “${press}”`;
|
|
98
|
-
else if (say !== null)
|
|
99
|
-
line += next.event?.startsWith('FAIL') === true ? `: it fails, because ${say}` : `: ${say}`;
|
|
100
|
-
}
|
|
101
|
-
lines.push(line);
|
|
102
|
-
});
|
|
103
|
-
const last = steps[steps.length - 1];
|
|
104
|
-
return {
|
|
105
|
-
given: parts.length > 0 ? capital(parts.join(', ')) : 'Any request',
|
|
106
|
-
steps: lines,
|
|
107
|
-
result: titleOf(g.flow, last.state),
|
|
108
|
-
};
|
|
109
|
-
}
|