import * as typescript from './typescript' ; import { watchFile , unwatchFile , Stats } from 'fs' ; export function watchFiles ( rootFileNames , originalServicePath , cb ) { const tsConfig = typescript . getTypescriptConfig ( originalServicePath ) ; let watchedFiles = typescript . getSourceFiles ( rootFileNames , tsConfig ) ; watchedFiles . forEach ( fileName => { watchFile ( fileName , { persistent : true , interval : 250 } , watchCallback ) ; } ) ; function watchCallback ( curr , prev ) { if ( + curr . mtime <= + prev . mtime ) { return ; } cb ( ) ; const newWatchFiles = typescript . getSourceFiles ( rootFileNames , tsConfig ) ; watchedFiles . forEach ( fileName => { if ( newWatchFiles . indexOf ( fileName ) < 0 ) { unwatchFile ( fileName , watchCallback ) ; } } ) ; newWatchFiles . forEach ( fileName => { if ( watchedFiles . indexOf ( fileName ) < 0 ) { watchFile ( fileName , { persistent : true , interval : 250 } , watchCallback ) ; } } ) ; watchedFiles = newWatchFiles ; } }