locize-cli 10.3.2 → 11.0.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,148 @@
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
+ let url = `${opt.apiEndpoint}/update/{{projectId}}/{{version}}/{{lng}}/{{ns}}`
60
60
  .replace('{{projectId}}', opt.projectId)
61
61
  .replace('{{ver}}', opt.version)
62
62
  .replace('{{version}}', opt.version)
63
63
  .replace('{{language}}', ns.language)
64
64
  .replace('{{lng}}', ns.language)
65
65
  .replace('{{ns}}', ns.namespace)
66
- .replace('{{namespace}}', ns.namespace);
66
+ .replace('{{namespace}}', ns.namespace)
67
67
 
68
- console.log(colors.yellow(`transfering ${opt.version}/${ns.language}/${ns.namespace}...`));
68
+ console.log(colors.yellow(`transfering ${opt.version}/${ns.language}/${ns.namespace}...`))
69
69
 
70
- if (!opt.replace) url = url.replace('/update/', '/missing/');
70
+ if (!opt.replace) url = url.replace('/update/', '/missing/')
71
71
 
72
- var data = ns.value;
73
- var keysToSend = Object.keys(data).length;
74
- if (keysToSend === 0) return cb(null);
72
+ const data = ns.value
73
+ const keysToSend = Object.keys(data).length
74
+ if (keysToSend === 0) return cb(null)
75
75
 
76
- var payloadKeysLimit = 1000;
76
+ const payloadKeysLimit = 1000
77
77
 
78
- function send(d, so, isFirst, clb, isRetrying) {
79
- const queryParams = new URLSearchParams();
78
+ function send (d, so, isFirst, clb, isRetrying) {
79
+ const queryParams = new URLSearchParams()
80
80
  if (so) {
81
- queryParams.append('omitstatsgeneration', 'true');
81
+ queryParams.append('omitstatsgeneration', 'true')
82
82
  }
83
83
  if (isFirst && opt.replace) {
84
- queryParams.append('replace', 'true');
84
+ queryParams.append('replace', 'true')
85
85
  }
86
86
 
87
- const queryString = queryParams.size > 0 ? '?' + queryParams.toString() : '';
87
+ const queryString = queryParams.size > 0 ? '?' + queryParams.toString() : ''
88
88
 
89
89
  request(url + queryString, {
90
90
  method: 'post',
91
91
  body: d,
92
92
  headers: {
93
- 'Authorization': opt.apiKey
93
+ Authorization: opt.apiKey
94
94
  }
95
95
  }, (err, res, obj) => {
96
96
  if (err || (obj && (obj.errorMessage || obj.message))) {
97
97
  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;
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
101
101
  }
102
102
  if (res.status === 504 && !isRetrying) {
103
- return setTimeout(() => send(d, so, isFirst, clb, true), 3000);
103
+ return setTimeout(() => send(d, so, isFirst, clb, true), 3000)
104
104
  }
105
- console.log(colors.red(`transfer failed for ${Object.keys(d).length} keys ${opt.version}/${ns.language}/${ns.namespace}...`));
105
+ console.log(colors.red(`transfer failed for ${Object.keys(d).length} keys ${opt.version}/${ns.language}/${ns.namespace}...`))
106
106
 
107
- if (err) return clb(err);
108
- if (obj && (obj.errorMessage || obj.message)) return clb(new Error((obj.errorMessage || obj.message)));
107
+ if (err) return clb(err)
108
+ if (obj && (obj.errorMessage || obj.message)) return clb(new Error((obj.errorMessage || obj.message)))
109
109
  }
110
110
  if (res.status >= 300 && res.status !== 412) {
111
111
  if (obj && (obj.errorMessage || obj.message)) {
112
- return clb(new Error((obj.errorMessage || obj.message)));
112
+ return clb(new Error((obj.errorMessage || obj.message)))
113
113
  }
114
- return clb(new Error(res.statusText + ' (' + res.status + ')'));
114
+ return clb(new Error(res.statusText + ' (' + res.status + ')'))
115
115
  }
116
- console.log(colors.green(`transfered ${Object.keys(d).length} keys ${opt.version}/${ns.language}/${ns.namespace}...`));
117
- clb(null);
118
- });
116
+ console.log(colors.green(`transfered ${Object.keys(d).length} keys ${opt.version}/${ns.language}/${ns.namespace}...`))
117
+ clb(null)
118
+ })
119
119
  }
120
120
 
121
121
  if (keysToSend > payloadKeysLimit) {
122
- var tasks = [];
123
- var keysInObj = Object.keys(data);
122
+ const tasks = []
123
+ const keysInObj = Object.keys(data)
124
124
 
125
125
  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
- })();
126
+ (function () {
127
+ const pagedData = {}
128
+ keysInObj.splice(0, payloadKeysLimit).forEach((k) => { pagedData[k] = data[k] })
129
+ const hasMoreKeys = keysInObj.length > 0
130
+ tasks.push((c) => send(pagedData, hasMoreKeys, false, c))
131
+ })()
132
132
  }
133
133
 
134
- if (keysInObj.length === 0) return cb(null);
134
+ if (keysInObj.length === 0) return cb(null)
135
135
 
136
- var finalPagedData = {};
137
- keysInObj.splice(0, keysInObj.length).forEach((k) => finalPagedData[k] = data[k]);
138
- tasks.push((c) => send(finalPagedData, false, false, c));
136
+ const finalPagedData = {}
137
+ keysInObj.splice(0, keysInObj.length).forEach((k) => { finalPagedData[k] = data[k] })
138
+ tasks.push((c) => send(finalPagedData, false, false, c))
139
139
 
140
- async.series(tasks, cb);
141
- return;
140
+ async.series(tasks, cb)
141
+ return
142
142
  }
143
143
 
144
- send(data, false, true, cb);
145
- };
144
+ send(data, false, true, cb)
145
+ }
146
146
 
147
147
  const upload = (opt, nss, cb) => {
148
148
  if (!opt.referenceLanguage) {
@@ -151,129 +151,129 @@ const upload = (opt, nss, cb) => {
151
151
  require('os').cpus().length,
152
152
  (ns, done) => transfer(opt, ns, done),
153
153
  cb
154
- );
155
- return;
154
+ )
155
+ return
156
156
  }
157
157
 
158
- const nssRefLng = nss.filter((n) => n.language === opt.referenceLanguage);
159
- const nssNonRefLng = nss.filter((n) => n.language !== opt.referenceLanguage);
158
+ const nssRefLng = nss.filter((n) => n.language === opt.referenceLanguage)
159
+ const nssNonRefLng = nss.filter((n) => n.language !== opt.referenceLanguage)
160
160
 
161
161
  async.eachLimit(
162
162
  nssRefLng,
163
163
  require('os').cpus().length,
164
164
  (ns, done) => transfer(opt, ns, done),
165
165
  (err) => {
166
- if (err) return cb(err);
166
+ if (err) return cb(err)
167
167
  async.eachLimit(
168
168
  nssNonRefLng,
169
169
  require('os').cpus().length,
170
170
  (ns, done) => transfer(opt, ns, done),
171
171
  cb
172
- );
172
+ )
173
173
  }
174
- );
175
- };
174
+ )
175
+ }
176
176
 
177
177
  const addLanguage = (opt, l, cb) => {
178
- var url = opt.apiPath + '/language/' + opt.projectId + '/' + l;
178
+ const url = opt.apiEndpoint + '/language/' + opt.projectId + '/' + l
179
179
 
180
180
  request(url, {
181
181
  method: 'post',
182
182
  headers: {
183
- 'Authorization': opt.apiKey
183
+ Authorization: opt.apiKey
184
184
  }
185
185
  }, (err, res, obj) => {
186
186
  if (err || (obj && (obj.errorMessage || obj.message))) {
187
- console.log(colors.red(`failed to add language ${l}...`));
187
+ console.log(colors.red(`failed to add language ${l}...`))
188
188
 
189
- if (err) return cb(err);
190
- if (obj && (obj.errorMessage || obj.message)) return cb(new Error((obj.errorMessage || obj.message)));
189
+ if (err) return cb(err)
190
+ if (obj && (obj.errorMessage || obj.message)) return cb(new Error((obj.errorMessage || obj.message)))
191
191
  }
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
- };
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
+ }
197
197
 
198
198
  const migrate = (opt, cb) => {
199
199
  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;
200
+ const err = new Error(`Format ${opt.format} is not accepted!`)
201
+ if (!cb) throw err
202
+ if (cb) cb(err)
203
+ return
204
204
  }
205
205
 
206
- opt.apiPath = opt.apiPath || 'https://api.locize.app';
206
+ opt.apiEndpoint = opt.apiEndpoint || 'https://api.locize.app'
207
207
 
208
208
  if (opt.language) {
209
- const files = getFiles(opt.path);
209
+ const files = getFiles(opt.path)
210
210
 
211
211
  const namespaces = files.map((file) => {
212
212
  return {
213
213
  language: opt.language,
214
214
  namespace: path.basename(file, '.json'),
215
215
  path: path.join(opt.path, file)
216
- };
217
- });
216
+ }
217
+ })
218
218
 
219
219
  load(namespaces, (err, nss) => {
220
220
  if (err) {
221
- if (!cb) { console.error(colors.red(err.stack)); process.exit(1); }
222
- if (cb) cb(err);
223
- return;
221
+ if (!cb) { console.error(colors.red(err.stack)); process.exit(1) }
222
+ if (cb) cb(err)
223
+ return
224
224
  }
225
225
  upload(opt, nss, (err) => {
226
226
  if (err) {
227
227
  if (!cb) {
228
- console.error(colors.red(err.stack));
229
- process.exit(1);
228
+ console.error(colors.red(err.stack))
229
+ process.exit(1)
230
230
  }
231
- if (cb) cb(err);
232
- return;
231
+ if (cb) cb(err)
232
+ return
233
233
  }
234
- if (!cb) console.log(colors.green('FINISHED'));
235
- if (cb) cb(null);
236
- });
237
- });
238
- return;
234
+ if (!cb) console.log(colors.green('FINISHED'))
235
+ if (cb) cb(null)
236
+ })
237
+ })
238
+ return
239
239
  }
240
240
 
241
241
  if (opt.parseLanguage) {
242
242
  parseLanguage(opt.path, (err, nss) => {
243
243
  if (err) {
244
- if (!cb) console.error(colors.red(err.stack)); process.exit(1);
245
- if (cb) cb(err);
246
- return;
244
+ if (!cb) console.error(colors.red(err.stack)); process.exit(1)
245
+ if (cb) cb(err)
246
+ return
247
247
  }
248
248
 
249
249
  getRemoteLanguages(opt, (err, remoteLanguages) => {
250
250
  if (err) {
251
- if (!cb) { console.error(colors.red(err.stack)); process.exit(1); }
252
- if (cb) cb(err);
253
- return;
251
+ if (!cb) { console.error(colors.red(err.stack)); process.exit(1) }
252
+ if (cb) cb(err)
253
+ return
254
254
  }
255
255
 
256
- const localLanguages = [];
256
+ const localLanguages = []
257
257
  nss.forEach((n) => {
258
- if (localLanguages.indexOf(n.language) < 0) localLanguages.push(n.language);
259
- });
258
+ if (localLanguages.indexOf(n.language) < 0) localLanguages.push(n.language)
259
+ })
260
260
 
261
- const notExistingLanguages = localLanguages.filter((l) => remoteLanguages.indexOf(l) < 0);
261
+ const notExistingLanguages = localLanguages.filter((l) => remoteLanguages.indexOf(l) < 0)
262
262
 
263
263
  if (notExistingLanguages.length === 0) {
264
264
  upload(opt, nss, (err) => {
265
265
  if (err) {
266
266
  if (!cb) {
267
- console.error(colors.red(err.stack));
268
- process.exit(1);
267
+ console.error(colors.red(err.stack))
268
+ process.exit(1)
269
269
  }
270
- if (cb) cb(err);
271
- return;
270
+ if (cb) cb(err)
271
+ return
272
272
  }
273
- if (!cb) console.log(colors.green('FINISHED'));
274
- if (cb) cb(null);
275
- });
276
- return;
273
+ if (!cb) console.log(colors.green('FINISHED'))
274
+ if (cb) cb(null)
275
+ })
276
+ return
277
277
  }
278
278
 
279
279
  async.eachLimit(
@@ -283,34 +283,32 @@ const migrate = (opt, cb) => {
283
283
  (err) => {
284
284
  if (err) {
285
285
  if (!cb) {
286
- console.error(colors.red(err.stack));
287
- process.exit(1);
286
+ console.error(colors.red(err.stack))
287
+ process.exit(1)
288
288
  }
289
- if (cb) cb(err);
290
- return;
289
+ if (cb) cb(err)
290
+ return
291
291
  }
292
292
  setTimeout(() => {
293
293
  // wait a bit to make sure project is up-to-date also in cache
294
294
  upload(opt, nss, (err) => {
295
295
  if (err) {
296
296
  if (!cb) {
297
- console.error(colors.red(err.stack));
298
- process.exit(1);
297
+ console.error(colors.red(err.stack))
298
+ process.exit(1)
299
299
  }
300
- if (cb) cb(err);
301
- return;
300
+ if (cb) cb(err)
301
+ return
302
302
  }
303
- if (!cb) console.log(colors.green('FINISHED'));
304
- if (cb) cb(null);
305
- });
306
- }, 5000);
303
+ if (!cb) console.log(colors.green('FINISHED'))
304
+ if (cb) cb(null)
305
+ })
306
+ }, 5000)
307
307
  }
308
- );
309
- return;
310
- });
311
- });
312
- return;
308
+ )
309
+ })
310
+ })
313
311
  }
314
- };
312
+ }
315
313
 
316
- module.exports = migrate;
314
+ module.exports = migrate