import * as path from 'path' ; import * as EventEmitter from 'events' ; import { watch } from 'chokidar' ; export interface FileWatcherOptions { directory : string ; files : string [ ] ; } export const FileWatcherEvent = { REBUILD : 'REBUILD' } ; export class FileWatcher { private watchEvent : EventEmitter = new EventEmitter ( ) ; constructor ( private config ) { } start ( ) { console . info ( 'Watching for file changes.' ) ; let debounceTime = 500 ; let watchTimer = null ; let watcher = watch ( path . resolve ( this . config . directory ) , { persistent : true } ) ; watcher . on ( 'change' , ( path ) => { if ( ! this . shouldBeIncluded ( path ) ) { return ; } clearTimeout ( watchTimer ) ; watchTimer = setTimeout ( ( ) => this . eventHandler ( path ) , debounceTime ) ; } ) ; } addListener ( eventType , callback ) { if ( eventType ) { this . watchEvent . addListener ( eventType , callback ) ; } } private shouldBeIncluded ( fileName ) { const result = this . config . files . find ( ( testFile ) => { return testFile . endsWith ( fileName ) ; } ) ; return result !== undefined ; } private eventHandler ( fileName ) { console . info ( 'File change detected. Starting incremental build...' ) ; this . watchEvent . emit ( FileWatcherEvent . REBUILD , fileName ) ; console . info ( 'Watching for file changes.' ) ; } }