@lingui/cli 3.14.0 → 3.16.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.
Files changed (46) hide show
  1. package/CHANGELOG.md +482 -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 +19 -13
  30. package/api/catalog.js +0 -775
  31. package/api/compile.js +0 -169
  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 -50
  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 -199
  45. package/lingui-extract-template.js +0 -71
  46. package/lingui-extract.js +0 -286
package/api/catalog.js DELETED
@@ -1,775 +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
- return R.mapObjIndexed(function (_value, key) {
330
- return _this2.getTranslation(catalogs, locale, key, options);
331
- }, catalogs[locale]);
332
- }
333
- }, {
334
- key: "getTranslation",
335
- value: function getTranslation(catalogs, locale, key, _ref4) {
336
- var _this3 = this;
337
-
338
- var fallbackLocales = _ref4.fallbackLocales,
339
- sourceLocale = _ref4.sourceLocale;
340
-
341
- if (!catalogs[locale].hasOwnProperty(key)) {
342
- console.error("Message with key ".concat(key, " is missing in locale ").concat(locale));
343
- }
344
-
345
- var getTranslation = function getTranslation(locale) {
346
- var configLocales = _this3.config.locales.join('", "');
347
-
348
- var localeCatalog = catalogs[locale];
349
-
350
- if (!localeCatalog) {
351
- 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 "));
352
- return null;
353
- }
354
-
355
- if (!localeCatalog.hasOwnProperty(key)) {
356
- console.error("Message with key ".concat(key, " is missing in locale ").concat(locale));
357
- return null;
358
- }
359
-
360
- if (catalogs[locale]) {
361
- return catalogs[locale][key].translation;
362
- }
363
-
364
- return null;
365
- };
366
-
367
- var getMultipleFallbacks = function getMultipleFallbacks(locale) {
368
- var fL = fallbackLocales && fallbackLocales[locale]; // some probably the fallback will be undefined, so just search by locale
369
-
370
- if (!fL) return null;
371
-
372
- if (Array.isArray(fL)) {
373
- var _iterator2 = _createForOfIteratorHelper(fL),
374
- _step2;
375
-
376
- try {
377
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
378
- var fallbackLocale = _step2.value;
379
-
380
- if (catalogs[fallbackLocale]) {
381
- return getTranslation(fallbackLocale);
382
- }
383
- }
384
- } catch (err) {
385
- _iterator2.e(err);
386
- } finally {
387
- _iterator2.f();
388
- }
389
- } else {
390
- return getTranslation(fL);
391
- }
392
- };
393
-
394
- return (// Get translation in target locale
395
- getTranslation(locale) || // We search in fallbackLocales as dependent of each locale
396
- getMultipleFallbacks(locale) || // Get translation in fallbackLocales.default (if any)
397
- fallbackLocales && fallbackLocales.default && getTranslation(fallbackLocales.default) || // Get message default
398
- catalogs[locale][key].defaults || // If sourceLocale is either target locale of fallback one, use key
399
- sourceLocale && sourceLocale === locale && key || sourceLocale && fallbackLocales && fallbackLocales.default && sourceLocale === fallbackLocales.default && key || // Otherwise no translation is available
400
- undefined
401
- );
402
- }
403
- }, {
404
- key: "write",
405
- value: function write(locale, messages) {
406
- var filename = this.path.replace(LOCALE, locale) + this.format.catalogExtension;
407
- var created = !_fsExtra.default.existsSync(filename);
408
-
409
- var basedir = _path.default.dirname(filename);
410
-
411
- if (!_fsExtra.default.existsSync(basedir)) {
412
- _fsExtra.default.mkdirpSync(basedir);
413
- }
414
-
415
- var options = _objectSpread(_objectSpread({}, this.config.formatOptions), {}, {
416
- locale: locale
417
- });
418
-
419
- this.format.write(filename, messages, options);
420
- return [created, filename];
421
- }
422
- }, {
423
- key: "writeAll",
424
- value: function writeAll(catalogs) {
425
- var _this4 = this;
426
-
427
- this.locales.forEach(function (locale) {
428
- return _this4.write(locale, catalogs[locale]);
429
- });
430
- }
431
- }, {
432
- key: "writeTemplate",
433
- value: function writeTemplate(messages) {
434
- var filename = this.templateFile;
435
-
436
- var basedir = _path.default.dirname(filename);
437
-
438
- if (!_fsExtra.default.existsSync(basedir)) {
439
- _fsExtra.default.mkdirpSync(basedir);
440
- }
441
-
442
- var options = _objectSpread(_objectSpread({}, this.config.formatOptions), {}, {
443
- locale: undefined
444
- });
445
-
446
- this.format.write(filename, messages, options);
447
- }
448
- }, {
449
- key: "writeCompiled",
450
- value: function writeCompiled(locale, compiledCatalog, namespace) {
451
- var ext;
452
-
453
- if (namespace === "es") {
454
- ext = "mjs";
455
- } else if (namespace === "ts") {
456
- ext = "ts";
457
- } else {
458
- ext = "js";
459
- }
460
-
461
- var filename = "".concat(this.path.replace(LOCALE, locale), ".").concat(ext);
462
-
463
- var basedir = _path.default.dirname(filename);
464
-
465
- if (!_fsExtra.default.existsSync(basedir)) {
466
- _fsExtra.default.mkdirpSync(basedir);
467
- }
468
-
469
- _fsExtra.default.writeFileSync(filename, compiledCatalog);
470
-
471
- return filename;
472
- }
473
- }, {
474
- key: "read",
475
- value: function read(locale) {
476
- var filename = this.path.replace(LOCALE, locale) + this.format.catalogExtension;
477
- if (!_fsExtra.default.existsSync(filename)) return null;
478
- return this.format.read(filename);
479
- }
480
- }, {
481
- key: "readAll",
482
- value: function readAll() {
483
- var _this5 = this;
484
-
485
- return R.mergeAll(this.locales.map(function (locale) {
486
- return (0, _defineProperty2.default)({}, locale, _this5.read(locale));
487
- }));
488
- }
489
- }, {
490
- key: "readTemplate",
491
- value: function readTemplate() {
492
- var filename = this.templateFile;
493
- if (!_fsExtra.default.existsSync(filename)) return null;
494
- return this.format.read(filename);
495
- }
496
- }, {
497
- key: "sourcePaths",
498
- get: function get() {
499
- var includeGlobs = this.include.map(function (includePath) {
500
- var isDir = _fsExtra.default.existsSync(includePath) && _fsExtra.default.lstatSync(includePath).isDirectory();
501
- /**
502
- * glob library results from absolute patterns such as /foo/* are mounted onto the root setting using path.join.
503
- * On windows, this will by default result in /foo/* matching C:\foo\bar.txt.
504
- */
505
-
506
-
507
- return isDir ? (0, _normalizePath.default)(_path.default.resolve(process.cwd(), includePath === "/" ? "" : includePath, "**/*.*")) : includePath;
508
- });
509
- var patterns = includeGlobs.length > 1 ? "{".concat(includeGlobs.join(","), "}") : includeGlobs[0];
510
- return _glob.default.sync(patterns, {
511
- ignore: this.exclude,
512
- mark: true
513
- });
514
- }
515
- }, {
516
- key: "templateFile",
517
- get: function get() {
518
- return this.path.replace(LOCALE_SUFFIX_RE, "messages.pot");
519
- }
520
- }, {
521
- key: "localeDir",
522
- get: function get() {
523
- var localePatternIndex = this.path.indexOf(LOCALE);
524
-
525
- if (localePatternIndex === -1) {
526
- throw Error("Invalid catalog path: ".concat(LOCALE, " variable is missing"));
527
- }
528
-
529
- return this.path.substr(0, localePatternIndex);
530
- }
531
- }, {
532
- key: "locales",
533
- get: function get() {
534
- return this.config.locales;
535
- }
536
- }]);
537
- return Catalog;
538
- }();
539
- /**
540
- * Parse `config.catalogs` and return a list of configured Catalog instances.
541
- */
542
-
543
-
544
- exports.Catalog = Catalog;
545
-
546
- function getCatalogs(config) {
547
- var catalogsConfig = config.catalogs;
548
- var catalogs = [];
549
- catalogsConfig.forEach(function (catalog) {
550
- // Validate that `catalogPath` doesn't end with trailing slash
551
- if (catalog.path.endsWith(PATHSEP)) {
552
- var extension = (0, _formats.default)(config.format).catalogExtension;
553
- var correctPath = catalog.path.slice(0, -1);
554
- var examplePath = correctPath.replace(LOCALE, // Show example using one of configured locales (if any)
555
- (config.locales || [])[0] || "en") + extension;
556
- throw new Error( // prettier-ignore
557
- "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, "\"."));
558
- }
559
-
560
- var include = ensureArray(catalog.include).map(normalizeRelativePath);
561
- var exclude = ensureArray(catalog.exclude).map(normalizeRelativePath); // catalog.path without {name} pattern -> always refers to a single catalog
562
-
563
- if (!catalog.path.includes(NAME)) {
564
- // Validate that sourcePaths doesn't use {name} pattern either
565
- var invalidSource = include.find(function (path) {
566
- return path.includes(NAME);
567
- });
568
-
569
- if (invalidSource !== undefined) {
570
- 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.");
571
- } // catalog name is the last directory of catalog.path.
572
- // If the last part is {locale}, then catalog doesn't have an explicit name
573
-
574
-
575
- var name = function () {
576
- var _name = catalog.path.split(PATHSEP).slice(-1)[0];
577
- return _name !== LOCALE ? _name : null;
578
- }();
579
-
580
- catalogs.push(new Catalog({
581
- name: name,
582
- path: normalizeRelativePath(catalog.path),
583
- include: include,
584
- exclude: exclude
585
- }, config));
586
- return;
587
- }
588
-
589
- var patterns = include.map(function (path) {
590
- return path.replace(NAME, "*");
591
- });
592
-
593
- var candidates = _glob.default.sync(patterns.length > 1 ? "{".concat(patterns.join(","), "}") : patterns[0], {
594
- ignore: exclude,
595
- mark: true
596
- });
597
-
598
- candidates.forEach(function (catalogDir) {
599
- var name = _path.default.basename(catalogDir);
600
-
601
- catalogs.push(new Catalog({
602
- name: name,
603
- path: normalizeRelativePath(catalog.path.replace(NAME, name)),
604
- include: include.map(function (path) {
605
- return path.replace(NAME, name);
606
- }),
607
- exclude: exclude.map(function (path) {
608
- return path.replace(NAME, name);
609
- })
610
- }, config));
611
- });
612
- });
613
- return catalogs;
614
- }
615
-
616
- function getCatalogForFile(file, catalogs) {
617
- var _iterator3 = _createForOfIteratorHelper(catalogs),
618
- _step3;
619
-
620
- try {
621
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
622
- var _catalog = _step3.value;
623
- var catalogFile = "".concat(_catalog.path).concat(_catalog.format.catalogExtension);
624
- var catalogGlob = catalogFile.replace(LOCALE, "*");
625
-
626
- var match = _micromatch.default.capture(normalizeRelativePath(_path.default.relative(_catalog.config.rootDir, catalogGlob)), normalizeRelativePath(file));
627
-
628
- if (match) {
629
- return {
630
- locale: match[0],
631
- catalog: _catalog
632
- };
633
- }
634
- }
635
- } catch (err) {
636
- _iterator3.e(err);
637
- } finally {
638
- _iterator3.f();
639
- }
640
-
641
- return null;
642
- }
643
- /**
644
- * Create catalog for merged messages.
645
- */
646
-
647
-
648
- function getCatalogForMerge(config) {
649
- var catalogConfig = config;
650
-
651
- if (catalogConfig.catalogsMergePath.endsWith(PATHSEP)) {
652
- var extension = (0, _formats.default)(config.format).catalogExtension;
653
- var correctPath = catalogConfig.catalogsMergePath.slice(0, -1);
654
- var examplePath = correctPath.replace(LOCALE, // Show example using one of configured locales (if any)
655
- (config.locales || [])[0] || "en") + extension;
656
- throw new Error( // prettier-ignore
657
- "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, "\"."));
658
- } // catalog name is the last directory of catalogPath.
659
- // If the last part is {locale}, then catalog doesn't have an explicit name
660
-
661
-
662
- var name = function () {
663
- var _name = _path.default.basename(normalizeRelativePath(catalogConfig.catalogsMergePath));
664
-
665
- return _name !== LOCALE ? _name : null;
666
- }();
667
-
668
- var catalog = new Catalog({
669
- name: name,
670
- path: normalizeRelativePath(catalogConfig.catalogsMergePath),
671
- include: [],
672
- exclude: []
673
- }, config);
674
- return catalog;
675
- }
676
- /**
677
- * Merge origins and extractedComments for messages found in different places. All other attributes
678
- * should be the same (raise an error if defaults are different).
679
- */
680
-
681
-
682
- function mergeOriginsAndExtractedComments(msgId, prev, next) {
683
- if (prev.defaults !== next.defaults) {
684
- 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));
685
- }
686
-
687
- return _objectSpread(_objectSpread({}, next), {}, {
688
- extractedComments: R.concat(prev.extractedComments, next.extractedComments),
689
- origin: R.concat(prev.origin, next.origin)
690
- });
691
- }
692
- /**
693
- * Ensure that value is always array. If not, turn it into an array of one element.
694
- */
695
-
696
-
697
- var ensureArray = function ensureArray(value) {
698
- if (value == null) return [];
699
- return Array.isArray(value) ? value : [value];
700
- };
701
- /**
702
- * Remove ./ at the beginning: ./relative => relative
703
- * relative => relative
704
- * Preserve directories: ./relative/ => relative/
705
- * Preserve absolute paths: /absolute/path => /absolute/path
706
- */
707
-
708
-
709
- function normalizeRelativePath(sourcePath) {
710
- if (_path.default.isAbsolute(sourcePath)) {
711
- // absolute path
712
- return (0, _normalizePath.default)(sourcePath, false);
713
- }
714
-
715
- var isDir = _fsExtra.default.existsSync(sourcePath) && _fsExtra.default.lstatSync(sourcePath).isDirectory();
716
-
717
- return (0, _normalizePath.default)(_path.default.relative(process.cwd(), sourcePath), false) + (isDir ? "/" : "");
718
- }
719
-
720
- var cleanObsolete = R.filter(function (message) {
721
- return !message.obsolete;
722
- });
723
- exports.cleanObsolete = cleanObsolete;
724
-
725
- function order(by) {
726
- return {
727
- messageId: orderByMessageId,
728
- origin: orderByOrigin
729
- }[by];
730
- }
731
- /**
732
- * Object keys are in the same order as they were created
733
- * https://stackoverflow.com/a/31102605/1535540
734
- */
735
-
736
-
737
- function orderByMessageId(messages) {
738
- var orderedMessages = {};
739
- Object.keys(messages).sort().forEach(function (key) {
740
- orderedMessages[key] = messages[key];
741
- });
742
- return orderedMessages;
743
- }
744
-
745
- function orderByOrigin(messages) {
746
- function getFirstOrigin(messageKey) {
747
- var sortedOrigins = messages[messageKey].origin.sort(function (a, b) {
748
- if (a[0] < b[0]) return -1;
749
- if (a[0] > b[0]) return 1;
750
- return 0;
751
- });
752
- return sortedOrigins[0];
753
- }
754
-
755
- return Object.keys(messages).sort(function (a, b) {
756
- var _getFirstOrigin = getFirstOrigin(a),
757
- _getFirstOrigin2 = (0, _slicedToArray2.default)(_getFirstOrigin, 2),
758
- aFile = _getFirstOrigin2[0],
759
- aLineNumber = _getFirstOrigin2[1];
760
-
761
- var _getFirstOrigin3 = getFirstOrigin(b),
762
- _getFirstOrigin4 = (0, _slicedToArray2.default)(_getFirstOrigin3, 2),
763
- bFile = _getFirstOrigin4[0],
764
- bLineNumber = _getFirstOrigin4[1];
765
-
766
- if (aFile < bFile) return -1;
767
- if (aFile > bFile) return 1;
768
- if (aLineNumber < bLineNumber) return -1;
769
- if (aLineNumber > bLineNumber) return 1;
770
- return 0;
771
- }).reduce(function (acc, key) {
772
- acc[key] = messages[key];
773
- return acc;
774
- }, {});
775
- }