-
Notifications
You must be signed in to change notification settings - Fork 10
/
json-generator.js
40 lines (34 loc) · 1.47 KB
/
json-generator.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
var fs = require('fs');
var path = require('path');
var Config = require(path.resolve(process.cwd(), process.argv[2]));
var CsvParser = require('./csv-parser');
var DirManager = require('./dir-manager');
module.exports = {
generateJson: generateJson
}
function generateJson() {
fs.readFile(path.resolve(process.cwd(), Config.csvFileIn), { encoding: 'utf8' }, function (error, fileContents) {
if (error) {
console.error("Error opening CSV file: " + error.message);
return;
}
var to = CsvParser.processCsv(fileContents);
for (var language in to) {
if (to.hasOwnProperty(language)) {
var translations = to[language];
var filePath = path.resolve(process.cwd(), Config.jsonDirOut, Config.jsonFileName + '.' + language + '.' + Config.jsonExt);
(function (translations, filePath) {
DirManager.ensureDirectoryExistence(filePath);
fs.writeFile(filePath, JSON.stringify(translations), function (error) {
if (error) {
console.error('write error: ' + error.message);
}
else {
console.log('Translation JSON file generated at ' + filePath);
}
});
})(translations, filePath);
}
}
});
}