@esgettext/tools 1.0.0 → 1.2.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 (113) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +197 -22
  3. package/dist/command.js +2 -0
  4. package/dist/commands/convert.js +286 -0
  5. package/dist/commands/install.js +257 -0
  6. package/dist/commands/msgfmt-all.js +219 -0
  7. package/dist/commands/msgmerge-all.js +230 -0
  8. package/dist/commands/xgettext/file-resolver.js +20 -0
  9. package/dist/commands/xgettext/files-collector.js +39 -0
  10. package/dist/commands/xgettext.js +534 -0
  11. package/dist/configuration.js +277 -0
  12. package/dist/esgettext-package-json.js +2 -0
  13. package/dist/index.js +135 -0
  14. package/dist/package.js +10 -12
  15. package/dist/parser/javascript.js +9 -28
  16. package/dist/parser/parser.js +176 -148
  17. package/dist/parser/po.js +144 -128
  18. package/dist/parser/typescript.js +10 -28
  19. package/dist/pot/catalog.js +134 -83
  20. package/dist/pot/entry.js +173 -115
  21. package/dist/pot/keyword.js +66 -92
  22. package/package.json +38 -25
  23. package/bin/esgettext-install.js +0 -5
  24. package/bin/esgettext-mo2json.js +0 -5
  25. package/bin/esgettext-msgfmt-all.js +0 -5
  26. package/bin/esgettext-msgmerge-all.js +0 -5
  27. package/bin/esgettext-xgettext.js +0 -5
  28. package/dist/cli/getopt.d.ts +0 -34
  29. package/dist/cli/getopt.js +0 -150
  30. package/dist/cli/getopt.js.map +0 -1
  31. package/dist/cli/getopt.spec.d.ts +0 -1
  32. package/dist/cli/getopt.spec.js +0 -164
  33. package/dist/cli/getopt.spec.js.map +0 -1
  34. package/dist/cli/install.d.ts +0 -1
  35. package/dist/cli/install.js +0 -91
  36. package/dist/cli/install.js.map +0 -1
  37. package/dist/cli/mo2json-cli.d.ts +0 -1
  38. package/dist/cli/mo2json-cli.js +0 -48
  39. package/dist/cli/mo2json-cli.js.map +0 -1
  40. package/dist/cli/msgfmt-all.d.ts +0 -1
  41. package/dist/cli/msgfmt-all.js +0 -99
  42. package/dist/cli/msgfmt-all.js.map +0 -1
  43. package/dist/cli/msgmerge-all.d.ts +0 -1
  44. package/dist/cli/msgmerge-all.js +0 -85
  45. package/dist/cli/msgmerge-all.js.map +0 -1
  46. package/dist/cli/xgettext-cli.d.ts +0 -1
  47. package/dist/cli/xgettext-cli.js +0 -303
  48. package/dist/cli/xgettext-cli.js.map +0 -1
  49. package/dist/install/install.d.ts +0 -10
  50. package/dist/install/install.js +0 -157
  51. package/dist/install/install.js.map +0 -1
  52. package/dist/msgfmt-all/msgfmt-all.d.ts +0 -8
  53. package/dist/msgfmt-all/msgfmt-all.js +0 -142
  54. package/dist/msgfmt-all/msgfmt-all.js.map +0 -1
  55. package/dist/msgmerge-all/msgmerge-all.d.ts +0 -9
  56. package/dist/msgmerge-all/msgmerge-all.js +0 -157
  57. package/dist/msgmerge-all/msgmerge-all.js.map +0 -1
  58. package/dist/package.d.ts +0 -4
  59. package/dist/package.js.map +0 -1
  60. package/dist/parser/__snapshots__/javascript.spec.js.snap +0 -56
  61. package/dist/parser/__snapshots__/po.spec.js.snap +0 -176
  62. package/dist/parser/javascript.d.ts +0 -4
  63. package/dist/parser/javascript.js.map +0 -1
  64. package/dist/parser/javascript.spec.d.ts +0 -1
  65. package/dist/parser/javascript.spec.js +0 -436
  66. package/dist/parser/javascript.spec.js.map +0 -1
  67. package/dist/parser/parser.d.ts +0 -45
  68. package/dist/parser/parser.js.map +0 -1
  69. package/dist/parser/po.d.ts +0 -22
  70. package/dist/parser/po.js.map +0 -1
  71. package/dist/parser/po.spec.d.ts +0 -1
  72. package/dist/parser/po.spec.js +0 -296
  73. package/dist/parser/po.spec.js.map +0 -1
  74. package/dist/parser/typescript.d.ts +0 -4
  75. package/dist/parser/typescript.js.map +0 -1
  76. package/dist/parser/typescript.spec.d.ts +0 -1
  77. package/dist/parser/typescript.spec.js +0 -106
  78. package/dist/parser/typescript.spec.js.map +0 -1
  79. package/dist/pot/__snapshots__/catalog.spec.js.snap +0 -591
  80. package/dist/pot/catalog.d.ts +0 -26
  81. package/dist/pot/catalog.js.map +0 -1
  82. package/dist/pot/catalog.spec.d.ts +0 -1
  83. package/dist/pot/catalog.spec.js +0 -240
  84. package/dist/pot/catalog.spec.js.map +0 -1
  85. package/dist/pot/entry.d.ts +0 -35
  86. package/dist/pot/entry.js.map +0 -1
  87. package/dist/pot/entry.spec.d.ts +0 -1
  88. package/dist/pot/entry.spec.js +0 -317
  89. package/dist/pot/entry.spec.js.map +0 -1
  90. package/dist/pot/keyword.d.ts +0 -17
  91. package/dist/pot/keyword.js.map +0 -1
  92. package/dist/pot/keyword.spec.d.ts +0 -1
  93. package/dist/pot/keyword.spec.js +0 -54
  94. package/dist/pot/keyword.spec.js.map +0 -1
  95. package/dist/xgettext/__snapshots__/xgettext.spec.js.snap +0 -462
  96. package/dist/xgettext/file-resolver.d.ts +0 -5
  97. package/dist/xgettext/file-resolver.js +0 -23
  98. package/dist/xgettext/file-resolver.js.map +0 -1
  99. package/dist/xgettext/file-resolver.spec.d.ts +0 -1
  100. package/dist/xgettext/file-resolver.spec.js +0 -22
  101. package/dist/xgettext/file-resolver.spec.js.map +0 -1
  102. package/dist/xgettext/files-collector.d.ts +0 -5
  103. package/dist/xgettext/files-collector.js +0 -41
  104. package/dist/xgettext/files-collector.js.map +0 -1
  105. package/dist/xgettext/files-collector.spec.d.ts +0 -1
  106. package/dist/xgettext/files-collector.spec.js +0 -78
  107. package/dist/xgettext/files-collector.spec.js.map +0 -1
  108. package/dist/xgettext/xgettext.d.ts +0 -16
  109. package/dist/xgettext/xgettext.js +0 -335
  110. package/dist/xgettext/xgettext.js.map +0 -1
  111. package/dist/xgettext/xgettext.spec.d.ts +0 -1
  112. package/dist/xgettext/xgettext.spec.js +0 -743
  113. package/dist/xgettext/xgettext.spec.js.map +0 -1
@@ -1,21 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Catalog = void 0;
4
- var entry_1 = require("./entry");
5
- var Catalog = (function () {
6
- function Catalog(properties) {
7
- if (properties === void 0) { properties = {}; }
4
+ const entry_1 = require("./entry");
5
+ /**
6
+ * An entire message catalog that is subsequently filled.
7
+ */
8
+ class Catalog {
9
+ constructor(properties = {}) {
8
10
  this.properties = properties;
9
11
  this.cache = {};
10
12
  this.entries = new Array();
11
- var barePkg;
13
+ let barePkg;
12
14
  if (typeof properties.package === 'undefined') {
13
15
  barePkg = 'PACKAGE';
14
16
  properties.package = 'PACKAGE VERSION';
15
17
  }
16
18
  else {
17
19
  barePkg = properties.package.replace(/\n/g, '\\n');
18
- ;
19
20
  if (typeof properties.version !== 'undefined') {
20
21
  properties.package += ' ' + properties.version;
21
22
  }
@@ -27,41 +28,60 @@ var Catalog = (function () {
27
28
  properties.msgidBugsAddress = 'MSGID_BUGS_ADDRESS';
28
29
  }
29
30
  if (typeof properties.date === 'undefined') {
30
- var now = new Date();
31
- var year = now.getFullYear().toString();
31
+ const now = new Date();
32
+ // Avoid if/else, so we do not spoil our test coverage. :)
33
+ let year = now.getFullYear().toString();
32
34
  year = '0'.repeat(4 - year.length) + year;
33
- var month = (1 + now.getMonth()).toString();
35
+ let month = (1 + now.getMonth()).toString();
34
36
  month = '0'.repeat(2 - month.length) + month;
35
- var mday = now.getDate().toString();
37
+ let mday = now.getDate().toString();
36
38
  mday = '0'.repeat(2 - mday.length) + mday;
37
- var hour = now.getHours().toString();
39
+ let hour = now.getHours().toString();
38
40
  hour = '0'.repeat(2 - hour.length) + hour;
39
- var minutes = now.getMinutes().toString();
41
+ let minutes = now.getMinutes().toString();
40
42
  minutes = '0'.repeat(2 - minutes.length) + minutes;
41
- var offset = now.getTimezoneOffset();
42
- var sign = ['+', '-'][Math.sign(offset) + 1];
43
+ // Do not depend on the timezone for test coverage.
44
+ let offset = now.getTimezoneOffset();
45
+ const sign = ['+', '-'][Math.sign(offset) + 1];
43
46
  offset = Math.abs(offset);
44
- var offsetHours = Math.floor(offset / 60).toString();
47
+ let offsetHours = Math.floor(offset / 60).toString();
45
48
  offsetHours = '0'.repeat(2 - offsetHours.length) + offsetHours;
46
- var offsetMinutes = (offset % 60).toString();
49
+ let offsetMinutes = (offset % 60).toString();
47
50
  offsetMinutes = '0'.repeat(2 - offsetMinutes.length) + offsetMinutes;
48
51
  properties.date =
49
52
  [year, month, mday].join('-') +
50
- " ".concat(hour, ":").concat(minutes) +
51
- "".concat(sign).concat(offsetHours).concat(offsetMinutes);
52
- }
53
- var pkg = properties.package.replace(/\n/g, '\\n');
54
- var msgidBugsAddress = properties.msgidBugsAddress.replace(/\n/g, '\\n');
55
- var copyrightHolder = properties.copyrightHolder.replace(/\n/g, '\\n');
56
- var header = "Project-Id-Version: ".concat(pkg, "\nReport-Msgid-Bugs-To: ").concat(msgidBugsAddress, "\nPOT-Creation-Date: ").concat(properties.date, "\nPO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\nLast-Translator: FULL NAME <EMAIL@ADDRESS>\nLanguage-Team: LANGUAGE <LL@li.org>\nLanguage: \nMIME-Version: 1.0\nContent-Type: text/plain; charset=CHARSET\nContent-Transfer-Encoding: 8bit\n");
57
- var comment;
53
+ ` ${hour}:${minutes}` +
54
+ `${sign}${offsetHours}${offsetMinutes}`;
55
+ }
56
+ const pkg = properties.package.replace(/\n/g, '\\n');
57
+ const msgidBugsAddress = properties.msgidBugsAddress.replace(/\n/g, '\\n');
58
+ const copyrightHolder = properties.copyrightHolder.replace(/\n/g, '\\n');
59
+ const header = `Project-Id-Version: ${pkg}
60
+ Report-Msgid-Bugs-To: ${msgidBugsAddress}
61
+ POT-Creation-Date: ${properties.date}
62
+ PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE
63
+ Last-Translator: FULL NAME <EMAIL@ADDRESS>
64
+ Language-Team: LANGUAGE <LL@li.org>
65
+ Language:\u0020
66
+ MIME-Version: 1.0
67
+ Content-Type: text/plain; charset=CHARSET
68
+ Content-Transfer-Encoding: 8bit
69
+ `;
70
+ let comment;
58
71
  if (properties.foreignUser) {
59
- comment = "SOME DESCRIPTIVE TITLE\nThis file is put in the public domain.\nFIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n";
72
+ comment = `SOME DESCRIPTIVE TITLE
73
+ This file is put in the public domain.
74
+ FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
75
+ `;
60
76
  }
61
77
  else {
62
- comment = "SOME DESCRIPTIVE TITLE\nCopyright (C) YEAR ".concat(copyrightHolder, "\nThis file is distributed under the same license as the ").concat(barePkg, " package.\nFIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n");
78
+ comment = `SOME DESCRIPTIVE TITLE
79
+ Copyright (C) YEAR ${copyrightHolder}
80
+ This file is distributed under the same license as the ${barePkg} package.
81
+ FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
82
+ `;
63
83
  }
64
- var headerEntry = new entry_1.POTEntry({
84
+ const headerEntry = new entry_1.POTEntry({
65
85
  msgid: '',
66
86
  msgstr: header,
67
87
  flags: ['fuzzy'],
@@ -70,33 +90,46 @@ var Catalog = (function () {
70
90
  });
71
91
  this.addEntry(headerEntry);
72
92
  }
73
- Catalog.prototype.toString = function (options) {
74
- if (options === void 0) { options = {}; }
75
- var width = options && typeof options.width !== 'undefined' ? options.width : 76;
76
- var isNotHeader = function (entry) {
93
+ /**
94
+ * Render the entire catalog.
95
+ *
96
+ * @param renderOptions - options for rendering
97
+ *
98
+ * The following options are supported:
99
+ *
100
+ * width: wrap lines to that page width (default: 76)
101
+ *
102
+ * @returns the catalog rendered as a po(t) file
103
+ */
104
+ toString(options = {}) {
105
+ const width = options && typeof options.width !== 'undefined' ? options.width : 76;
106
+ const isNotHeader = (entry) => {
77
107
  return !(entry.properties.msgid === '' &&
78
108
  typeof entry.properties.msgctxt === 'undefined');
79
109
  };
80
- var isHeader = function (entry) {
110
+ const isHeader = (entry) => {
81
111
  return !isNotHeader(entry);
82
112
  };
83
- var header = options.omitHeader
113
+ const header = options.omitHeader
84
114
  ? undefined
85
115
  : this.entries
86
116
  .filter(isHeader)
87
- .map(function (entry) { return entry.toString(width); })
117
+ .map(entry => entry.toString(width))
88
118
  .join('\n');
89
- var body = '';
119
+ let body = '';
90
120
  if (options.sortOutput) {
91
121
  body = this.entries
92
122
  .filter(isNotHeader)
93
- .sort(function (a, b) {
94
- var cmp = a.properties.msgid.localeCompare(b.properties.msgid);
123
+ .sort((a, b) => {
124
+ const cmp = a.properties.msgid.localeCompare(b.properties.msgid);
95
125
  if (cmp) {
96
126
  return cmp;
97
127
  }
98
- var actx = a.properties.msgctxt;
99
- var bctx = b.properties.msgctxt;
128
+ const actx = a.properties.msgctxt;
129
+ const bctx = b.properties.msgctxt;
130
+ // It is impossible that both msgctxts are undefined
131
+ // because that would mean that the msgids are identical
132
+ // which is prevented.
100
133
  if (typeof bctx === 'undefined') {
101
134
  return +1;
102
135
  }
@@ -105,34 +138,35 @@ var Catalog = (function () {
105
138
  }
106
139
  return actx.localeCompare(bctx);
107
140
  })
108
- .map(function (entry) { return entry.toString(width); })
141
+ .map(entry => entry.toString(width))
109
142
  .join('\n');
110
143
  }
111
144
  else if (options.sortByFile) {
112
- var copy = this.copy(this.properties);
113
- var splitRef_1 = function (ref) {
114
- var parts = ref.split(':');
115
- var lineno = Number.parseInt(parts.pop(), 10);
116
- var filename = parts.join(':');
117
- return { filename: filename, lineno: lineno };
145
+ const copy = this.copy(this.properties);
146
+ const splitRef = (ref) => {
147
+ const parts = ref.split(':');
148
+ const lineno = Number.parseInt(parts.pop(), 10);
149
+ const filename = parts.join(':');
150
+ return { filename, lineno };
118
151
  };
119
- copy.entries.forEach(function (entry) {
152
+ // First order all references
153
+ copy.entries.forEach(entry => {
120
154
  if (entry.properties.references) {
121
155
  entry.properties.references = entry.properties.references
122
- .map(splitRef_1)
123
- .sort(function (a, b) {
156
+ .map(splitRef)
157
+ .sort((a, b) => {
124
158
  return (a.filename.localeCompare(b.filename) ||
125
159
  Math.sign(a.lineno - b.lineno));
126
160
  })
127
- .map(function (ref) { return "".concat(ref.filename, ":").concat(ref.lineno); });
161
+ .map(ref => `${ref.filename}:${ref.lineno}`);
128
162
  }
129
163
  });
130
- var compareSplitRefs = function (a, b) {
131
- var arefs = a.refs;
132
- var brefs = b.refs;
133
- var min = Math.min(arefs.length, brefs.length);
134
- for (var i = 0; i < min; ++i) {
135
- var cmp = arefs[i].filename.localeCompare(brefs[i].filename) ||
164
+ const compareSplitRefs = (a, b) => {
165
+ const arefs = a.refs;
166
+ const brefs = b.refs;
167
+ const min = Math.min(arefs.length, brefs.length);
168
+ for (let i = 0; i < min; ++i) {
169
+ const cmp = arefs[i].filename.localeCompare(brefs[i].filename) ||
136
170
  Math.sign(arefs[i].lineno - brefs[i].lineno);
137
171
  if (cmp) {
138
172
  return cmp;
@@ -140,35 +174,45 @@ var Catalog = (function () {
140
174
  }
141
175
  return Math.sign(arefs.length - brefs.length);
142
176
  };
143
- var splitRefs_1 = function (refs) {
144
- return refs ? refs.map(splitRef_1) : [];
177
+ const splitRefs = (refs) => {
178
+ return refs ? refs.map(splitRef) : [];
145
179
  };
146
180
  body = copy.entries
147
181
  .filter(isNotHeader)
148
- .map(function (entry) {
149
- return { refs: splitRefs_1(entry.properties.references), entry: entry };
182
+ .map(entry => {
183
+ return {
184
+ refs: splitRefs(entry.properties.references),
185
+ entry,
186
+ };
150
187
  })
151
188
  .sort(compareSplitRefs)
152
- .map(function (split) { return split.entry; })
153
- .map(function (entry) { return entry.toString(width); })
189
+ .map(split => split.entry)
190
+ .map(entry => entry.toString(width))
154
191
  .join('\n');
155
192
  }
156
193
  else {
157
194
  body = this.entries
158
195
  .filter(isNotHeader)
159
- .map(function (entry) { return entry.toString(width); })
196
+ .map(entry => entry.toString(width))
160
197
  .join('\n');
161
198
  }
162
199
  if (body && header) {
163
- return "".concat(header, "\n").concat(body);
200
+ return `${header}\n${body}`;
164
201
  }
165
202
  else {
166
203
  return header || body || '';
167
204
  }
168
- };
169
- Catalog.prototype.addEntry = function (entry) {
170
- var msgid = entry.properties.msgid;
171
- var msgctxt = entry.properties.msgctxt;
205
+ }
206
+ /**
207
+ * Add an entry to the catalog. If an entry with the same msgid
208
+ * and msgctxt already exists, the entry is merged into
209
+ * the existing one instead.
210
+ *
211
+ * @param entry - the `POTEntry` to add
212
+ */
213
+ addEntry(entry) {
214
+ const msgid = entry.properties.msgid;
215
+ const msgctxt = entry.properties.msgctxt;
172
216
  if (!Object.prototype.hasOwnProperty.call(this.cache, msgid)) {
173
217
  this.cache[msgid] = {};
174
218
  }
@@ -179,32 +223,39 @@ var Catalog = (function () {
179
223
  else {
180
224
  this.cache[msgid][msgctxt].merge(entry);
181
225
  }
182
- };
183
- Catalog.prototype.deleteEntry = function (msgid, msgctxt) {
226
+ }
227
+ deleteEntry(msgid, msgctxt) {
184
228
  if (!this.cache[msgid]) {
185
229
  return;
186
230
  }
187
- var entry = this.cache[msgid][msgctxt];
231
+ const entry = this.cache[msgid][msgctxt];
188
232
  if (!entry) {
189
233
  return;
190
234
  }
191
235
  delete this.cache[msgid][msgctxt];
192
- this.entries = this.entries.filter(function (other) { return other !== entry; });
193
- };
194
- Catalog.prototype.copy = function (properties) {
195
- var other = new Catalog(properties);
196
- this.entries.forEach(function (entry) { return other.addEntry(entry); });
236
+ this.entries = this.entries.filter(other => other !== entry);
237
+ }
238
+ /**
239
+ * Copy a catalog with other options. This is for testing only.
240
+ *
241
+ * @returns a deep copy of the catalog.
242
+ */
243
+ copy(properties) {
244
+ const other = new Catalog(properties);
245
+ this.entries.forEach(entry => other.addEntry(entry));
197
246
  return other;
198
- };
199
- Catalog.prototype.makePOT = function () {
200
- this.entries.forEach(function (entry) {
247
+ }
248
+ /**
249
+ * Removes all translations from a catalog, so that it can be used as
250
+ * a .pot file.
251
+ */
252
+ makePOT() {
253
+ this.entries.forEach(entry => {
201
254
  if (!(entry.properties.msgid === '' &&
202
255
  typeof entry.properties.msgctxt === 'undefined')) {
203
256
  delete entry.properties.msgstr;
204
257
  }
205
258
  });
206
- };
207
- return Catalog;
208
- }());
259
+ }
260
+ }
209
261
  exports.Catalog = Catalog;
210
- //# sourceMappingURL=catalog.js.map