@@ -5,6 +5,9 @@ import iso6391, { LanguageCode } from 'iso-639-1';
55import path from 'path' ;
66import fs from 'fs-extra' ;
77import chokidar from 'chokidar' ;
8+ import { AsyncQueue } from '@sapphire/async-queue' ;
9+
10+ const processFrontendMessagesQueue = new AsyncQueue ( ) ;
811
912const SLAVIC_PLURAL_EXAMPLES = {
1013 uk : 'яблук | Яблуко | Яблука | Яблук' , // zero | singular | 2-4 | 5+
524527 }
525528
526529 async processExtractedMessages ( adminforth : IAdminForth , filePath : string ) {
530+ await processFrontendMessagesQueue . wait ( ) ;
527531 // messages file is in i18n-messages.json
528532 let messages ;
529533 try {
535539 return ;
536540 }
537541 // 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 ) => {
539551 const key = missingKey . path ;
540552 const file = missingKey . file ;
541553 const category = 'frontend' ;
567579 const serveDir = adminforth . codeInjector . getServeDir ( ) ;
568580 // messages file is in i18n-messages.json
569581 const messagesFile = path . join ( serveDir , 'i18n-messages.json' ) ;
570- console . log ( '🪲messagesFile' , messagesFile ) ;
582+ process . env . HEAVY_DEBUG && console . log ( '🪲🔔 messagesFile read started ' , messagesFile ) ;
571583 this . processExtractedMessages ( adminforth , messagesFile ) ;
572584 // 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
573585 // 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+ } ) ;
575590 w . on ( 'change' , ( ) => {
591+ process . env . HEAVY_DEBUG && console . log ( '🪲🔔messagesFile change' , messagesFile ) ;
592+
576593 this . processExtractedMessages ( adminforth , messagesFile ) ;
577594 } ) ;
578595 w . on ( 'add' , ( ) => {
596+ process . env . HEAVY_DEBUG && console . log ( '🪲🔔messagesFile add' , messagesFile ) ;
597+
579598 this . processExtractedMessages ( adminforth , messagesFile ) ;
580599 } ) ;
581600
0 commit comments