543a2b23c2eda50f157680f15377ee335ec697da
1 import { type FSWatcher
, readFileSync
, watch
, type WatchListener
} from
'node:fs'
3 import type { FileType
, JsonType
} from
'../types/index.js'
4 import { handleFileException
} from
'./ErrorUtils.js'
5 import { logger
} from
'./Logger.js'
6 import { isNotEmptyString
} from
'./Utils.js'
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
!= null &&
18 (refreshedVariable
= JSON
.parse(readFileSync(file
, 'utf8')) as T
)
20 handleFileException(file
, fileType
, error
as NodeJS
.ErrnoException
, logPrefix
, {
26 ): FSWatcher
| undefined => {
27 if (isNotEmptyString(file
)) {
29 return watch(file
, listener
)
31 handleFileException(file
, fileType
, error
as NodeJS
.ErrnoException
, logPrefix
, {
36 logger
.info(`${logPrefix} No ${fileType} file to watch given. Not monitoring its changes`)