-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·198 lines (164 loc) · 5.63 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env node
const version = "1.1.0";
var dot = require("dot-object");
var fs = require('fs');
var csv = require("fast-csv");
var basepath = process.cwd();
var usageMessage = `
Merge JSON files created with ngx-translate-extract to a single CSV file and vice versa.
--------
Usage:
ngx-translate-extract-csv -l <languages> [-i <input path>] [-o <output path>] [-s <separator>]
ngx-translate-extract-csv -r -i <input csv file> [-o <output path>] [-s <separator>]
ngx-translate-extract-csv -h | --help
ngx-translate-extract-csv -v | --version
Options:
<languages> : comma separated values of the input json files. Assumes .json extension
<input path> : location of the .json files. Default "./src/assets/i18n"
<output path> : the path of the output file. If ommited the results are printed in screen only.
<separator> : the separator character. Must be 1 character long. Defaults to "," (comma) separator.
-r : Reverse operation. Split a CSV file to multiple JSON files.
<input csv file> : The CSV file to be processed
<output path> : The target folder in wich the .json files will be created. Default "./src/assets/i18n"
`;
function getArguments() {
let params = {};
for (let i = 2; i < process.argv.length; i++) {
if (process.argv[i].substring(0, 1) === "-") {
if ( typeof process.argv[i + 1] !== "undefined" && process.argv[i + 1].substring(0, 1) !== "-") {
params[process.argv[i].substring(1)] = process.argv[i + 1];
} else {
params[process.argv[i].substring(1)] = null;
}
}
}
return params;
}
function injectLangObject(lang, langObj, tableObj) {
let flatened = dot.dot(langObj);
let keys = Object.keys(flatened);
for (let key of keys) {
let source = flatened[key];
if (typeof tableObj[key] !== "undefined") {
tableObj[key][lang] = source;
} else {
tableObj[key] = {};
tableObj[key][lang] = source;
}
}
}
// Main Execution goes here
// React to easy parameter (version and help)
let params = getArguments();
if (params.hasOwnProperty("h") || params.hasOwnProperty("-help")) {
console.log(usageMessage);
process.exit(0);
}
if (params.hasOwnProperty("v") || params.hasOwnProperty("-version")) {
console.log("version: "+version);
process.exit(0);
}
var delimiter = params["s"] ? params["s"] : ",";
if (delimiter.length > 1) {
console.log('Delimiter must be 1 character long');
process.exit(1);
}
if (params.hasOwnProperty("r")) {
//Reverse mode: CSV to JSON files
if (params.hasOwnProperty("i")) {
//set paths
let sourcePath = params["i"];
//check if input path to csv is defined
if (sourcePath === "undefined" || sourcePath === null || sourcePath === "") {
console.log(usageMessage);
process.exit(0);
}
let destinationPath = params["o"] || "./src/assets/i18n";
//Reading the stream
let allLangsObject = {};
csv
.fromPath(`${basepath}/${sourcePath}`, {headers: true, ignoreEmpty: true, delimiter: delimiter})
.on("data", function(data){
//mix lines into a flat-object with 'lang' prepending the termId
let termId=data["termID"];
for (let key in data) {
let dataval=data[key];
if (key!=="termID") {
allLangsObject[`${key}.${termId}`] = dataval;
}
}
})
.on("end", function(){
//Give depth to the flat-object. 1st level of properties of the object are each language
let depthedAllLangsObject = dot.object(allLangsObject);
for (let lang in depthedAllLangsObject) {
//write language specific object to language file
let filepath = `${basepath}/${destinationPath}/${lang}.json`;
var outputFile = fs.createWriteStream(filepath, {
flags: 'w'
});
console.log(`Creating: ${filepath}`);
outputFile.write(JSON.stringify(depthedAllLangsObject[lang],null,'\t'));
outputFile.end();
}
console.log('Done...');
});
} else {
console.log(usageMessage);
process.exit(0);
}
let sourcePath = params["i"] || "./src/assets/i18n";
let destinationPath = params["o"];
let langsArg = params["l"];
} else {
// Forward mode: JSON files to CSV
let sourcePath = params["i"] || "./src/assets/i18n";
let destinationPath = params["o"];
let langsArg = params["l"];
if (
typeof sourcePath !== "string" || sourcePath === ""
|| typeof langsArg !== "string" || langsArg === ""
) {
console.log(usageMessage);
process.exit(0);
}
let languages = langsArg.split(",");
if (!Array.isArray(languages) || languages.length === 0) {
console.log(usageMessage);
process.exit(0);
}
// Read source files into a single CSV-ready object
let tableObj = {};
for (i = 0; i < languages.length; i++) {
let langObj = require(basepath + "/" + sourcePath + "/" + languages[i] + ".json");
injectLangObject(languages[i], langObj, tableObj);
}
if (typeof destinationPath === "string") {
var outputFile = fs.createWriteStream(destinationPath, {
flags: 'w'
})
}
//Print the CSV
let header = '"termID"';
for (let lang of languages) {
header +=`${delimiter}"${lang}"`;
}
console.log(header);
if (typeof destinationPath === "string") {
outputFile.write(header+"\n");
}
let terms = Object.keys(tableObj);
for (let term of terms) {
let line = `"${term}"`;
for (let lang of languages) {
line += `${delimiter}"${tableObj[term][lang]}"`;
}
console.log(line);
if (typeof destinationPath === "string") {
outputFile.write(line+"\n");
}
}
if (typeof destinationPath === "string") {
outputFile.end();
}
}