@@ -5,6 +5,7 @@ const globby = require('globby');
55const git = require ( 'simple-git' ) ( './' ) ;
66const path = require ( 'path' ) ;
77const fs = require ( 'fs' ) ;
8+ const chokidar = require ( 'chokidar' ) ;
89
910const winston = require ( 'winston' ) ;
1011
@@ -22,8 +23,6 @@ const GITHUB = 'github';
2223const GITLAB = 'gitlab' ;
2324const BB = 'bitbucket' ;
2425
25- const paths = new Set ( ) ;
26-
2726const uriNoTrailingSlash = ( uri ) => {
2827 return uri . endsWith ( '/' ) ? uri . substr ( 0 , uri . length - 1 ) : uri ;
2928} ;
@@ -171,7 +170,21 @@ class CML {
171170 const { remark } = await import ( 'remark' ) ;
172171 const { visit } = await import ( 'unist-util-visit' ) ;
173172
174- paths . add ( markdownfile ) ;
173+ const watcher = chokidar . watch ( markdownfile , {
174+ persistent : true ,
175+
176+ awaitWriteFinish : {
177+ stabilityThreshold : 2000 ,
178+ pollInterval : 100
179+ }
180+ } ) ;
181+
182+ if ( watch ) {
183+ watcher . on ( 'all' , ( ) => {
184+ console . log ( 'updating report...' ) ;
185+ this . commentCreate ( { ...opts , update : true , watch : false } ) ;
186+ } ) ;
187+ }
175188
176189 const publishLocalFiles = ( ) => async ( tree ) => {
177190 const nodes = [ ] ;
@@ -186,7 +199,7 @@ class CML {
186199 return ;
187200 }
188201 node . url = await this . publish ( { ...opts , path : node . url } ) ;
189- paths . add ( node . url ) ;
202+ watcher . add ( node . url ) ;
190203 }
191204 } ;
192205
@@ -196,12 +209,6 @@ class CML {
196209 if ( publish )
197210 report = String ( await remark ( ) . use ( publishLocalFiles ) . process ( report ) ) ;
198211
199- if ( watch )
200- setInterval ( ( ) => {
201- console . log ( 'updating report...' ) ;
202- this . commentCreate ( { ...opts , update : true , watch : false } ) ;
203- } , 16384 ) ; // TODO: use a proper file watcher on the `paths` set
204-
205212 let comment ;
206213 const updatableComment = ( comments ) => {
207214 return comments . reverse ( ) . find ( ( { body } ) => {
0 commit comments