@@ -11,13 +11,13 @@ import getPort from 'get-port'
11
11
import picomatch from 'picomatch'
12
12
import type tsStatic from 'typescript'
13
13
import { type ExecaChildProcess } from 'execa'
14
+ import type { Watcher } from '@poppinss/chokidar-ts'
14
15
import { cliui , type Logger } from '@poppinss/cliui'
15
16
import { EnvLoader , EnvParser } from '@adonisjs/env'
16
17
17
- import { run } from './run.js'
18
18
import { watch } from './watch.js'
19
+ import { run , runNode } from './run.js'
19
20
import type { DevServerOptions } from './types.js'
20
- import type { Watcher } from '@poppinss/chokidar-ts'
21
21
22
22
/**
23
23
* Instance of CLIUI
@@ -45,6 +45,7 @@ export class DevServer {
45
45
#isMetaFileWithReloadsEnabled: picomatch . Matcher
46
46
#isMetaFileWithReloadsDisabled: picomatch . Matcher
47
47
#watcher?: ReturnType < Watcher [ 'watch' ] >
48
+ #assetsServerProcess?: ExecaChildProcess < string >
48
49
#onError?: ( error : any ) => any
49
50
#onClose?: ( exitCode : number ) => any
50
51
@@ -141,7 +142,7 @@ export class DevServer {
141
142
* Starts the HTTP server
142
143
*/
143
144
#startHTTPServer( port : string , mode : 'blocking' | 'nonblocking' ) {
144
- this . #httpServerProcess = run ( this . #cwd, {
145
+ this . #httpServerProcess = runNode ( this . #cwd, {
145
146
script : this . #scriptFile,
146
147
env : { PORT : port , ...this . #options. env } ,
147
148
nodeArgs : this . #options. nodeArgs ,
@@ -179,6 +180,35 @@ export class DevServer {
179
180
} )
180
181
}
181
182
183
+ /**
184
+ * Starts the assets bundler server. The assets bundler server process is
185
+ * considered as the secondary process and therefore we do not perform
186
+ * any cleanup if it dies.
187
+ */
188
+ #startAssetsServer( ) {
189
+ const assetsBundler = this . #options. assets
190
+ if ( ! assetsBundler ?. serve ) {
191
+ return
192
+ }
193
+
194
+ this . #logger. info ( `starting "${ assetsBundler . driver } " dev server...` )
195
+ this . #assetsServerProcess = run ( assetsBundler . cmd , {
196
+ script : this . #scriptFile,
197
+ scriptArgs : this . #options. scriptArgs ,
198
+ } )
199
+
200
+ this . #assetsServerProcess
201
+ . then ( ( result ) => {
202
+ this . #logger. warning (
203
+ `"${ assetsBundler . driver } " dev server closed with status code "${ result . exitCode } "`
204
+ )
205
+ } )
206
+ . catch ( ( error ) => {
207
+ this . #logger. warning ( `unable to connect to "${ assetsBundler . driver } " dev server` )
208
+ this . #logger. fatal ( error )
209
+ } )
210
+ }
211
+
182
212
/**
183
213
* Restart the development server
184
214
*/
@@ -224,26 +254,36 @@ export class DevServer {
224
254
this . #clearScreen( )
225
255
this . #logger. info ( 'starting HTTP server...' )
226
256
this . #startHTTPServer( String ( await this . #getPort( ) ) , 'nonblocking' )
257
+
258
+ this . #startAssetsServer( )
227
259
}
228
260
229
261
/**
230
262
* Start the development server in watch mode
231
263
*/
232
264
async startAndWatch ( ts : typeof tsStatic , options ?: { poll : boolean } ) {
233
- this . #isWatching = true
234
- this . #clearScreen( )
235
-
236
265
const port = String ( await this . #getPort( ) )
266
+ this . #isWatching = true
237
267
268
+ this . #clearScreen( )
238
269
this . #logger. info ( 'starting HTTP server...' )
270
+
239
271
this . #startHTTPServer( port , 'blocking' )
272
+ this . #startAssetsServer( )
240
273
274
+ /**
275
+ * Create watcher using tsconfig.json file
276
+ */
241
277
const output = watch ( this . #cwd, ts , options || { } )
242
278
if ( ! output ) {
243
279
this . #onClose?.( 1 )
244
280
return
245
281
}
246
282
283
+ /**
284
+ * Storing reference to watcher, so that we can close it
285
+ * when HTTP server exists with error
286
+ */
247
287
this . #watcher = output . chokidar
248
288
249
289
/**
@@ -253,6 +293,9 @@ export class DevServer {
253
293
this . #logger. info ( 'watching file system for changes...' )
254
294
} )
255
295
296
+ /**
297
+ * Cleanup when watcher dies
298
+ */
256
299
output . chokidar . on ( 'error' , ( error ) => {
257
300
this . #logger. warning ( 'file system watcher failure' )
258
301
this . #logger. fatal ( error )
0 commit comments