/** * Update grade code format in all JSON files * Old format: GUUL (4 digits) - e.g., 1011 = Grade 1, Unit 01, Lesson 1 * New format: GGUULL (6 digits) - e.g., 010101 = Grade 01, Unit 01, Lesson 01 */ const fs = require('fs'); const path = require('path'); // Configuration const DATA_DIR = path.join(__dirname, 'data'); const DRY_RUN = false; // Set to true to preview changes without updating files function convertGradeCode(oldCode) { const codeStr = oldCode.toString(); if (codeStr.length === 4) { // Format: GUUL -> GGUULL const grade = codeStr[0]; // G const unit = codeStr.substring(1, 3); // UU const lesson = codeStr[3]; // L return `0${grade}${unit}0${lesson}`; } else if (codeStr.length === 3) { // Format: GUL -> GGUULL const grade = codeStr[0]; // G const unit = codeStr[1]; // U const lesson = codeStr[2]; // L return `0${grade}0${unit}0${lesson}`; } else if (codeStr.length === 6) { // Already in new format return codeStr; } // Unknown format, return as is console.log(` āš ļø Unknown format: ${codeStr}`); return codeStr; } function processJsonFile(filePath) { try { const data = JSON.parse(fs.readFileSync(filePath, 'utf8')); if (!Array.isArray(data)) { return { updated: 0, changes: [] }; } let updated = 0; const changes = []; // Update grade codes data.forEach(entry => { if (entry.grade) { const oldGrade = entry.grade; const newGrade = convertGradeCode(oldGrade); if (oldGrade !== newGrade) { changes.push({ old: oldGrade, new: newGrade }); entry.grade = newGrade; updated++; } } }); // Write updated data back to file if (!DRY_RUN && updated > 0) { fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8'); } return { updated, changes }; } catch (error) { console.error(` āŒ Error processing ${filePath}: ${error.message}`); return { updated: 0, changes: [] }; } } function processDirectory(dir) { const entries = fs.readdirSync(dir, { withFileTypes: true }); let totalUpdated = 0; const allChanges = []; for (const entry of entries) { const fullPath = path.join(dir, entry.name); if (entry.isDirectory()) { const result = processDirectory(fullPath); totalUpdated += result.updated; allChanges.push(...result.changes); } else if (entry.isFile() && entry.name.endsWith('.json')) { console.log(` šŸ“„ Processing ${entry.name}...`); const { updated, changes } = processJsonFile(fullPath); if (updated > 0) { console.log(` āœ… Updated ${updated} grade codes`); changes.forEach(c => { console.log(` ${c.old} → ${c.new}`); }); totalUpdated += updated; allChanges.push(...changes); } else { console.log(` ā­ļø No changes needed`); } } } return { updated: totalUpdated, changes: allChanges }; } async function main() { try { console.log('šŸ”„ Starting grade code format update...\n'); if (DRY_RUN) { console.log('āš ļø DRY RUN MODE - No files will be modified\n'); } // Check if data directory exists if (!fs.existsSync(DATA_DIR)) { console.error('āŒ Data directory not found:', DATA_DIR); process.exit(1); } // Process all JSON files in data directory console.log('šŸ“‚ Processing JSON files...\n'); const { updated, changes } = processDirectory(DATA_DIR); console.log('\nāœ… Processing complete!'); console.log(`šŸ“Š Total grade codes updated: ${updated}`); if (DRY_RUN) { console.log('\nāš ļø This was a DRY RUN. Set DRY_RUN = false to apply changes.'); } process.exit(0); } catch (error) { console.error('āŒ Error:', error.message); console.error(error.stack); process.exit(1); } } // Run the script main();