import { resolve } from 'path' ; import { normalizePath , readFileAsync } from '.' ; export interface I18nConfig { paths : Record < string , string [ ] > ; exclude : string [ ] ; translations : string [ ] ; prefix ? : string ; } export async function checkConfigNamespacePrefix ( configPath ) { const { prefix , paths } = JSON . parse ( await readFileAsync ( resolve ( configPath ) ) ) ; for ( const [ namespace ] of Object . entries ( paths ) ) { if ( prefix && prefix !== namespace . split ( '.' ) [ 0 ] ) { throw new Error ( `namespace ${ namespace } must be prefixed with ${ prefix } in ${ configPath }` ) ; } } } export async function assignConfigFromPath ( config = { exclude : [ ] , translations : [ ] , paths : { } } , configPath ) { const additionalConfig = { paths : { } , exclude : [ ] , translations : [ ] , ... JSON . parse ( await readFileAsync ( resolve ( configPath ) ) ) , } ; for ( const [ namespace , namespacePaths ] of Object . entries ( additionalConfig . paths ) ) { const paths = Array . isArray ( namespacePaths ) ? namespacePaths : [ namespacePaths ] ; config . paths [ namespace ] = paths . map ( ( path ) => normalizePath ( resolve ( configPath , '..' , path ) ) ) ; } for ( const exclude of additionalConfig . exclude ) { config . exclude . push ( normalizePath ( resolve ( configPath , '..' , exclude ) ) ) ; } for ( const translations of additionalConfig . translations ) { config . translations . push ( normalizePath ( resolve ( configPath , '..' , translations ) ) ) ; } return config ; } param inputPaths param config export function filterConfigPaths ( inputPaths , config ) { const availablePaths = Object . values ( config . paths ) . flat ( ) ; const pathsForExtraction = new Set ( ) ; for ( const inputPath of inputPaths ) { const normalizedPath = normalizePath ( inputPath ) ; if ( availablePaths . some ( ( path ) => normalizedPath . startsWith ( `${ path }/` ) || path === normalizedPath ) ) { pathsForExtraction . add ( normalizedPath ) ; } else { availablePaths . filter ( ( path ) => ! normalizedPath || path . startsWith ( `${ normalizedPath }/` ) ) . forEach ( ( ePath ) => pathsForExtraction . add ( ePath ) ) ; } } return [ ... pathsForExtraction ] ; }