locize-cli 10.3.2 → 10.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/migrate.js CHANGED
@@ -1,148 +1,149 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const flatten = require('flat');
4
- const async = require('async');
5
- const colors = require('colors');
6
- const request = require('./request');
7
- const getRemoteLanguages = require('./getRemoteLanguages');
1
+ const fs = require('fs')
2
+ const path = require('path')
3
+ const flatten = require('flat')
4
+ const async = require('async')
5
+ const colors = require('colors')
6
+ const request = require('./request')
7
+ const getRemoteLanguages = require('./getRemoteLanguages')
8
8
 
9
9
  const getDirectories = (srcpath) => {
10
- return fs.readdirSync(srcpath).filter(function(file) {
11
- return fs.statSync(path.join(srcpath, file)).isDirectory();
12
- });
13
- };
10
+ return fs.readdirSync(srcpath).filter(function (file) {
11
+ return fs.statSync(path.join(srcpath, file)).isDirectory()
12
+ })
13
+ }
14
14
 
15
15
  const getFiles = (srcpath) => {
16
- return fs.readdirSync(srcpath).filter(function(file) {
17
- return !fs.statSync(path.join(srcpath, file)).isDirectory();
18
- });
19
- };
16
+ return fs.readdirSync(srcpath).filter(function (file) {
17
+ return !fs.statSync(path.join(srcpath, file)).isDirectory()
18
+ })
19
+ }
20
20
 
21
21
  const load = (namespaces, cb) => {
22
22
  async.each(namespaces, (ns, done) => {
23
23
  fs.readFile(ns.path, 'utf8', (err, data) => {
24
- if (err) return done(err);
24
+ if (err) return done(err)
25
25
  try {
26
- ns.value = flatten(JSON.parse(data));
26
+ ns.value = flatten(JSON.parse(data))
27
27
  } catch (err) {
28
- console.error(colors.red(err.stack));
29
- ns.value = {};
28
+ console.error(colors.red(err.stack))
29
+ ns.value = {}
30
30
  }
31
- done();
32
- });
33
- }, (err) => cb(err, namespaces));
34
- };
31
+ done()
32
+ })
33
+ }, (err) => cb(err, namespaces))
34
+ }
35
35
 
36
36
  const parseLanguage = (p, cb) => {
37
- const dirs = getDirectories(p);
37
+ const dirs = getDirectories(p)
38
38
 
39
- const namespaces = [];
39
+ const namespaces = []
40
40
 
41
41
  dirs.forEach((lng) => {
42
- const files = getFiles(path.join(p, lng));
42
+ const files = getFiles(path.join(p, lng))
43
43
 
44
44
  files.forEach((file) => {
45
- if (path.extname(file) !== '.json') return;
45
+ if (path.extname(file) !== '.json') return
46
46
 
47
47
  namespaces.push({
48
48
  language: lng,
49
49
  namespace: path.basename(file, '.json'),
50
50
  path: path.join(p, lng, file)
51
- });
52
- });
53
- });
51
+ })
52
+ })
53
+ })
54
54
 
55
- load(namespaces, cb);
56
- };
55
+ load(namespaces, cb)
56
+ }
57
57
 
58
58
  const transfer = (opt, ns, cb) => {
59
- var url = opt.addPath
59
+ if (!opt.addPath) opt.addPath = `${opt.apiPath}/update/{{projectId}}/{{version}}/{{lng}}/{{ns}}`
60
+ let url = opt.addPath
60
61
  .replace('{{projectId}}', opt.projectId)
61
62
  .replace('{{ver}}', opt.version)
62
63
  .replace('{{version}}', opt.version)
63
64
  .replace('{{language}}', ns.language)
64
65
  .replace('{{lng}}', ns.language)
65
66
  .replace('{{ns}}', ns.namespace)
66
- .replace('{{namespace}}', ns.namespace);
67
+ .replace('{{namespace}}', ns.namespace)
67
68
 
68
- console.log(colors.yellow(`transfering ${opt.version}/${ns.language}/${ns.namespace}...`));
69
+ console.log(colors.yellow(`transfering ${opt.version}/${ns.language}/${ns.namespace}...`))
69
70
 
70
- if (!opt.replace) url = url.replace('/update/', '/missing/');
71
+ if (!opt.replace) url = url.replace('/update/', '/missing/')
71
72
 
72
- var data = ns.value;
73
- var keysToSend = Object.keys(data).length;
74
- if (keysToSend === 0) return cb(null);
73
+ const data = ns.value
74
+ const keysToSend = Object.keys(data).length
75
+ if (keysToSend === 0) return cb(null)
75
76
 
76
- var payloadKeysLimit = 1000;
77
+ const payloadKeysLimit = 1000
77
78
 
78
- function send(d, so, isFirst, clb, isRetrying) {
79
- const queryParams = new URLSearchParams();
79
+ function send (d, so, isFirst, clb, isRetrying) {
80
+ const queryParams = new URLSearchParams()
80
81
  if (so) {
81
- queryParams.append('omitstatsgeneration', 'true');
82
+ queryParams.append('omitstatsgeneration', 'true')
82
83
  }
83
84
  if (isFirst && opt.replace) {
84
- queryParams.append('replace', 'true');
85
+ queryParams.append('replace', 'true')
85
86
  }
86
87
 
87
- const queryString = queryParams.size > 0 ? '?' + queryParams.toString() : '';
88
+ const queryString = queryParams.size > 0 ? '?' + queryParams.toString() : ''
88
89
 
89
90
  request(url + queryString, {
90
91
  method: 'post',
91
92
  body: d,
92
93
  headers: {
93
- 'Authorization': opt.apiKey
94
+ Authorization: opt.apiKey
94
95
  }
95
96
  }, (err, res, obj) => {
96
97
  if (err || (obj && (obj.errorMessage || obj.message))) {
97
98
  if (url.indexOf('/missing/') > -1 && res.status === 412) {
98
- console.log(colors.green(`transfered ${Object.keys(d).length} keys ${opt.version}/${ns.language}/${ns.namespace} (but all keys already existed)...`));
99
- clb(null);
100
- return;
99
+ console.log(colors.green(`transfered ${Object.keys(d).length} keys ${opt.version}/${ns.language}/${ns.namespace} (but all keys already existed)...`))
100
+ clb(null)
101
+ return
101
102
  }
102
103
  if (res.status === 504 && !isRetrying) {
103
- return setTimeout(() => send(d, so, isFirst, clb, true), 3000);
104
+ return setTimeout(() => send(d, so, isFirst, clb, true), 3000)
104
105
  }
105
- console.log(colors.red(`transfer failed for ${Object.keys(d).length} keys ${opt.version}/${ns.language}/${ns.namespace}...`));
106
+ console.log(colors.red(`transfer failed for ${Object.keys(d).length} keys ${opt.version}/${ns.language}/${ns.namespace}...`))
106
107
 
107
- if (err) return clb(err);
108
- if (obj && (obj.errorMessage || obj.message)) return clb(new Error((obj.errorMessage || obj.message)));
108
+ if (err) return clb(err)
109
+ if (obj && (obj.errorMessage || obj.message)) return clb(new Error((obj.errorMessage || obj.message)))
109
110
  }
110
111
  if (res.status >= 300 && res.status !== 412) {
111
112
  if (obj && (obj.errorMessage || obj.message)) {
112
- return clb(new Error((obj.errorMessage || obj.message)));
113
+ return clb(new Error((obj.errorMessage || obj.message)))
113
114
  }
114
- return clb(new Error(res.statusText + ' (' + res.status + ')'));
115
+ return clb(new Error(res.statusText + ' (' + res.status + ')'))
115
116
  }
116
- console.log(colors.green(`transfered ${Object.keys(d).length} keys ${opt.version}/${ns.language}/${ns.namespace}...`));
117
- clb(null);
118
- });
117
+ console.log(colors.green(`transfered ${Object.keys(d).length} keys ${opt.version}/${ns.language}/${ns.namespace}...`))
118
+ clb(null)
119
+ })
119
120
  }
120
121
 
121
122
  if (keysToSend > payloadKeysLimit) {
122
- var tasks = [];
123
- var keysInObj = Object.keys(data);
123
+ const tasks = []
124
+ const keysInObj = Object.keys(data)
124
125
 
125
126
  while (keysInObj.length > payloadKeysLimit) {
126
- (function() {
127
- var pagedData = {};
128
- keysInObj.splice(0, payloadKeysLimit).forEach((k) => pagedData[k] = data[k]);
129
- var hasMoreKeys = keysInObj.length > 0;
130
- tasks.push((c) => send(pagedData, hasMoreKeys, false, c));
131
- })();
127
+ (function () {
128
+ const pagedData = {}
129
+ keysInObj.splice(0, payloadKeysLimit).forEach((k) => { pagedData[k] = data[k] })
130
+ const hasMoreKeys = keysInObj.length > 0
131
+ tasks.push((c) => send(pagedData, hasMoreKeys, false, c))
132
+ })()
132
133
  }
133
134
 
134
- if (keysInObj.length === 0) return cb(null);
135
+ if (keysInObj.length === 0) return cb(null)
135
136
 
136
- var finalPagedData = {};
137
- keysInObj.splice(0, keysInObj.length).forEach((k) => finalPagedData[k] = data[k]);
138
- tasks.push((c) => send(finalPagedData, false, false, c));
137
+ const finalPagedData = {}
138
+ keysInObj.splice(0, keysInObj.length).forEach((k) => { finalPagedData[k] = data[k] })
139
+ tasks.push((c) => send(finalPagedData, false, false, c))
139
140
 
140
- async.series(tasks, cb);
141
- return;
141
+ async.series(tasks, cb)
142
+ return
142
143
  }
143
144
 
144
- send(data, false, true, cb);
145
- };
145
+ send(data, false, true, cb)
146
+ }
146
147
 
147
148
  const upload = (opt, nss, cb) => {
148
149
  if (!opt.referenceLanguage) {
@@ -151,129 +152,129 @@ const upload = (opt, nss, cb) => {
151
152
  require('os').cpus().length,
152
153
  (ns, done) => transfer(opt, ns, done),
153
154
  cb
154
- );
155
- return;
155
+ )
156
+ return
156
157
  }
157
158
 
158
- const nssRefLng = nss.filter((n) => n.language === opt.referenceLanguage);
159
- const nssNonRefLng = nss.filter((n) => n.language !== opt.referenceLanguage);
159
+ const nssRefLng = nss.filter((n) => n.language === opt.referenceLanguage)
160
+ const nssNonRefLng = nss.filter((n) => n.language !== opt.referenceLanguage)
160
161
 
161
162
  async.eachLimit(
162
163
  nssRefLng,
163
164
  require('os').cpus().length,
164
165
  (ns, done) => transfer(opt, ns, done),
165
166
  (err) => {
166
- if (err) return cb(err);
167
+ if (err) return cb(err)
167
168
  async.eachLimit(
168
169
  nssNonRefLng,
169
170
  require('os').cpus().length,
170
171
  (ns, done) => transfer(opt, ns, done),
171
172
  cb
172
- );
173
+ )
173
174
  }
174
- );
175
- };
175
+ )
176
+ }
176
177
 
177
178
  const addLanguage = (opt, l, cb) => {
178
- var url = opt.apiPath + '/language/' + opt.projectId + '/' + l;
179
+ const url = opt.apiPath + '/language/' + opt.projectId + '/' + l
179
180
 
180
181
  request(url, {
181
182
  method: 'post',
182
183
  headers: {
183
- 'Authorization': opt.apiKey
184
+ Authorization: opt.apiKey
184
185
  }
185
186
  }, (err, res, obj) => {
186
187
  if (err || (obj && (obj.errorMessage || obj.message))) {
187
- console.log(colors.red(`failed to add language ${l}...`));
188
+ console.log(colors.red(`failed to add language ${l}...`))
188
189
 
189
- if (err) return cb(err);
190
- if (obj && (obj.errorMessage || obj.message)) return cb(new Error((obj.errorMessage || obj.message)));
190
+ if (err) return cb(err)
191
+ if (obj && (obj.errorMessage || obj.message)) return cb(new Error((obj.errorMessage || obj.message)))
191
192
  }
192
- if (res.status >= 300 && res.status !== 412) return cb(new Error(res.statusText + ' (' + res.status + ')'));
193
- console.log(colors.green(`added language ${l}...`));
194
- cb(null);
195
- });
196
- };
193
+ if (res.status >= 300 && res.status !== 412) return cb(new Error(res.statusText + ' (' + res.status + ')'))
194
+ console.log(colors.green(`added language ${l}...`))
195
+ cb(null)
196
+ })
197
+ }
197
198
 
198
199
  const migrate = (opt, cb) => {
199
200
  if (opt.format !== 'json') {
200
- var err = new Error(`Format ${opt.format} is not accepted!`);
201
- if (!cb) throw err;
202
- if (cb) cb(err);
203
- return;
201
+ const err = new Error(`Format ${opt.format} is not accepted!`)
202
+ if (!cb) throw err
203
+ if (cb) cb(err)
204
+ return
204
205
  }
205
206
 
206
- opt.apiPath = opt.apiPath || 'https://api.locize.app';
207
+ opt.apiPath = opt.apiPath || 'https://api.locize.app'
207
208
 
208
209
  if (opt.language) {
209
- const files = getFiles(opt.path);
210
+ const files = getFiles(opt.path)
210
211
 
211
212
  const namespaces = files.map((file) => {
212
213
  return {
213
214
  language: opt.language,
214
215
  namespace: path.basename(file, '.json'),
215
216
  path: path.join(opt.path, file)
216
- };
217
- });
217
+ }
218
+ })
218
219
 
219
220
  load(namespaces, (err, nss) => {
220
221
  if (err) {
221
- if (!cb) { console.error(colors.red(err.stack)); process.exit(1); }
222
- if (cb) cb(err);
223
- return;
222
+ if (!cb) { console.error(colors.red(err.stack)); process.exit(1) }
223
+ if (cb) cb(err)
224
+ return
224
225
  }
225
226
  upload(opt, nss, (err) => {
226
227
  if (err) {
227
228
  if (!cb) {
228
- console.error(colors.red(err.stack));
229
- process.exit(1);
229
+ console.error(colors.red(err.stack))
230
+ process.exit(1)
230
231
  }
231
- if (cb) cb(err);
232
- return;
232
+ if (cb) cb(err)
233
+ return
233
234
  }
234
- if (!cb) console.log(colors.green('FINISHED'));
235
- if (cb) cb(null);
236
- });
237
- });
238
- return;
235
+ if (!cb) console.log(colors.green('FINISHED'))
236
+ if (cb) cb(null)
237
+ })
238
+ })
239
+ return
239
240
  }
240
241
 
241
242
  if (opt.parseLanguage) {
242
243
  parseLanguage(opt.path, (err, nss) => {
243
244
  if (err) {
244
- if (!cb) console.error(colors.red(err.stack)); process.exit(1);
245
- if (cb) cb(err);
246
- return;
245
+ if (!cb) console.error(colors.red(err.stack)); process.exit(1)
246
+ if (cb) cb(err)
247
+ return
247
248
  }
248
249
 
249
250
  getRemoteLanguages(opt, (err, remoteLanguages) => {
250
251
  if (err) {
251
- if (!cb) { console.error(colors.red(err.stack)); process.exit(1); }
252
- if (cb) cb(err);
253
- return;
252
+ if (!cb) { console.error(colors.red(err.stack)); process.exit(1) }
253
+ if (cb) cb(err)
254
+ return
254
255
  }
255
256
 
256
- const localLanguages = [];
257
+ const localLanguages = []
257
258
  nss.forEach((n) => {
258
- if (localLanguages.indexOf(n.language) < 0) localLanguages.push(n.language);
259
- });
259
+ if (localLanguages.indexOf(n.language) < 0) localLanguages.push(n.language)
260
+ })
260
261
 
261
- const notExistingLanguages = localLanguages.filter((l) => remoteLanguages.indexOf(l) < 0);
262
+ const notExistingLanguages = localLanguages.filter((l) => remoteLanguages.indexOf(l) < 0)
262
263
 
263
264
  if (notExistingLanguages.length === 0) {
264
265
  upload(opt, nss, (err) => {
265
266
  if (err) {
266
267
  if (!cb) {
267
- console.error(colors.red(err.stack));
268
- process.exit(1);
268
+ console.error(colors.red(err.stack))
269
+ process.exit(1)
269
270
  }
270
- if (cb) cb(err);
271
- return;
271
+ if (cb) cb(err)
272
+ return
272
273
  }
273
- if (!cb) console.log(colors.green('FINISHED'));
274
- if (cb) cb(null);
275
- });
276
- return;
274
+ if (!cb) console.log(colors.green('FINISHED'))
275
+ if (cb) cb(null)
276
+ })
277
+ return
277
278
  }
278
279
 
279
280
  async.eachLimit(
@@ -283,34 +284,32 @@ const migrate = (opt, cb) => {
283
284
  (err) => {
284
285
  if (err) {
285
286
  if (!cb) {
286
- console.error(colors.red(err.stack));
287
- process.exit(1);
287
+ console.error(colors.red(err.stack))
288
+ process.exit(1)
288
289
  }
289
- if (cb) cb(err);
290
- return;
290
+ if (cb) cb(err)
291
+ return
291
292
  }
292
293
  setTimeout(() => {
293
294
  // wait a bit to make sure project is up-to-date also in cache
294
295
  upload(opt, nss, (err) => {
295
296
  if (err) {
296
297
  if (!cb) {
297
- console.error(colors.red(err.stack));
298
- process.exit(1);
298
+ console.error(colors.red(err.stack))
299
+ process.exit(1)
299
300
  }
300
- if (cb) cb(err);
301
- return;
301
+ if (cb) cb(err)
302
+ return
302
303
  }
303
- if (!cb) console.log(colors.green('FINISHED'));
304
- if (cb) cb(null);
305
- });
306
- }, 5000);
304
+ if (!cb) console.log(colors.green('FINISHED'))
305
+ if (cb) cb(null)
306
+ })
307
+ }, 5000)
307
308
  }
308
- );
309
- return;
310
- });
311
- });
312
- return;
309
+ )
310
+ })
311
+ })
313
312
  }
314
- };
313
+ }
315
314
 
316
- module.exports = migrate;
315
+ module.exports = migrate