Files
CDN-APP-INSIDER/Middlewares/watcherMiddleware.js
Dinawo 8f3e604774
All checks were successful
continuous-integration/drone/push Build is passing
Update .gitignore and add new dependencies and routes
2024-04-02 20:59:13 +02:00

51 lines
1.5 KiB
JavaScript

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)
};