-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsort.js
56 lines (45 loc) · 1.44 KB
/
sort.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
const fs = require('fs');
const args = process.argv.slice(2);
const { execSync } = require('child_process');
/*
Usage:
node sort.js {arguments}
`arguments` is one or more file paths, separated by spaces.
The script updates each file by sorting the labels by their keys.
If no arguments are passed, all language files will be sorted.
Example:
node sort.js languages/de-DE/assess-api.json languages/de-DE/questions-api.json
node sort.js
*/
let files;
if (args.length) {
files = args;
} else {
files = execSync("find ./languages -type f -name '*.json'", {
encoding: 'utf8'
}).trim().split(/[\r\n]+/gm);
}
files.forEach((file) => {
const text = fs.readFileSync(file);
let result = JSON.parse(text);
result = recursiveSort(result);
result = JSON.stringify(result, null,' ');
fs.writeFileSync(file, result);
});
function recursiveSort(obj) {
const sortedKeys = Object.keys(obj).sort();
let result = {};
sortedKeys.forEach((key) => {
const val = obj[key];
// Do not sort arrays, as the order of elements is important.
// E.g. reordering questions-api.json's characterMapCharacters would
// cause the special characters to appear in a different order in
// the product.
if (val && typeof val === 'object' && !Array.isArray(val)) {
result[key] = recursiveSort(val);
} else {
result[key] = val;
}
});
return result;
}