11// scripts/ecosystem-check.ts
22import { spawnSync } from 'node:child_process' ;
3- import { mkdtempSync , rmSync } from 'node:fs' ;
3+ import { mkdtempSync , rmSync , writeFileSync } from 'node:fs' ;
44import os from 'node:os' ;
55import { dirname , join } from 'node:path' ;
66import { fileURLToPath } from 'node:url' ;
@@ -51,32 +51,6 @@ const tests: Record<string, { cmd: string; args: string[] }> = {
5151 } ,
5252} ;
5353
54- // Worker test using simple import check
55- const workerTest = {
56- cmd : 'node' ,
57- args : [
58- '-e' ,
59- `
60- try {
61- // Test if the worker build exists and can be imported
62- import("${ libEsm } /index.worker.js").then(m => {
63- if (m.WorkOS) {
64- console.log('✅ Worker: WorkOS import successful');
65- } else {
66- throw new Error('WorkOS not found in worker build');
67- }
68- }).catch(err => {
69- console.log('❌ Worker test failed:', err.message);
70- process.exit(1);
71- });
72- } catch (error) {
73- console.log('❌ Worker test failed:', error.message);
74- process.exit(1);
75- }
76- ` ,
77- ] ,
78- } ;
79-
8054let allOK = true ;
8155let ranTests = 0 ;
8256
@@ -103,22 +77,56 @@ for (const [name, { cmd, args }] of Object.entries(tests)) {
10377 }
10478}
10579
106- // Try worker test if miniflare is available
107- console . log ( `\nTesting worker.........` ) ;
108- const workerResult = spawnSync ( workerTest . cmd , workerTest . args , {
109- stdio : [ 'inherit' , 'pipe' , 'pipe' ] ,
80+ // Test Cloudflare Worker environment using miniflare
81+ process . stdout . write ( `Testing worker ... ` ) ;
82+
83+ // 1. Check if miniflare is available
84+ const miniflareCheck = spawnSync ( 'npx' , [ 'miniflare' , '--version' ] , {
85+ stdio : 'ignore' , // We don't need to see the version output
11086 encoding : 'utf8' ,
11187} ) ;
11288
113- if ( workerResult . status === 0 ) {
114- ranTests ++ ;
115- console . log ( `✅ Passed` ) ;
89+ if ( miniflareCheck . status !== 0 ) {
90+ console . log ( `⚠️ Skipped (miniflare not found or failed to execute)` ) ;
91+ } else {
92+ // 2. Create the temporary worker script
93+ const workerScriptPath = join ( tmp , 'worker-test.js' ) ;
94+ const safeLibEsmPath = libEsm . replace ( / \\ / g, '\\\\' ) ; // For Windows compatibility
95+
96+ const workerScriptContent = `
97+ import { WorkOS } from '${ safeLibEsmPath } /index.js';
98+
99+ if (WorkOS && WorkOS.name === 'WorkOS') {
100+ console.log('✅ Worker (miniflare): SDK imported successfully.');
116101} else {
117- console . log ( `⚠️ Skipped (miniflare test failed)` ) ;
118- if ( workerResult . stderr ) {
119- console . log ( ` Error: ${ workerResult . stderr . trim ( ) } ` ) ;
102+ console.error('❌ Worker (miniflare): SDK import failed or WorkOS class is incorrect.');
103+ process.exit(1);
104+ }
105+ ` ;
106+
107+ writeFileSync ( workerScriptPath , workerScriptContent ) ;
108+
109+ // 3. Execute the worker script with miniflare
110+ const { status, stderr } = spawnSync (
111+ 'npx' ,
112+ [ 'miniflare' , '--modules' , workerScriptPath ] ,
113+ {
114+ stdio : [ 'inherit' , 'pipe' , 'pipe' ] ,
115+ encoding : 'utf8' ,
116+ } ,
117+ ) ;
118+
119+ // 4. Process the result
120+ if ( status !== 0 ) {
121+ allOK = false ;
122+ console . error ( `❌ Failed` ) ;
123+ if ( stderr ) {
124+ console . error ( ` Error: ${ stderr . trim ( ) } ` ) ;
125+ }
126+ } else {
127+ ranTests ++ ;
128+ console . log ( `✅ Passed` ) ;
120129 }
121- // Don't fail overall test for worker issues
122130}
123131
124132// Cleanup
0 commit comments