@terai/dev 0.0.12 → 0.0.14

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 (52) hide show
  1. package/bin.js +1 -1
  2. package/dist/cli.default.cjs +71 -0
  3. package/dist/cli.default.cjs.map +1 -0
  4. package/dist/cli.default.d.cts +1 -0
  5. package/dist/cli.default.d.mts +1 -1
  6. package/dist/cli.default.mjs +67 -20936
  7. package/dist/cli.default.mjs.map +1 -1
  8. package/dist/index.cjs +2 -0
  9. package/dist/index.cjs.map +1 -0
  10. package/dist/index.d.cts +9 -0
  11. package/dist/index.d.cts.map +1 -0
  12. package/dist/index.d.mts +6 -4
  13. package/dist/index.d.mts.map +1 -0
  14. package/dist/index.mjs +1 -14
  15. package/dist/index.mjs.map +1 -1
  16. package/package.json +21 -16
  17. package/dist/cli.d.mts +0 -3
  18. package/dist/cli.d.ts +0 -3
  19. package/dist/cli.default.d.ts +0 -1
  20. package/dist/cli.default.js +0 -20920
  21. package/dist/cli.default.js.map +0 -1
  22. package/dist/cli.js +0 -20906
  23. package/dist/cli.js.map +0 -1
  24. package/dist/cli.mjs +0 -20914
  25. package/dist/cli.mjs.map +0 -1
  26. package/dist/commands/extract.d.mts +0 -12
  27. package/dist/commands/extract.d.ts +0 -12
  28. package/dist/commands/extract.js +0 -694
  29. package/dist/commands/extract.js.map +0 -1
  30. package/dist/commands/extract.mjs +0 -662
  31. package/dist/commands/extract.mjs.map +0 -1
  32. package/dist/commands/index.d.mts +0 -5
  33. package/dist/commands/index.d.ts +0 -5
  34. package/dist/commands/index.js +0 -1004
  35. package/dist/commands/index.js.map +0 -1
  36. package/dist/commands/index.mjs +0 -966
  37. package/dist/commands/index.mjs.map +0 -1
  38. package/dist/commands/init.d.mts +0 -12
  39. package/dist/commands/init.d.ts +0 -12
  40. package/dist/commands/init.js +0 -784
  41. package/dist/commands/init.js.map +0 -1
  42. package/dist/commands/init.mjs +0 -748
  43. package/dist/commands/init.mjs.map +0 -1
  44. package/dist/commands/translate.d.mts +0 -10
  45. package/dist/commands/translate.d.ts +0 -10
  46. package/dist/commands/translate.js +0 -731
  47. package/dist/commands/translate.js.map +0 -1
  48. package/dist/commands/translate.mjs +0 -693
  49. package/dist/commands/translate.mjs.map +0 -1
  50. package/dist/index.d.ts +0 -7
  51. package/dist/index.js +0 -41
  52. package/dist/index.js.map +0 -1
@@ -1,693 +0,0 @@
1
- // ../generator/dist/chunk-N4N3TZO6.mjs
2
- import { runtime } from "@terai/runtime";
3
- import { stringify } from "@terai/utils";
4
- async function generate({
5
- dictionaries,
6
- locale,
7
- cwd,
8
- outDir
9
- }) {
10
- const folderPath = runtime.path.resolve(cwd, outDir, locale);
11
- const output = [];
12
- for (const id in dictionaries) {
13
- const fileDir = runtime.path.resolve(folderPath, `${id}.json`);
14
- const fileContent = dictionaries[id];
15
- const idOutput = stringify(fileContent);
16
- runtime.fs.write(fileDir, idOutput);
17
- output.push(idOutput);
18
- }
19
- return output;
20
- }
21
-
22
- // src/commands/translate.ts
23
- import { loadConfig, runtime as runtime2 } from "@terai/runtime";
24
-
25
- // ../logger/dist/chunk-HH7ZIRTH.mjs
26
- import process from "process";
27
- import os from "os";
28
- import tty from "tty";
29
- var ANSI_BACKGROUND_OFFSET = 10;
30
- var wrapAnsi16 = (offset = 0) => (code) => `\x1B[${code + offset}m`;
31
- var wrapAnsi256 = (offset = 0) => (code) => `\x1B[${38 + offset};5;${code}m`;
32
- var wrapAnsi16m = (offset = 0) => (red, green, blue) => `\x1B[${38 + offset};2;${red};${green};${blue}m`;
33
- var styles = {
34
- modifier: {
35
- reset: [0, 0],
36
- // 21 isn't widely supported and 22 does the same thing
37
- bold: [1, 22],
38
- dim: [2, 22],
39
- italic: [3, 23],
40
- underline: [4, 24],
41
- overline: [53, 55],
42
- inverse: [7, 27],
43
- hidden: [8, 28],
44
- strikethrough: [9, 29]
45
- },
46
- color: {
47
- black: [30, 39],
48
- red: [31, 39],
49
- green: [32, 39],
50
- yellow: [33, 39],
51
- blue: [34, 39],
52
- magenta: [35, 39],
53
- cyan: [36, 39],
54
- white: [37, 39],
55
- // Bright color
56
- blackBright: [90, 39],
57
- gray: [90, 39],
58
- // Alias of `blackBright`
59
- grey: [90, 39],
60
- // Alias of `blackBright`
61
- redBright: [91, 39],
62
- greenBright: [92, 39],
63
- yellowBright: [93, 39],
64
- blueBright: [94, 39],
65
- magentaBright: [95, 39],
66
- cyanBright: [96, 39],
67
- whiteBright: [97, 39]
68
- },
69
- bgColor: {
70
- bgBlack: [40, 49],
71
- bgRed: [41, 49],
72
- bgGreen: [42, 49],
73
- bgYellow: [43, 49],
74
- bgBlue: [44, 49],
75
- bgMagenta: [45, 49],
76
- bgCyan: [46, 49],
77
- bgWhite: [47, 49],
78
- // Bright color
79
- bgBlackBright: [100, 49],
80
- bgGray: [100, 49],
81
- // Alias of `bgBlackBright`
82
- bgGrey: [100, 49],
83
- // Alias of `bgBlackBright`
84
- bgRedBright: [101, 49],
85
- bgGreenBright: [102, 49],
86
- bgYellowBright: [103, 49],
87
- bgBlueBright: [104, 49],
88
- bgMagentaBright: [105, 49],
89
- bgCyanBright: [106, 49],
90
- bgWhiteBright: [107, 49]
91
- }
92
- };
93
- Object.keys(styles.modifier);
94
- var foregroundColorNames = Object.keys(styles.color);
95
- var backgroundColorNames = Object.keys(styles.bgColor);
96
- [...foregroundColorNames, ...backgroundColorNames];
97
- function assembleStyles() {
98
- const codes = /* @__PURE__ */ new Map();
99
- for (const [groupName, group] of Object.entries(styles)) {
100
- for (const [styleName, style] of Object.entries(group)) {
101
- styles[styleName] = {
102
- open: `\x1B[${style[0]}m`,
103
- close: `\x1B[${style[1]}m`
104
- };
105
- group[styleName] = styles[styleName];
106
- codes.set(style[0], style[1]);
107
- }
108
- Object.defineProperty(styles, groupName, {
109
- value: group,
110
- enumerable: false
111
- });
112
- }
113
- Object.defineProperty(styles, "codes", {
114
- value: codes,
115
- enumerable: false
116
- });
117
- styles.color.close = "\x1B[39m";
118
- styles.bgColor.close = "\x1B[49m";
119
- styles.color.ansi = wrapAnsi16();
120
- styles.color.ansi256 = wrapAnsi256();
121
- styles.color.ansi16m = wrapAnsi16m();
122
- styles.bgColor.ansi = wrapAnsi16(ANSI_BACKGROUND_OFFSET);
123
- styles.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET);
124
- styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET);
125
- Object.defineProperties(styles, {
126
- rgbToAnsi256: {
127
- value(red, green, blue) {
128
- if (red === green && green === blue) {
129
- if (red < 8) {
130
- return 16;
131
- }
132
- if (red > 248) {
133
- return 231;
134
- }
135
- return Math.round((red - 8) / 247 * 24) + 232;
136
- }
137
- return 16 + 36 * Math.round(red / 255 * 5) + 6 * Math.round(green / 255 * 5) + Math.round(blue / 255 * 5);
138
- },
139
- enumerable: false
140
- },
141
- hexToRgb: {
142
- value(hex) {
143
- const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex.toString(16));
144
- if (!matches) {
145
- return [0, 0, 0];
146
- }
147
- let [colorString] = matches;
148
- if (colorString.length === 3) {
149
- colorString = [...colorString].map((character) => character + character).join("");
150
- }
151
- const integer = Number.parseInt(colorString, 16);
152
- return [
153
- /* eslint-disable no-bitwise */
154
- integer >> 16 & 255,
155
- integer >> 8 & 255,
156
- integer & 255
157
- /* eslint-enable no-bitwise */
158
- ];
159
- },
160
- enumerable: false
161
- },
162
- hexToAnsi256: {
163
- value: (hex) => styles.rgbToAnsi256(...styles.hexToRgb(hex)),
164
- enumerable: false
165
- },
166
- ansi256ToAnsi: {
167
- value(code) {
168
- if (code < 8) {
169
- return 30 + code;
170
- }
171
- if (code < 16) {
172
- return 90 + (code - 8);
173
- }
174
- let red;
175
- let green;
176
- let blue;
177
- if (code >= 232) {
178
- red = ((code - 232) * 10 + 8) / 255;
179
- green = red;
180
- blue = red;
181
- } else {
182
- code -= 16;
183
- const remainder = code % 36;
184
- red = Math.floor(code / 36) / 5;
185
- green = Math.floor(remainder / 6) / 5;
186
- blue = remainder % 6 / 5;
187
- }
188
- const value = Math.max(red, green, blue) * 2;
189
- if (value === 0) {
190
- return 30;
191
- }
192
- let result = 30 + (Math.round(blue) << 2 | Math.round(green) << 1 | Math.round(red));
193
- if (value === 2) {
194
- result += 60;
195
- }
196
- return result;
197
- },
198
- enumerable: false
199
- },
200
- rgbToAnsi: {
201
- value: (red, green, blue) => styles.ansi256ToAnsi(styles.rgbToAnsi256(red, green, blue)),
202
- enumerable: false
203
- },
204
- hexToAnsi: {
205
- value: (hex) => styles.ansi256ToAnsi(styles.hexToAnsi256(hex)),
206
- enumerable: false
207
- }
208
- });
209
- return styles;
210
- }
211
- var ansiStyles = assembleStyles();
212
- var ansi_styles_default = ansiStyles;
213
- function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process.argv) {
214
- const prefix = flag.startsWith("-") ? "" : flag.length === 1 ? "-" : "--";
215
- const position = argv.indexOf(prefix + flag);
216
- const terminatorPosition = argv.indexOf("--");
217
- return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
218
- }
219
- var { env } = process;
220
- var flagForceColor;
221
- if (hasFlag("no-color") || hasFlag("no-colors") || hasFlag("color=false") || hasFlag("color=never")) {
222
- flagForceColor = 0;
223
- } else if (hasFlag("color") || hasFlag("colors") || hasFlag("color=true") || hasFlag("color=always")) {
224
- flagForceColor = 1;
225
- }
226
- function envForceColor() {
227
- if ("FORCE_COLOR" in env) {
228
- if (env.FORCE_COLOR === "true") {
229
- return 1;
230
- }
231
- if (env.FORCE_COLOR === "false") {
232
- return 0;
233
- }
234
- return env.FORCE_COLOR.length === 0 ? 1 : Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
235
- }
236
- }
237
- function translateLevel(level) {
238
- if (level === 0) {
239
- return false;
240
- }
241
- return {
242
- level,
243
- hasBasic: true,
244
- has256: level >= 2,
245
- has16m: level >= 3
246
- };
247
- }
248
- function _supportsColor(haveStream, { streamIsTTY, sniffFlags = true } = {}) {
249
- const noFlagForceColor = envForceColor();
250
- if (noFlagForceColor !== void 0) {
251
- flagForceColor = noFlagForceColor;
252
- }
253
- const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
254
- if (forceColor === 0) {
255
- return 0;
256
- }
257
- if (sniffFlags) {
258
- if (hasFlag("color=16m") || hasFlag("color=full") || hasFlag("color=truecolor")) {
259
- return 3;
260
- }
261
- if (hasFlag("color=256")) {
262
- return 2;
263
- }
264
- }
265
- if ("TF_BUILD" in env && "AGENT_NAME" in env) {
266
- return 1;
267
- }
268
- if (haveStream && !streamIsTTY && forceColor === void 0) {
269
- return 0;
270
- }
271
- const min = forceColor || 0;
272
- if (env.TERM === "dumb") {
273
- return min;
274
- }
275
- if (process.platform === "win32") {
276
- const osRelease = os.release().split(".");
277
- if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) {
278
- return Number(osRelease[2]) >= 14931 ? 3 : 2;
279
- }
280
- return 1;
281
- }
282
- if ("CI" in env) {
283
- if (["GITHUB_ACTIONS", "GITEA_ACTIONS", "CIRCLECI"].some((key) => key in env)) {
284
- return 3;
285
- }
286
- if (["TRAVIS", "APPVEYOR", "GITLAB_CI", "BUILDKITE", "DRONE"].some((sign) => sign in env) || env.CI_NAME === "codeship") {
287
- return 1;
288
- }
289
- return min;
290
- }
291
- if ("TEAMCITY_VERSION" in env) {
292
- return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
293
- }
294
- if (env.COLORTERM === "truecolor") {
295
- return 3;
296
- }
297
- if (env.TERM === "xterm-kitty") {
298
- return 3;
299
- }
300
- if (env.TERM === "xterm-ghostty") {
301
- return 3;
302
- }
303
- if (env.TERM === "wezterm") {
304
- return 3;
305
- }
306
- if ("TERM_PROGRAM" in env) {
307
- const version = Number.parseInt((env.TERM_PROGRAM_VERSION || "").split(".")[0], 10);
308
- switch (env.TERM_PROGRAM) {
309
- case "iTerm.app": {
310
- return version >= 3 ? 3 : 2;
311
- }
312
- case "Apple_Terminal": {
313
- return 2;
314
- }
315
- }
316
- }
317
- if (/-256(color)?$/i.test(env.TERM)) {
318
- return 2;
319
- }
320
- if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
321
- return 1;
322
- }
323
- if ("COLORTERM" in env) {
324
- return 1;
325
- }
326
- return min;
327
- }
328
- function createSupportsColor(stream, options = {}) {
329
- const level = _supportsColor(stream, {
330
- streamIsTTY: stream && stream.isTTY,
331
- ...options
332
- });
333
- return translateLevel(level);
334
- }
335
- var supportsColor = {
336
- stdout: createSupportsColor({ isTTY: tty.isatty(1) }),
337
- stderr: createSupportsColor({ isTTY: tty.isatty(2) })
338
- };
339
- var supports_color_default = supportsColor;
340
- function stringReplaceAll(string, substring, replacer) {
341
- let index = string.indexOf(substring);
342
- if (index === -1) {
343
- return string;
344
- }
345
- const substringLength = substring.length;
346
- let endIndex = 0;
347
- let returnValue = "";
348
- do {
349
- returnValue += string.slice(endIndex, index) + substring + replacer;
350
- endIndex = index + substringLength;
351
- index = string.indexOf(substring, endIndex);
352
- } while (index !== -1);
353
- returnValue += string.slice(endIndex);
354
- return returnValue;
355
- }
356
- function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) {
357
- let endIndex = 0;
358
- let returnValue = "";
359
- do {
360
- const gotCR = string[index - 1] === "\r";
361
- returnValue += string.slice(endIndex, gotCR ? index - 1 : index) + prefix + (gotCR ? "\r\n" : "\n") + postfix;
362
- endIndex = index + 1;
363
- index = string.indexOf("\n", endIndex);
364
- } while (index !== -1);
365
- returnValue += string.slice(endIndex);
366
- return returnValue;
367
- }
368
- var { stdout: stdoutColor, stderr: stderrColor } = supports_color_default;
369
- var GENERATOR = Symbol("GENERATOR");
370
- var STYLER = Symbol("STYLER");
371
- var IS_EMPTY = Symbol("IS_EMPTY");
372
- var levelMapping = [
373
- "ansi",
374
- "ansi",
375
- "ansi256",
376
- "ansi16m"
377
- ];
378
- var styles2 = /* @__PURE__ */ Object.create(null);
379
- var applyOptions = (object, options = {}) => {
380
- if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) {
381
- throw new Error("The `level` option should be an integer from 0 to 3");
382
- }
383
- const colorLevel = stdoutColor ? stdoutColor.level : 0;
384
- object.level = options.level === void 0 ? colorLevel : options.level;
385
- };
386
- var chalkFactory = (options) => {
387
- const chalk2 = (...strings) => strings.join(" ");
388
- applyOptions(chalk2, options);
389
- Object.setPrototypeOf(chalk2, createChalk.prototype);
390
- return chalk2;
391
- };
392
- function createChalk(options) {
393
- return chalkFactory(options);
394
- }
395
- Object.setPrototypeOf(createChalk.prototype, Function.prototype);
396
- for (const [styleName, style] of Object.entries(ansi_styles_default)) {
397
- styles2[styleName] = {
398
- get() {
399
- const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]);
400
- Object.defineProperty(this, styleName, { value: builder });
401
- return builder;
402
- }
403
- };
404
- }
405
- styles2.visible = {
406
- get() {
407
- const builder = createBuilder(this, this[STYLER], true);
408
- Object.defineProperty(this, "visible", { value: builder });
409
- return builder;
410
- }
411
- };
412
- var getModelAnsi = (model, level, type, ...arguments_) => {
413
- if (model === "rgb") {
414
- if (level === "ansi16m") {
415
- return ansi_styles_default[type].ansi16m(...arguments_);
416
- }
417
- if (level === "ansi256") {
418
- return ansi_styles_default[type].ansi256(ansi_styles_default.rgbToAnsi256(...arguments_));
419
- }
420
- return ansi_styles_default[type].ansi(ansi_styles_default.rgbToAnsi(...arguments_));
421
- }
422
- if (model === "hex") {
423
- return getModelAnsi("rgb", level, type, ...ansi_styles_default.hexToRgb(...arguments_));
424
- }
425
- return ansi_styles_default[type][model](...arguments_);
426
- };
427
- var usedModels = ["rgb", "hex", "ansi256"];
428
- for (const model of usedModels) {
429
- styles2[model] = {
430
- get() {
431
- const { level } = this;
432
- return function(...arguments_) {
433
- const styler = createStyler(getModelAnsi(model, levelMapping[level], "color", ...arguments_), ansi_styles_default.color.close, this[STYLER]);
434
- return createBuilder(this, styler, this[IS_EMPTY]);
435
- };
436
- }
437
- };
438
- const bgModel = "bg" + model[0].toUpperCase() + model.slice(1);
439
- styles2[bgModel] = {
440
- get() {
441
- const { level } = this;
442
- return function(...arguments_) {
443
- const styler = createStyler(getModelAnsi(model, levelMapping[level], "bgColor", ...arguments_), ansi_styles_default.bgColor.close, this[STYLER]);
444
- return createBuilder(this, styler, this[IS_EMPTY]);
445
- };
446
- }
447
- };
448
- }
449
- var proto = Object.defineProperties(() => {
450
- }, {
451
- ...styles2,
452
- level: {
453
- enumerable: true,
454
- get() {
455
- return this[GENERATOR].level;
456
- },
457
- set(level) {
458
- this[GENERATOR].level = level;
459
- }
460
- }
461
- });
462
- var createStyler = (open, close, parent) => {
463
- let openAll;
464
- let closeAll;
465
- if (parent === void 0) {
466
- openAll = open;
467
- closeAll = close;
468
- } else {
469
- openAll = parent.openAll + open;
470
- closeAll = close + parent.closeAll;
471
- }
472
- return {
473
- open,
474
- close,
475
- openAll,
476
- closeAll,
477
- parent
478
- };
479
- };
480
- var createBuilder = (self, _styler, _isEmpty) => {
481
- const builder = (...arguments_) => applyStyle(builder, arguments_.length === 1 ? "" + arguments_[0] : arguments_.join(" "));
482
- Object.setPrototypeOf(builder, proto);
483
- builder[GENERATOR] = self;
484
- builder[STYLER] = _styler;
485
- builder[IS_EMPTY] = _isEmpty;
486
- return builder;
487
- };
488
- var applyStyle = (self, string) => {
489
- if (self.level <= 0 || !string) {
490
- return self[IS_EMPTY] ? "" : string;
491
- }
492
- let styler = self[STYLER];
493
- if (styler === void 0) {
494
- return string;
495
- }
496
- const { openAll, closeAll } = styler;
497
- if (string.includes("\x1B")) {
498
- while (styler !== void 0) {
499
- string = stringReplaceAll(string, styler.close, styler.open);
500
- styler = styler.parent;
501
- }
502
- }
503
- const lfIndex = string.indexOf("\n");
504
- if (lfIndex !== -1) {
505
- string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
506
- }
507
- return openAll + string + closeAll;
508
- };
509
- Object.defineProperties(createChalk.prototype, styles2);
510
- var chalk = createChalk();
511
- createChalk({ level: stderrColor ? stderrColor.level : 0 });
512
- var source_default = chalk;
513
- function log(message) {
514
- return process.stdout.write(`${message}
515
- `);
516
- }
517
- function logError(message) {
518
- return process.stderr.write(`${message}
519
- `);
520
- }
521
- function logBullet(logger2, colorizePrefix, colorizeText, symbol, prefix, text) {
522
- const textParts = Array.isArray(text) ? text : [text || ""].filter(Boolean);
523
- const formattedText = textParts.map((textPart) => colorizeText(textPart)).join("");
524
- logger2(
525
- `${" ".repeat(3)} ${colorizePrefix(symbol)} ${colorizePrefix(
526
- prefix
527
- )} ${formattedText}`
528
- );
529
- }
530
- function warn(prefix, text) {
531
- logBullet(log, source_default.yellow, source_default.yellow, "\u25CF", prefix, text);
532
- }
533
- function debug(prefix, text) {
534
- logBullet(log, source_default.yellow, source_default.dim, "\u25CF", prefix, text);
535
- }
536
- function info(prefix, text) {
537
- logBullet(log, source_default.cyan, source_default.dim, "\u25FC", prefix, text);
538
- }
539
- function success(text) {
540
- logBullet(log, source_default.green, source_default.dim, "\u2714", text);
541
- }
542
- function error(prefix, text) {
543
- log("");
544
- logBullet(logError, source_default.red, source_default.red, "\u25B2", prefix, text);
545
- }
546
- function heading(heading2, description) {
547
- return log(
548
- `
549
- ${source_default.bgWhite(` ${source_default.whiteBright(heading2)} `)} ${source_default.bold(
550
- description
551
- )}
552
- `
553
- );
554
- }
555
- var timing = (fn) => () => {
556
- const start = performance.now();
557
- return (msg) => {
558
- const end = performance.now();
559
- const ms = end - start;
560
- fn(`${msg} ${source_default.gray(`(${ms.toFixed(2)}ms)`)}`);
561
- };
562
- };
563
- var logger = {
564
- colors: source_default,
565
- log,
566
- heading,
567
- debug,
568
- info,
569
- warn,
570
- success,
571
- error,
572
- time: {
573
- success: timing(success),
574
- info: timing(info),
575
- debug: timing(debug)
576
- }
577
- };
578
-
579
- // src/commands/translate.ts
580
- import { translate } from "@terai/translator";
581
- import { merge, stringify as stringify2, groupDictionaryByChunks } from "@terai/utils";
582
- function createTranslateCommand(cli, cwd) {
583
- return cli.command("translate", "Translate messages from your project").option("--cwd <cwd>", "Current working directory", { default: cwd }).action(translateCmd);
584
- }
585
- async function translateCmd({ cwd }) {
586
- logger.heading("translate", "Translate messages from your project");
587
- const done = logger.time.success();
588
- const config = await loadConfig({ cwd });
589
- const outDirPath = runtime2.path.resolve(cwd, config.outDir);
590
- const tmuDirPath = runtime2.path.resolve(cwd, config.outDir, ".terai");
591
- const manifestPath = runtime2.path.resolve(tmuDirPath, "locale-manifest.json");
592
- const existsManifest = runtime2.fs.exists(manifestPath);
593
- if (!existsManifest) {
594
- return logger.error(
595
- "translate",
596
- `No locale-manifest.json found in ${tmuDirPath}. Please run "extract" first.`
597
- );
598
- }
599
- const buildManifest = JSON.parse(
600
- runtime2.fs.readFile(manifestPath)
601
- );
602
- let totalTranslatedMsgs = 0;
603
- let totalCachedMsgs = 0;
604
- await Promise.all(
605
- config.outLocales.map(async (locale) => {
606
- try {
607
- let translatedDictionary = {};
608
- const untranslatedDictionary = {};
609
- const localeDirPath = runtime2.path.resolve(outDirPath, locale);
610
- const localeCachePath = runtime2.path.resolve(
611
- tmuDirPath,
612
- "cache",
613
- `${locale}.cache.json`
614
- );
615
- const cacheExists = runtime2.fs.exists(localeCachePath);
616
- let cache = cacheExists ? JSON.parse(runtime2.fs.readFile(localeCachePath)) : {};
617
- let localeCachedMsgs = 0;
618
- let localeTranslatedMsgs = 0;
619
- for (const id in buildManifest.messages) {
620
- const { value, chunksIds } = buildManifest.messages[id];
621
- const chnks = chunksIds.length ? chunksIds : [locale];
622
- for (const chunkId of chnks) {
623
- const chunkPath = runtime2.path.resolve(
624
- localeDirPath,
625
- `${chunkId}.json`
626
- );
627
- const existsChunk = runtime2.fs.exists(chunkPath);
628
- const chunk = existsChunk ? JSON.parse(runtime2.fs.readFile(chunkPath)) : {};
629
- const localeValue = chunk[id] ?? cache[id];
630
- if (!localeValue) {
631
- untranslatedDictionary[id] = value;
632
- localeTranslatedMsgs++;
633
- } else {
634
- translatedDictionary[id] = localeValue;
635
- cache[id] = localeValue;
636
- localeCachedMsgs++;
637
- }
638
- }
639
- }
640
- if (localeTranslatedMsgs > 0) {
641
- const translation = await translate({
642
- ...config,
643
- dictionary: untranslatedDictionary,
644
- locale
645
- });
646
- translatedDictionary = merge(translation, translatedDictionary);
647
- }
648
- cache = {
649
- ...cache,
650
- ...translatedDictionary
651
- };
652
- runtime2.fs.remove(localeDirPath);
653
- const dictionaries = groupDictionaryByChunks(
654
- translatedDictionary,
655
- buildManifest,
656
- locale
657
- );
658
- await generate({
659
- ...config,
660
- dictionaries,
661
- locale,
662
- cwd
663
- });
664
- runtime2.fs.write(localeCachePath, stringify2(cache));
665
- totalTranslatedMsgs += localeTranslatedMsgs;
666
- totalCachedMsgs += localeCachedMsgs;
667
- logger.info(
668
- `Locale ${locale.toUpperCase()}:`,
669
- `${localeTranslatedMsgs} new messages translated, ${localeCachedMsgs} recovered from cache`
670
- );
671
- } catch (err) {
672
- if (err instanceof Error) {
673
- logger.error(
674
- `${locale} translation:`,
675
- `Error translating to ${locale}: ${err.message}
676
- `
677
- );
678
- }
679
- }
680
- })
681
- );
682
- if (!totalTranslatedMsgs) {
683
- return done("Everything up-to-date");
684
- }
685
- done(
686
- `Translated ${totalTranslatedMsgs} messages, ${totalCachedMsgs} read from cache`
687
- );
688
- }
689
- export {
690
- createTranslateCommand,
691
- translateCmd
692
- };
693
- //# sourceMappingURL=translate.mjs.map