All checks were successful
Generate Release / validate_and_release (push) Successful in 5s
Merge new PIS Updates Co-authored-by: Fred Boniface <fred@fjla.uk> Co-authored-by: Robotic Owl <owlbot@owlboard.info> Reviewed-on: #1
69 lines
2.0 KiB
JavaScript
69 lines
2.0 KiB
JavaScript
const fs = require('fs')
|
|
const yaml = require('yaml')
|
|
|
|
const directoryPath = './pis/'
|
|
|
|
function sortAndMergeYAMLFiles() {
|
|
try {
|
|
const files = fs.readdirSync(directoryPath);
|
|
|
|
let mergedData = [];
|
|
|
|
files.forEach(file => {
|
|
const data = fs.readFileSync(directoryPath + file, 'utf-8');
|
|
|
|
const parsedData = yaml.parse(data)
|
|
|
|
if (parsedData && typeof parsedData === 'object' && parsedData.pis) {
|
|
mergedData.push(...parsedData.pis)
|
|
} else {
|
|
if (Array.isArray(parsedData)) {
|
|
mergedData.push(...parsedData);
|
|
} else {
|
|
console.error("Incorrect YAML")
|
|
}
|
|
}
|
|
});
|
|
|
|
mergedData.sort((a, b) => {
|
|
const codeA = parseInt(a.code);
|
|
const codeB = parseInt(b.code);
|
|
return codeA - codeB
|
|
})
|
|
|
|
|
|
|
|
// Construct the output string, removing duplicates
|
|
let outputString = "pis:\n";
|
|
const seenCodes = new Set();
|
|
mergedData.forEach(item => {
|
|
if (!seenCodes.has(item.code) && item.stops.length > 0) {
|
|
seenCodes.add(item.code);
|
|
outputString += ` - code: "${item.code}"\n`;
|
|
outputString += ` stops: [${item.stops.map(stop => `${stop}`).join(',')}]\n`;
|
|
outputString += ` toc: "${item.toc || "gw"}"\n`;
|
|
}
|
|
});
|
|
|
|
try {
|
|
fs.writeFileSync('./pis/codes.yaml', outputString);
|
|
console.log("Overwritten codes.yaml")
|
|
} catch (err) {
|
|
console.error("Error writing codes.yaml", err)
|
|
}
|
|
|
|
// Remove any new files
|
|
files.forEach(file => {
|
|
if (file !== 'codes.yaml') {
|
|
fs.unlinkSync(directoryPath + file);
|
|
console.log(`Deleted file: ${file}`);
|
|
}
|
|
});
|
|
|
|
} catch(err) {
|
|
console.error('Error merging YAML:', err)
|
|
}
|
|
}
|
|
|
|
|
|
sortAndMergeYAMLFiles(); |