-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathjson-file-fix.mjs
71 lines (56 loc) · 2.53 KB
/
json-file-fix.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
}
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"));
});