1
- // Simple local server
1
+ // Simple server for local testing.
2
+
2
3
import * as path from "path" ;
3
4
import commandLineArgs from "command-line-args" ;
4
5
import esMain from "es-main" ;
5
- import LocalWebServer from "local-web-server" ;
6
+ import LocalWebServer from "lws" ;
7
+ import "lws-cors" ;
8
+ import "lws-index" ;
9
+ import "lws-log" ;
10
+ import "lws-static" ;
6
11
7
12
const ROOT_DIR = path . join ( process . cwd ( ) , "./" ) ;
8
13
@@ -14,20 +19,35 @@ export default async function serve(port) {
14
19
directory : ROOT_DIR ,
15
20
corsOpenerPolicy : "same-origin" ,
16
21
corsEmbedderPolicy : "require-corp" ,
22
+ logFormat : "dev" ,
23
+ stack : [ "lws-log" , "lws-cors" , "lws-static" , "lws-index" ] ,
17
24
} ) ;
18
- console . log ( `Server started on http://localhost:${ port } ` ) ;
25
+ await verifyStartup ( ws , port ) ;
26
+
19
27
process . on ( "exit" , ( ) => ws . server . close ( ) ) ;
28
+
20
29
return {
21
30
close ( ) {
22
31
ws . server . close ( ) ;
23
- }
32
+ } ,
24
33
} ;
25
34
}
26
35
36
+ async function verifyStartup ( ws , port ) {
37
+ await new Promise ( ( resolve , reject ) => {
38
+ ws . server . on ( "listening" , ( ) => {
39
+ console . log ( `Server started on http://localhost:${ port } ` ) ;
40
+ resolve ( ) ;
41
+ } ) ;
42
+ ws . server . on ( "error" , ( e ) => {
43
+ console . error ( "Error while starting the server" , e ) ;
44
+ reject ( e ) ;
45
+ } ) ;
46
+ } ) ;
47
+ }
48
+
27
49
function main ( ) {
28
- const optionDefinitions = [
29
- { name : "port" , type : Number , defaultValue : 8010 , description : "Set the test-server port, The default value is 8010." } ,
30
- ] ;
50
+ const optionDefinitions = [ { name : "port" , type : Number , defaultValue : 8010 , description : "Set the test-server port, The default value is 8010." } ] ;
31
51
const options = commandLineArgs ( optionDefinitions ) ;
32
52
serve ( options . port ) ;
33
53
}
0 commit comments