const fs = require("fs"); const path = require("path"); const resultsFilePath = path.join(__dirname, "results.json"); // Flag to track if the file has been reset let isFileReset = false; function updateResultsDatabase(newResult) { // Check if the file needs to be reset if (!isFileReset) { if (fs.existsSync(resultsFilePath)) { const fileContent = fs.readFileSync(resultsFilePath, "utf-8"); if (fileContent.trim()) { // Reset the file to an empty array fs.writeFileSync(resultsFilePath, JSON.stringify([], null, 2)); console.log("Old data removed from results.json."); } } isFileReset = true; // Mark the file as reset } // Read existing results or initialize an empty array let results = []; if (fs.existsSync(resultsFilePath)) { const fileContent = fs.readFileSync(resultsFilePath, "utf-8"); if (fileContent.trim()) { results = JSON.parse(fileContent); } } // Add the new result to the array results.push(newResult); // Write the updated results back to the file fs.writeFileSync(resultsFilePath, JSON.stringify(results, null, 2)); } module.exports = updateResultsDatabase;