| 64 | const contentDir = path.join(__dirname, '../../content/compare'); |
| 65 | |
| 66 | function loadCompareSource(): CompareData[] { |
| 67 | try { |
| 68 | // Check if directory exists before trying to read it |
| 69 | if (!fs.existsSync(contentDir)) { |
| 70 | return []; |
| 71 | } |
| 72 | |
| 73 | const files = fs |
| 74 | .readdirSync(contentDir) |
| 75 | .filter((file) => file.endsWith('.json')); |
| 76 | |
| 77 | return files |
| 78 | .map((file) => { |
| 79 | const filePath = path.join(contentDir, file); |
| 80 | const fileContents = fs.readFileSync(filePath, 'utf8'); |
| 81 | try { |
| 82 | return JSON.parse(fileContents) as CompareData; |
| 83 | } catch (error) { |
| 84 | console.error(`Error parsing compare data for ${file}:`, error); |
| 85 | return null; |
| 86 | } |
| 87 | }) |
| 88 | .flatMap((item) => (item ? [item] : [])) |
| 89 | .map((item) => ({ |
| 90 | ...item, |
| 91 | url: `/compare/${item.slug}`, |
| 92 | })); |
| 93 | } catch (error) { |
| 94 | console.error('Error loading compare source:', error); |
| 95 | return []; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | export const compareSource: CompareData[] = loadCompareSource(); |
| 100 | |