import DEFAULT_CONFIG from './config.json' ; import { SchemaConfig } from '@phensley/cldr-core' ; const LIM = 10 ; const leftpad = ( s , n ) => `${ ' ' . repeat ( Math . max ( 0 , n - s . length ) ) }'${ s }'` ; const quote = ( s ) => s . map ( ( v ) => `'${ v }'` ) . join ( ', ' ) ; const quotevals = ( s ) => ( s . length < LIM ? quote ( s ) : quote ( s . slice ( 0 , LIM ) ) + ' ...' ) ; /**
 * Compare custom config against canonical and emit messages.
 */ export const validateConfig = ( config ) : SchemaConfig => { console . log ( `[validate] Validating schema config:` ) ; const res = { } ; let fail = false ; const keys = Object . keys ( config ) . sort ( ) ; const maxlen = keys . reduce ( ( p , c ) => Math . max ( p , c . length ) , 0 ) ; const missing = Object . keys ( DEFAULT_CONFIG ) . filter ( ( k ) => keys . indexOf ( k ) === - 1 ) ; const nonarray = [ ] ; const ignore = [ ] ; for ( const key of keys ) { const canonical = DEFAULT_CONFIG [ key as keyof SchemaConfig ] ; if ( canonical === undefined ) { ignore . push ( key ) ; continue ; } const padkey = leftpad ( key , maxlen ) ; const valid = new Set ( canonical ) ; const keep = [ ] ; const unk = [ ] ; const vals = config [ key as keyof SchemaConfig ] as string [ ] ; if ( ! Array . isArray ( vals ) ) { nonarray . push ( key ) ; continue ; } for ( const val of vals ) { if ( ! valid . has ( val ) ) { unk . push ( val ) ; } else { keep . push ( val ) ; } } const kept = new Set ( keep ) ; const excluded = [ ] ; for ( const val of canonical ) { if ( ! kept . has ( val ) ) { excluded . push ( val ) ; } } if ( excluded . length ) { console . log ( `[validate] ${ padkey } excluded: ${ quotevals ( excluded ) }` ) ; } else { console . log ( `[validate] ${ padkey } complete.` ) ; } if ( unk . length ) { console . log ( `[validate] ${ padkey }  invalid: ${ quote ( unk ) }` ) ; } res [ key as keyof SchemaConfig ] = keep ; } if ( ignore . length ) { console . log ( `[validate] Ignoring unknown keys: ${ quote ( ignore ) }` ) ; } console . log ( ) ; if ( missing . length ) { console . log ( `[validate] WARN: config is missing keys: ${ quote ( missing ) }` ) ; } if ( nonarray . length ) { console . log ( `[validate] FATAL: keys not pointing to arrays: ${ quote ( nonarray ) }` ) ; fail = true ; } if ( fail ) { console . log ( `[validate] Failed validation, aborting.` ) ; process . exit ( 1 ) ; } return res ; } ;