-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
98 lines (73 loc) · 2.1 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env node
var program = require('commander');
var fs = require('fs');
var path = require('path');
function merge(obj1, obj2){
var obj3 = {};
for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
for (var attrname2 in obj2) { obj3[attrname2] = obj2[attrname2]; }
return obj3;
}
function upperCaseText(match, group1) {
return group1.toUpperCase();
}
function eachRecursive(obj, key) {
var newObject = {};
for (var k in obj) {
var newKey = k;
newKey = (key) ? key + "." + newKey : newKey;
if (typeof obj[k] == "object" && obj[k] !== null) {
newObject = merge(newObject, eachRecursive(obj[k], newKey));
}
if (typeof obj[k] === "string") {
var keyId = newKey.replace(/\.(.)/g, upperCaseText);
newObject[keyId] = {
id: newKey,
defaultMessage: obj[k]
};
}
}
return newObject;
}
function eachTranslation(obj, key) {
var newObject = {};
for (var k in obj) {
var newKey = k;
newKey = (key) ? key + "." + newKey : newKey;
if (typeof obj[k] == "object" && obj[k] !== null) {
newObject = merge(newObject, eachTranslation(obj[k], newKey));
}
if (typeof obj[k] === "string") {
newObject[newKey] = obj[k];
}
}
return newObject;
}
function flatten(args) {
var filename = args[0];
fs.exists(filename, function(exists) {
if (exists) {
var fileObj = require(filename);
var newObj = eachRecursive(fileObj);
console.log(newObj);
var newFilename = path.basename(filename, path.extname(filename))
+ "-default-flat.json";
var translation = eachTranslation(fileObj);
// console.log(translation);
fs.writeFile(newFilename, JSON.stringify(newObj), function(err) {
if (err) {
return console.log(err);
}
console.log(newFilename + " saved");
});
} else {
console.error('File does not exist');
}
});
}
program
.version('0.0.1')
.usage('[options] <file ...>')
.option('-f, --flatten', 'Flatten')
.parse(process.argv);
if (program.flatten) flatten(program.args);