@@ -6,14 +6,15 @@ import { ServerStyleSheet } from 'styled-components';
6
6
7
7
import { compile } from 'handlebars' ;
8
8
import { createServer , IncomingMessage , ServerResponse } from 'http' ;
9
- import { dirname , join } from 'path' ;
9
+ import { dirname , join , resolve } from 'path' ;
10
10
11
11
import * as zlib from 'zlib' ;
12
12
13
13
// @ts -ignore
14
14
import { createStore , loadAndBundleSpec , Redoc } from 'redoc' ;
15
15
16
- import { createReadStream , existsSync , readFileSync , ReadStream , watch , writeFileSync } from 'fs' ;
16
+ import { watch } from 'chokidar' ;
17
+ import { createReadStream , existsSync , readFileSync , ReadStream , writeFileSync } from 'fs' ;
17
18
import * as mkdirp from 'mkdirp' ;
18
19
19
20
import * as YargsParser from 'yargs' ;
@@ -167,28 +168,25 @@ async function serve(port: number, pathToSpec: string, options: Options = {}) {
167
168
server . listen ( port , ( ) => console . log ( `Server started: http://127.0.0.1:${ port } ` ) ) ;
168
169
169
170
if ( options . watch && existsSync ( pathToSpec ) ) {
170
- const pathToSpecDirectory = dirname ( pathToSpec ) ;
171
+ const pathToSpecDirectory = resolve ( dirname ( pathToSpec ) ) ;
171
172
const watchOptions = {
172
- recursive : true ,
173
+ ignored : / ( ^ | [ \/ \\ ] ) \. . / ,
173
174
} ;
174
175
175
- watch (
176
- pathToSpecDirectory ,
177
- watchOptions ,
178
- debounce ( async ( event , filename ) => {
179
- if ( event === 'change' || event === 'rename' ) {
180
- console . log ( `${ join ( pathToSpecDirectory , filename ) } changed, updating docs` ) ;
181
- try {
182
- spec = await loadAndBundleSpec ( pathToSpec ) ;
183
- pageHTML = await getPageHTML ( spec , pathToSpec , options ) ;
184
- console . log ( 'Updated successfully' ) ;
185
- } catch ( e ) {
186
- console . error ( 'Error while updating: ' , e . message ) ;
187
- }
188
- }
189
- } , 2200 ) ,
190
- ) ;
191
- console . log ( `👀 Watching ${ pathToSpecDirectory } for changes...` ) ;
176
+ const watcher = watch ( pathToSpecDirectory , watchOptions ) ;
177
+ const log = console . log . bind ( console ) ;
178
+ watcher
179
+ . on ( 'change' , async path => {
180
+ log ( `${ path } changed, updating docs` ) ;
181
+ try {
182
+ spec = await loadAndBundleSpec ( pathToSpec ) ;
183
+ pageHTML = await getPageHTML ( spec , pathToSpec , options ) ;
184
+ log ( 'Updated successfully' ) ;
185
+ } catch ( e ) {
186
+ console . error ( 'Error while updating: ' , e . message ) ;
187
+ } } )
188
+ . on ( 'error' , error => console . error ( `Watcher error: ${ error } ` ) )
189
+ . on ( 'ready' , ( ) => log ( `👀 Watching ${ pathToSpecDirectory } for changes...` ) ) ;
192
190
}
193
191
}
194
192
@@ -291,17 +289,6 @@ function respondWithGzip(
291
289
}
292
290
}
293
291
294
- function debounce ( callback : ( ...args ) = > void , time : number ) {
295
- let interval ;
296
- return ( ...args ) => {
297
- clearTimeout ( interval ) ;
298
- interval = setTimeout ( ( ) => {
299
- interval = null ;
300
- callback ( ...args ) ;
301
- } , time ) ;
302
- } ;
303
- }
304
-
305
292
function isURL ( str : string ) : boolean {
306
293
return / ^ ( h t t p s ? : ) \/ \/ / m. test ( str ) ;
307
294
}
0 commit comments