-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_keys.js
83 lines (70 loc) · 2.76 KB
/
copy_keys.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const fs = require('fs');
const path = require('path');
const core = require('@actions/core');
function loadJSONFile(filePath) {
const data = fs.readFileSync(filePath, 'utf8');
return JSON.parse(data);
}
function saveJSONFile(filePath, data) {
fs.writeFileSync(filePath, JSON.stringify(data, null, 2) + '\n'); // Updated to 2 spaces and appending newline
}
function updateJSONFiles(baseFileName, directory) {
/*
const baseFilePath = path.join(directory, baseFileName);
const baseData = loadJSONFile(baseFilePath);
// Sort the base data alphabetically by key (case insensitive)
const sortedBaseData = {};
Object.keys(baseData)
.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
.forEach(key => {
sortedBaseData[key] = baseData[key];
});
*/
fs.readdirSync(directory).forEach(filename => {
const filePath = path.join(directory, filename);
if (filename.endsWith('.json')) {
let otherData = loadJSONFile(filePath);
// Check if keys are in alphabetical order
const keys = Object.keys(otherData);
for (let i = 1; i < keys.length; i++) {
if (keys[i] < keys[i - 1]) {
throw new Error('Keys are not alphabetized in ' + filename);
}
}
}
/*
if (filename.endsWith('.json') && filename !== baseFileName) {
let otherData = loadJSONFile(filePath);
// Check if there are keys in another langague that are not in the base English file
for (const key in otherData) {
if (!(key in sortedBaseData)) {
throw new Error(`Key '${key}' found in '${filename}' but not in '${baseFileName}' file`);
}
}
// Copy missing fields from the base data
for (const key in sortedBaseData) {
if (!(key in otherData)) {
console.log(`Copying key '${key}' to '${filename}'`);
otherData[key] = sortedBaseData[key];
}
}
const keys = Object.keys(otherData);
// Sort the other data alphabetically by key (case insensitive)
const sortedOtherData = {};
keys.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()))
.forEach(key => {
sortedOtherData[key] = otherData[key];
});
saveJSONFile(filePath, sortedOtherData);
}
*/
});
}
const directory = core.getInput('directory');
try {
updateJSONFiles(`en-us.json`, directory);
console.log("All files in alphabetical order");
} catch (error) {
console.error(error.message);
core.setFailed(error.message);
}