sanket3280/code-execution
0
1const fs = require('fs');2const path = require('path');3const Problem = require('../models/Problem');4 5/**6 * Sync problemname.txt with current problems in MongoDB7 * Auto-updates when problems are added or deleted8 */9async function syncProblemNames() {10 try {11 // Fetch all problems from MongoDB12 const problems = await Problem.find({})13 .select('questionNumber title')14 .sort({ questionNumber: 1 });15 16 // Format: "1. Two Sum\n2. Add Two Numbers\n..."17 const content = problems18 .map(p => `${p.questionNumber}. ${p.title}`)19 .join('\n');20 21 // Write to problemname.txt (async)22 const filePath = path.join(__dirname, '../scripts/problemname.txt');23 await fs.promises.writeFile(filePath, content + '\n', 'utf8');24 25 console.log(`✅ Synced ${problems.length} problems to problemname.txt`);26 return { success: true, count: problems.length };27 } catch (error) {28 console.error('❌ Error syncing problem names:', error);29 return { success: false, error: error.message };30 }31}32 33module.exports = { syncProblemNames };34 