1
1
import * as jsYAML from 'js-yaml' ;
2
2
import { promises as fs } from 'fs' ;
3
3
import * as path from 'path' ;
4
- import { addShutdownJob , localModulePath , logger } from '../utils' ;
4
+ import {
5
+ addShutdownJob ,
6
+ localModulePath ,
7
+ logger ,
8
+ removeShutdownJob ,
9
+ } from '../utils' ;
5
10
6
11
export interface ServeCommandOptions {
7
12
config : string ;
@@ -13,6 +18,15 @@ const defaultOptions: ServeCommandOptions = {
13
18
port : 3000 ,
14
19
} ;
15
20
21
+ export const mergeServeDefaultOption = (
22
+ options : Partial < ServeCommandOptions >
23
+ ) => {
24
+ return {
25
+ ...defaultOptions ,
26
+ ...options ,
27
+ } as ServeCommandOptions ;
28
+ } ;
29
+
16
30
export const serveVulcan = async ( options : ServeCommandOptions ) => {
17
31
const configPath = path . resolve ( process . cwd ( ) , options . config ) ;
18
32
const config : any = jsYAML . load ( await fs . readFile ( configPath , 'utf-8' ) ) ;
@@ -25,19 +39,24 @@ export const serveVulcan = async (options: ServeCommandOptions) => {
25
39
const server = new VulcanServer ( config ) ;
26
40
await server . start ( options . port ) ;
27
41
logger . info ( `Server is listening at port ${ options . port } .` ) ;
28
- addShutdownJob ( async ( ) => {
42
+
43
+ const closeServerJob = async ( ) => {
29
44
logger . info ( `Stopping server...` ) ;
30
45
await server . close ( ) ;
31
46
logger . info ( `Server stopped` ) ;
32
- } ) ;
47
+ } ;
48
+ addShutdownJob ( closeServerJob ) ;
49
+
50
+ return {
51
+ stopServer : async ( ) => {
52
+ removeShutdownJob ( closeServerJob ) ;
53
+ await closeServerJob ( ) ;
54
+ } ,
55
+ } ;
33
56
} ;
34
57
35
58
export const handleServe = async (
36
59
options : Partial < ServeCommandOptions >
37
60
) : Promise < void > => {
38
- options = {
39
- ...defaultOptions ,
40
- ...options ,
41
- } ;
42
- await serveVulcan ( options as ServeCommandOptions ) ;
61
+ await serveVulcan ( mergeServeDefaultOption ( options ) ) ;
43
62
} ;
0 commit comments