1 import fs from
'node:fs';
3 import { ErrorUtils
} from
'./ErrorUtils';
4 import { logger
} from
'./Logger';
5 import { Utils
} from
'./Utils';
6 import type { FileType
, JsonType
} from
'../types';
8 export class FileUtils
{
9 private constructor() {
10 // This is intentional
13 public static watchJsonFile
<T
extends JsonType
>(
17 refreshedVariable
?: T
,
18 listener
: fs
.WatchListener
<string> = (event
, filename
) => {
19 if (Utils
.isNotEmptyString(filename
) && event
=== 'change') {
21 logger
.debug(`${logPrefix} ${fileType} file ${file} have changed, reload`);
22 refreshedVariable
&& (refreshedVariable
= JSON
.parse(fs
.readFileSync(file
, 'utf8')) as T
);
24 ErrorUtils
.handleFileException(
27 error
as NodeJS
.ErrnoException
,
36 ): fs
.FSWatcher
| undefined {
37 if (Utils
.isNotEmptyString(file
)) {
39 return fs
.watch(file
, listener
);
41 ErrorUtils
.handleFileException(file
, fileType
, error
as NodeJS
.ErrnoException
, logPrefix
, {
46 logger
.info(`${logPrefix} No ${fileType} file to watch given. Not monitoring its changes`);