forked from harshavardhanc/sunbird-localization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
152 lines (147 loc) · 8.25 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
var _ = require('lodash');
var json = require('./formatjson');
var phrase = require('./phrase');
var fs = require('fs');
var async = require('async');
var flatten = require('flat');
var unflatten = require('flat').unflatten;
var authToken, project, locale, fileformat;
const args = process.argv.slice(2);
const projectURL = 'https://api.phrase.com/v2/projects';
const encoding = 'UTF-8';
// Valid options are "UTF-8", "UTF-16" and "ISO-8859-1"
var distFolder = 'sunbirdresourcebundle';
var files = [];
var merge = false;
makeDistFolder = (distFolderName) => {
fs.mkdir(distFolderName, { recursive: true }, (err) => {
if (err) throw err;
});
}
if (_.findIndex(args, function(arg) { return arg.includes('-distFolder') }) !== -1) {
const value = args[_.findIndex(args, function(arg) { return arg.includes('-distFolder') })];
distFolder = value.slice(12);
makeDistFolder(distFolder);
} else {
makeDistFolder(distFolder);
}
_.forEach(args, function (arg) {
if (arg.includes('-authToken')) {
authToken = arg.slice(11);
}
if (arg.includes('-project')) {
project = arg.slice(9).split(",");
}
if (arg.includes('-locale')) {
locale = arg.slice(8).split(",");
}
if (arg.includes('-fileformat')) {
fileformat = arg.slice(12);
}
if (arg.includes('-merge')) {
merge = arg.slice(7);
}
});
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
const run = async () => {
if (authToken) {
if (project) {
if (locale) {
if (fileformat) {
await phrase.getProjects(authToken, projectURL)
.then(async (data) => {
const projects = await JSON.parse(data);
await asyncForEach(projects, async (Project) => {
await asyncForEach(project, async (prj) => {
if (Project['name'] === prj) {
const projectId = await Project['id'];
await phrase.getLocales(authToken, projectURL, projectId)
.then(async (data) => {
const locales = await JSON.parse(data);
if (locales && (locales.length > 0)) {
await asyncForEach(locales, async (element) => {
if (locale.indexOf(element['code']) !== -1) {
const localeId = await element['id'];
await phrase.downloadFile(authToken, projectURL, projectId, localeId, fileformat, encoding)
.then(async (data) => {
if (fileformat === 'nested_json' || fileformat === 'json' || fileformat === 'react_simple_json'
|| fileformat === 'react_nested_json' || fileformat === 'simple_json' || fileformat === 'go_i18n'
|| fileformat === 'angular_translate' || fileformat === 'i18next') {
const object = await JSON.parse(data);
await json.formatjson(object);
await fs.writeFile(distFolder + '/' + prj + ' - ' + element['code'] + '.json', JSON.stringify(object), encoding, () => {
files.push(prj + ' - ' + element['code'] + '.json');
if (merge === false) {
console.log(prj + ' - ' + element['code'] + '.json File Generated');
}
});
} else {
await fs.writeFile(distFolder + '/' + prj + ' - ' + element['code'] + '.' + fileformat, data, encoding, () => {
console.log(prj + ' - ' + element['code'] + '.' + fileformat + ' File Generated');
});
}
})
.catch((err) => {
console.log('Error ', err);
});
}
})
} else {
console.log('Locales Not Found');
}
})
.catch((err) => {
console.log('Error ', err);
});
}
})
})
})
.catch((err) => {
console.log('Error ', err);
});
if ((fileformat === 'nested_json' || fileformat === 'json' || fileformat === 'react_simple_json'
|| fileformat === 'react_nested_json' || fileformat === 'simple_json' || fileformat === 'go_i18n'
|| fileformat === 'angular_translate' || fileformat === 'i18next') && (merge === 'true')) {
setTimeout(async () => {
await asyncForEach(locale, async (Locale) => {
let result = [];
let jsonresult = {};
for (let index = 0; index < files.length; index++) {
if (files[index].includes(Locale)) {
const json = await JSON.parse(fs.readFileSync(distFolder + '/' + files[index], 'utf8'));
const flatjson = await flatten(json);
result.push(flatjson);
fs.unlinkSync(distFolder + '/' + files[index]);
}
if (index === files.length - 1) {
await asyncForEach(result, async (res) => {
await Object.assign(jsonresult, res);
const unfjson = await unflatten(jsonresult);
await fs.writeFile(distFolder + '/' + Locale.substring(0, 2) + '.json', JSON.stringify(unfjson), encoding, () => {
console.log(Locale.substring(0, 2) + '.json File Generated');
});
})
}
}
})
}, 2000);
}
} else {
console.log('FileFormat is Missing');
}
} else {
console.log('Locale is Missing');
}
} else {
console.log('Project is Missing');
}
} else {
console.log('AuthToken is Missing');
}
}
run();