@adminforth/i18n 1.0.12 → 1.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.
package/Changelog.md CHANGED
@@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+
9
+ ## [1.0.14]
10
+
11
+ ### Fixed
12
+
13
+ - Add `ignoreInitial` for watch to prevent initial messages loading
14
+ - Add locking mechanism to prevent initial messages loading call in parallel (just in case)
15
+
16
+ ## [1.0.13]
17
+
18
+ - Deduplicate frontend strings before creating translations
19
+
20
+
8
21
  ## [1.0.12]
9
22
 
10
23
  ### Fixed
package/dist/index.js CHANGED
@@ -12,6 +12,8 @@ import iso6391 from 'iso-639-1';
12
12
  import path from 'path';
13
13
  import fs from 'fs-extra';
14
14
  import chokidar from 'chokidar';
15
+ import { AsyncQueue } from '@sapphire/async-queue';
16
+ const processFrontendMessagesQueue = new AsyncQueue();
15
17
  const SLAVIC_PLURAL_EXAMPLES = {
16
18
  uk: 'яблук | Яблуко | Яблука | Яблук', // zero | singular | 2-4 | 5+
17
19
  bg: 'ябълки | ябълка | ябълки | ябълки', // zero | singular | 2-4 | 5+
@@ -433,6 +435,7 @@ ${JSON.stringify(strings.reduce((acc, s) => {
433
435
  }
434
436
  processExtractedMessages(adminforth, filePath) {
435
437
  return __awaiter(this, void 0, void 0, function* () {
438
+ yield processFrontendMessagesQueue.wait();
436
439
  // messages file is in i18n-messages.json
437
440
  let messages;
438
441
  try {
@@ -444,7 +447,13 @@ ${JSON.stringify(strings.reduce((acc, s) => {
444
447
  return;
445
448
  }
446
449
  // loop over missingKeys[i].path and add them to database if not exists
447
- yield Promise.all(messages.missingKeys.map((missingKey) => __awaiter(this, void 0, void 0, function* () {
450
+ const missingKeysDeduplicated = messages.missingKeys.reduce((acc, missingKey) => {
451
+ if (!acc.find((a) => a.path === missingKey.path)) {
452
+ acc.push(missingKey);
453
+ }
454
+ return acc;
455
+ }, []);
456
+ yield Promise.all(missingKeysDeduplicated.map((missingKey) => __awaiter(this, void 0, void 0, function* () {
448
457
  const key = missingKey.path;
449
458
  const file = missingKey.file;
450
459
  const category = 'frontend';
@@ -472,15 +481,20 @@ ${JSON.stringify(strings.reduce((acc, s) => {
472
481
  const serveDir = adminforth.codeInjector.getServeDir();
473
482
  // messages file is in i18n-messages.json
474
483
  const messagesFile = path.join(serveDir, 'i18n-messages.json');
475
- console.log('🪲messagesFile', messagesFile);
484
+ process.env.HEAVY_DEBUG && console.log('🪲🔔messagesFile read started', messagesFile);
476
485
  this.processExtractedMessages(adminforth, messagesFile);
477
486
  // we use watcher because file can't be yet created when we start - bundleNow can be done in build time or can be done now
478
487
  // that is why we make attempt to process it now and then watch for changes
479
- const w = chokidar.watch(messagesFile, { persistent: true });
488
+ const w = chokidar.watch(messagesFile, {
489
+ persistent: true,
490
+ ignoreInitial: true, // don't trigger 'add' event for existing file on start
491
+ });
480
492
  w.on('change', () => {
493
+ process.env.HEAVY_DEBUG && console.log('🪲🔔messagesFile change', messagesFile);
481
494
  this.processExtractedMessages(adminforth, messagesFile);
482
495
  });
483
496
  w.on('add', () => {
497
+ process.env.HEAVY_DEBUG && console.log('🪲🔔messagesFile add', messagesFile);
484
498
  this.processExtractedMessages(adminforth, messagesFile);
485
499
  });
486
500
  });
package/index.ts CHANGED
@@ -5,6 +5,9 @@ import iso6391, { LanguageCode } from 'iso-639-1';
5
5
  import path from 'path';
6
6
  import fs from 'fs-extra';
7
7
  import chokidar from 'chokidar';
8
+ import { AsyncQueue } from '@sapphire/async-queue';
9
+
10
+ const processFrontendMessagesQueue = new AsyncQueue();
8
11
 
9
12
  const SLAVIC_PLURAL_EXAMPLES = {
10
13
  uk: 'яблук | Яблуко | Яблука | Яблук', // zero | singular | 2-4 | 5+
@@ -524,6 +527,7 @@ ${
524
527
  }
525
528
 
526
529
  async processExtractedMessages(adminforth: IAdminForth, filePath: string) {
530
+ await processFrontendMessagesQueue.wait();
527
531
  // messages file is in i18n-messages.json
528
532
  let messages;
529
533
  try {
@@ -535,7 +539,15 @@ ${
535
539
  return;
536
540
  }
537
541
  // loop over missingKeys[i].path and add them to database if not exists
538
- await Promise.all(messages.missingKeys.map(async (missingKey: any) => {
542
+
543
+ const missingKeysDeduplicated = messages.missingKeys.reduce((acc: any[], missingKey: any) => {
544
+ if (!acc.find((a) => a.path === missingKey.path)) {
545
+ acc.push(missingKey);
546
+ }
547
+ return acc;
548
+ }, []);
549
+
550
+ await Promise.all(missingKeysDeduplicated.map(async (missingKey: any) => {
539
551
  const key = missingKey.path;
540
552
  const file = missingKey.file;
541
553
  const category = 'frontend';
@@ -567,15 +579,22 @@ ${
567
579
  const serveDir = adminforth.codeInjector.getServeDir();
568
580
  // messages file is in i18n-messages.json
569
581
  const messagesFile = path.join(serveDir, 'i18n-messages.json');
570
- console.log('🪲messagesFile', messagesFile);
582
+ process.env.HEAVY_DEBUG && console.log('🪲🔔messagesFile read started', messagesFile);
571
583
  this.processExtractedMessages(adminforth, messagesFile);
572
584
  // we use watcher because file can't be yet created when we start - bundleNow can be done in build time or can be done now
573
585
  // that is why we make attempt to process it now and then watch for changes
574
- const w = chokidar.watch(messagesFile, { persistent: true });
586
+ const w = chokidar.watch(messagesFile, {
587
+ persistent: true,
588
+ ignoreInitial: true, // don't trigger 'add' event for existing file on start
589
+ });
575
590
  w.on('change', () => {
591
+ process.env.HEAVY_DEBUG && console.log('🪲🔔messagesFile change', messagesFile);
592
+
576
593
  this.processExtractedMessages(adminforth, messagesFile);
577
594
  });
578
595
  w.on('add', () => {
596
+ process.env.HEAVY_DEBUG && console.log('🪲🔔messagesFile add', messagesFile);
597
+
579
598
  this.processExtractedMessages(adminforth, messagesFile);
580
599
  });
581
600
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/i18n",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -15,6 +15,7 @@
15
15
  "description": "",
16
16
  "dependencies": {
17
17
  "@aws-sdk/client-ses": "^3.654.0",
18
+ "@sapphire/async-queue": "^1.5.5",
18
19
  "chokidar": "^4.0.1",
19
20
  "iso-639-1": "^3.1.3"
20
21
  },