@@ -14,6 +14,19 @@ import { makeListing } from './crawl.js';
14
14
// Make sure that makeListing doesn't cache imported spec files. See crawl().
15
15
process . env . STANDALONE_DEV_SERVER = '1' ;
16
16
17
+ function usage ( rc : number ) : void {
18
+ console . error ( `\
19
+ Usage:
20
+ tools/dev_server
21
+ tools/dev_server 0.0.0.0
22
+ npm start
23
+ npm start 0.0.0.0
24
+
25
+ By default, serves on localhost only. If the argument 0.0.0.0 is passed, serves on all interfaces.
26
+ ` ) ;
27
+ process . exit ( rc ) ;
28
+ }
29
+
17
30
const srcDir = path . resolve ( __dirname , '../../' ) ;
18
31
19
32
// Import the project's babel.config.js. We'll use the same config for the runtime compiler.
@@ -110,7 +123,7 @@ app.use('/out-wpt', express.static(path.resolve(srcDir, '../out-wpt')));
110
123
app . use ( '/docs/tsdoc' , express . static ( path . resolve ( srcDir , '../docs/tsdoc' ) ) ) ;
111
124
112
125
// Serve a suite's listing.js file by crawling the filesystem for all tests.
113
- app . get ( '/out/:suite/listing.js' , async ( req , res , next ) => {
126
+ app . get ( '/out/:suite([a-zA-Z0-9_-]+) /listing.js' , async ( req , res , next ) => {
114
127
const suite = req . params [ 'suite' ] ;
115
128
116
129
if ( listingCache . has ( suite ) ) {
@@ -162,28 +175,40 @@ app.get('/out/**/*.js', async (req, res, next) => {
162
175
}
163
176
} ) ;
164
177
165
- const host = '0.0.0.0' ;
166
- const port = 8080 ;
167
- // Find an available port, starting at 8080.
168
- portfinder . getPort ( { host, port } , ( err , port ) => {
169
- if ( err ) {
170
- throw err ;
178
+ // Serve everything else (not .js) as static, and directories as directory listings.
179
+ app . use ( '/out' , serveIndex ( path . resolve ( srcDir , '../src' ) ) ) ;
180
+ app . use ( '/out' , express . static ( path . resolve ( srcDir , '../src' ) ) ) ;
181
+
182
+ void ( async ( ) => {
183
+ let host = '127.0.0.1' ;
184
+ if ( process . argv . length >= 3 ) {
185
+ if ( process . argv . length !== 3 ) usage ( 1 ) ;
186
+ if ( process . argv [ 2 ] === '0.0.0.0' ) {
187
+ host = '0.0.0.0' ;
188
+ } else {
189
+ usage ( 1 ) ;
190
+ }
171
191
}
192
+
193
+ console . log ( `Finding an available port on ${ host } ...` ) ;
194
+ const kPortFinderStart = 8080 ;
195
+ const port = await portfinder . getPortPromise ( { host, port : kPortFinderStart } ) ;
196
+
172
197
watcher . on ( 'ready' , ( ) => {
173
198
// Listen on the available port.
174
199
app . listen ( port , host , ( ) => {
175
200
console . log ( 'Standalone test runner running at:' ) ;
176
- for ( const iface of Object . values ( os . networkInterfaces ( ) ) ) {
177
- for ( const details of iface || [ ] ) {
178
- if ( details . family === 'IPv4' ) {
179
- console . log ( ` http://${ details . address } :${ port } /standalone/` ) ;
201
+ if ( host === '0.0.0.0' ) {
202
+ for ( const iface of Object . values ( os . networkInterfaces ( ) ) ) {
203
+ for ( const details of iface || [ ] ) {
204
+ if ( details . family === 'IPv4' ) {
205
+ console . log ( ` http://${ details . address } :${ port } /standalone/` ) ;
206
+ }
180
207
}
181
208
}
209
+ } else {
210
+ console . log ( ` http://${ host } :${ port } /standalone/` ) ;
182
211
}
183
212
} ) ;
184
213
} ) ;
185
- } ) ;
186
-
187
- // Serve everything else (not .js) as static, and directories as directory listings.
188
- app . use ( '/out' , serveIndex ( path . resolve ( srcDir , '../src' ) ) ) ;
189
- app . use ( '/out' , express . static ( path . resolve ( srcDir , '../src' ) ) ) ;
214
+ } ) ( ) ;
0 commit comments