Files
route-maps/scripts/parse-maps.js

88 lines
2.6 KiB
JavaScript

import yaml from 'js-yaml';
import fs from 'fs';
import path from 'path';
const inputDir = './static/mapFiles/yaml';
const stationInputDir = './static/stations';
const outputDir = './src/lib/assets/route';
const stationOutputDir = './src/lib/assets/station';
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 = [];
const stationList = [];
fs.readdirSync(stationInputDir).forEach((file) => {
if (file.endsWith('.yaml')) {
const fullPath = path.join(stationInputDir, file);
const content = yaml.load(fs.readFileSync(fullPath, 'utf8'));
if (content.crs) {
stationList.push(content.crs);
}
const fileName = file.replace('.yaml', '.json');
fs.writeFileSync(path.join(stationOutputDir, fileName), JSON.stringify(content));
}
});
console.log(`Found station declarations for the following: ${JSON.stringify(stationList)}`);
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');
const contentSet = new Set();
// Use this loop to add a 'link' to each station if its CRS exists in 'stationList'
if (Array.isArray(content.routeDetail)) {
content.routeDetail.forEach((item) => {
if (item.type === 'station' && item.crs) {
// Edit the item if item.crs exists in 'stationList' - maybe a `linkable: true`?
if (stationList.includes(item.crs)) {
item.stationInfo = true;
}
}
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);
}
}
});
}
fs.writeFileSync(path.join(outputDir, fileName), JSON.stringify(content));
mapList.push({
routeId: content.routeId || null,
routeStart: content.routeStart || null,
routeEnd: content.routeEnd || null,
updated: content.updated || null,
checked: content.checked || null,
contents: Array.from(contentSet)
});
}
});
fs.writeFileSync(indexFile, JSON.stringify(mapList));
console.log(`Generated ${mapList.length} map files and index.`);