@@ -4,32 +4,56 @@ import { join, dirname } from "path";
44const scriptDir = dirname ( __filename ) ;
55const projectRoot = join ( scriptDir , ".." ) ;
66
7- if ( process . env . GITHUB_TEST !== "test" ) {
7+ if (
8+ process . env . GITHUB_ACTIONS === "true" &&
9+ process . env . GITHUB_TEST !== "test"
10+ ) {
811 console . error ( "Please set the GITHUB_TEST variable to 'test'" ) ;
12+ process . exit ( 2 ) ;
913}
1014if ( process . env . SUPABASE_PROJECT_ID !== "test" ) {
1115 console . error ( "Please set the SUPABASE_PROJECT_ID variable to 'test'" ) ;
16+ process . exit ( 2 ) ;
1217}
1318
1419const serve = spawn ( "supabase" , [ "functions" , "serve" ] , {
1520 cwd : projectRoot ,
1621 detached : true ,
1722} ) ;
1823
19- let resolveCallback : ( ( v : unknown ) => void ) | undefined = undefined ;
24+ let resolveCallback : ( ( value : unknown ) => void ) | undefined = undefined ;
25+ let rejectCallback : ( ( reason : unknown ) => void ) | undefined = undefined ;
26+ let serveSuccess = false ;
27+ let timeoutClear : NodeJS . Timeout | undefined = undefined ;
2028
21- const servingReady = new Promise ( ( rsc ) => {
29+ const servingReady = new Promise ( ( rsc , rjc ) => {
2230 resolveCallback = rsc ;
31+ rejectCallback = rjc ;
32+
33+ // Add timeout
34+ timeoutClear = setTimeout ( ( ) => {
35+ rjc ( new Error ( "Timeout waiting for functions to serve" ) ) ;
36+ } , 30000 ) ; // 30 second timeout
2337} ) ;
2438
25- serve . stdout . on ( "data" , ( data : string ) => {
26- console . log ( `stdout: ${ data } ` ) ;
27- if ( data . includes ( "Serving functions " ) ) {
39+ serve . stdout . on ( "data" , ( data : Buffer ) => {
40+ const output = data . toString ( ) ;
41+ console . log ( `stdout: ${ output } ` ) ;
42+ if ( output . includes ( "Serving functions " ) ) {
2843 console . log ( "Found serving functions" ) ;
44+ serveSuccess = true ;
45+ clearTimeout ( timeoutClear ) ;
2946 if ( resolveCallback === undefined ) throw new Error ( "did not get callback" ) ;
3047 resolveCallback ( null ) ;
3148 }
3249} ) ;
50+ serve . on ( "close" , ( ) => {
51+ if ( ! serveSuccess && rejectCallback )
52+ rejectCallback ( new Error ( "serve closed without being ready" ) ) ;
53+ } ) ;
54+ serve . on ( "error" , ( err ) => {
55+ if ( rejectCallback ) rejectCallback ( err ) ;
56+ } ) ;
3357
3458const doTest = async ( ) => {
3559 await servingReady ;
@@ -47,8 +71,10 @@ const doTest = async () => {
4771doTest ( )
4872 . then ( ( ) => {
4973 console . log ( "success" ) ;
74+ clearTimeout ( timeoutClear ) ;
5075 } )
5176 . catch ( ( err ) => {
5277 console . error ( err ) ;
53- process . exit ( - 1 ) ;
78+ clearTimeout ( timeoutClear ) ;
79+ process . exit ( 1 ) ;
5480 } ) ;
0 commit comments