waygraph 0.15.21 → 0.15.23
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/dist/auto-explore-run.d.ts +2 -1
- package/dist/auto-explore-run.js +2 -1
- package/dist/auto-session.js +2 -1
- package/dist/cli.js +21 -6067
- package/dist/runner/chain-run.js +102 -0
- package/dist/runner/cursor.js +140 -0
- package/dist/runner/demo-log.js +314 -0
- package/dist/runner/device-stage.js +400 -0
- package/dist/runner/discovery.js +105 -0
- package/dist/runner/index.js +2 -0
- package/dist/runner/interaction-patch.js +353 -0
- package/dist/runner/main.js +397 -0
- package/dist/runner/overlay-css.js +342 -0
- package/dist/runner/overlay-install.js +1301 -0
- package/dist/runner/rings.js +490 -0
- package/dist/runner/seed-mem.js +110 -0
- package/dist/runner/slides.js +272 -0
- package/dist/runner/step-mode.js +776 -0
- package/dist/runner/step-panels.js +723 -0
- package/dist/runner/teardown.js +195 -0
- package/dist/runner/todo-dock.js +142 -0
- package/examples/saucedemo/.waygraph-auto/19b9eee5.log +0 -0
- package/examples/saucedemo/.waygraph-auto/422d4390.log +0 -0
- package/examples/saucedemo/.waygraph-auto/6c9f8e10.log +0 -0
- package/examples/saucedemo/.waygraph-auto/74ce745a.log +0 -0
- package/examples/saucedemo/.waygraph-auto/89477e92.log +0 -0
- package/examples/saucedemo/.waygraph-auto/b7e545f0.log +0 -0
- package/examples/saucedemo/.waygraph-auto/cb2726d7.log +0 -0
- package/package.json +8 -4
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
// Moved verbatim from the former CHAIN_RUNNER_SCRIPT template string in cli.ts (see src/ARCHITECTURE.md).
|
|
2
|
+
// Runs inside the target project's own waygraph copy - keep it dependency-light and self-contained.
|
|
3
|
+
import { resolveDeviceState } from "../highlights.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Fit + center the OS browser window on the device viewport so mobile/tablet
|
|
7
|
+
* is not stuck top-left inside a maximized frame (Dan 0.13.4).
|
|
8
|
+
* Desktop clear restores maximized.
|
|
9
|
+
*/
|
|
10
|
+
async function fitWindowToDeviceViewport(page, viewport, opts) {
|
|
11
|
+
const maximize = !!(opts && opts.maximize);
|
|
12
|
+
let client;
|
|
13
|
+
try {
|
|
14
|
+
client = await page.context().newCDPSession(page);
|
|
15
|
+
const { windowId } = await client.send("Browser.getWindowForTarget");
|
|
16
|
+
if (maximize) {
|
|
17
|
+
await client.send("Browser.setWindowBounds", {
|
|
18
|
+
windowId,
|
|
19
|
+
bounds: { windowState: "maximized" },
|
|
20
|
+
});
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (!viewport || !viewport.width || !viewport.height) return;
|
|
24
|
+
const metrics = await page
|
|
25
|
+
.evaluate(() => {
|
|
26
|
+
const chromeW = Math.max(0, (window.outerWidth || 0) - (window.innerWidth || 0));
|
|
27
|
+
const chromeH = Math.max(0, (window.outerHeight || 0) - (window.innerHeight || 0));
|
|
28
|
+
return {
|
|
29
|
+
chromeW: Number.isFinite(chromeW) ? chromeW : 0,
|
|
30
|
+
// First paint after maximize often reports 0 chrome - use a floor.
|
|
31
|
+
chromeH: chromeH > 20 ? chromeH : 88,
|
|
32
|
+
screenW: window.screen.availWidth || 1920,
|
|
33
|
+
screenH: window.screen.availHeight || 1080,
|
|
34
|
+
};
|
|
35
|
+
})
|
|
36
|
+
.catch(() => ({ chromeW: 0, chromeH: 88, screenW: 1920, screenH: 1080 }));
|
|
37
|
+
// Leave maximized before setting pixel bounds (Chromium ignores size while max).
|
|
38
|
+
await client.send("Browser.setWindowBounds", {
|
|
39
|
+
windowId,
|
|
40
|
+
bounds: { windowState: "normal" },
|
|
41
|
+
});
|
|
42
|
+
const winW = Math.min(
|
|
43
|
+
metrics.screenW,
|
|
44
|
+
Math.max(320, Math.floor(viewport.width + metrics.chromeW)),
|
|
45
|
+
);
|
|
46
|
+
const winH = Math.min(
|
|
47
|
+
metrics.screenH,
|
|
48
|
+
Math.max(320, Math.floor(viewport.height + metrics.chromeH)),
|
|
49
|
+
);
|
|
50
|
+
const left = Math.max(0, Math.floor((metrics.screenW - winW) / 2));
|
|
51
|
+
const top = Math.max(0, Math.floor((metrics.screenH - winH) / 2));
|
|
52
|
+
await client.send("Browser.setWindowBounds", {
|
|
53
|
+
windowId,
|
|
54
|
+
bounds: {
|
|
55
|
+
windowState: "normal",
|
|
56
|
+
left,
|
|
57
|
+
top,
|
|
58
|
+
width: winW,
|
|
59
|
+
height: winH,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
} catch {
|
|
63
|
+
/* headless / no window - ignore */
|
|
64
|
+
} finally {
|
|
65
|
+
if (client) await client.detach().catch(() => {});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Video recording locks Playwright viewport to recordVideo.size. Shrinking
|
|
72
|
+
* setViewportSize to mobile/tablet letterboxes the .webm top-left. Instead keep
|
|
73
|
+
* the stage size and center a device-sized #wg-device-shell inside the frame.
|
|
74
|
+
*/
|
|
75
|
+
export async function applyVideoDeviceStage(page, target, stage, opts) {
|
|
76
|
+
const clear = !!(opts && opts.clear);
|
|
77
|
+
const shutterOut = !!(opts && opts.shutterOut);
|
|
78
|
+
const enterIn = !!(opts && opts.enterIn);
|
|
79
|
+
const desktopFlat = !!(opts && (opts.desktopFlat || opts.shutterOut));
|
|
80
|
+
const result = await page
|
|
81
|
+
.evaluate(
|
|
82
|
+
({ tw, th, sw, sh, clear, shutterOut, enterIn, desktopFlat }) => {
|
|
83
|
+
const MATTE = "#0b1220";
|
|
84
|
+
const unwrap = () => {
|
|
85
|
+
const shell = document.getElementById("wg-device-shell");
|
|
86
|
+
if (shell) {
|
|
87
|
+
const parent = shell.parentNode;
|
|
88
|
+
if (parent) {
|
|
89
|
+
while (shell.firstChild) parent.insertBefore(shell.firstChild, shell);
|
|
90
|
+
}
|
|
91
|
+
shell.remove();
|
|
92
|
+
}
|
|
93
|
+
document.documentElement.classList.remove("wg-video-device-stage");
|
|
94
|
+
document.documentElement.style.removeProperty("background");
|
|
95
|
+
const b = document.body;
|
|
96
|
+
if (b) {
|
|
97
|
+
b.style.margin = "";
|
|
98
|
+
b.style.minHeight = "";
|
|
99
|
+
b.style.display = "";
|
|
100
|
+
b.style.alignItems = "";
|
|
101
|
+
b.style.justifyContent = "";
|
|
102
|
+
b.style.background = "";
|
|
103
|
+
b.style.overflow = "";
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
if (clear) {
|
|
107
|
+
unwrap();
|
|
108
|
+
return { waitMs: 0 };
|
|
109
|
+
}
|
|
110
|
+
if (!document.body) return { waitMs: 0 };
|
|
111
|
+
document.documentElement.classList.add("wg-video-device-stage");
|
|
112
|
+
document.documentElement.style.background = MATTE;
|
|
113
|
+
document.body.style.margin = "0";
|
|
114
|
+
document.body.style.minHeight = "100vh";
|
|
115
|
+
document.body.style.display = "flex";
|
|
116
|
+
document.body.style.alignItems = "center";
|
|
117
|
+
document.body.style.justifyContent = "center";
|
|
118
|
+
document.body.style.background = MATTE;
|
|
119
|
+
document.body.style.overflow = "hidden";
|
|
120
|
+
|
|
121
|
+
let shell = document.getElementById("wg-device-shell");
|
|
122
|
+
const created = !shell;
|
|
123
|
+
if (!shell) {
|
|
124
|
+
shell = document.createElement("div");
|
|
125
|
+
shell.id = "wg-device-shell";
|
|
126
|
+
const move = [];
|
|
127
|
+
for (const child of [...document.body.childNodes]) {
|
|
128
|
+
if (
|
|
129
|
+
child.nodeType === 1 &&
|
|
130
|
+
child.getAttribute &&
|
|
131
|
+
child.getAttribute("data-wg-ui") === "1"
|
|
132
|
+
) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (child.nodeType === 1 && child.id === "wg-device-shell") continue;
|
|
136
|
+
move.push(child);
|
|
137
|
+
}
|
|
138
|
+
for (const n of move) shell.appendChild(n);
|
|
139
|
+
document.body.appendChild(shell);
|
|
140
|
+
}
|
|
141
|
+
// Desktop flat: always full-bleed, never reintroduce bezel on overlay rebuild.
|
|
142
|
+
if (desktopFlat) {
|
|
143
|
+
shell.classList.remove("wg-shell-enter");
|
|
144
|
+
shell.classList.add("wg-shell-shutter-out");
|
|
145
|
+
shell.classList.add("wg-shell-desktop-flat");
|
|
146
|
+
shell.style.width = sw + "px";
|
|
147
|
+
shell.style.height = sh + "px";
|
|
148
|
+
shell.style.maxWidth = sw + "px";
|
|
149
|
+
shell.style.maxHeight = sh + "px";
|
|
150
|
+
shell.style.transform = "scale(1)";
|
|
151
|
+
shell.style.transformOrigin = "center center";
|
|
152
|
+
shell.style.borderRadius = "0";
|
|
153
|
+
shell.style.boxShadow = "none";
|
|
154
|
+
shell.style.border = "none";
|
|
155
|
+
shell.style.outline = "none";
|
|
156
|
+
return { waitMs: shutterOut ? 480 : 0 };
|
|
157
|
+
}
|
|
158
|
+
// Leaving desktop-flat: clear INLINE radius/shadow overrides so CSS
|
|
159
|
+
// border-radius:16px applies evenly on all corners again.
|
|
160
|
+
shell.style.removeProperty("border-radius");
|
|
161
|
+
shell.style.removeProperty("box-shadow");
|
|
162
|
+
shell.style.removeProperty("border");
|
|
163
|
+
shell.style.removeProperty("outline");
|
|
164
|
+
if (enterIn && (created || shell.classList.contains("wg-shell-shutter-out"))) {
|
|
165
|
+
shell.classList.remove("wg-shell-shutter-out");
|
|
166
|
+
shell.classList.remove("wg-shell-desktop-flat");
|
|
167
|
+
shell.classList.add("wg-shell-enter");
|
|
168
|
+
void shell.offsetWidth;
|
|
169
|
+
}
|
|
170
|
+
const scale = Math.min(sw / tw, sh / th) * 0.92;
|
|
171
|
+
shell.style.width = tw + "px";
|
|
172
|
+
shell.style.height = th + "px";
|
|
173
|
+
shell.style.maxWidth = tw + "px";
|
|
174
|
+
shell.style.maxHeight = th + "px";
|
|
175
|
+
shell.style.transform = "scale(" + scale + ")";
|
|
176
|
+
shell.style.transformOrigin = "center center";
|
|
177
|
+
shell.classList.remove("wg-shell-shutter-out");
|
|
178
|
+
shell.classList.remove("wg-shell-desktop-flat");
|
|
179
|
+
if (enterIn && shell.classList.contains("wg-shell-enter")) {
|
|
180
|
+
requestAnimationFrame(() => {
|
|
181
|
+
shell.classList.remove("wg-shell-enter");
|
|
182
|
+
});
|
|
183
|
+
return { waitMs: 520 };
|
|
184
|
+
}
|
|
185
|
+
shell.classList.remove("wg-shell-enter");
|
|
186
|
+
return { waitMs: 0 };
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
tw: target.width,
|
|
190
|
+
th: target.height,
|
|
191
|
+
sw: stage.width,
|
|
192
|
+
sh: stage.height,
|
|
193
|
+
clear,
|
|
194
|
+
shutterOut,
|
|
195
|
+
enterIn,
|
|
196
|
+
desktopFlat,
|
|
197
|
+
},
|
|
198
|
+
)
|
|
199
|
+
.catch(() => ({ waitMs: 0 }));
|
|
200
|
+
const waitMs = result && result.waitMs ? Number(result.waitMs) : 0;
|
|
201
|
+
if (waitMs > 0) await new Promise((r) => setTimeout(r, waitMs));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Apply authored device fixture to the live page (0.13+).
|
|
207
|
+
* Seamless viewport lerp + centered window + toast on set/clear.
|
|
208
|
+
* With --video: keep recordVideo stage size and center a device shell in-frame.
|
|
209
|
+
*/
|
|
210
|
+
export async function applyDeviceToPage(page, device, sync) {
|
|
211
|
+
const mode = sync || (device ? "set" : "keep");
|
|
212
|
+
if (mode === "keep" && !device) return;
|
|
213
|
+
const d =
|
|
214
|
+
device ||
|
|
215
|
+
resolveDeviceState("desktop", false) || {
|
|
216
|
+
preset: "desktop",
|
|
217
|
+
viewport: { width: 1280, height: 720, deviceScaleFactor: 1 },
|
|
218
|
+
isMobile: false,
|
|
219
|
+
hasTouch: false,
|
|
220
|
+
touchMode: false,
|
|
221
|
+
};
|
|
222
|
+
const realSet =
|
|
223
|
+
typeof page.__wgRealSetViewportSize === "function"
|
|
224
|
+
? page.__wgRealSetViewportSize
|
|
225
|
+
: page.setViewportSize.bind(page);
|
|
226
|
+
const target = {
|
|
227
|
+
width: Math.max(200, Math.floor(d.viewport.width)),
|
|
228
|
+
height: Math.max(200, Math.floor(d.viewport.height)),
|
|
229
|
+
};
|
|
230
|
+
const videoStage =
|
|
231
|
+
page.__wgVideoViewport &&
|
|
232
|
+
page.__wgVideoViewport.width > 0 &&
|
|
233
|
+
page.__wgVideoViewport.height > 0
|
|
234
|
+
? {
|
|
235
|
+
width: Math.floor(page.__wgVideoViewport.width),
|
|
236
|
+
height: Math.floor(page.__wgVideoViewport.height),
|
|
237
|
+
}
|
|
238
|
+
: null;
|
|
239
|
+
let from = null;
|
|
240
|
+
try {
|
|
241
|
+
from = page.viewportSize();
|
|
242
|
+
} catch {
|
|
243
|
+
from = null;
|
|
244
|
+
}
|
|
245
|
+
const announce = mode === "set" || mode === "clear";
|
|
246
|
+
const toDesktop =
|
|
247
|
+
mode === "clear" || (d.preset === "desktop" && !d.touchMode);
|
|
248
|
+
const shouldAnimate =
|
|
249
|
+
announce &&
|
|
250
|
+
from &&
|
|
251
|
+
from.width > 0 &&
|
|
252
|
+
from.height > 0 &&
|
|
253
|
+
(from.width !== target.width || from.height !== target.height);
|
|
254
|
+
|
|
255
|
+
// Always clear camera zoom when returning to desktop (video + headed).
|
|
256
|
+
if (toDesktop) {
|
|
257
|
+
page.__wgDesktopFlat = true;
|
|
258
|
+
await page
|
|
259
|
+
.evaluate(() => {
|
|
260
|
+
window.__wgZoomOutOnHide = true;
|
|
261
|
+
if (window.__wgClearZoom) window.__wgClearZoom();
|
|
262
|
+
})
|
|
263
|
+
.catch(() => {});
|
|
264
|
+
} else if (announce) {
|
|
265
|
+
page.__wgDesktopFlat = false;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Swipe trail is for scrolling (see ensureLocatorInView), not device morph.
|
|
269
|
+
const nextOrient =
|
|
270
|
+
d.orientation === "landscape" || d.orientation === "portrait"
|
|
271
|
+
? d.orientation
|
|
272
|
+
: target.height > target.width
|
|
273
|
+
? "portrait"
|
|
274
|
+
: "landscape";
|
|
275
|
+
page.__wgDeviceOrient = nextOrient;
|
|
276
|
+
page.__wgDevicePreset = toDesktop ? "desktop" : d.preset || "";
|
|
277
|
+
|
|
278
|
+
if (videoStage) {
|
|
279
|
+
// Lock Playwright viewport to the recordVideo size so frames fill the
|
|
280
|
+
// .webm; center the device shell inside (OS window centering alone does not).
|
|
281
|
+
// Do NOT fitWindow to the device size - that shrinks the viewport below the
|
|
282
|
+
// recordVideo canvas and Playwright pads the .webm with grey (top-left bias).
|
|
283
|
+
try {
|
|
284
|
+
await realSet(videoStage);
|
|
285
|
+
} catch {
|
|
286
|
+
/* ignore */
|
|
287
|
+
}
|
|
288
|
+
const shellFrom = page.__wgDeviceShell || target;
|
|
289
|
+
if (toDesktop) {
|
|
290
|
+
// Zoom already cleared above. Full-bleed desktop shell (no matte frame).
|
|
291
|
+
// Shutter-out: soft rounded -> radius 0, then flat edge-to-edge.
|
|
292
|
+
const desk = {
|
|
293
|
+
width: videoStage.width,
|
|
294
|
+
height: videoStage.height,
|
|
295
|
+
};
|
|
296
|
+
page.__wgDeviceShell = { ...desk };
|
|
297
|
+
await applyVideoDeviceStage(page, desk, videoStage, {
|
|
298
|
+
clear: false,
|
|
299
|
+
shutterOut: true,
|
|
300
|
+
desktopFlat: true,
|
|
301
|
+
});
|
|
302
|
+
try {
|
|
303
|
+
await realSet(videoStage);
|
|
304
|
+
} catch {
|
|
305
|
+
/* ignore */
|
|
306
|
+
}
|
|
307
|
+
} else {
|
|
308
|
+
// Lerp shell size when switching mobile <-> tablet mid-video.
|
|
309
|
+
const firstDevice = !page.__wgDeviceShell;
|
|
310
|
+
if (
|
|
311
|
+
announce &&
|
|
312
|
+
shellFrom &&
|
|
313
|
+
(shellFrom.width !== target.width || shellFrom.height !== target.height)
|
|
314
|
+
) {
|
|
315
|
+
const steps = 14;
|
|
316
|
+
const ms = Number(process.env.WAYGRAPH_DEVICE_TRANSITION_MS || 520);
|
|
317
|
+
const stepMs = Math.max(12, Math.floor(ms / steps));
|
|
318
|
+
for (let i = 1; i <= steps; i++) {
|
|
319
|
+
const t = i / steps;
|
|
320
|
+
const e = t * t * (3 - 2 * t);
|
|
321
|
+
const mid = {
|
|
322
|
+
width: Math.round(shellFrom.width + (target.width - shellFrom.width) * e),
|
|
323
|
+
height: Math.round(shellFrom.height + (target.height - shellFrom.height) * e),
|
|
324
|
+
};
|
|
325
|
+
await applyVideoDeviceStage(page, mid, videoStage, {
|
|
326
|
+
clear: false,
|
|
327
|
+
enterIn: i === 1 && firstDevice,
|
|
328
|
+
});
|
|
329
|
+
if (i < steps) await new Promise((r) => setTimeout(r, stepMs));
|
|
330
|
+
}
|
|
331
|
+
} else {
|
|
332
|
+
await applyVideoDeviceStage(page, target, videoStage, {
|
|
333
|
+
clear: false,
|
|
334
|
+
enterIn: firstDevice || announce,
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
page.__wgDeviceShell = { ...target };
|
|
338
|
+
// Re-assert stage size after any prior non-video fitWindow.
|
|
339
|
+
try {
|
|
340
|
+
await realSet(videoStage);
|
|
341
|
+
} catch {
|
|
342
|
+
/* ignore */
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
} else {
|
|
346
|
+
// Unmaximize + center early so the lerp does not sit in the corner of a
|
|
347
|
+
// maximized frame. Desktop clear maximizes at the end instead.
|
|
348
|
+
if (!toDesktop) {
|
|
349
|
+
await fitWindowToDeviceViewport(page, from && from.width ? from : target, {
|
|
350
|
+
maximize: false,
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
try {
|
|
355
|
+
if (shouldAnimate) {
|
|
356
|
+
const steps = 14;
|
|
357
|
+
const ms = Number(process.env.WAYGRAPH_DEVICE_TRANSITION_MS || 520);
|
|
358
|
+
const stepMs = Math.max(12, Math.floor(ms / steps));
|
|
359
|
+
for (let i = 1; i <= steps; i++) {
|
|
360
|
+
const t = i / steps;
|
|
361
|
+
const e = t * t * (3 - 2 * t); // smoothstep
|
|
362
|
+
const mid = {
|
|
363
|
+
width: Math.round(from.width + (target.width - from.width) * e),
|
|
364
|
+
height: Math.round(from.height + (target.height - from.height) * e),
|
|
365
|
+
};
|
|
366
|
+
await realSet(mid);
|
|
367
|
+
if (!toDesktop && (i === 1 || i === steps || i % 3 === 0)) {
|
|
368
|
+
await fitWindowToDeviceViewport(page, mid, { maximize: false });
|
|
369
|
+
}
|
|
370
|
+
if (i < steps) await new Promise((r) => setTimeout(r, stepMs));
|
|
371
|
+
}
|
|
372
|
+
} else {
|
|
373
|
+
await realSet(target);
|
|
374
|
+
}
|
|
375
|
+
} catch {
|
|
376
|
+
/* maximized / null viewport hosts may reject - toast still updates */
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (toDesktop) {
|
|
380
|
+
await fitWindowToDeviceViewport(page, target, { maximize: true });
|
|
381
|
+
} else {
|
|
382
|
+
await fitWindowToDeviceViewport(page, target, { maximize: false });
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Fire toast near the end of the lerp so it reads as one seamless beat.
|
|
387
|
+
await page
|
|
388
|
+
.evaluate(
|
|
389
|
+
(payload) => {
|
|
390
|
+
if (window.__wgSyncDevice) window.__wgSyncDevice(payload);
|
|
391
|
+
},
|
|
392
|
+
{
|
|
393
|
+
sync: mode,
|
|
394
|
+
device: d,
|
|
395
|
+
remain: mode === "keep" || mode === "set",
|
|
396
|
+
announce,
|
|
397
|
+
},
|
|
398
|
+
)
|
|
399
|
+
.catch(() => {});
|
|
400
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Moved verbatim from the former CHAIN_RUNNER_SCRIPT template string in cli.ts (see src/ARCHITECTURE.md).
|
|
2
|
+
// Runs inside the target project's own waygraph copy - keep it dependency-light and self-contained.
|
|
3
|
+
import { readdirSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
|
|
6
|
+
function walkDir(dir, pattern) {
|
|
7
|
+
const results = [];
|
|
8
|
+
let entries;
|
|
9
|
+
try {
|
|
10
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
11
|
+
} catch {
|
|
12
|
+
return results;
|
|
13
|
+
}
|
|
14
|
+
for (const entry of entries) {
|
|
15
|
+
const full = join(dir, entry.name);
|
|
16
|
+
if (entry.isDirectory()) {
|
|
17
|
+
if (entry.name === "node_modules" || entry.name === "dist" || entry.name === ".git") continue;
|
|
18
|
+
results.push(...walkDir(full, pattern));
|
|
19
|
+
} else if (pattern.test(entry.name)) {
|
|
20
|
+
results.push(full);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return results;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
function isBlockLike(val) {
|
|
28
|
+
if (val === null || typeof val !== "object") return false;
|
|
29
|
+
if (typeof val.name !== "string") return false;
|
|
30
|
+
if (val.instruction === null || typeof val.instruction !== "object") return false;
|
|
31
|
+
return typeof val.instruction.act === "function";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
export async function findBlock(projectDir, ref) {
|
|
36
|
+
const files = walkDir(projectDir, /\.block\.ts$/);
|
|
37
|
+
const byRuntimeName = [];
|
|
38
|
+
for (const file of files) {
|
|
39
|
+
let mod;
|
|
40
|
+
try {
|
|
41
|
+
mod = await import("file://" + file);
|
|
42
|
+
} catch {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
for (const [exportName, exported] of Object.entries(mod)) {
|
|
46
|
+
if (!isBlockLike(exported)) continue;
|
|
47
|
+
if (exportName === ref) return { block: exported, exportName, file };
|
|
48
|
+
if (exported.name === ref) byRuntimeName.push({ block: exported, exportName, file });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (byRuntimeName.length === 1) return byRuntimeName[0];
|
|
52
|
+
if (byRuntimeName.length > 1) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
"waygraph chain: \"" + ref + "\" matches " + byRuntimeName.length + " Blocks by name - " +
|
|
55
|
+
byRuntimeName.map((b) => b.exportName + " (" + b.file + ")").join(", ") +
|
|
56
|
+
". Use the export name to disambiguate.",
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
throw new Error("waygraph chain: no Block named \"" + ref + "\" found under " + projectDir);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Finds an already-defined Flow by its export name (e.g. loginFlow) among
|
|
65
|
+
* the project's *.flow.ts files - "I already have this wired up, just point
|
|
66
|
+
* the stepper at it, no chain spec to hand-write." Flows have no runtime
|
|
67
|
+
* .name of their own (unlike Blocks), so this only matches by export
|
|
68
|
+
* identifier. Returns null (not a throw) when nothing matches - the caller
|
|
69
|
+
* falls back to ordinary block-chain spec parsing.
|
|
70
|
+
*/
|
|
71
|
+
export async function findFlow(projectDir, flowName) {
|
|
72
|
+
const files = walkDir(projectDir, /\.flow\.ts$/);
|
|
73
|
+
for (const file of files) {
|
|
74
|
+
let mod;
|
|
75
|
+
try {
|
|
76
|
+
mod = await import("file://" + file);
|
|
77
|
+
} catch {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const candidate = mod[flowName];
|
|
81
|
+
if (candidate && typeof candidate.run === "function") {
|
|
82
|
+
return candidate;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
export function parseChainSpec(spec) {
|
|
90
|
+
return spec
|
|
91
|
+
.split(/\bthen\b/)
|
|
92
|
+
.map((s) => s.trim())
|
|
93
|
+
.filter((s) => s.length > 0)
|
|
94
|
+
.map((s) => {
|
|
95
|
+
const m = /^([A-Za-z_$][\w-]*)\s*(?:\(([\s\S]*)\))?$/.exec(s);
|
|
96
|
+
if (!m) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
"waygraph chain: could not parse segment \"" + s + "\" - expected \"blockName\" or \"blockName({...json...})\"",
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
const ref = m[1];
|
|
102
|
+
const json = m[2] ? m[2].trim() : undefined;
|
|
103
|
+
return json ? { ref, json } : { ref };
|
|
104
|
+
});
|
|
105
|
+
}
|