@@ -227,12 +227,19 @@ const readFile = async (path: string): Promise<string | undefined> => {
227227 catch { }
228228} ;
229229
230+ const manifestFileCache = new Map < string , Record < string , any > | null > ( ) ;
231+
230232export class Thumbnail implements ThumbnailProvider {
231- public static async create ( workspaceDir : string , registryStoreDir : string ) : Promise < Thumbnail | undefined > {
232- const depsQuery = new DepsQuery ( registryStoreDir ) ;
233+ public static clearCache ( ) : void {
234+ manifestFileCache . clear ( ) ;
235+ }
236+
237+ public static async create ( workspaceDir : string , registryStoreDir : string , lang : string ) : Promise < Thumbnail | undefined > {
238+ const depsQuery = new DepsQuery ( registryStoreDir , lang ) ;
233239 const wsPkgData = await PkgData . createWS (
234240 depsQuery ,
235241 workspaceDir ,
242+ lang ,
236243 ) ;
237244 if ( wsPkgData ) {
238245 return new Thumbnail ( wsPkgData ) ;
@@ -462,7 +469,7 @@ export class Thumbnail implements ThumbnailProvider {
462469class DepsQuery {
463470 private cache : Map < string , PkgData | null | Promise < PkgData | undefined > > = new Map ( ) ;
464471
465- public constructor ( public readonly searchPath : string ) { }
472+ public constructor ( public readonly searchPath : string , public readonly lang : string ) { }
466473
467474 public async getPkgData ( packageName : string , packageVersion : string ) : Promise < PkgData | undefined > {
468475 const packageId = `${ packageName } -${ packageVersion } ` ;
@@ -475,7 +482,7 @@ class DepsQuery {
475482 return pkgData ;
476483 }
477484 }
478- const p = PkgData . create ( this , packageName , packageVersion , path . join ( this . searchPath , packageId ) ) ;
485+ const p = PkgData . create ( this , packageName , packageVersion , path . join ( this . searchPath , packageId ) , this . lang ) ;
479486 this . cache . set ( packageId , p ) ;
480487 const pkgData = await p ;
481488 this . cache . set ( packageId , pkgData || null ) ;
@@ -484,24 +491,30 @@ class DepsQuery {
484491}
485492
486493class PkgData {
487- public static async createWS ( depsQuery : DepsQuery , workspaceDir : string ) : Promise < PkgData | undefined > {
494+ public static async createWS ( depsQuery : DepsQuery , workspaceDir : string , lang : string ) : Promise < PkgData | undefined > {
488495 const packagePath = await resolveManifest ( workspaceDir , "package" ) ;
489496 if ( packagePath ) {
490497 const data = await readManifestFile ( packagePath ) ;
491- const localePath = await resolveLocaleFile ( workspaceDir ) ;
498+ const localePath = await resolveLocaleFile ( workspaceDir , lang ) ;
492499 const userLocale = localePath ? await readJSONFile ( localePath ) : undefined ;
500+ if ( ! userLocale && lang && lang !== "en" ) {
501+ return ;
502+ }
493503 if ( data && data . name && data . version ) {
494504 return new PkgData ( depsQuery , data . name , data . version , workspaceDir , packagePath , data , userLocale ) ;
495505 }
496506 }
497507 }
498508
499- public static async create ( depsQuery : DepsQuery , packageName : string , packageVersion : string , packageDir : string ) : Promise < PkgData | undefined > {
509+ public static async create ( depsQuery : DepsQuery , packageName : string , packageVersion : string , packageDir : string , lang : string ) : Promise < PkgData | undefined > {
500510 const packagePath = await resolveManifest ( packageDir , "package" ) ;
501511 if ( packagePath ) {
502- const data = readManifestFile ( packagePath ) ;
503- const localePath = await resolveLocaleFile ( packageDir ) ;
512+ const data = await readManifestFile ( packagePath ) ;
513+ const localePath = await resolveLocaleFile ( packageDir , lang ) ;
504514 const userLocale = localePath ? await readJSONFile ( localePath ) : undefined ;
515+ if ( ! userLocale && lang && lang !== "en" ) {
516+ return ;
517+ }
505518 if ( data ) {
506519 return new PkgData ( depsQuery , packageName , packageVersion , packageDir , packagePath , data , userLocale ) ;
507520 }
@@ -712,8 +725,8 @@ class SubflowBlockData implements SharedBlockData, FlowLikeData {
712725 }
713726}
714727
715- async function resolveLocaleFile ( dirPath : string ) : Promise < string | undefined > {
716- const filePath = path . join ( dirPath , "oo-locales/en .json" ) ;
728+ async function resolveLocaleFile ( dirPath : string , lang ?: string ) : Promise < string | undefined > {
729+ const filePath = path . join ( dirPath , "oo-locales" , ` ${ lang || "en" } .json` ) ;
717730 if ( await isFile ( filePath ) ) {
718731 return filePath ;
719732 }
@@ -731,16 +744,22 @@ async function resolveManifest(dirPath: string, type: string): Promise<string |
731744}
732745
733746async function readManifestFile ( manifestPath : string ) : Promise < Record < string , any > | undefined > {
747+ if ( manifestFileCache . has ( manifestPath ) ) {
748+ return manifestFileCache . get ( manifestPath ) || undefined ;
749+ }
734750 try {
735751 const content = await readFile ( manifestPath ) ;
736752 if ( content ) {
737753 const data = YAML . parse ( content ) ;
738754 if ( isPlainObject ( data ) ) {
755+ manifestFileCache . set ( manifestPath , data ) ;
739756 return data ;
740757 }
741758 }
759+ manifestFileCache . set ( manifestPath , null ) ;
742760 }
743761 catch {
762+ manifestFileCache . set ( manifestPath , null ) ;
744763 return undefined ;
745764 }
746765}
0 commit comments