#!/usr/bin/env node const fs = require('fs'); // Create backup const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5); const backupPath = `src/mocks/resumeInterviewMock.js.backup_${timestamp}_optimize_indent`; const mockContent = fs.readFileSync('src/mocks/resumeInterviewMock.js', 'utf-8'); fs.writeFileSync(backupPath, mockContent); console.log(`✓ Backup created: ${backupPath}`); // Function to optimize answer indentation function optimizeIndentation(answer) { // Split by newlines const lines = answer.split('\\n'); const cleanedLines = []; for (let line of lines) { let cleanedLine = line; // Check if this is a list item or numbered item const hasListMarker = /^\s*[-•·*]/.test(line); const hasNumberedList = /^\s*\d+\./.test(line); if (hasListMarker || hasNumberedList) { // For top-level list items, remove all indentation if (/^\s{0,4}[-•·*\d]/.test(line)) { cleanedLine = line.trimStart(); } else { // For nested list items (more than 4 spaces), keep minimal indentation (2 spaces) cleanedLine = ' ' + line.trimStart(); } } else if (line.trim()) { // For non-list content, check if it's continuation of a list item const leadingSpaces = line.match(/^(\s*)/)[1].length; if (leadingSpaces >= 4) { // This seems to be nested content, reduce to 2 spaces cleanedLine = ' ' + line.trimStart(); } else { // Regular content, no indentation cleanedLine = line.trimStart(); } } cleanedLines.push(cleanedLine); } return cleanedLines.join('\\n'); } // Process the file let updatedContent = mockContent; let count = 0; // Pattern to match answer fields const answerRegex = /"answer":\s*"((?:[^"\\]|\\.)*)"/g; updatedContent = updatedContent.replace(answerRegex, (match, answerContent) => { // Unescape the answer content for processing let unescaped = answerContent .replace(/\\n/g, '\n') .replace(/\\"/g, '"') .replace(/\\\\/g, '\\'); // Optimize indentation let cleaned = optimizeIndentation(unescaped); // Re-escape for JSON let escaped = cleaned .replace(/\\/g, '\\\\') .replace(/"/g, '\\"') .replace(/\n/g, '\\n'); count++; return `"answer": "${escaped}"`; }); console.log(`\n✅ Optimized indentation in ${count} answer fields`); // Save the file fs.writeFileSync('src/mocks/resumeInterviewMock.js', updatedContent); // Verify syntax const { execSync } = require('child_process'); try { execSync('node -c src/mocks/resumeInterviewMock.js', { encoding: 'utf-8' }); console.log('✅ Syntax validation passed'); // Show samples of the optimized content console.log('\n📋 Sample of optimized answers:'); console.log('(Note: Nested items now have minimal 2-space indentation)\n'); const matches = updatedContent.matchAll(/"answer":\s*"([^"]{0,400})/g); let samples = 0; for (const match of matches) { if (samples >= 2) break; console.log(`Sample ${samples + 1}:`); const formatted = match[1].replace(/\\n/g, '\n').substring(0, 350); console.log(formatted); console.log('\n' + '-'.repeat(50) + '\n'); samples++; } } catch (error) { console.error('❌ Syntax error detected, restoring backup...'); fs.writeFileSync('src/mocks/resumeInterviewMock.js', mockContent); console.log('Backup restored'); console.error('Error:', error.message); }