11#!/usr/bin/env ts-node
22
3- /* eslint-disable @typescript-eslint/no-misused-promises */
43/* eslint-disable no-console */
54import express from 'express'
65import { MockServer } from './MockServer'
76import Router from 'express-promise-router'
87import cors from 'cors'
98
10- // const startMockServerController = async () => {
11- // /**
12- // * @type {MockServer }
13- // */
14- // const mockServer = null
15-
16- // app.get('/stop', (req, res) => {
17- // res.send('Hello World!')
18- // })
19-
20- // app.listen(port, () => {
21- // console.log(`Example app listening on port ${port}`)
22- // })
23- // }
24-
25- // type ExpressGetHandler = Parameters<IRouterMatcher<'get'>>[1]
26-
279class MockServerController {
2810 private readonly mockServers : MockServer [ ] = [ ]
2911 private readonly app = express ( )
@@ -32,7 +14,7 @@ class MockServerController {
3214 private readonly port = 3000
3315 server : import ( 'http' ) . Server
3416 constructor ( ) {
35- this . router . get < '/start' , { port ?: string } > ( '/start' , async ( req , res , next ) => {
17+ this . router . get < '/start' , { port ?: string } > ( '/start' , async ( req , res , next ) => { // eslint-disable-line @typescript-eslint/no-misused-promises
3618 const { port } = req . params
3719
3820 let mockServer : MockServer | null = null
@@ -57,7 +39,7 @@ class MockServerController {
5739 /**
5840 * A client will request to shut down it's mockServer by port, which it should have received upon calling '/start'
5941 */
60- this . router . get < '/stop/:port' , { port : string } > ( '/stop/:port' , async ( req , res , next ) => {
42+ this . router . get < '/stop/:port' , { port : string } > ( '/stop/:port' , async ( req , res , next ) => { // eslint-disable-line @typescript-eslint/no-misused-promises
6143 const { port } = req . params
6244
6345 const mockServer = this . mockServers . find ( ( mockS ) => mockS . basePath . includes ( port ) )
@@ -84,21 +66,27 @@ class MockServerController {
8466 } )
8567
8668 // And you'll want to make sure you close the server when your process exits
87- process . on ( 'beforeExit' , this . shutdown )
88- process . on ( 'SIGTERM' , this . shutdown )
89- process . on ( 'SIGINT' , this . shutdown )
90- process . on ( 'SIGHUP' , this . shutdown )
69+ process . on ( 'beforeExit' , this . shutdownSync )
70+ process . on ( 'SIGTERM' , this . shutdownSync )
71+ process . on ( 'SIGINT' , this . shutdownSync )
72+ process . on ( 'SIGHUP' , this . shutdownSync )
9173
9274 // To prevent duplicated cleanup, remove the process listeners on server close.
9375 this . server . on ( 'close' , ( ) => {
9476 } )
9577 }
9678
79+ private shutdownSync ( ) {
80+ this . shutdown ( ) . catch ( ( err ) => {
81+ console . error ( err )
82+ } )
83+ }
84+
9785 async shutdown ( ) {
98- process . off ( 'beforeExit' , this . shutdown )
99- process . off ( 'SIGTERM' , this . shutdown )
100- process . off ( 'SIGINT' , this . shutdown )
101- process . off ( 'SIGHUP' , this . shutdown )
86+ process . off ( 'beforeExit' , this . shutdownSync )
87+ process . off ( 'SIGTERM' , this . shutdownSync )
88+ process . off ( 'SIGINT' , this . shutdownSync )
89+ process . off ( 'SIGHUP' , this . shutdownSync )
10290 await new Promise < void > ( ( resolve , reject ) => {
10391 this . server . close ( ( err ) => {
10492 if ( err != null ) {
0 commit comments