@@ -144,25 +144,48 @@ export const application = (
144144 get serveOutput ( ) {
145145 return serveOutput ;
146146 } ,
147- serve : async ( opts : { port ?: number ; manualStart ?: boolean } = { } ) => {
147+ serve : async ( opts : { port ?: number ; manualStart ?: boolean ; detached ?: boolean ; serverUrl ?: string } = { } ) => {
148148 const log = logger . child ( { prefix : 'serve' } ) . info ;
149149 const port = opts . port || ( await getPort ( ) ) ;
150- // TODO: get serverUrl as in dev()
151- const serverUrl = `http://localhost:${ port } ` ;
152- // If this is ever used as a background process, we need to make sure
153- // it's not using the log function. See the dev() method above
150+ const getServerUrl = ( ) => {
151+ if ( opts . serverUrl ) {
152+ return opts . serverUrl . includes ( ':' ) ? opts . serverUrl : `${ opts . serverUrl } :${ port } ` ;
153+ }
154+ return serverUrl || `http://localhost:${ port } ` ;
155+ } ;
156+ const runtimeServerUrl = getServerUrl ( ) ;
157+ log ( `Will try to serve app at ${ runtimeServerUrl } ` ) ;
158+
159+ if ( opts . manualStart ) {
160+ state . serverUrl = runtimeServerUrl ;
161+ return { port, serverUrl : runtimeServerUrl } ;
162+ }
163+
154164 const proc = run ( scripts . serve , {
155165 cwd : appDirPath ,
156166 env : { PORT : port . toString ( ) } ,
157- log : ( msg : string ) => {
158- serveOutput += `\n${ msg } ` ;
159- log ( msg ) ;
160- } ,
167+ detached : opts . detached ,
168+ stdout : opts . detached ? fs . openSync ( stdoutFilePath , 'a' ) : undefined ,
169+ stderr : opts . detached ? fs . openSync ( stderrFilePath , 'a' ) : undefined ,
170+ log : opts . detached
171+ ? undefined
172+ : ( msg : string ) => {
173+ serveOutput += `\n${ msg } ` ;
174+ log ( msg ) ;
175+ } ,
161176 } ) ;
177+
178+ if ( opts . detached ) {
179+ const shouldExit = ( ) => ! ! proc . exitCode && proc . exitCode !== 0 ;
180+ await waitForServer ( runtimeServerUrl , { log, maxAttempts : Infinity , shouldExit } ) ;
181+ } else {
182+ await waitForIdleProcess ( proc ) ;
183+ }
184+
185+ log ( `Server started at ${ runtimeServerUrl } , pid: ${ proc . pid } ` ) ;
162186 cleanupFns . push ( ( ) => awaitableTreekill ( proc . pid , 'SIGKILL' ) ) ;
163- await waitForIdleProcess ( proc ) ;
164- state . serverUrl = serverUrl ;
165- return { port, serverUrl, pid : proc } ;
187+ state . serverUrl = runtimeServerUrl ;
188+ return { port, serverUrl : runtimeServerUrl , pid : proc . pid } ;
166189 } ,
167190 stop : async ( ) => {
168191 logger . info ( 'Stopping...' ) ;
0 commit comments