import chokidar , { FSWatcher } from "chokidar" ; import { EventEmitter } from "events" ; import { FsalEvents , ChokidarEvents } from "@common/events" ; export default class FSALWatchdog extends EventEmitter { private _process : FSWatcher | null ; private _isBooting : boolean ; private _paths : Set < string > ; private _ignoreDotfiles : boolean ; constructor ( ignoreDotfiles = true ) { super ( ) ; this . _ignoreDotfiles = ignoreDotfiles ; this . _process = null ; this . _paths = new Set < string > ( ) ; this . _isBooting = false ; } param p async init ( p ? ) { console . log ( "fsal-watcher :: init() :: path=" , p ) ; if ( this . _paths . size < 1 || this . isBooting ( ) ) { return ; } this . _isBooting = true ; todo let ignoreDirs = this . _ignoreDotfiles ? [ '**/.noteworthy/**' , '**/.git/**' , '**/.vscode/**' ] : [ ] ; this . _process = chokidar . watch ( ( p ? p : [ ] ) , { ignored : ignoreDirs , persistent : true , ignoreInitial : true } ) ; this . _process . on ( ChokidarEvents . READY , ( event , path ) => { if ( ! this . _process ) { console . error ( "chokidar :: ready :: unknown startup error" ) ; return ; } console . log ( "chokidar :: ready" ) ; let alreadyWatched = Object . keys ( this . _process . getWatched ( ) ) ; for ( let p of this . _paths ) { if ( ! alreadyWatched . includes ( p ) ) { this . _process . add ( p ) ; } } this . _isBooting = false ; } ) ; this . _process . on ( ChokidarEvents . ALL , ( event , path ) => { this . emit ( FsalEvents . CHOKIDAR_EVENT , event , { path } ) ; } ) ; } destroy ( ) { if ( this . _process ) { this . _process . removeAllListeners ( ) ; this . _process . close ( ) ; this . _process = null ; } this . _paths . clear ( ) ; this . _isBooting = false ; } isBooting ( ) { return this . _isBooting ; } ; ignoreOnce ( ) { } returns watch ( p ) { console . log ( "fsal-watcher :: watch ::" , p ) ; if ( this . _paths . has ( p ) ) { console . log ( "\tpath already watched" ) ; return this ; } this . _paths . add ( p ) ; if ( this . isBooting ( ) ) { console . log ( "\twatcher booting" ) ; return this ; } if ( ! this . _process ) { console . log ( "\tinitializing watcher" ) ; this . init ( p ) ; } else { this . _process . add ( p ) ; } return this ; } param p returns unwatch ( p ) { console . log ( "fsal-watcher :: unwatch ::" , p ) ; if ( ! this . _process ) { return this ; } this . _paths . delete ( p ) ; this . _process . unwatch ( p ) ; return this ; } unwatchAll ( ) { for ( let p in this . _paths ) { this . unwatch ( p ) ; } } }