import * as chokidar from 'chokidar' ; import chalk from 'chalk' ; import { App , AppMode } from './app' ; export enum WatcherEventType { INIT , ADD , REMOVE , CHANGE , REFRESH_TREE , UNLINK } export class Watcher { chokidarWatcher : any ; watchCallback : any ; watcherListeners : any ; watcherState : any ; disabled = false ; constructor ( private app ) { } enable ( ) { setTimeout ( ( ) => { this . disabled = false ; } , 200 ) ; } disable ( ) { this . disabled = true ; } load ( ) { if ( this . app . mode === AppMode . DEVELOPMENT ) { this . watch ( [ '*.json' , 'server/**/*.json' ] , ( p , type ) => { if ( ! this . disabled ) { this . app . logger . log ( ` └── ${ type }: ${ p }` ) ; this . app . materiaApi . websocket . broadcast ( { type , path : p } ) ; } } ) ; } } watch ( path , callback ) { this . dispose ( ) ; this . watcherListeners = { } ; this . watchCallback = callback ; this . _watchChokidar ( path ) ; } dispose ( ) { if ( this . chokidarWatcher ) { this . chokidarWatcher . removeListener ( 'add' , this . _addEvent ) ; this . chokidarWatcher . removeListener ( 'change' , this . _changeEvent ) ; this . chokidarWatcher . removeListener ( 'unlink' , this . _unlinkEvent ) ; this . chokidarWatcher . close ( ) ; this . chokidarWatcher = null ; } return Promise . resolve ( ) ; } private _watchChokidar ( watchPath ) { const watcher = chokidar . watch ( watchPath , { ignoreInitial : true , ignorePermissionErrors : true , followSymlinks : true , interval : 1000 , binaryInterval : 1000 , cwd : this . app . path , useFsEvents : true } ) ; this . chokidarWatcher = watcher ; this . app . logger . log ( ` └── Watchers: ${ chalk . bold . green ( 'OK' ) }` ) ; watcher . on ( 'add' , this . _addEvent . bind ( this ) ) ; watcher . on ( 'addDir' , this . _addEvent . bind ( this ) ) ; watcher . on ( 'change' , this . _changeEvent . bind ( this ) ) ; watcher . on ( 'unlink' , this . _unlinkEvent . bind ( this ) ) ; watcher . on ( 'unlinkDir' , this . _unlinkEvent . bind ( this ) ) ; } private _eventCallback ( filePath , type ) { if ( filePath == '' ) { return ; } this . watchCallback ( filePath , type ) ; } private _addEvent ( filePath ) { this . _eventCallback ( filePath , 'add' ) ; } private _changeEvent ( filePath ) { if ( /.*\.sqlite/ . test ( filePath ) ) { return ; } this . _eventCallback ( filePath , 'change' ) ; } private _unlinkEvent ( filePath ) { this . _eventCallback ( filePath , 'unlink' ) ; } }