1- import type { Locale } from '@lunariajs/core'
21import type { LocaleObject } from '@nuxtjs/i18n'
32import * as path from 'node:path'
43import * as fs from 'node:fs/promises'
@@ -8,18 +7,29 @@ import { deepCopy } from '@intlify/shared'
87const destFolder = path . resolve ( 'lunaria/files' )
98const localesFolder = path . resolve ( 'i18n/locales' )
109
11- const defaultLocale = currentLocales . find ( l => l . code === 'en-US' ) !
10+ const defaultLocale = currentLocales . find ( l => l . code === 'en-US' )
11+ if ( ! defaultLocale ?. name ) {
12+ throw new Error ( 'Default locale en-US not found or has no name' )
13+ }
1214export { lunariaJSONFiles }
1315export const sourceLocale = {
1416 label : defaultLocale . name ,
1517 lang : defaultLocale . code ,
1618}
17- export const locales : Locale [ ] = currentLocales
18- . filter ( l => l . code !== 'en-US' )
19- . map ( l => ( {
19+ const filteredLocales = currentLocales . filter (
20+ ( l ) : l is typeof l & { name : string } => l . code !== 'en-US' && typeof l . name === 'string' ,
21+ )
22+ const firstLocale = filteredLocales [ 0 ]
23+ if ( ! firstLocale ) {
24+ throw new Error ( 'No locales found besides en-US' )
25+ }
26+ export const locales : [ { label : string ; lang : string } , ...{ label : string ; lang : string } [ ] ] = [
27+ { label : firstLocale . name , lang : firstLocale . code } ,
28+ ...filteredLocales . slice ( 1 ) . map ( l => ( {
2029 label : l . name ,
2130 lang : l . code ,
22- } ) )
31+ } ) ) ,
32+ ]
2333
2434export async function prepareJsonFiles ( ) {
2535 await fs . rm ( destFolder , { recursive : true , force : true } )
@@ -31,17 +41,27 @@ async function loadJsonFile(name: string) {
3141 return JSON . parse ( await fs . readFile ( path . resolve ( `${ localesFolder } /${ name } ` ) , 'utf8' ) )
3242}
3343
44+ function getFileName ( file : string | { path : string } ) : string {
45+ return typeof file === 'string' ? file : file . path
46+ }
47+
3448async function mergeLocale ( locale : LocaleObject ) {
35- if ( locale . file || locale . files . length === 1 ) {
36- const json = locale . file || locale . files [ 0 ]
49+ const files = locale . files ?? [ ]
50+ if ( locale . file || files . length === 1 ) {
51+ const json = locale . file ?? ( files [ 0 ] ? getFileName ( files [ 0 ] ) : undefined )
52+ if ( ! json ) return
3753 await fs . cp ( path . resolve ( `${ localesFolder } /${ json } ` ) , path . resolve ( `${ destFolder } /${ json } ` ) )
3854 return
3955 }
4056
41- const source = await loadJsonFile ( locale . files [ 0 ] as string )
57+ const firstFile = files [ 0 ]
58+ if ( ! firstFile ) return
59+ const source = await loadJsonFile ( getFileName ( firstFile ) )
4260 let currentSource : unknown
43- for ( let i = 1 ; i < locale . files . length ; i ++ ) {
44- currentSource = await loadJsonFile ( locale . files [ i ] as string )
61+ for ( let i = 1 ; i < files . length ; i ++ ) {
62+ const file = files [ i ]
63+ if ( ! file ) continue
64+ currentSource = await loadJsonFile ( getFileName ( file ) )
4565 deepCopy ( currentSource , source )
4666 }
4767
0 commit comments