From cb35b5bb9326af058c164571d6fb695c1722b010 Mon Sep 17 00:00:00 2001 From: William Oldham Date: Mon, 30 Dec 2024 14:08:25 +0000 Subject: [PATCH] Add fix file I used --- json-file-fix.mjs | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 json-file-fix.mjs diff --git a/json-file-fix.mjs b/json-file-fix.mjs new file mode 100644 index 00000000..4acd363b --- /dev/null +++ b/json-file-fix.mjs @@ -0,0 +1,76 @@ +import fs from 'fs'; +import path from 'path'; + +// This script does a number of things: +// - Normalises the descriptions and solutions to be "Unknown cause." and "Unknown solution." rather than "N/A" +// - If one of the descriptions or solutions is "Unknown cause." or "Unknown solution.", the other will be copied to it +// - Ensures all descriptions and solutions end with a full stop if they do not already and if the last character is a letter +// - Writes the changes back to the file in place + +const arg = process.argv[2]; +if (!arg || !fs.existsSync(arg) || !fs.lstatSync(arg).isDirectory()) { + console.log("Please provide a valid directory path."); + process.exit(1); +} + +fs.readdirSync(arg, { recursive: true, withFileTypes: true }).forEach(file => { + if (!file.isFile() && !file.name.endsWith(".json")) return; + + const fullPath = path.join(file.parentPath, file.name); + + console.log(`Processing ${fullPath}`); + + const data = fs.readFileSync(fullPath, 'utf8'); + const json = JSON.parse(data); + + const key1 = Object.keys(json)[0]; + const key2 = Object.keys(json[key1])[0]; + + const errorObject = json[key1][key2]; + if (!errorObject || typeof errorObject !== "object") { + console.log(`No error object found in ${fullPath}`); + return; + } + + if (!errorObject["short_description"] || !errorObject["long_description"] || !errorObject["short_solution"] || !errorObject["long_solution"]) { + console.log(`Missing fields in ${fullPath}`); + return; + } + + if (errorObject["short_description"] === "N/A") errorObject["short_description"] = "Unknown cause."; + if (errorObject["long_description"] === "N/A") errorObject["long_description"] = "Unknown cause."; + if (errorObject["short_solution"] === "N/A") errorObject["short_solution"] = "Unknown solution."; + if (errorObject["long_solution"] === "N/A") errorObject["long_solution"] = "Unknown solution."; + + const { + short_description, + long_description, + short_solution, + long_solution, + } = errorObject; + + if (short_description === "Unknown cause." && long_description !== "Unknown cause.") { + errorObject["short_description"] = long_description; + } else if (short_description !== "Unknown cause." && long_description === "Unknown cause.") { + errorObject["long_description"] = short_description; + } + + if (short_solution === "Unknown solution." && long_solution !== "Unknown solution.") { + errorObject["short_solution"] = long_solution; + } else if (short_solution !== "Unknown solution." && long_solution === "Unknown solution.") { + errorObject["long_solution"] = short_solution; + } + + const keys = ["short_description", "long_description", "short_solution", "long_solution"]; + + // Ensure all end with a full stop + keys.forEach(key => { + const value = errorObject[key]; + // If the value does not end with a full stop and the last character is a letter, add a full stop + if (value && !value.endsWith(".") && /[A-Za-z]$/.test(value)) { + errorObject[key] = value + "."; + } + }); + + fs.writeFileSync(fullPath, JSON.stringify(json, null, "\t")); +}); \ No newline at end of file