@lingui/cli 3.15.0 → 3.16.1

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.
Files changed (46) hide show
  1. package/CHANGELOG.md +495 -0
  2. package/build/LICENSE +21 -0
  3. package/build/api/catalog.js +582 -0
  4. package/build/api/compile.js +89 -0
  5. package/{api → build/api}/detect.js +23 -9
  6. package/build/api/extract.js +78 -0
  7. package/build/api/extractors/babel.js +51 -0
  8. package/build/api/extractors/index.js +51 -0
  9. package/build/api/extractors/typescript.js +71 -0
  10. package/build/api/formats/csv.js +65 -0
  11. package/{api → build/api}/formats/index.js +8 -5
  12. package/build/api/formats/lingui.js +67 -0
  13. package/build/api/formats/minimal.js +63 -0
  14. package/build/api/formats/po-gettext.js +296 -0
  15. package/build/api/formats/po.js +122 -0
  16. package/{api → build/api}/help.js +6 -18
  17. package/{api → build/api}/index.js +7 -7
  18. package/build/api/locales.js +45 -0
  19. package/{api → build/api}/pseudoLocalize.js +13 -13
  20. package/build/api/stats.js +46 -0
  21. package/{api → build/api}/utils.js +21 -40
  22. package/build/lingui-add-locale.js +11 -0
  23. package/build/lingui-compile.js +192 -0
  24. package/build/lingui-extract-template.js +64 -0
  25. package/build/lingui-extract.js +181 -0
  26. package/{lingui.js → build/lingui.js} +2 -2
  27. package/{services → build/services}/translationIO.js +78 -94
  28. package/build/tests.js +78 -0
  29. package/package.json +24 -12
  30. package/api/catalog.js +0 -778
  31. package/api/compile.js +0 -172
  32. package/api/extract.js +0 -192
  33. package/api/extractors/babel.js +0 -61
  34. package/api/extractors/index.js +0 -130
  35. package/api/extractors/typescript.js +0 -77
  36. package/api/formats/csv.js +0 -71
  37. package/api/formats/lingui.js +0 -64
  38. package/api/formats/minimal.js +0 -63
  39. package/api/formats/po-gettext.js +0 -331
  40. package/api/formats/po.js +0 -130
  41. package/api/locales.js +0 -43
  42. package/api/stats.js +0 -51
  43. package/lingui-add-locale.js +0 -11
  44. package/lingui-compile.js +0 -198
  45. package/lingui-extract-template.js +0 -123
  46. package/lingui-extract.js +0 -286
package/api/catalog.js DELETED
@@ -1,778 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
-
5
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
-
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- exports.getCatalogs = getCatalogs;
11
- exports.getCatalogForFile = getCatalogForFile;
12
- exports.getCatalogForMerge = getCatalogForMerge;
13
- exports.normalizeRelativePath = normalizeRelativePath;
14
- exports.order = order;
15
- exports.orderByMessageId = orderByMessageId;
16
- exports.orderByOrigin = orderByOrigin;
17
- exports.cleanObsolete = exports.Catalog = void 0;
18
-
19
- var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
20
-
21
- var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
22
-
23
- var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
24
-
25
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
26
-
27
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
28
-
29
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
30
-
31
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
32
-
33
- var _os = _interopRequireDefault(require("os"));
34
-
35
- var _fsExtra = _interopRequireDefault(require("fs-extra"));
36
-
37
- var _path = _interopRequireDefault(require("path"));
38
-
39
- var R = _interopRequireWildcard(require("ramda"));
40
-
41
- var _chalk = _interopRequireDefault(require("chalk"));
42
-
43
- var _glob = _interopRequireDefault(require("glob"));
44
-
45
- var _micromatch = _interopRequireDefault(require("micromatch"));
46
-
47
- var _normalizePath = _interopRequireDefault(require("normalize-path"));
48
-
49
- var _formats = _interopRequireDefault(require("./formats"));
50
-
51
- var _extractors = _interopRequireDefault(require("./extractors"));
52
-
53
- var _utils = require("./utils");
54
-
55
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
56
-
57
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
58
-
59
- function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
60
-
61
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
62
-
63
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
64
-
65
- var NAME = "{name}";
66
- var LOCALE = "{locale}";
67
- var LOCALE_SUFFIX_RE = /\{locale\}.*$/;
68
- var PATHSEP = "/"; // force posix everywhere
69
-
70
- var Catalog = /*#__PURE__*/function () {
71
- function Catalog(_ref, config) {
72
- var name = _ref.name,
73
- path = _ref.path,
74
- include = _ref.include,
75
- _ref$exclude = _ref.exclude,
76
- exclude = _ref$exclude === void 0 ? [] : _ref$exclude;
77
- (0, _classCallCheck2.default)(this, Catalog);
78
- (0, _defineProperty2.default)(this, "name", void 0);
79
- (0, _defineProperty2.default)(this, "path", void 0);
80
- (0, _defineProperty2.default)(this, "include", void 0);
81
- (0, _defineProperty2.default)(this, "exclude", void 0);
82
- (0, _defineProperty2.default)(this, "config", void 0);
83
- (0, _defineProperty2.default)(this, "format", void 0);
84
- this.name = name;
85
- this.path = normalizeRelativePath(path);
86
- this.include = include.map(normalizeRelativePath);
87
- this.exclude = [this.localeDir].concat((0, _toConsumableArray2.default)(exclude.map(normalizeRelativePath)));
88
- this.config = config;
89
- this.format = (0, _formats.default)(config.format);
90
- }
91
-
92
- (0, _createClass2.default)(Catalog, [{
93
- key: "make",
94
- value: function () {
95
- var _make = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(options) {
96
- var nextCatalog, prevCatalogs, catalogs, cleanAndSort, sortedCatalogs;
97
- return _regenerator.default.wrap(function _callee$(_context) {
98
- while (1) {
99
- switch (_context.prev = _context.next) {
100
- case 0:
101
- _context.next = 2;
102
- return this.collect(options);
103
-
104
- case 2:
105
- nextCatalog = _context.sent;
106
- prevCatalogs = this.readAll();
107
- catalogs = this.merge(prevCatalogs, nextCatalog, {
108
- overwrite: options.overwrite,
109
- files: options.files
110
- }); // Map over all locales and post-process each catalog
111
-
112
- cleanAndSort = R.map(R.pipe( // Clean obsolete messages
113
- options.clean ? cleanObsolete : R.identity, // Sort messages
114
- order(options.orderBy)));
115
- sortedCatalogs = cleanAndSort(catalogs);
116
-
117
- if (options.locale) {
118
- this.write(options.locale, sortedCatalogs[options.locale]);
119
- } else {
120
- this.writeAll(sortedCatalogs);
121
- }
122
-
123
- case 8:
124
- case "end":
125
- return _context.stop();
126
- }
127
- }
128
- }, _callee, this);
129
- }));
130
-
131
- function make(_x) {
132
- return _make.apply(this, arguments);
133
- }
134
-
135
- return make;
136
- }()
137
- }, {
138
- key: "makeTemplate",
139
- value: function () {
140
- var _makeTemplate = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(options) {
141
- var catalog, sort;
142
- return _regenerator.default.wrap(function _callee2$(_context2) {
143
- while (1) {
144
- switch (_context2.prev = _context2.next) {
145
- case 0:
146
- _context2.next = 2;
147
- return this.collect(options);
148
-
149
- case 2:
150
- catalog = _context2.sent;
151
- sort = order(options.orderBy);
152
- this.writeTemplate(sort(catalog));
153
-
154
- case 5:
155
- case "end":
156
- return _context2.stop();
157
- }
158
- }
159
- }, _callee2, this);
160
- }));
161
-
162
- function makeTemplate(_x2) {
163
- return _makeTemplate.apply(this, arguments);
164
- }
165
-
166
- return makeTemplate;
167
- }()
168
- /**
169
- * Collect messages from source paths. Return a raw message catalog as JSON.
170
- */
171
-
172
- }, {
173
- key: "collect",
174
- value: function () {
175
- var _collect = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(options) {
176
- var tmpDir, paths, regex, _iterator, _step, filename;
177
-
178
- return _regenerator.default.wrap(function _callee3$(_context3) {
179
- while (1) {
180
- switch (_context3.prev = _context3.next) {
181
- case 0:
182
- tmpDir = _path.default.join(_os.default.tmpdir(), "lingui-".concat(process.pid));
183
-
184
- if (_fsExtra.default.existsSync(tmpDir)) {
185
- (0, _utils.removeDirectory)(tmpDir, true);
186
- } else {
187
- _fsExtra.default.mkdirSync(tmpDir);
188
- }
189
-
190
- _context3.prev = 2;
191
- paths = this.sourcePaths;
192
-
193
- if (options.files) {
194
- options.files = options.files.map(function (p) {
195
- return (0, _normalizePath.default)(p, false);
196
- });
197
- regex = new RegExp(options.files.join("|"), "i");
198
- paths = paths.filter(function (path) {
199
- return regex.test(path);
200
- });
201
- }
202
-
203
- _iterator = _createForOfIteratorHelper(paths);
204
- _context3.prev = 6;
205
-
206
- _iterator.s();
207
-
208
- case 8:
209
- if ((_step = _iterator.n()).done) {
210
- _context3.next = 14;
211
- break;
212
- }
213
-
214
- filename = _step.value;
215
- _context3.next = 12;
216
- return (0, _extractors.default)(filename, tmpDir, {
217
- verbose: options.verbose,
218
- configPath: options.configPath,
219
- babelOptions: this.config.extractBabelOptions,
220
- extractors: options.extractors,
221
- projectType: options.projectType
222
- });
223
-
224
- case 12:
225
- _context3.next = 8;
226
- break;
227
-
228
- case 14:
229
- _context3.next = 19;
230
- break;
231
-
232
- case 16:
233
- _context3.prev = 16;
234
- _context3.t0 = _context3["catch"](6);
235
-
236
- _iterator.e(_context3.t0);
237
-
238
- case 19:
239
- _context3.prev = 19;
240
-
241
- _iterator.f();
242
-
243
- return _context3.finish(19);
244
-
245
- case 22:
246
- return _context3.abrupt("return", function traverse(directory) {
247
- return _fsExtra.default.readdirSync(directory).map(function (filename) {
248
- var filepath = _path.default.join(directory, filename);
249
-
250
- if (_fsExtra.default.lstatSync(filepath).isDirectory()) {
251
- return traverse(filepath);
252
- }
253
-
254
- if (!filename.endsWith(".json")) return;
255
-
256
- try {
257
- return JSON.parse(_fsExtra.default.readFileSync(filepath).toString());
258
- } catch (e) {}
259
- }).filter(Boolean).reduce(function (catalog, messages) {
260
- return R.mergeWithKey(mergeOriginsAndExtractedComments, catalog, messages);
261
- }, {});
262
- }(tmpDir));
263
-
264
- case 25:
265
- _context3.prev = 25;
266
- _context3.t1 = _context3["catch"](2);
267
- throw _context3.t1;
268
-
269
- case 28:
270
- _context3.prev = 28;
271
- (0, _utils.removeDirectory)(tmpDir);
272
- return _context3.finish(28);
273
-
274
- case 31:
275
- case "end":
276
- return _context3.stop();
277
- }
278
- }
279
- }, _callee3, this, [[2, 25, 28, 31], [6, 16, 19, 22]]);
280
- }));
281
-
282
- function collect(_x3) {
283
- return _collect.apply(this, arguments);
284
- }
285
-
286
- return collect;
287
- }()
288
- }, {
289
- key: "merge",
290
- value: function merge(prevCatalogs, nextCatalog, options) {
291
- var _this = this;
292
-
293
- var nextKeys = R.keys(nextCatalog).map(String);
294
- return R.mapObjIndexed(function (prevCatalog, locale) {
295
- var prevKeys = R.keys(prevCatalog).map(String);
296
- var newKeys = R.difference(nextKeys, prevKeys);
297
- var mergeKeys = R.intersection(nextKeys, prevKeys);
298
- var obsoleteKeys = R.difference(prevKeys, nextKeys); // Initialize new catalog with new keys
299
-
300
- var newMessages = R.mapObjIndexed(function (message, key) {
301
- return _objectSpread({
302
- translation: _this.config.sourceLocale === locale ? message.message || key : ""
303
- }, message);
304
- }, R.pick(newKeys, nextCatalog)); // Merge translations from previous catalog
305
-
306
- var mergedMessages = mergeKeys.map(function (key) {
307
- var updateFromDefaults = _this.config.sourceLocale === locale && (prevCatalog[key].translation === prevCatalog[key].message || options.overwrite);
308
- var translation = updateFromDefaults ? nextCatalog[key].message || key : prevCatalog[key].translation;
309
- return (0, _defineProperty2.default)({}, key, _objectSpread({
310
- translation: translation
311
- }, R.omit(["obsolete, translation"], nextCatalog[key])));
312
- }); // Mark all remaining translations as obsolete
313
- // Only if *options.files* is not provided
314
-
315
- var obsoleteMessages = obsoleteKeys.map(function (key) {
316
- return (0, _defineProperty2.default)({}, key, _objectSpread(_objectSpread({}, prevCatalog[key]), {}, {
317
- obsolete: options.files ? false : true
318
- }));
319
- });
320
- return R.mergeAll([newMessages].concat((0, _toConsumableArray2.default)(mergedMessages), (0, _toConsumableArray2.default)(obsoleteMessages)));
321
- }, prevCatalogs);
322
- }
323
- }, {
324
- key: "getTranslations",
325
- value: function getTranslations(locale, options) {
326
- var _this2 = this;
327
-
328
- var catalogs = this.readAll();
329
- var template = this.readTemplate() || {};
330
- return R.mapObjIndexed(function (_value, key) {
331
- return _this2.getTranslation(catalogs, locale, key, options);
332
- }, _objectSpread(_objectSpread({}, template), catalogs[locale]));
333
- }
334
- }, {
335
- key: "getTranslation",
336
- value: function getTranslation(catalogs, locale, key, _ref4) {
337
- var _this3 = this,
338
- _catalog$key;
339
-
340
- var fallbackLocales = _ref4.fallbackLocales,
341
- sourceLocale = _ref4.sourceLocale;
342
- var catalog = catalogs[locale] || {};
343
-
344
- if (!catalog.hasOwnProperty(key)) {
345
- console.error("Message with key ".concat(key, " is missing in locale ").concat(locale));
346
- }
347
-
348
- var getTranslation = function getTranslation(_locale) {
349
- var configLocales = _this3.config.locales.join('", "');
350
-
351
- var localeCatalog = catalogs[_locale] || {};
352
-
353
- if (!localeCatalog) {
354
- console.warn("\n Catalog \"".concat(_locale, "\" isn't present in locales config parameter\n Add \"").concat(_locale, "\" to your lingui.config.js:\n {\n locales: [\"").concat(configLocales, "\", \"").concat(_locale, "\"]\n }\n "));
355
- return null;
356
- }
357
-
358
- if (!localeCatalog.hasOwnProperty(key)) {
359
- console.error("Message with key ".concat(key, " is missing in locale ").concat(_locale));
360
- return null;
361
- }
362
-
363
- if (catalogs[_locale]) {
364
- return catalogs[_locale][key].translation;
365
- }
366
-
367
- return null;
368
- };
369
-
370
- var getMultipleFallbacks = function getMultipleFallbacks(_locale) {
371
- var fL = fallbackLocales && fallbackLocales[_locale]; // some probably the fallback will be undefined, so just search by locale
372
-
373
- if (!fL) return null;
374
-
375
- if (Array.isArray(fL)) {
376
- var _iterator2 = _createForOfIteratorHelper(fL),
377
- _step2;
378
-
379
- try {
380
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
381
- var fallbackLocale = _step2.value;
382
-
383
- if (catalogs[fallbackLocale]) {
384
- return getTranslation(fallbackLocale);
385
- }
386
- }
387
- } catch (err) {
388
- _iterator2.e(err);
389
- } finally {
390
- _iterator2.f();
391
- }
392
- } else {
393
- return getTranslation(fL);
394
- }
395
- };
396
-
397
- return (// Get translation in target locale
398
- getTranslation(locale) || // We search in fallbackLocales as dependent of each locale
399
- getMultipleFallbacks(locale) || // Get translation in fallbackLocales.default (if any)
400
- (fallbackLocales === null || fallbackLocales === void 0 ? void 0 : fallbackLocales.default) && getTranslation(fallbackLocales.default) || ( // Get message default
401
- (_catalog$key = catalog[key]) === null || _catalog$key === void 0 ? void 0 : _catalog$key.defaults) || // If sourceLocale is either target locale of fallback one, use key
402
- sourceLocale && sourceLocale === locale && key || sourceLocale && (fallbackLocales === null || fallbackLocales === void 0 ? void 0 : fallbackLocales.default) && sourceLocale === fallbackLocales.default && key || // Otherwise no translation is available
403
- undefined
404
- );
405
- }
406
- }, {
407
- key: "write",
408
- value: function write(locale, messages) {
409
- var filename = this.path.replace(LOCALE, locale) + this.format.catalogExtension;
410
- var created = !_fsExtra.default.existsSync(filename);
411
-
412
- var basedir = _path.default.dirname(filename);
413
-
414
- if (!_fsExtra.default.existsSync(basedir)) {
415
- _fsExtra.default.mkdirpSync(basedir);
416
- }
417
-
418
- var options = _objectSpread(_objectSpread({}, this.config.formatOptions), {}, {
419
- locale: locale
420
- });
421
-
422
- this.format.write(filename, messages, options);
423
- return [created, filename];
424
- }
425
- }, {
426
- key: "writeAll",
427
- value: function writeAll(catalogs) {
428
- var _this4 = this;
429
-
430
- this.locales.forEach(function (locale) {
431
- return _this4.write(locale, catalogs[locale]);
432
- });
433
- }
434
- }, {
435
- key: "writeTemplate",
436
- value: function writeTemplate(messages) {
437
- var filename = this.templateFile;
438
-
439
- var basedir = _path.default.dirname(filename);
440
-
441
- if (!_fsExtra.default.existsSync(basedir)) {
442
- _fsExtra.default.mkdirpSync(basedir);
443
- }
444
-
445
- var options = _objectSpread(_objectSpread({}, this.config.formatOptions), {}, {
446
- locale: undefined
447
- });
448
-
449
- this.format.write(filename, messages, options);
450
- }
451
- }, {
452
- key: "writeCompiled",
453
- value: function writeCompiled(locale, compiledCatalog, namespace) {
454
- var ext;
455
-
456
- if (namespace === "es") {
457
- ext = "mjs";
458
- } else if (namespace === "ts") {
459
- ext = "ts";
460
- } else {
461
- ext = "js";
462
- }
463
-
464
- var filename = "".concat(this.path.replace(LOCALE, locale), ".").concat(ext);
465
-
466
- var basedir = _path.default.dirname(filename);
467
-
468
- if (!_fsExtra.default.existsSync(basedir)) {
469
- _fsExtra.default.mkdirpSync(basedir);
470
- }
471
-
472
- _fsExtra.default.writeFileSync(filename, compiledCatalog);
473
-
474
- return filename;
475
- }
476
- }, {
477
- key: "read",
478
- value: function read(locale) {
479
- var filename = this.path.replace(LOCALE, locale) + this.format.catalogExtension;
480
- if (!_fsExtra.default.existsSync(filename)) return null;
481
- return this.format.read(filename);
482
- }
483
- }, {
484
- key: "readAll",
485
- value: function readAll() {
486
- var _this5 = this;
487
-
488
- return R.mergeAll(this.locales.map(function (locale) {
489
- return (0, _defineProperty2.default)({}, locale, _this5.read(locale));
490
- }));
491
- }
492
- }, {
493
- key: "readTemplate",
494
- value: function readTemplate() {
495
- var filename = this.templateFile;
496
- if (!_fsExtra.default.existsSync(filename)) return null;
497
- return this.format.read(filename);
498
- }
499
- }, {
500
- key: "sourcePaths",
501
- get: function get() {
502
- var includeGlobs = this.include.map(function (includePath) {
503
- var isDir = _fsExtra.default.existsSync(includePath) && _fsExtra.default.lstatSync(includePath).isDirectory();
504
- /**
505
- * glob library results from absolute patterns such as /foo/* are mounted onto the root setting using path.join.
506
- * On windows, this will by default result in /foo/* matching C:\foo\bar.txt.
507
- */
508
-
509
-
510
- return isDir ? (0, _normalizePath.default)(_path.default.resolve(process.cwd(), includePath === "/" ? "" : includePath, "**/*.*")) : includePath;
511
- });
512
- var patterns = includeGlobs.length > 1 ? "{".concat(includeGlobs.join(","), "}") : includeGlobs[0];
513
- return _glob.default.sync(patterns, {
514
- ignore: this.exclude,
515
- mark: true
516
- });
517
- }
518
- }, {
519
- key: "templateFile",
520
- get: function get() {
521
- return this.path.replace(LOCALE_SUFFIX_RE, "messages.pot");
522
- }
523
- }, {
524
- key: "localeDir",
525
- get: function get() {
526
- var localePatternIndex = this.path.indexOf(LOCALE);
527
-
528
- if (localePatternIndex === -1) {
529
- throw Error("Invalid catalog path: ".concat(LOCALE, " variable is missing"));
530
- }
531
-
532
- return this.path.substr(0, localePatternIndex);
533
- }
534
- }, {
535
- key: "locales",
536
- get: function get() {
537
- return this.config.locales;
538
- }
539
- }]);
540
- return Catalog;
541
- }();
542
- /**
543
- * Parse `config.catalogs` and return a list of configured Catalog instances.
544
- */
545
-
546
-
547
- exports.Catalog = Catalog;
548
-
549
- function getCatalogs(config) {
550
- var catalogsConfig = config.catalogs;
551
- var catalogs = [];
552
- catalogsConfig.forEach(function (catalog) {
553
- // Validate that `catalogPath` doesn't end with trailing slash
554
- if (catalog.path.endsWith(PATHSEP)) {
555
- var extension = (0, _formats.default)(config.format).catalogExtension;
556
- var correctPath = catalog.path.slice(0, -1);
557
- var examplePath = correctPath.replace(LOCALE, // Show example using one of configured locales (if any)
558
- (config.locales || [])[0] || "en") + extension;
559
- throw new Error( // prettier-ignore
560
- "Remove trailing slash from \"".concat(catalog.path, "\". Catalog path isn't a directory,") + " but translation file without extension. For example, catalog path \"".concat(correctPath, "\"") + " results in translation file \"".concat(examplePath, "\"."));
561
- }
562
-
563
- var include = ensureArray(catalog.include).map(normalizeRelativePath);
564
- var exclude = ensureArray(catalog.exclude).map(normalizeRelativePath); // catalog.path without {name} pattern -> always refers to a single catalog
565
-
566
- if (!catalog.path.includes(NAME)) {
567
- // Validate that sourcePaths doesn't use {name} pattern either
568
- var invalidSource = include.find(function (path) {
569
- return path.includes(NAME);
570
- });
571
-
572
- if (invalidSource !== undefined) {
573
- throw new Error("Catalog with path \"".concat(catalog.path, "\" doesn't have a {name} pattern") + " in it, but one of source directories uses it: \"".concat(invalidSource, "\".") + " Either add {name} pattern to \"".concat(catalog.path, "\" or remove it") + " from all source directories.");
574
- } // catalog name is the last directory of catalog.path.
575
- // If the last part is {locale}, then catalog doesn't have an explicit name
576
-
577
-
578
- var name = function () {
579
- var _name = catalog.path.split(PATHSEP).slice(-1)[0];
580
- return _name !== LOCALE ? _name : null;
581
- }();
582
-
583
- catalogs.push(new Catalog({
584
- name: name,
585
- path: normalizeRelativePath(catalog.path),
586
- include: include,
587
- exclude: exclude
588
- }, config));
589
- return;
590
- }
591
-
592
- var patterns = include.map(function (path) {
593
- return path.replace(NAME, "*");
594
- });
595
-
596
- var candidates = _glob.default.sync(patterns.length > 1 ? "{".concat(patterns.join(","), "}") : patterns[0], {
597
- ignore: exclude,
598
- mark: true
599
- });
600
-
601
- candidates.forEach(function (catalogDir) {
602
- var name = _path.default.basename(catalogDir);
603
-
604
- catalogs.push(new Catalog({
605
- name: name,
606
- path: normalizeRelativePath(catalog.path.replace(NAME, name)),
607
- include: include.map(function (path) {
608
- return path.replace(NAME, name);
609
- }),
610
- exclude: exclude.map(function (path) {
611
- return path.replace(NAME, name);
612
- })
613
- }, config));
614
- });
615
- });
616
- return catalogs;
617
- }
618
-
619
- function getCatalogForFile(file, catalogs) {
620
- var _iterator3 = _createForOfIteratorHelper(catalogs),
621
- _step3;
622
-
623
- try {
624
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
625
- var _catalog = _step3.value;
626
- var catalogFile = "".concat(_catalog.path).concat(_catalog.format.catalogExtension);
627
- var catalogGlob = catalogFile.replace(LOCALE, "*");
628
-
629
- var match = _micromatch.default.capture(normalizeRelativePath(_path.default.relative(_catalog.config.rootDir, catalogGlob)), normalizeRelativePath(file));
630
-
631
- if (match) {
632
- return {
633
- locale: match[0],
634
- catalog: _catalog
635
- };
636
- }
637
- }
638
- } catch (err) {
639
- _iterator3.e(err);
640
- } finally {
641
- _iterator3.f();
642
- }
643
-
644
- return null;
645
- }
646
- /**
647
- * Create catalog for merged messages.
648
- */
649
-
650
-
651
- function getCatalogForMerge(config) {
652
- var catalogConfig = config;
653
-
654
- if (catalogConfig.catalogsMergePath.endsWith(PATHSEP)) {
655
- var extension = (0, _formats.default)(config.format).catalogExtension;
656
- var correctPath = catalogConfig.catalogsMergePath.slice(0, -1);
657
- var examplePath = correctPath.replace(LOCALE, // Show example using one of configured locales (if any)
658
- (config.locales || [])[0] || "en") + extension;
659
- throw new Error( // prettier-ignore
660
- "Remove trailing slash from \"".concat(catalogConfig.catalogsMergePath, "\". Catalog path isn't a directory,") + " but translation file without extension. For example, catalog path \"".concat(correctPath, "\"") + " results in translation file \"".concat(examplePath, "\"."));
661
- } // catalog name is the last directory of catalogPath.
662
- // If the last part is {locale}, then catalog doesn't have an explicit name
663
-
664
-
665
- var name = function () {
666
- var _name = _path.default.basename(normalizeRelativePath(catalogConfig.catalogsMergePath));
667
-
668
- return _name !== LOCALE ? _name : null;
669
- }();
670
-
671
- var catalog = new Catalog({
672
- name: name,
673
- path: normalizeRelativePath(catalogConfig.catalogsMergePath),
674
- include: [],
675
- exclude: []
676
- }, config);
677
- return catalog;
678
- }
679
- /**
680
- * Merge origins and extractedComments for messages found in different places. All other attributes
681
- * should be the same (raise an error if defaults are different).
682
- */
683
-
684
-
685
- function mergeOriginsAndExtractedComments(msgId, prev, next) {
686
- if (prev.defaults !== next.defaults) {
687
- throw new Error("Encountered different defaults for message ".concat(_chalk.default.yellow(msgId)) + "\n".concat(_chalk.default.yellow((0, _utils.prettyOrigin)(prev.origin)), " ").concat(prev.defaults) + "\n".concat(_chalk.default.yellow((0, _utils.prettyOrigin)(next.origin)), " ").concat(next.defaults));
688
- }
689
-
690
- return _objectSpread(_objectSpread({}, next), {}, {
691
- extractedComments: R.concat(prev.extractedComments, next.extractedComments),
692
- origin: R.concat(prev.origin, next.origin)
693
- });
694
- }
695
- /**
696
- * Ensure that value is always array. If not, turn it into an array of one element.
697
- */
698
-
699
-
700
- var ensureArray = function ensureArray(value) {
701
- if (value == null) return [];
702
- return Array.isArray(value) ? value : [value];
703
- };
704
- /**
705
- * Remove ./ at the beginning: ./relative => relative
706
- * relative => relative
707
- * Preserve directories: ./relative/ => relative/
708
- * Preserve absolute paths: /absolute/path => /absolute/path
709
- */
710
-
711
-
712
- function normalizeRelativePath(sourcePath) {
713
- if (_path.default.isAbsolute(sourcePath)) {
714
- // absolute path
715
- return (0, _normalizePath.default)(sourcePath, false);
716
- }
717
-
718
- var isDir = _fsExtra.default.existsSync(sourcePath) && _fsExtra.default.lstatSync(sourcePath).isDirectory();
719
-
720
- return (0, _normalizePath.default)(_path.default.relative(process.cwd(), sourcePath), false) + (isDir ? "/" : "");
721
- }
722
-
723
- var cleanObsolete = R.filter(function (message) {
724
- return !message.obsolete;
725
- });
726
- exports.cleanObsolete = cleanObsolete;
727
-
728
- function order(by) {
729
- return {
730
- messageId: orderByMessageId,
731
- origin: orderByOrigin
732
- }[by];
733
- }
734
- /**
735
- * Object keys are in the same order as they were created
736
- * https://stackoverflow.com/a/31102605/1535540
737
- */
738
-
739
-
740
- function orderByMessageId(messages) {
741
- var orderedMessages = {};
742
- Object.keys(messages).sort().forEach(function (key) {
743
- orderedMessages[key] = messages[key];
744
- });
745
- return orderedMessages;
746
- }
747
-
748
- function orderByOrigin(messages) {
749
- function getFirstOrigin(messageKey) {
750
- var sortedOrigins = messages[messageKey].origin.sort(function (a, b) {
751
- if (a[0] < b[0]) return -1;
752
- if (a[0] > b[0]) return 1;
753
- return 0;
754
- });
755
- return sortedOrigins[0];
756
- }
757
-
758
- return Object.keys(messages).sort(function (a, b) {
759
- var _getFirstOrigin = getFirstOrigin(a),
760
- _getFirstOrigin2 = (0, _slicedToArray2.default)(_getFirstOrigin, 2),
761
- aFile = _getFirstOrigin2[0],
762
- aLineNumber = _getFirstOrigin2[1];
763
-
764
- var _getFirstOrigin3 = getFirstOrigin(b),
765
- _getFirstOrigin4 = (0, _slicedToArray2.default)(_getFirstOrigin3, 2),
766
- bFile = _getFirstOrigin4[0],
767
- bLineNumber = _getFirstOrigin4[1];
768
-
769
- if (aFile < bFile) return -1;
770
- if (aFile > bFile) return 1;
771
- if (aLineNumber < bLineNumber) return -1;
772
- if (aLineNumber > bLineNumber) return 1;
773
- return 0;
774
- }).reduce(function (acc, key) {
775
- acc[key] = messages[key];
776
- return acc;
777
- }, {});
778
- }