1- import { spawn , spawnSync } from "node:child_process" ;
1+ import { spawn } from "node:child_process" ;
22import http from "node:http" ;
33import path from "node:path" ;
44import { fileURLToPath } from "node:url" ;
5+ import { runMain , runNodeSubprocess } from "../../helpers/scenario-runtime" ;
56
67const scenarioDir = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
78const PORT = 3999 ;
@@ -11,28 +12,15 @@ const PORT = 3999;
1112const nextBin = new URL ( "./node_modules/next/dist/bin/next" , import . meta. url )
1213 . pathname ;
1314
14- // Run Next.js build (Turbopack is the default bundler in Next.js 16)
15- const buildResult = spawnSync ( process . execPath , [ nextBin , "build" ] , {
16- cwd : scenarioDir ,
17- stdio : "inherit" ,
18- env : { ...process . env , NEXT_TELEMETRY_DISABLED : "1" } ,
19- } ) ;
20-
21- if ( buildResult . status !== 0 ) {
22- throw new Error ( `next build failed with exit code ${ buildResult . status } ` ) ;
15+ function withScenarioEnv (
16+ env : NodeJS . ProcessEnv = process . env ,
17+ ) : NodeJS . ProcessEnv {
18+ return {
19+ ...env ,
20+ NEXT_TELEMETRY_DISABLED : "1" ,
21+ } ;
2322}
2423
25- // Start the Next.js server
26- const server = spawn (
27- process . execPath ,
28- [ nextBin , "start" , "--port" , String ( PORT ) ] ,
29- {
30- cwd : scenarioDir ,
31- stdio : "inherit" ,
32- env : { ...process . env , NEXT_TELEMETRY_DISABLED : "1" } ,
33- } ,
34- ) ;
35-
3624function httpGet ( url : string ) : Promise < { status : number ; body : string } > {
3725 return new Promise ( ( resolve , reject ) => {
3826 http
@@ -60,8 +48,27 @@ async function waitForServer(timeoutMs = 30_000): Promise<void> {
6048}
6149
6250// Top-level await is not supported in CJS output, so use an explicit async
63- // function and propagate failures via process.exit .
51+ // function and run it through the shared scenario wrapper .
6452async function main ( ) {
53+ const env = withScenarioEnv ( process . env ) ;
54+ await runNodeSubprocess ( {
55+ args : [ nextBin , "build" ] ,
56+ cwd : scenarioDir ,
57+ env,
58+ timeoutMs : 180_000 ,
59+ } ) ;
60+
61+ // Start the Next.js server
62+ const server = spawn (
63+ process . execPath ,
64+ [ nextBin , "start" , "--port" , String ( PORT ) ] ,
65+ {
66+ cwd : scenarioDir ,
67+ stdio : "inherit" ,
68+ env,
69+ } ,
70+ ) ;
71+
6572 try {
6673 await waitForServer ( ) ;
6774
@@ -82,7 +89,4 @@ async function main() {
8289 }
8390}
8491
85- main ( ) . catch ( ( err ) => {
86- console . error ( err ) ;
87- process . exit ( 1 ) ;
88- } ) ;
92+ runMain ( main ) ;
0 commit comments