data/scripts/merge-yaml.js

67 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2024-03-08 12:22:56 +00:00
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
})
const mergedYaml = yaml.stringify(mergedData);
// Construct the output string
let outputString = "pis:\n";
mergedData.forEach(item => {
if (item.stops.length > 0) {
outputString += ` - code: "${item.code}"\n`;
outputString += ` stops: [${item.stops.map(stop => `${stop}`).join(',')}]\n`;
2024-11-10 19:04:27 +00:00
outputString += ` toc: "${item.toc || "gw"}"\n`;
}
2024-03-08 13:14:22 +00:00
});
2024-03-08 13:16:25 +00:00
try {
fs.writeFileSync('./pis/gw.yaml', outputString);
console.log("Overwritten gw.pis")
} catch (err) {
console.error("Error writing gw.pis", err)
}
2024-03-08 13:14:22 +00:00
// Remove any files created by dgp2
2024-03-08 13:14:22 +00:00
files.forEach(file => {
if (file.startsWith('auto-')) {
2024-03-08 13:14:22 +00:00
fs.unlinkSync(directoryPath + file);
console.log(`Deleted file: ${file}`);
}
});
2024-03-08 12:22:56 +00:00
} catch(err) {
console.error('Error merging YAML:', err)
}
}
2024-03-08 12:22:56 +00:00
sortAndMergeYAMLFiles();