67 lines
2.2 KiB
JavaScript
67 lines
2.2 KiB
JavaScript
const path = require('path');
|
|
const chokidar = require('chokidar');
|
|
const fs = require('fs');
|
|
const { logger, ErrorLogger, logRequestInfo } = require('../config/logs');
|
|
|
|
// Define file paths
|
|
const userFilePath = path.resolve(__dirname, '../data/user.json');
|
|
const setupFilePath = path.resolve(__dirname, '../data/setup.json');
|
|
const collaborationFilePath = path.resolve(__dirname, '../data/collaboration.json');
|
|
|
|
// Initialize data objects
|
|
let userData, setupData, collaborationData;
|
|
|
|
// Load initial user data
|
|
try {
|
|
userData = JSON.parse(fs.readFileSync(userFilePath, 'utf-8'));
|
|
} catch (error) {
|
|
ErrorLogger.error(`Error parsing user.json: ${error}`);
|
|
}
|
|
|
|
// Load initial setup data
|
|
try {
|
|
setupData = JSON.parse(fs.readFileSync(setupFilePath, 'utf-8'));
|
|
} catch (error) {
|
|
ErrorLogger.error(`Error parsing setup.json: ${error}`);
|
|
}
|
|
|
|
// Load initial collaboration data
|
|
try {
|
|
collaborationData = JSON.parse(fs.readFileSync(collaborationFilePath, 'utf-8'));
|
|
} catch (error) {
|
|
ErrorLogger.error(`Error parsing collaboration.json: ${error}`);
|
|
}
|
|
|
|
// Set up file watcher
|
|
const watcher = chokidar.watch([userFilePath, setupFilePath, collaborationFilePath], {
|
|
persistent: true
|
|
});
|
|
|
|
// Handle file changes
|
|
watcher.on('change', (filePath) => {
|
|
let modifiedFile;
|
|
|
|
try {
|
|
if (filePath === userFilePath) {
|
|
userData = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
modifiedFile = 'user.json';
|
|
} else if (filePath === setupFilePath) {
|
|
setupData = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
modifiedFile = 'setup.json';
|
|
} else if (filePath === collaborationFilePath) {
|
|
collaborationData = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
modifiedFile = 'collaboration.json';
|
|
}
|
|
|
|
logger.info(`File ${modifiedFile} has been modified`);
|
|
} catch (error) {
|
|
ErrorLogger.error(`Error parsing ${modifiedFile}: ${error}`);
|
|
}
|
|
});
|
|
|
|
// Export data access functions
|
|
module.exports = {
|
|
getUserData: () => Promise.resolve(userData),
|
|
getSetupData: () => Promise.resolve(setupData),
|
|
getCollaborationData: () => Promise.resolve(collaborationData)
|
|
}; |