-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmigrate.js
265 lines (233 loc) · 7.23 KB
/
migrate.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
const fs = require('fs');
const path = require('path');
const flatten = require('flat');
const async = require('async');
const colors = require('colors');
const request = require('./request');
const getRemoteLanguages = require('./getRemoteLanguages');
const getDirectories = (srcpath) => {
return fs.readdirSync(srcpath).filter(function(file) {
return fs.statSync(path.join(srcpath, file)).isDirectory();
});
};
const getFiles = (srcpath) => {
return fs.readdirSync(srcpath).filter(function(file) {
return !fs.statSync(path.join(srcpath, file)).isDirectory();
});
};
const load = (namespaces, cb) => {
async.each(namespaces, (ns, done) => {
fs.readFile(ns.path, 'utf8', (err, data) => {
if (err) return done(err);
try {
ns.value = flatten(JSON.parse(data));
} catch (err) {
console.error(colors.red(err.stack));
ns.value = {};
}
done();
});
}, (err) => cb(err, namespaces));
};
const parseLanguage = (p, cb) => {
const dirs = getDirectories(p);
const namespaces = [];
dirs.forEach((lng) => {
const files = getFiles(path.join(p, lng));
files.forEach((file) => {
if (path.extname(file) !== '.json') return;
namespaces.push({
language: lng,
namespace: path.basename(file, '.json'),
path: path.join(p, lng, file)
});
});
});
load(namespaces, cb);
};
const transfer = (opt, ns, cb) => {
var url = opt.addPath
.replace('{{projectId}}', opt.projectId)
.replace('{{ver}}', opt.version)
.replace('{{version}}', opt.version)
.replace('{{language}}', ns.language)
.replace('{{lng}}', ns.language)
.replace('{{ns}}', ns.namespace)
.replace('{{namespace}}', ns.namespace);
console.log(colors.yellow(`transfering ${opt.version}/${ns.language}/${ns.namespace}...`));
if (!opt.replace) url = url.replace('/update/', '/missing/');
request(url + `?replace=${!!opt.replace}`, {
method: 'post',
body: ns.value,
headers: {
'Authorization': opt.apiKey
}
}, (err, res, obj) => {
if (err || (obj && (obj.errorMessage || obj.message))) {
if (url.indexOf('/missing/') > -1 && res.status === 412) {
console.log(colors.green(`transfered ${opt.version}/${ns.language}/${ns.namespace} (but all keys already existed)...`));
cb(null);
return;
}
console.log(colors.red(`transfer failed for ${opt.version}/${ns.language}/${ns.namespace}...`));
if (err) return cb(err);
if (obj && (obj.errorMessage || obj.message)) return cb(new Error((obj.errorMessage || obj.message)));
}
if (res.status >= 300 && res.status !== 412) return cb(new Error(res.statusText + ' (' + res.status + ')'));
console.log(colors.green(`transfered ${opt.version}/${ns.language}/${ns.namespace}...`));
cb(null);
});
};
const upload = (opt, nss, cb) => {
if (!opt.referenceLanguage) {
async.eachLimit(
nss,
require('os').cpus().length,
(ns, done) => transfer(opt, ns, done),
cb
);
return;
}
const nssRefLng = nss.filter((n) => n.language === opt.referenceLanguage);
const nssNonRefLng = nss.filter((n) => n.language !== opt.referenceLanguage);
async.eachLimit(
nssRefLng,
require('os').cpus().length,
(ns, done) => transfer(opt, ns, done),
(err) => {
if (err) return cb(err);
async.eachLimit(
nssNonRefLng,
require('os').cpus().length,
(ns, done) => transfer(opt, ns, done),
cb
);
}
);
};
const addLanguage = (opt, l, cb) => {
var url = opt.apiPath + '/language/' + opt.projectId + '/' + l;
request(url, {
method: 'post',
headers: {
'Authorization': opt.apiKey
}
}, (err, res, obj) => {
if (err || (obj && (obj.errorMessage || obj.message))) {
console.log(colors.red(`failed to add language ${l}...`));
if (err) return cb(err);
if (obj && (obj.errorMessage || obj.message)) return cb(new Error((obj.errorMessage || obj.message)));
}
if (res.status >= 300 && res.status !== 412) return cb(new Error(res.statusText + ' (' + res.status + ')'));
console.log(colors.green(`added language ${l}...`));
cb(null);
});
};
const migrate = (opt, cb) => {
if (opt.format !== 'json') {
var err = new Error(`Format ${opt.format} is not accepted!`);
if (!cb) throw err;
if (cb) cb(err);
return;
}
opt.apiPath = opt.apiPath || 'https://api.locize.app';
if (opt.language) {
const files = getFiles(opt.path);
const namespaces = files.map((file) => {
return {
language: opt.language,
namespace: path.basename(file, '.json'),
path: path.join(opt.path, file)
};
});
load(namespaces, (err, nss) => {
if (err) {
if (!cb) { console.error(colors.red(err.stack)); process.exit(1); }
if (cb) cb(err);
return;
}
upload(opt, nss, (err) => {
if (err) {
if (!cb) {
console.error(colors.red(err.stack));
process.exit(1);
}
if (cb) cb(err);
return;
}
if (!cb) console.log(colors.green('FINISHED'));
if (cb) cb(null);
});
});
return;
}
if (opt.parseLanguage) {
parseLanguage(opt.path, (err, nss) => {
if (err) {
if (!cb) console.error(colors.red(err.stack)); process.exit(1);
if (cb) cb(err);
return;
}
getRemoteLanguages(opt, (err, remoteLanguages) => {
if (err) {
if (!cb) { console.error(colors.red(err.stack)); process.exit(1); }
if (cb) cb(err);
return;
}
const localLanguages = [];
nss.forEach((n) => {
if (localLanguages.indexOf(n.language) < 0) localLanguages.push(n.language);
});
const notExistingLanguages = localLanguages.filter((l) => remoteLanguages.indexOf(l) < 0);
if (notExistingLanguages.length === 0) {
upload(opt, nss, (err) => {
if (err) {
if (!cb) {
console.error(colors.red(err.stack));
process.exit(1);
}
if (cb) cb(err);
return;
}
if (!cb) console.log(colors.green('FINISHED'));
if (cb) cb(null);
});
return;
}
async.eachLimit(
notExistingLanguages,
require('os').cpus().length,
(l, done) => addLanguage(opt, l, done),
(err) => {
if (err) {
if (!cb) {
console.error(colors.red(err.stack));
process.exit(1);
}
if (cb) cb(err);
return;
}
setTimeout(() => {
// wait a bit to make sure project is up-to-date also in cache
upload(opt, nss, (err) => {
if (err) {
if (!cb) {
console.error(colors.red(err.stack));
process.exit(1);
}
if (cb) cb(err);
return;
}
if (!cb) console.log(colors.green('FINISHED'));
if (cb) cb(null);
});
}, 5000);
}
);
return;
});
});
return;
}
};
module.exports = migrate;