d1d5d629ab575ba7f5b238896eba114624271275
1 import { type FSWatcher
, type WatchListener
, readFileSync
, watch
} from
'node:fs';
3 import { handleFileException
} from
'./ErrorUtils';
4 import { logger
} from
'./Logger';
5 import { isNotEmptyString
} from
'./Utils';
6 import type { FileType
, JsonType
} from
'../types';
8 export const watchJsonFile
= <T
extends JsonType
>(
12 refreshedVariable
?: T
,
13 listener
: WatchListener
<string> = (event
, filename
) => {
14 if (isNotEmptyString(filename
) && event
=== 'change') {
16 logger
.debug(`${logPrefix} ${fileType} file ${file} have changed, reload`);
17 refreshedVariable
&& (refreshedVariable
= JSON
.parse(readFileSync(file
, 'utf8')) as T
);
19 handleFileException(file
, fileType
, error
as NodeJS
.ErrnoException
, logPrefix
, {
25 ): FSWatcher
| undefined => {
26 if (isNotEmptyString(file
)) {
28 return watch(file
, listener
);
30 handleFileException(file
, fileType
, error
as NodeJS
.ErrnoException
, logPrefix
, {
35 logger
.info(`${logPrefix} No ${fileType} file to watch given. Not monitoring its changes`);