const path = require('path'); const chokidar = require('chokidar'); const fs = require('fs'); const { logger, ErrorLogger, logRequestInfo } = require('../config/logs'); const userFilePath = path.resolve(__dirname, '../data/user.json'); const setupFilePath = path.resolve(__dirname, '../data/setup.json'); let userData, setupData; try { userData = JSON.parse(fs.readFileSync(userFilePath, 'utf-8')); } catch (error) { ErrorLogger.error(`Error parsing user.json: ${error}`); } try { setupData = JSON.parse(fs.readFileSync(setupFilePath, 'utf-8')); } catch (error) { ErrorLogger.error(`Error parsing setup.json: ${error}`); } const watcher = chokidar.watch([userFilePath, setupFilePath], { persistent: true }); watcher.on('change', (filePath) => { let modifiedFile; if (filePath === userFilePath) { try { userData = JSON.parse(fs.readFileSync(filePath, 'utf-8')); modifiedFile = 'user.json'; } catch (error) { logger.error(`Error parsing user.json: ${error}`); } } else if (filePath === setupFilePath) { try { setupData = JSON.parse(fs.readFileSync(filePath, 'utf-8')); modifiedFile = 'setup.json'; } catch (error) { logger.error(`Error parsing setup.json: ${error}`); } } logger.info(`File ${modifiedFile} has been modified`); }); module.exports = { getUserData: () => Promise.resolve(userData), getSetupData: () => Promise.resolve(setupData) };