@@ -13,6 +13,7 @@ import crypto from 'crypto';
1313import fs from 'fs' ;
1414import * as readlineSync from 'readline-sync' ;
1515import path from 'path' ;
16+ import * as chokidar from 'chokidar' ;
1617import { findSmythPath } from '../../../../helpers/Sysconfig.helper' ;
1718
1819const console = Logger ( 'JSONFileVault' ) ;
@@ -29,6 +30,7 @@ export class JSONFileVault extends VaultConnector {
2930 private index : any ;
3031 private shared : string ;
3132 private vaultFile : string ;
33+ private watcher : chokidar . FSWatcher | null = null ;
3234
3335 constructor ( protected _settings : JSONFileVaultConfig ) {
3436 super ( _settings ) ;
@@ -156,7 +158,6 @@ export class JSONFileVault extends VaultConnector {
156158 }
157159
158160 private fetchVaultData ( vaultFile : string , _settings : JSONFileVaultConfig ) {
159-
160161 if ( fs . existsSync ( vaultFile ) ) {
161162 try {
162163 if ( _settings . fileKey && fs . existsSync ( _settings . fileKey ) ) {
@@ -200,10 +201,21 @@ export class JSONFileVault extends VaultConnector {
200201 }
201202
202203 private initFileWatcher ( ) {
203- fs . watch ( this . vaultFile , ( eventType , filename ) => {
204- if ( eventType === 'change' ) {
205- this . fetchVaultData ( this . vaultFile , this . _settings ) ;
206- }
204+ this . watcher = chokidar . watch ( this . vaultFile , {
205+ persistent : false , // Don't keep the process running
206+ ignoreInitial : true ,
207+ } ) ;
208+
209+ this . watcher . on ( 'change' , ( ) => {
210+ this . fetchVaultData ( this . vaultFile , this . _settings ) ;
207211 } ) ;
208212 }
213+
214+ public async stop ( ) {
215+ super . stop ( ) ;
216+ if ( this . watcher ) {
217+ this . watcher . close ( ) ;
218+ this . watcher = null ;
219+ }
220+ }
209221}
0 commit comments