@@ -80,6 +80,15 @@ class PlaygroundCommandCrashError extends Error {
8080 }
8181}
8282
83+ class PlaygroundCliExitError extends Error {
84+ readonly code = "wp-codebox-playground-cli-exited"
85+
86+ constructor ( readonly exitCode : number ) {
87+ super ( `WordPress Playground CLI exited while booting the runtime with exit code ${ exitCode } .` )
88+ this . name = "PlaygroundCliExitError"
89+ }
90+ }
91+
8392class PlaygroundPreviewPortUnavailableError extends Error {
8493 readonly code = "wp-codebox-preview-port-in-use"
8594
@@ -115,6 +124,20 @@ function errorMessage(error: unknown): string {
115124 return error instanceof Error ? error . message : String ( error )
116125}
117126
127+ async function runPlaygroundCliWithoutProcessExit < T > ( callback : ( ) => Promise < T > ) : Promise < T > {
128+ const exit = process . exit
129+ process . exit = ( ( code ?: string | number | null | undefined ) : never => {
130+ const exitCode = typeof code === "number" ? code : 1
131+ throw new PlaygroundCliExitError ( exitCode )
132+ } ) as typeof process . exit
133+
134+ try {
135+ return await callback ( )
136+ } finally {
137+ process . exit = exit
138+ }
139+ }
140+
118141interface PlaygroundCliServer {
119142 playground : {
120143 run ( options : { code : string } | { scriptPath : string } ) : Promise < PlaygroundRunResponse >
@@ -1292,7 +1315,7 @@ echo json_encode(array('command' => 'inspect-mounted-inputs', 'mounts' => $inspe
12921315 }
12931316
12941317 try {
1295- const server = await runCLI ( {
1318+ const server = await runPlaygroundCliWithoutProcessExit ( ( ) => runCLI ( {
12961319 command : "server" ,
12971320 port : 0 ,
12981321 quiet : true ,
@@ -1304,7 +1327,7 @@ echo json_encode(array('command' => 'inspect-mounted-inputs', 'mounts' => $inspe
13041327 wp : this . spec . environment . version ,
13051328 "site-url" : this . spec . preview ?. siteUrl ,
13061329 blueprint : playgroundBlueprint ( this . spec . environment . blueprint , this . spec . policy , this . spec . preview ?. siteUrl ) ,
1307- } )
1330+ } ) )
13081331
13091332 if ( ! this . spec . preview ?. port ) {
13101333 return server
0 commit comments