import { createFailError } from '@kbn/dev-utils' ; import { I18nConfig , matchEntriesWithExctractors , normalizePath , readFileAsync , ErrorReporter , } from '..' ; function filterEntries ( entries , exclude ) { return entries . filter ( ( entry ) => exclude . every ( ( excludedPath ) => ! normalizePath ( entry ) . startsWith ( excludedPath ) ) ) ; } export async function extractUntrackedMessagesTask ( { path , config , reporter , } ) { const inputPaths = Array . isArray ( path ) ? path : [ path || './' ] ; const availablePaths = Object . values ( config . paths ) . flat ( ) ; const ignore = availablePaths . concat ( [ '**/build/**' , '**/__fixtures__/**' , '**/packages/kbn-i18n/**' , '**/packages/kbn-i18n-react/**' , '**/packages/kbn-plugin-generator/template/**' , '**/target/**' , '**/test/**' , '**/scripts/**' , '**/src/dev/**' , '**/target/**' , '**/dist/**' , ] ) ; for ( const inputPath of inputPaths ) { const categorizedEntries = await matchEntriesWithExctractors ( inputPath , { additionalIgnore : ignore , mark : true , absolute : true , } ) ; for ( const [ entries , extractFunction ] of categorizedEntries ) { const files = await Promise . all ( filterEntries ( entries , config . exclude ) . filter ( ( entry ) => { const normalizedEntry = normalizePath ( entry ) ; return ! availablePaths . some ( ( availablePath ) => normalizedEntry . startsWith ( `${ normalizePath ( availablePath ) }/` ) || normalizePath ( availablePath ) === normalizedEntry ) ; } ) . map ( async ( entry ) => ( { name : entry , content : await readFileAsync ( entry ) , } ) ) ) ; for ( const { name , content } of files ) { const reporterWithContext = reporter . withContext ( { name } ) ; for ( const [ id ] of extractFunction ( content , reporterWithContext ) ) { const errorMessage = `Untracked file contains i18n label (${ id }).` ; reporterWithContext . report ( createFailError ( errorMessage ) ) ; } } } } } export function extractUntrackedMessages ( inputPaths ) { return inputPaths . map ( ( inputPath ) => ( { title : `Checking untracked messages in ${ inputPath }` , task : async ( context ) => { const { reporter , config } = context ; const initialErrorsNumber = reporter . errors . length ; const result = await extractUntrackedMessagesTask ( { path : inputPath , config , reporter } ) ; if ( reporter . errors . length === initialErrorsNumber ) { return result ; } throw reporter ; } , } ) ) ; }