import { Worker , isMainThread , workerData } from 'worker_threads' ; import { watch } from 'fs' ; if ( isMainThread ) { const args = process . argv . slice ( 2 ) ; const options = [ ] ; const files = [ ] ; for ( const arg of args ) { if ( arg . startsWith ( '--' ) ) options . push ( arg ) ; else files . push ( arg ) ; } if ( files . length === 0 ) { console . log ( 'No test files specified' ) ; process . exit ( 1 ) ; } const watchMode = options . includes ( '--watch' ) ; const workerUrl = new URL ( `data:text/javascript,import('${ import . meta . url }');` ) ; let worker ; const reload = ( ) => { if ( worker ) { void worker . terminate ( ) ; } worker = new Worker ( workerUrl , { workerData : files } ) ; worker . on ( 'online' , ( ) => { console . log ( 'Worker started' ) ; } ) ; worker . on ( 'error' , ( err ) => { console . log ( `Worker error ❌ ${ err }` ) ; } ) ; worker . on ( 'exit' , ( exitCode ) => { if ( exitCode === 0 ) console . log ( 'Worker exit OK' ) ; if ( watchMode ) console . log ( 'Waiting for reload...' ) ; } ) ; } ; let debounceTimer ; if ( watchMode ) { console . log ( 'Watching for changes' ) ; for ( const file of files ) { watch ( file , ( eventName ) => { console . log ( `Watch ${ file } event ${ eventName }` ) ; if ( debounceTimer ) clearTimeout ( debounceTimer ) ; debounceTimer = setTimeout ( ( ) => { reload ( ) ; } , 500 ) ; } ) ; } process . stdin . on ( 'data' , ( chunk ) => { const text = chunk . toString ( ) ; if ( text === 're\n' || text === 'reload\n' ) { console . log ( 'Manual reload' ) ; reload ( ) ; } } ) ; } reload ( ) ; } else { process . stdout . isTTY = true ; process . stderr . isTTY = true ; const files = workerData as string [ ] ; console . log ( `Testing ${ files . join ( ', ' ) }` ) ; const { hold , report } = await import ( 'zora' ) ; hold ( ) ; const { createDiffReporter } = await import ( 'zora-reporters' ) ; await Promise . all ( files . map ( ( file ) => import ( file ) ) ) ; await report ( { reporter : createDiffReporter ( ) } ) ; }