import yaml from 'js-yaml'; import fs from 'fs'; import path from 'path'; const inputDir = './static/mapFiles/yaml'; const outputDir = './static/mapFiles/json'; const indexFile = './static/map-index.json'; const noiseRegex = /\s+(single line|junction|jn|junc|jct|gf|north|south|east|west)\.?$/i; if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true }); const mapList = []; fs.readdirSync(inputDir).forEach((file) => { if (file.endsWith('.yaml')) { const fullPath = path.join(inputDir, file); const content = yaml.load(fs.readFileSync(fullPath, 'utf8')); const fileName = file.replace('.yaml', '.json'); fs.writeFileSync(path.join(outputDir, fileName), JSON.stringify(content)); const contentSet = new Set(); if (Array.isArray(content.routeDetail)) { content.routeDetail.forEach((item) => { if ((item.type === 'junction' || item.type === 'station') && item.name) { let cleanName = item.name; // Run the replacement in a loop or twice to catch nested noise // e.g., "Reading West Junction" // Pass 1: "Reading West" // Pass 2: "Reading" let previousName; do { previousName = cleanName; cleanName = cleanName.replace(noiseRegex, '').trim(); } while (cleanName !== previousName); if (cleanName) { contentSet.add(cleanName); } } }); } mapList.push({ routeId: content.routeId || null, routeStart: content.routeStart || null, routeEnd: content.routeEnd || null, created: content.created || null, checked: content.checked || null, contents: Array.from(contentSet) }); } }); fs.writeFileSync(indexFile, JSON.stringify(mapList)); console.log(`Generated ${mapList.length} map files and index.`);