44
55const startTime = Date . now ( )
66
7+ import { resolve , join } from 'path'
8+ import { access } from 'fs/promises'
79import chalk from 'chalk'
810import { CapsuleSpineFactory } from "@stream44.studio/encapsulate/spine-factories/CapsuleSpineFactory.v0"
911import { CapsuleSpineContract } from "@stream44.studio/encapsulate/spine-contracts/CapsuleSpineContract.v0/Membrane.v0"
1012import { TimingObserver } from "@stream44.studio/encapsulate/spine-factories/TimingObserver"
1113
14+ async function findPackageRoot ( startDir : string ) : Promise < string > {
15+ let dir = resolve ( startDir )
16+ while ( true ) {
17+ try {
18+ await access ( join ( dir , 'package.json' ) )
19+ return dir
20+ } catch { }
21+ const parent = resolve ( dir , '..' )
22+ if ( parent === dir ) return resolve ( startDir )
23+ dir = parent
24+ }
25+ }
26+
1227export async function run ( encapsulateHandler : any , runHandler : any , options ?: { importMeta ?: { dir : string } } ) {
1328
1429 const timing = process . argv . includes ( '--trace' ) ? TimingObserver ( { startTime } ) : undefined
@@ -17,8 +32,12 @@ export async function run(encapsulateHandler: any, runHandler: any, options?: {
1732
1833 const eventsByKey = new Map < string , any > ( )
1934
35+ const spineFilesystemRoot = options ?. importMeta ?. dir
36+ ? await findPackageRoot ( options . importMeta . dir )
37+ : process . cwd ( )
38+
2039 const { encapsulate, freeze, CapsulePropertyTypes, makeImportStack, hoistSnapshot } = await CapsuleSpineFactory ( {
21- spineFilesystemRoot : process . cwd ( ) ,
40+ spineFilesystemRoot,
2241 capsuleModuleProjectionRoot : ( import . meta as any ) . dir ,
2342 enableCallerStackInference : true ,
2443 spineContracts : {
0 commit comments