|
| 1 | +#!/usr/bin/env bun |
| 2 | +/// <reference types="bun" /> |
| 3 | +/// <reference types="node" /> |
| 4 | + |
| 5 | +const startTime = Date.now() |
| 6 | + |
| 7 | +import chalk from 'chalk' |
| 8 | +import { CapsuleSpineFactory } from "@stream44.studio/encapsulate/spine-factories/CapsuleSpineFactory.v0" |
| 9 | +import { CapsuleSpineContract } from "@stream44.studio/encapsulate/spine-contracts/CapsuleSpineContract.v0/Membrane.v0" |
| 10 | +import { TimingObserver } from "@stream44.studio/encapsulate/spine-factories/TimingObserver" |
| 11 | + |
| 12 | +export async function run(encapsulateHandler: any, runHandler: any, options?: { importMeta?: { dir: string } }) { |
| 13 | + |
| 14 | + const timing = process.argv.includes('--trace') ? TimingObserver({ startTime }) : undefined |
| 15 | + |
| 16 | + timing?.recordMajor('INIT SPINE') |
| 17 | + |
| 18 | + const eventsByKey = new Map<string, any>() |
| 19 | + |
| 20 | + const { encapsulate, freeze, CapsulePropertyTypes, makeImportStack, hoistSnapshot } = await CapsuleSpineFactory({ |
| 21 | + spineFilesystemRoot: process.cwd(), |
| 22 | + capsuleModuleProjectionRoot: (import.meta as any).dir, |
| 23 | + enableCallerStackInference: true, |
| 24 | + spineContracts: { |
| 25 | + ['#' + CapsuleSpineContract['#']]: CapsuleSpineContract |
| 26 | + }, |
| 27 | + timing, |
| 28 | + onMembraneEvent: timing ? (event: any) => { |
| 29 | + const instanceId = event.target?.spineContractCapsuleInstanceId |
| 30 | + const eventIndex = event.eventIndex |
| 31 | + |
| 32 | + // Store event by composite key (instance ID + event index) |
| 33 | + const key = `${eventIndex}` |
| 34 | + eventsByKey.set(key, event) |
| 35 | + |
| 36 | + let capsuleRef = event.target?.capsuleSourceLineRef |
| 37 | + let prop = event.target?.prop |
| 38 | + let callerLocation = event.caller ? `${event.caller.filepath}:${event.caller.line}` : 'unknown' |
| 39 | + const eventType = event.event |
| 40 | + |
| 41 | + // For call-result events, look up the original call event to get all info |
| 42 | + if (eventType === 'call-result') { |
| 43 | + const callKey = `${event.callEventIndex}` |
| 44 | + const callEvent = eventsByKey.get(callKey) |
| 45 | + if (callEvent) { |
| 46 | + capsuleRef = callEvent.target?.capsuleSourceLineRef || capsuleRef |
| 47 | + prop = callEvent.target?.prop || prop |
| 48 | + if (callEvent.caller) { |
| 49 | + callerLocation = `${callEvent.caller.filepath}:${callEvent.caller.line}` |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + console.error( |
| 55 | + chalk.gray(`[${eventIndex}]`), |
| 56 | + chalk.cyan(eventType.padEnd(12)), |
| 57 | + chalk.yellow(capsuleRef), |
| 58 | + chalk.magenta(`.${prop}`), |
| 59 | + chalk.dim(`from ${callerLocation}`) |
| 60 | + ) |
| 61 | + } : undefined |
| 62 | + }) |
| 63 | + |
| 64 | + timing?.recordMajor('ENCAPSULATE') |
| 65 | + |
| 66 | + const exportedApi = await encapsulateHandler({ |
| 67 | + encapsulate, |
| 68 | + CapsulePropertyTypes, |
| 69 | + makeImportStack |
| 70 | + }) |
| 71 | + |
| 72 | + timing?.recordMajor('FREEZE') |
| 73 | + |
| 74 | + const snapshot = await freeze() |
| 75 | + |
| 76 | + timing?.recordMajor('HOIST SNAPSHOT') |
| 77 | + |
| 78 | + const { run } = await hoistSnapshot({ |
| 79 | + snapshot |
| 80 | + }) |
| 81 | + |
| 82 | + timing?.recordMajor('RUN') |
| 83 | + |
| 84 | + const result = await run({ |
| 85 | + overrides: { |
| 86 | + ['t44/caps/WorkspaceTest']: { |
| 87 | + '#': { |
| 88 | + testRootDir: options?.importMeta?.dir |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + }, async (opts) => { |
| 93 | + return runHandler({ |
| 94 | + ...opts, |
| 95 | + ...(exportedApi || {}) |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + timing?.recordMajor('DONE') |
| 100 | + |
| 101 | + return result |
| 102 | +} |
0 commit comments