@messagevisor/core 0.0.1 → 0.1.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 (211) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/LICENSE +21 -0
  3. package/README.md +7 -0
  4. package/jest.config.js +8 -0
  5. package/lib/benchmark/index.d.ts +2 -0
  6. package/lib/benchmark/index.js +417 -0
  7. package/lib/benchmark/index.js.map +1 -0
  8. package/lib/builder/index.d.ts +70 -0
  9. package/lib/builder/index.js +831 -0
  10. package/lib/builder/index.js.map +1 -0
  11. package/lib/cli/index.d.ts +28 -0
  12. package/lib/cli/index.js +182 -0
  13. package/lib/cli/index.js.map +1 -0
  14. package/lib/config/index.d.ts +61 -0
  15. package/lib/config/index.js +255 -0
  16. package/lib/config/index.js.map +1 -0
  17. package/lib/create/index.d.ts +2 -0
  18. package/lib/create/index.js +405 -0
  19. package/lib/create/index.js.map +1 -0
  20. package/lib/datasource/filesystemAdapter.d.ts +44 -0
  21. package/lib/datasource/filesystemAdapter.js +424 -0
  22. package/lib/datasource/filesystemAdapter.js.map +1 -0
  23. package/lib/datasource/index.d.ts +39 -0
  24. package/lib/datasource/index.js +96 -0
  25. package/lib/datasource/index.js.map +1 -0
  26. package/lib/error.d.ts +6 -0
  27. package/lib/error.js +49 -0
  28. package/lib/error.js.map +1 -0
  29. package/lib/evaluate/cli.d.ts +8 -0
  30. package/lib/evaluate/cli.js +179 -0
  31. package/lib/evaluate/cli.js.map +1 -0
  32. package/lib/evaluate/index.d.ts +10 -0
  33. package/lib/evaluate/index.js +131 -0
  34. package/lib/evaluate/index.js.map +1 -0
  35. package/lib/examples/coerceExampleIsoDates.d.ts +12 -0
  36. package/lib/examples/coerceExampleIsoDates.js +81 -0
  37. package/lib/examples/coerceExampleIsoDates.js.map +1 -0
  38. package/lib/examples/index.d.ts +63 -0
  39. package/lib/examples/index.js +713 -0
  40. package/lib/examples/index.js.map +1 -0
  41. package/lib/exporter/index.d.ts +60 -0
  42. package/lib/exporter/index.js +610 -0
  43. package/lib/exporter/index.js.map +1 -0
  44. package/lib/find-duplicates/index.d.ts +41 -0
  45. package/lib/find-duplicates/index.js +297 -0
  46. package/lib/find-duplicates/index.js.map +1 -0
  47. package/lib/generate-code/index.d.ts +11 -0
  48. package/lib/generate-code/index.js +157 -0
  49. package/lib/generate-code/index.js.map +1 -0
  50. package/lib/generate-code/typescript.d.ts +14 -0
  51. package/lib/generate-code/typescript.js +307 -0
  52. package/lib/generate-code/typescript.js.map +1 -0
  53. package/lib/importer/index.d.ts +64 -0
  54. package/lib/importer/index.js +1092 -0
  55. package/lib/importer/index.js.map +1 -0
  56. package/lib/index.d.ts +18 -0
  57. package/lib/index.js +35 -0
  58. package/lib/index.js.map +1 -0
  59. package/lib/info/index.d.ts +17 -0
  60. package/lib/info/index.js +132 -0
  61. package/lib/info/index.js.map +1 -0
  62. package/lib/init/index.d.ts +30 -0
  63. package/lib/init/index.js +348 -0
  64. package/lib/init/index.js.map +1 -0
  65. package/lib/lint/index.d.ts +1 -0
  66. package/lib/lint/index.js +6 -0
  67. package/lib/lint/index.js.map +1 -0
  68. package/lib/linter/attributeSchema.d.ts +7 -0
  69. package/lib/linter/attributeSchema.js +36 -0
  70. package/lib/linter/attributeSchema.js.map +1 -0
  71. package/lib/linter/checkLocaleCircularDependency.d.ts +7 -0
  72. package/lib/linter/checkLocaleCircularDependency.js +42 -0
  73. package/lib/linter/checkLocaleCircularDependency.js.map +1 -0
  74. package/lib/linter/conditionSchema.d.ts +3 -0
  75. package/lib/linter/conditionSchema.js +283 -0
  76. package/lib/linter/conditionSchema.js.map +1 -0
  77. package/lib/linter/formatSchema.d.ts +325 -0
  78. package/lib/linter/formatSchema.js +165 -0
  79. package/lib/linter/formatSchema.js.map +1 -0
  80. package/lib/linter/icuStyleLint.d.ts +6 -0
  81. package/lib/linter/icuStyleLint.js +226 -0
  82. package/lib/linter/icuStyleLint.js.map +1 -0
  83. package/lib/linter/index.d.ts +34 -0
  84. package/lib/linter/index.js +557 -0
  85. package/lib/linter/index.js.map +1 -0
  86. package/lib/linter/localeSchema.d.ts +672 -0
  87. package/lib/linter/localeSchema.js +50 -0
  88. package/lib/linter/localeSchema.js.map +1 -0
  89. package/lib/linter/messageSchema.d.ts +35 -0
  90. package/lib/linter/messageSchema.js +115 -0
  91. package/lib/linter/messageSchema.js.map +1 -0
  92. package/lib/linter/printError.d.ts +8 -0
  93. package/lib/linter/printError.js +41 -0
  94. package/lib/linter/printError.js.map +1 -0
  95. package/lib/linter/schema.d.ts +33 -0
  96. package/lib/linter/schema.js +192 -0
  97. package/lib/linter/schema.js.map +1 -0
  98. package/lib/linter/segmentSchema.d.ts +8 -0
  99. package/lib/linter/segmentSchema.js +18 -0
  100. package/lib/linter/segmentSchema.js.map +1 -0
  101. package/lib/linter/targetSchema.d.ts +337 -0
  102. package/lib/linter/targetSchema.js +39 -0
  103. package/lib/linter/targetSchema.js.map +1 -0
  104. package/lib/linter/testSchema.d.ts +71 -0
  105. package/lib/linter/testSchema.js +165 -0
  106. package/lib/linter/testSchema.js.map +1 -0
  107. package/lib/linter/zodHelpers.d.ts +2 -0
  108. package/lib/linter/zodHelpers.js +15 -0
  109. package/lib/linter/zodHelpers.js.map +1 -0
  110. package/lib/list/index.d.ts +8 -0
  111. package/lib/list/index.js +524 -0
  112. package/lib/list/index.js.map +1 -0
  113. package/lib/matrix.d.ts +4 -0
  114. package/lib/matrix.js +66 -0
  115. package/lib/matrix.js.map +1 -0
  116. package/lib/promoter/index.d.ts +65 -0
  117. package/lib/promoter/index.js +1208 -0
  118. package/lib/promoter/index.js.map +1 -0
  119. package/lib/prune/index.d.ts +37 -0
  120. package/lib/prune/index.js +673 -0
  121. package/lib/prune/index.js.map +1 -0
  122. package/lib/sets.d.ts +10 -0
  123. package/lib/sets.js +120 -0
  124. package/lib/sets.js.map +1 -0
  125. package/lib/tester/cliFormat.d.ts +8 -0
  126. package/lib/tester/cliFormat.js +15 -0
  127. package/lib/tester/cliFormat.js.map +1 -0
  128. package/lib/tester/index.d.ts +35 -0
  129. package/lib/tester/index.js +713 -0
  130. package/lib/tester/index.js.map +1 -0
  131. package/lib/tester/matrix.d.ts +14 -0
  132. package/lib/tester/matrix.js +76 -0
  133. package/lib/tester/matrix.js.map +1 -0
  134. package/lib/tester/prettyDuration.d.ts +1 -0
  135. package/lib/tester/prettyDuration.js +30 -0
  136. package/lib/tester/prettyDuration.js.map +1 -0
  137. package/lib/tester/printTestResult.d.ts +2 -0
  138. package/lib/tester/printTestResult.js +32 -0
  139. package/lib/tester/printTestResult.js.map +1 -0
  140. package/lib/tester/types.d.ts +29 -0
  141. package/lib/tester/types.js +3 -0
  142. package/lib/tester/types.js.map +1 -0
  143. package/package.json +41 -13
  144. package/src/benchmark/index.spec.ts +375 -0
  145. package/src/benchmark/index.ts +433 -0
  146. package/src/builder/index.spec.ts +822 -0
  147. package/src/builder/index.ts +920 -0
  148. package/src/cli/index.spec.ts +54 -0
  149. package/src/cli/index.ts +150 -0
  150. package/src/config/index.spec.ts +70 -0
  151. package/src/config/index.ts +259 -0
  152. package/src/create/index.spec.ts +272 -0
  153. package/src/create/index.ts +295 -0
  154. package/src/datasource/filesystemAdapter.ts +313 -0
  155. package/src/datasource/index.ts +135 -0
  156. package/src/error.ts +33 -0
  157. package/src/evaluate/cli.spec.ts +368 -0
  158. package/src/evaluate/cli.ts +130 -0
  159. package/src/evaluate/index.ts +161 -0
  160. package/src/examples/coerceExampleIsoDates.spec.ts +81 -0
  161. package/src/examples/coerceExampleIsoDates.ts +98 -0
  162. package/src/examples/index.spec.ts +453 -0
  163. package/src/examples/index.ts +854 -0
  164. package/src/exporter/index.spec.ts +443 -0
  165. package/src/exporter/index.ts +643 -0
  166. package/src/find-duplicates/index.spec.ts +289 -0
  167. package/src/find-duplicates/index.ts +314 -0
  168. package/src/generate-code/index.ts +92 -0
  169. package/src/generate-code/typescript.spec.ts +241 -0
  170. package/src/generate-code/typescript.ts +284 -0
  171. package/src/importer/index.spec.ts +1101 -0
  172. package/src/importer/index.ts +1190 -0
  173. package/src/index.ts +18 -0
  174. package/src/info/index.ts +67 -0
  175. package/src/init/index.spec.ts +279 -0
  176. package/src/init/index.ts +292 -0
  177. package/src/lint/index.ts +1 -0
  178. package/src/linter/attributeSchema.ts +38 -0
  179. package/src/linter/checkLocaleCircularDependency.ts +51 -0
  180. package/src/linter/conditionSchema.ts +386 -0
  181. package/src/linter/formatSchema.ts +170 -0
  182. package/src/linter/icuStyleLint.ts +312 -0
  183. package/src/linter/index.spec.ts +824 -0
  184. package/src/linter/index.ts +460 -0
  185. package/src/linter/localeSchema.ts +70 -0
  186. package/src/linter/messageSchema.ts +152 -0
  187. package/src/linter/printError.ts +52 -0
  188. package/src/linter/schema.ts +230 -0
  189. package/src/linter/segmentSchema.ts +15 -0
  190. package/src/linter/targetSchema.ts +50 -0
  191. package/src/linter/testSchema.spec.ts +405 -0
  192. package/src/linter/testSchema.ts +239 -0
  193. package/src/linter/zodHelpers.ts +16 -0
  194. package/src/list/index.spec.ts +431 -0
  195. package/src/list/index.ts +463 -0
  196. package/src/matrix.ts +69 -0
  197. package/src/promoter/index.spec.ts +584 -0
  198. package/src/promoter/index.ts +1267 -0
  199. package/src/prune/index.spec.ts +418 -0
  200. package/src/prune/index.ts +693 -0
  201. package/src/sets.ts +74 -0
  202. package/src/tester/cliFormat.ts +11 -0
  203. package/src/tester/featurevisorIntegration.spec.ts +101 -0
  204. package/src/tester/index.spec.ts +577 -0
  205. package/src/tester/index.ts +679 -0
  206. package/src/tester/matrix.ts +106 -0
  207. package/src/tester/prettyDuration.ts +34 -0
  208. package/src/tester/printTestResult.ts +40 -0
  209. package/src/tester/types.ts +32 -0
  210. package/tsconfig.cjs.json +11 -0
  211. package/tsconfig.typecheck.json +4 -0
@@ -0,0 +1,713 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
24
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
50
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
51
+ if (ar || !(i in from)) {
52
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
53
+ ar[i] = from[i];
54
+ }
55
+ }
56
+ return to.concat(ar || Array.prototype.slice.call(from));
57
+ };
58
+ Object.defineProperty(exports, "__esModule", { value: true });
59
+ exports.examplesPlugin = void 0;
60
+ exports.resolveExamples = resolveExamples;
61
+ var sdk_1 = require("@messagevisor/sdk");
62
+ var builder_1 = require("../builder");
63
+ var error_1 = require("../error");
64
+ var matrix_1 = require("../matrix");
65
+ var sets_1 = require("../sets");
66
+ var cliFormat_1 = require("../tester/cliFormat");
67
+ var coerceExampleIsoDates_1 = require("./coerceExampleIsoDates");
68
+ function parseOptionalPositiveInteger(name, value) {
69
+ if (typeof value === "undefined") {
70
+ return undefined;
71
+ }
72
+ var parsedValue = Number(value);
73
+ if (!Number.isInteger(parsedValue) || parsedValue < 1) {
74
+ throw new error_1.MessagevisorCLIError("Invalid ".concat(name, ": expected an integer greater than or equal to 1."));
75
+ }
76
+ return parsedValue;
77
+ }
78
+ function parseOptionalPattern(name, value) {
79
+ if (typeof value === "undefined") {
80
+ return undefined;
81
+ }
82
+ if (value instanceof RegExp) {
83
+ return value;
84
+ }
85
+ try {
86
+ return new RegExp(String(value), "i");
87
+ }
88
+ catch (error) {
89
+ throw new error_1.MessagevisorCLIError("Invalid ".concat(name, ": ").concat(error.message));
90
+ }
91
+ }
92
+ function getExampleFilters(parsed) {
93
+ return {
94
+ exampleIndex: parseOptionalPositiveInteger("--exampleIndex", parsed.exampleIndex),
95
+ matrixIndex: parseOptionalPositiveInteger("--matrixIndex", parsed.matrixIndex),
96
+ descriptionPattern: parseOptionalPattern("--descriptionPattern", parsed.descriptionPattern),
97
+ translationPattern: parseOptionalPattern("--translationPattern", parsed.translationPattern),
98
+ };
99
+ }
100
+ function getExampleSourceSelection(parsed) {
101
+ var onlyLocales = Boolean(parsed.onlyLocales);
102
+ var onlyMessages = Boolean(parsed.onlyMessages);
103
+ if (onlyLocales && onlyMessages) {
104
+ throw new error_1.MessagevisorCLIError("Pass either --onlyLocales or --onlyMessages, not both.");
105
+ }
106
+ return {
107
+ includeLocales: !onlyMessages,
108
+ includeMessages: !onlyLocales,
109
+ };
110
+ }
111
+ function matchesCommonFilters(example, filters) {
112
+ if (typeof filters.exampleIndex === "number" &&
113
+ example.exampleIndex + 1 !== filters.exampleIndex) {
114
+ return false;
115
+ }
116
+ if (typeof filters.matrixIndex === "number") {
117
+ if (typeof example.matrixIndex !== "number" ||
118
+ example.matrixIndex + 1 !== filters.matrixIndex) {
119
+ return false;
120
+ }
121
+ }
122
+ if (filters.descriptionPattern && !filters.descriptionPattern.test(example.description || "")) {
123
+ return false;
124
+ }
125
+ if (filters.translationPattern &&
126
+ !filters.translationPattern.test(String(example.evaluatedTranslation))) {
127
+ return false;
128
+ }
129
+ return true;
130
+ }
131
+ function readLocales(datasource) {
132
+ return __awaiter(this, void 0, void 0, function () {
133
+ var localeKeys, locales, _a, _b;
134
+ var _this = this;
135
+ return __generator(this, function (_c) {
136
+ switch (_c.label) {
137
+ case 0: return [4 /*yield*/, datasource.listLocales()];
138
+ case 1:
139
+ localeKeys = _c.sent();
140
+ _b = (_a = Object).fromEntries;
141
+ return [4 /*yield*/, Promise.all(localeKeys.map(function (localeKey) { return __awaiter(_this, void 0, void 0, function () { var _a; return __generator(this, function (_b) {
142
+ switch (_b.label) {
143
+ case 0:
144
+ _a = [localeKey];
145
+ return [4 /*yield*/, datasource.readLocale(localeKey)];
146
+ case 1: return [2 /*return*/, _a.concat([(_b.sent())])];
147
+ }
148
+ }); }); }))];
149
+ case 2:
150
+ locales = _b.apply(_a, [_c.sent()]);
151
+ return [2 /*return*/, { localeKeys: localeKeys, locales: locales }];
152
+ }
153
+ });
154
+ });
155
+ }
156
+ function readMessages(datasource) {
157
+ return __awaiter(this, void 0, void 0, function () {
158
+ var messageKeys, messages, _a, _b;
159
+ var _this = this;
160
+ return __generator(this, function (_c) {
161
+ switch (_c.label) {
162
+ case 0: return [4 /*yield*/, datasource.listMessages()];
163
+ case 1:
164
+ messageKeys = _c.sent();
165
+ _b = (_a = Object).fromEntries;
166
+ return [4 /*yield*/, Promise.all(messageKeys.map(function (messageKey) { return __awaiter(_this, void 0, void 0, function () { var _a; return __generator(this, function (_b) {
167
+ switch (_b.label) {
168
+ case 0:
169
+ _a = [messageKey];
170
+ return [4 /*yield*/, datasource.readMessage(messageKey)];
171
+ case 1: return [2 /*return*/, _a.concat([(_b.sent())])];
172
+ }
173
+ }); }); }))];
174
+ case 2:
175
+ messages = _b.apply(_a, [_c.sent()]);
176
+ return [2 /*return*/, { messageKeys: messageKeys, messages: messages }];
177
+ }
178
+ });
179
+ });
180
+ }
181
+ function resolveLocaleExampleChain(localeKey, locales) {
182
+ var _a;
183
+ var chain = [];
184
+ var seen = new Set();
185
+ var currentKey = localeKey;
186
+ while (currentKey && !seen.has(currentKey)) {
187
+ seen.add(currentKey);
188
+ chain.unshift(currentKey);
189
+ currentKey = (_a = locales[currentKey]) === null || _a === void 0 ? void 0 : _a.mergeExamplesFrom;
190
+ }
191
+ return chain;
192
+ }
193
+ function expandLocaleExample(localeKey, sourceLocale, example, exampleIndex) {
194
+ if (!example.matrix) {
195
+ return [
196
+ {
197
+ locale: localeKey,
198
+ sourceLocale: sourceLocale,
199
+ exampleIndex: exampleIndex,
200
+ description: example.description,
201
+ rawMessage: example.rawMessage,
202
+ message: example.message,
203
+ values: example.values,
204
+ context: example.context,
205
+ formats: example.formats,
206
+ currency: example.currency,
207
+ timeZone: example.timeZone,
208
+ },
209
+ ];
210
+ }
211
+ var combinations = (0, matrix_1.getMatrixCombinations)(example.matrix);
212
+ return combinations.map(function (combination, matrixIndex) { return ({
213
+ locale: localeKey,
214
+ sourceLocale: sourceLocale,
215
+ exampleIndex: exampleIndex,
216
+ matrixIndex: matrixIndex,
217
+ description: (0, matrix_1.applyCombinationToValue)(example.description, combination),
218
+ rawMessage: (0, matrix_1.applyCombinationToValue)(example.rawMessage, combination),
219
+ message: (0, matrix_1.applyCombinationToValue)(example.message, combination),
220
+ values: (0, matrix_1.applyCombinationToValue)(example.values, combination),
221
+ context: (0, matrix_1.applyCombinationToValue)(example.context, combination),
222
+ formats: (0, matrix_1.applyCombinationToValue)(example.formats, combination),
223
+ currency: (0, matrix_1.applyCombinationToValue)(example.currency, combination),
224
+ timeZone: (0, matrix_1.applyCombinationToValue)(example.timeZone, combination),
225
+ }); });
226
+ }
227
+ function resolveLocaleExamples(localeKey, locales) {
228
+ var _a;
229
+ var chain = resolveLocaleExampleChain(localeKey, locales);
230
+ var result = [];
231
+ var exampleIndex = 0;
232
+ for (var _i = 0, chain_1 = chain; _i < chain_1.length; _i++) {
233
+ var sourceLocale = chain_1[_i];
234
+ var examples = ((_a = locales[sourceLocale]) === null || _a === void 0 ? void 0 : _a.examples) || [];
235
+ for (var _b = 0, examples_1 = examples; _b < examples_1.length; _b++) {
236
+ var example = examples_1[_b];
237
+ result.push.apply(result, expandLocaleExample(localeKey, sourceLocale, example, exampleIndex));
238
+ exampleIndex += 1;
239
+ }
240
+ }
241
+ return result;
242
+ }
243
+ function expandMessageExample(messageKey, example, exampleIndex) {
244
+ if (!example.matrix) {
245
+ return [
246
+ {
247
+ message: messageKey,
248
+ locale: example.locale,
249
+ exampleIndex: exampleIndex,
250
+ description: example.description,
251
+ values: example.values,
252
+ context: example.context,
253
+ formats: example.formats,
254
+ currency: example.currency,
255
+ timeZone: example.timeZone,
256
+ },
257
+ ];
258
+ }
259
+ var combinations = (0, matrix_1.getMatrixCombinations)(example.matrix);
260
+ return combinations.map(function (combination, matrixIndex) { return ({
261
+ message: messageKey,
262
+ locale: (0, matrix_1.applyCombinationToValue)(example.locale, combination),
263
+ exampleIndex: exampleIndex,
264
+ matrixIndex: matrixIndex,
265
+ description: (0, matrix_1.applyCombinationToValue)(example.description, combination),
266
+ values: (0, matrix_1.applyCombinationToValue)(example.values, combination),
267
+ context: (0, matrix_1.applyCombinationToValue)(example.context, combination),
268
+ formats: (0, matrix_1.applyCombinationToValue)(example.formats, combination),
269
+ currency: (0, matrix_1.applyCombinationToValue)(example.currency, combination),
270
+ timeZone: (0, matrix_1.applyCombinationToValue)(example.timeZone, combination),
271
+ }); });
272
+ }
273
+ function resolveMessageExamples(messageKey, message) {
274
+ var result = [];
275
+ var exampleIndex = 0;
276
+ for (var _i = 0, _a = message.examples || []; _i < _a.length; _i++) {
277
+ var example = _a[_i];
278
+ result.push.apply(result, expandMessageExample(messageKey, example, exampleIndex));
279
+ exampleIndex += 1;
280
+ }
281
+ return result;
282
+ }
283
+ function evaluateLocaleExample(projectConfig, datasource, locales, revision, example, includeEvaluationInput) {
284
+ return __awaiter(this, void 0, void 0, function () {
285
+ var evaluationValues, datafile, messagevisor_1, resolvedDefaultFormats, defaultFormats, messagevisor;
286
+ var _a;
287
+ return __generator(this, function (_b) {
288
+ switch (_b.label) {
289
+ case 0:
290
+ evaluationValues = (0, coerceExampleIsoDates_1.coerceExampleValuesIsoDates)(example.values);
291
+ if (!example.message) return [3 /*break*/, 2];
292
+ return [4 /*yield*/, (0, builder_1.buildMessageDatafile)(projectConfig, datasource, example.message, example.locale, revision)];
293
+ case 1:
294
+ datafile = _b.sent();
295
+ messagevisor_1 = (0, sdk_1.createMessagevisor)({
296
+ datafile: datafile,
297
+ locale: example.locale,
298
+ context: example.context,
299
+ modules: projectConfig.modules || [],
300
+ logLevel: "warn",
301
+ });
302
+ return [2 /*return*/, __assign(__assign({}, example), { evaluatedTranslation: messagevisor_1.translate(example.message, evaluationValues, {
303
+ context: example.context,
304
+ formats: example.formats,
305
+ currency: example.currency,
306
+ timeZone: example.timeZone,
307
+ }), evaluationInput: includeEvaluationInput
308
+ ? {
309
+ datafile: datafile,
310
+ formats: example.formats,
311
+ context: example.context,
312
+ values: evaluationValues,
313
+ currency: example.currency,
314
+ timeZone: example.timeZone,
315
+ }
316
+ : undefined })];
317
+ case 2:
318
+ resolvedDefaultFormats = (0, builder_1.resolveFormats)(example.locale, locales);
319
+ defaultFormats = resolvedDefaultFormats
320
+ ? (_a = {},
321
+ _a[example.locale] = resolvedDefaultFormats,
322
+ _a) : undefined;
323
+ messagevisor = (0, sdk_1.createMessagevisor)({
324
+ locale: example.locale,
325
+ context: example.context,
326
+ defaultFormats: defaultFormats,
327
+ modules: projectConfig.modules || [],
328
+ logLevel: "warn",
329
+ });
330
+ return [2 /*return*/, __assign(__assign({}, example), { evaluatedTranslation: messagevisor.formatMessage(example.rawMessage || "", evaluationValues || {}, {
331
+ formats: example.formats,
332
+ currency: example.currency,
333
+ timeZone: example.timeZone,
334
+ }), evaluationInput: includeEvaluationInput
335
+ ? {
336
+ defaultFormats: defaultFormats,
337
+ formats: example.formats,
338
+ context: example.context,
339
+ values: evaluationValues,
340
+ currency: example.currency,
341
+ timeZone: example.timeZone,
342
+ }
343
+ : undefined })];
344
+ }
345
+ });
346
+ });
347
+ }
348
+ function evaluateMessageExample(projectConfig, datasource, revision, example, includeEvaluationInput) {
349
+ return __awaiter(this, void 0, void 0, function () {
350
+ var evaluationValues, datafile, messagevisor;
351
+ return __generator(this, function (_a) {
352
+ switch (_a.label) {
353
+ case 0:
354
+ evaluationValues = (0, coerceExampleIsoDates_1.coerceExampleValuesIsoDates)(example.values);
355
+ return [4 /*yield*/, (0, builder_1.buildMessageDatafile)(projectConfig, datasource, example.message, example.locale, revision)];
356
+ case 1:
357
+ datafile = _a.sent();
358
+ messagevisor = (0, sdk_1.createMessagevisor)({
359
+ datafile: datafile,
360
+ locale: example.locale,
361
+ context: example.context,
362
+ modules: projectConfig.modules || [],
363
+ logLevel: "warn",
364
+ });
365
+ return [2 /*return*/, __assign(__assign({}, example), { evaluatedTranslation: messagevisor.translate(example.message, evaluationValues, {
366
+ context: example.context,
367
+ formats: example.formats,
368
+ currency: example.currency,
369
+ timeZone: example.timeZone,
370
+ }), evaluationInput: includeEvaluationInput
371
+ ? {
372
+ datafile: datafile,
373
+ formats: example.formats,
374
+ context: example.context,
375
+ values: evaluationValues,
376
+ currency: example.currency,
377
+ timeZone: example.timeZone,
378
+ }
379
+ : undefined })];
380
+ }
381
+ });
382
+ });
383
+ }
384
+ function collectExamplesForExecution(projectConfig_1, datasource_1, sourceSelection_1, localeFilter_1, messageFilter_1, setKey_1) {
385
+ return __awaiter(this, arguments, void 0, function (projectConfig, datasource, sourceSelection, localeFilter, messageFilter, setKey, includeEvaluationInput) {
386
+ var _a, localeKeys, locales, _b, messageKeys, messages, selectedLocaleKeys, selectedMessageKeys, revision, localeResults, messageResults, _i, selectedLocaleKeys_1, localeKey, examples, _c, examples_2, example, _d, _e, _f, _g, selectedMessageKeys_1, messageKey, examples, _h, examples_3, example, _j, _k, _l;
387
+ if (includeEvaluationInput === void 0) { includeEvaluationInput = false; }
388
+ return __generator(this, function (_m) {
389
+ switch (_m.label) {
390
+ case 0: return [4 /*yield*/, readLocales(datasource)];
391
+ case 1:
392
+ _a = _m.sent(), localeKeys = _a.localeKeys, locales = _a.locales;
393
+ return [4 /*yield*/, readMessages(datasource)];
394
+ case 2:
395
+ _b = _m.sent(), messageKeys = _b.messageKeys, messages = _b.messages;
396
+ if (localeFilter && !localeKeys.includes(localeFilter)) {
397
+ throw new error_1.MessagevisorCLIError("Unknown locale \"".concat(localeFilter, "\". Available locales: ").concat(localeKeys.join(", ") || "none", "."));
398
+ }
399
+ if (messageFilter && !messageKeys.includes(messageFilter)) {
400
+ throw new error_1.MessagevisorCLIError("Unknown message \"".concat(messageFilter, "\". Available messages: ").concat(messageKeys.join(", ") || "none", "."));
401
+ }
402
+ selectedLocaleKeys = localeFilter ? [localeFilter] : localeKeys;
403
+ selectedMessageKeys = messageFilter ? [messageFilter] : messageKeys;
404
+ return [4 /*yield*/, datasource.readRevision()];
405
+ case 3:
406
+ revision = _m.sent();
407
+ localeResults = [];
408
+ messageResults = [];
409
+ if (!sourceSelection.includeLocales) return [3 /*break*/, 9];
410
+ _i = 0, selectedLocaleKeys_1 = selectedLocaleKeys;
411
+ _m.label = 4;
412
+ case 4:
413
+ if (!(_i < selectedLocaleKeys_1.length)) return [3 /*break*/, 9];
414
+ localeKey = selectedLocaleKeys_1[_i];
415
+ examples = resolveLocaleExamples(localeKey, locales);
416
+ _c = 0, examples_2 = examples;
417
+ _m.label = 5;
418
+ case 5:
419
+ if (!(_c < examples_2.length)) return [3 /*break*/, 8];
420
+ example = examples_2[_c];
421
+ _e = (_d = localeResults).push;
422
+ _f = [{}];
423
+ return [4 /*yield*/, evaluateLocaleExample(projectConfig, datasource, locales, revision, example, includeEvaluationInput)];
424
+ case 6:
425
+ _e.apply(_d, [__assign.apply(void 0, [__assign.apply(void 0, _f.concat([(_m.sent())])), { set: setKey || undefined }])]);
426
+ _m.label = 7;
427
+ case 7:
428
+ _c++;
429
+ return [3 /*break*/, 5];
430
+ case 8:
431
+ _i++;
432
+ return [3 /*break*/, 4];
433
+ case 9:
434
+ if (!sourceSelection.includeMessages) return [3 /*break*/, 15];
435
+ _g = 0, selectedMessageKeys_1 = selectedMessageKeys;
436
+ _m.label = 10;
437
+ case 10:
438
+ if (!(_g < selectedMessageKeys_1.length)) return [3 /*break*/, 15];
439
+ messageKey = selectedMessageKeys_1[_g];
440
+ examples = resolveMessageExamples(messageKey, messages[messageKey]);
441
+ _h = 0, examples_3 = examples;
442
+ _m.label = 11;
443
+ case 11:
444
+ if (!(_h < examples_3.length)) return [3 /*break*/, 14];
445
+ example = examples_3[_h];
446
+ if (!localeKeys.includes(example.locale)) {
447
+ throw new Error("Unknown locale \"".concat(example.locale, "\" in examples for message \"").concat(messageKey, "\". Available locales: ").concat(localeKeys.join(", ") || "none", "."));
448
+ }
449
+ if (localeFilter && example.locale !== localeFilter) {
450
+ return [3 /*break*/, 13];
451
+ }
452
+ _k = (_j = messageResults).push;
453
+ _l = [{}];
454
+ return [4 /*yield*/, evaluateMessageExample(projectConfig, datasource, revision, example, includeEvaluationInput)];
455
+ case 12:
456
+ _k.apply(_j, [__assign.apply(void 0, [__assign.apply(void 0, _l.concat([(_m.sent())])), { set: setKey || undefined }])]);
457
+ _m.label = 13;
458
+ case 13:
459
+ _h++;
460
+ return [3 /*break*/, 11];
461
+ case 14:
462
+ _g++;
463
+ return [3 /*break*/, 10];
464
+ case 15: return [2 /*return*/, {
465
+ locales: localeResults,
466
+ messages: messageResults,
467
+ }];
468
+ }
469
+ });
470
+ });
471
+ }
472
+ function resolveExamples(projectConfig_1, datasource_1) {
473
+ return __awaiter(this, arguments, void 0, function (projectConfig, datasource, options) {
474
+ var filters, sourceSelection, executions, results, _i, executions_1, execution, executionResults;
475
+ var _a, _b;
476
+ if (options === void 0) { options = {}; }
477
+ return __generator(this, function (_c) {
478
+ switch (_c.label) {
479
+ case 0:
480
+ filters = getExampleFilters(options);
481
+ sourceSelection = getExampleSourceSelection(options);
482
+ return [4 /*yield*/, (0, sets_1.getProjectSetExecutions)(projectConfig, datasource, options.set)];
483
+ case 1:
484
+ executions = _c.sent();
485
+ results = {
486
+ locales: [],
487
+ messages: [],
488
+ };
489
+ _i = 0, executions_1 = executions;
490
+ _c.label = 2;
491
+ case 2:
492
+ if (!(_i < executions_1.length)) return [3 /*break*/, 5];
493
+ execution = executions_1[_i];
494
+ return [4 /*yield*/, collectExamplesForExecution(execution.projectConfig, execution.datasource, sourceSelection, options.locale, options.message, execution.set, options.includeEvaluationInput)];
495
+ case 3:
496
+ executionResults = _c.sent();
497
+ (_a = results.locales).push.apply(_a, executionResults.locales.filter(function (example) { return matchesCommonFilters(example, filters); }));
498
+ (_b = results.messages).push.apply(_b, executionResults.messages.filter(function (example) { return matchesCommonFilters(example, filters); }));
499
+ _c.label = 4;
500
+ case 4:
501
+ _i++;
502
+ return [3 /*break*/, 2];
503
+ case 5: return [2 /*return*/, results];
504
+ }
505
+ });
506
+ });
507
+ }
508
+ function printPlainLocaleExamples(examples, hasSets) {
509
+ if (examples.length === 0) {
510
+ console.log((0, cliFormat_1.colorize)("No locale examples found.", 33));
511
+ return;
512
+ }
513
+ var currentSet;
514
+ var currentLocale;
515
+ for (var _i = 0, examples_4 = examples; _i < examples_4.length; _i++) {
516
+ var example = examples_4[_i];
517
+ if (hasSets && example.set !== currentSet) {
518
+ currentSet = example.set;
519
+ currentLocale = undefined;
520
+ console.log((0, cliFormat_1.colorize)("Set \"".concat(currentSet, "\":"), 36));
521
+ console.log("");
522
+ }
523
+ if (example.locale !== currentLocale) {
524
+ currentLocale = example.locale;
525
+ console.log((0, cliFormat_1.colorize)("".concat(hasSets ? " " : "", "Locale \"").concat(currentLocale, "\":"), 1));
526
+ }
527
+ var indent = hasSets ? " " : " ";
528
+ var titleParts = ["Example #".concat(example.exampleIndex + 1)];
529
+ if (typeof example.matrixIndex === "number") {
530
+ titleParts.push("matrix #".concat(example.matrixIndex + 1));
531
+ }
532
+ titleParts.push("from ".concat(example.sourceLocale));
533
+ console.log((0, cliFormat_1.colorize)("".concat(indent).concat(titleParts.join(" · ")), 36));
534
+ if (example.description) {
535
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Description:"), 2));
536
+ console.log("".concat(indent, " ").concat(example.description));
537
+ }
538
+ if (example.message) {
539
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Message:"), 2));
540
+ console.log("".concat(indent, " ").concat(example.message));
541
+ }
542
+ if (example.rawMessage) {
543
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Raw message:"), 2));
544
+ console.log("".concat(indent, " ").concat(example.rawMessage));
545
+ }
546
+ if (typeof example.values !== "undefined") {
547
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Values:"), 2));
548
+ console.log("".concat(indent, " ").concat(JSON.stringify(example.values)));
549
+ }
550
+ if (typeof example.context !== "undefined") {
551
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Context:"), 2));
552
+ console.log("".concat(indent, " ").concat(JSON.stringify(example.context)));
553
+ }
554
+ if (typeof example.formats !== "undefined") {
555
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Formats:"), 2));
556
+ console.log("".concat(indent, " ").concat(JSON.stringify(example.formats)));
557
+ }
558
+ if (example.currency) {
559
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Currency:"), 2));
560
+ console.log("".concat(indent, " ").concat(example.currency));
561
+ }
562
+ if (example.timeZone) {
563
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Time zone:"), 2));
564
+ console.log("".concat(indent, " ").concat(example.timeZone));
565
+ }
566
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Evaluated translation:"), 2));
567
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " ").concat(JSON.stringify(example.evaluatedTranslation)), 32));
568
+ console.log("");
569
+ }
570
+ }
571
+ function printPlainMessageExamples(examples, hasSets) {
572
+ if (examples.length === 0) {
573
+ console.log((0, cliFormat_1.colorize)("No message examples found.", 33));
574
+ return;
575
+ }
576
+ var currentSet;
577
+ var currentMessage;
578
+ for (var _i = 0, examples_5 = examples; _i < examples_5.length; _i++) {
579
+ var example = examples_5[_i];
580
+ if (hasSets && example.set !== currentSet) {
581
+ currentSet = example.set;
582
+ currentMessage = undefined;
583
+ console.log((0, cliFormat_1.colorize)("Set \"".concat(currentSet, "\":"), 36));
584
+ console.log("");
585
+ }
586
+ if (example.message !== currentMessage) {
587
+ currentMessage = example.message;
588
+ console.log((0, cliFormat_1.colorize)("".concat(hasSets ? " " : "", "Message \"").concat(currentMessage, "\":"), 1));
589
+ }
590
+ var indent = hasSets ? " " : " ";
591
+ var titleParts = ["Example #".concat(example.exampleIndex + 1)];
592
+ if (typeof example.matrixIndex === "number") {
593
+ titleParts.push("matrix #".concat(example.matrixIndex + 1));
594
+ }
595
+ titleParts.push("locale ".concat(example.locale));
596
+ console.log((0, cliFormat_1.colorize)("".concat(indent).concat(titleParts.join(" · ")), 36));
597
+ if (example.description) {
598
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Description:"), 2));
599
+ console.log("".concat(indent, " ").concat(example.description));
600
+ }
601
+ if (typeof example.values !== "undefined") {
602
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Values:"), 2));
603
+ console.log("".concat(indent, " ").concat(JSON.stringify(example.values)));
604
+ }
605
+ if (typeof example.context !== "undefined") {
606
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Context:"), 2));
607
+ console.log("".concat(indent, " ").concat(JSON.stringify(example.context)));
608
+ }
609
+ if (typeof example.formats !== "undefined") {
610
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Formats:"), 2));
611
+ console.log("".concat(indent, " ").concat(JSON.stringify(example.formats)));
612
+ }
613
+ if (example.currency) {
614
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Currency:"), 2));
615
+ console.log("".concat(indent, " ").concat(example.currency));
616
+ }
617
+ if (example.timeZone) {
618
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Time zone:"), 2));
619
+ console.log("".concat(indent, " ").concat(example.timeZone));
620
+ }
621
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " Evaluated translation:"), 2));
622
+ console.log((0, cliFormat_1.colorize)("".concat(indent, " ").concat(JSON.stringify(example.evaluatedTranslation)), 32));
623
+ console.log("");
624
+ }
625
+ }
626
+ function printPlainExamples(result, hasSets) {
627
+ if (result.locales.length === 0 && result.messages.length === 0) {
628
+ console.log((0, cliFormat_1.colorize)("No examples found.", 33));
629
+ return;
630
+ }
631
+ console.log("");
632
+ console.log((0, cliFormat_1.colorize)("Messagevisor examples", 1));
633
+ console.log(" Locale examples: ".concat(result.locales.length));
634
+ console.log(" Message examples: ".concat(result.messages.length));
635
+ if (hasSets) {
636
+ console.log(" Sets: ".concat(new Set(__spreadArray(__spreadArray([], result.locales, true), result.messages, true).map(function (example) { return example.set; }).filter(Boolean)).size));
637
+ }
638
+ console.log("");
639
+ if (result.locales.length > 0) {
640
+ console.log((0, cliFormat_1.colorize)("Locales", 1));
641
+ console.log("");
642
+ printPlainLocaleExamples(result.locales, hasSets);
643
+ }
644
+ if (result.messages.length > 0) {
645
+ console.log((0, cliFormat_1.colorize)("Messages", 1));
646
+ console.log("");
647
+ printPlainMessageExamples(result.messages, hasSets);
648
+ }
649
+ var total = result.locales.length + result.messages.length;
650
+ console.log((0, cliFormat_1.colorize)("Found ".concat(total, " example").concat(total === 1 ? "" : "s", "."), 32));
651
+ console.log((0, cliFormat_1.colorize)("Tip: use --json --pretty for structured output.", 2));
652
+ }
653
+ exports.examplesPlugin = {
654
+ command: "examples",
655
+ handler: function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
656
+ var results, error_2;
657
+ var projectConfig = _b.projectConfig, datasource = _b.datasource, parsed = _b.parsed;
658
+ return __generator(this, function (_c) {
659
+ switch (_c.label) {
660
+ case 0:
661
+ _c.trys.push([0, 2, , 3]);
662
+ return [4 /*yield*/, resolveExamples(projectConfig, datasource, {
663
+ set: parsed.set,
664
+ locale: parsed.locale,
665
+ exampleIndex: parsed.exampleIndex,
666
+ matrixIndex: parsed.matrixIndex,
667
+ descriptionPattern: parsed.descriptionPattern,
668
+ translationPattern: parsed.translationPattern,
669
+ onlyMessages: parsed.onlyMessages,
670
+ onlyLocales: parsed.onlyLocales,
671
+ includeEvaluationInput: parsed.includeEvaluationInput,
672
+ })];
673
+ case 1:
674
+ results = _c.sent();
675
+ if (parsed.json) {
676
+ console.log(parsed.pretty ? JSON.stringify(results, null, 2) : JSON.stringify(results));
677
+ return [2 /*return*/];
678
+ }
679
+ printPlainExamples(results, projectConfig.sets);
680
+ return [3 /*break*/, 3];
681
+ case 2:
682
+ error_2 = _c.sent();
683
+ if ((0, error_1.printMessagevisorCLIError)(error_2)) {
684
+ return [2 /*return*/, false];
685
+ }
686
+ throw error_2;
687
+ case 3: return [2 /*return*/];
688
+ }
689
+ });
690
+ }); },
691
+ examples: [
692
+ { command: "examples", description: "list all locale and message examples" },
693
+ { command: "examples --locale=en-US", description: "list examples for a specific locale" },
694
+ {
695
+ command: "examples --exampleIndex=2 --matrixIndex=3",
696
+ description: "list a specific expanded matrix example",
697
+ },
698
+ {
699
+ command: "examples --descriptionPattern=welcome --translationPattern=adult",
700
+ description: "filter examples by description or evaluated translation",
701
+ },
702
+ {
703
+ command: "examples --onlyMessages",
704
+ description: "list only message examples",
705
+ },
706
+ {
707
+ command: "examples --onlyLocales",
708
+ description: "list only locale examples",
709
+ },
710
+ { command: "examples --json --pretty", description: "print examples as JSON" },
711
+ ],
712
+ };
713
+ //# sourceMappingURL=index.js.map