@@ -21,6 +21,96 @@ export class NextDevInstance extends NextInstance {
2121 return this . _cliOutput || ''
2222 }
2323
24+ private handleStdio = ( childProcess ) => {
25+ childProcess . stdout . on ( 'data' , ( chunk ) => {
26+ const msg = chunk . toString ( )
27+ process . stdout . write ( chunk )
28+ this . _cliOutput += msg
29+ this . emit ( 'stdout' , [ msg ] )
30+ } )
31+ childProcess . stderr . on ( 'data' , ( chunk ) => {
32+ const msg = chunk . toString ( )
33+ process . stderr . write ( chunk )
34+ this . _cliOutput += msg
35+ this . emit ( 'stderr' , [ msg ] )
36+ } )
37+ }
38+
39+ private getBuildArgs ( args ?: string [ ] ) {
40+ let buildArgs = [ 'pnpm' , 'next' , 'build' ]
41+
42+ if ( this . buildCommand ) {
43+ buildArgs = this . buildCommand . split ( ' ' )
44+ }
45+
46+ if ( this . buildArgs ) {
47+ buildArgs . push ( ...this . buildArgs )
48+ }
49+
50+ if ( args ) {
51+ buildArgs . push ( ...args )
52+ }
53+
54+ if ( process . env . NEXT_SKIP_ISOLATE ) {
55+ // without isolation yarn can't be used and pnpm must be used instead
56+ if ( buildArgs [ 0 ] === 'yarn' ) {
57+ buildArgs [ 0 ] = 'pnpm'
58+ }
59+ }
60+
61+ return buildArgs
62+ }
63+
64+ private getSpawnOpts (
65+ env ?: Record < string , string >
66+ ) : import ( 'child_process' ) . SpawnOptions {
67+ return {
68+ cwd : this . testDir ,
69+ stdio : [ 'ignore' , 'pipe' , 'pipe' ] ,
70+ shell : false ,
71+ env : {
72+ ...process . env ,
73+ ...this . env ,
74+ ...env ,
75+ NODE_ENV : this . env . NODE_ENV || ( '' as any ) ,
76+ PORT : this . forcedPort || '0' ,
77+ __NEXT_TEST_MODE : 'e2e' ,
78+ } ,
79+ }
80+ }
81+
82+ public async build (
83+ options : { env ?: Record < string , string > ; args ?: string [ ] } = { }
84+ ) {
85+ if ( this . childProcess ) {
86+ throw new Error (
87+ `can not run build while server is running, use next.stop() first`
88+ )
89+ }
90+
91+ return new Promise < {
92+ exitCode : NodeJS . Signals | number | null
93+ cliOutput : string
94+ } > ( ( resolve ) => {
95+ const curOutput = this . _cliOutput . length
96+ const spawnOpts = this . getSpawnOpts ( options . env )
97+ const buildArgs = this . getBuildArgs ( options . args )
98+
99+ console . log ( 'running' , shellQuote ( buildArgs ) )
100+
101+ this . childProcess = spawn ( buildArgs [ 0 ] , buildArgs . slice ( 1 ) , spawnOpts )
102+ this . handleStdio ( this . childProcess )
103+
104+ this . childProcess . on ( 'exit' , ( code , signal ) => {
105+ this . childProcess = undefined
106+ resolve ( {
107+ exitCode : signal || code ,
108+ cliOutput : this . cliOutput . slice ( curOutput ) ,
109+ } )
110+ } )
111+ } )
112+ }
113+
24114 public async start ( ) {
25115 if ( this . childProcess ) {
26116 throw new Error ( 'next already started' )
0 commit comments