forked from osmosis-labs/osmosis-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix-machine-translate.js
65 lines (51 loc) · 1.61 KB
/
fix-machine-translate.js
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
const fs = require("fs");
const path = require("path");
const directoryPath = "packages/web/localizations";
function fixJsonFiles(directory) {
const allFiles = fs.readdirSync(directory);
const files = allFiles.filter((file) => path.extname(file) === ".json");
files.forEach((file) => {
const filePath = path.join(directory, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
fixJsonFiles(filePath);
} else if (stats.isFile() && file.endsWith(".json")) {
try {
fixJsonFile(filePath);
} catch (error) {
console.log(`Error fixing JSON file: ${filePath}`);
console.error(error);
}
}
});
}
function fixJsonFile(file) {
const filePath = path.resolve(file);
const data = fs.readFileSync(filePath);
const jsonData = JSON.parse(data);
const fixedData = nestKeys(jsonData);
fs.writeFileSync(filePath, JSON.stringify(fixedData, null, 2));
console.log(`Fixed JSON file: ${filePath}`);
}
function nestKeys(data) {
const fixedData = {};
for (const key in data) {
if (key.includes(".")) {
const nestedKeys = key.split(".");
let nestedData = fixedData;
for (let i = 0; i < nestedKeys.length - 1; i++) {
const nestedKey = nestedKeys[i];
if (!nestedData.hasOwnProperty(nestedKey)) {
nestedData[nestedKey] = {};
}
nestedData = nestedData[nestedKey];
}
nestedData[nestedKeys[nestedKeys.length - 1]] = data[key];
} else {
fixedData[key] = data[key];
}
}
return fixedData;
}
// Fix JSON files in the specified directory
fixJsonFiles(directoryPath);