import path from 'path' ; import { cosmiconfig } from 'cosmiconfig' ; import { IConfigService , ISyncOptions , IDependencySection , ICLIArguments , } from './types' ; import { shrinkObject } from './util' ; const explorer = cosmiconfig ( 'typesync' ) ; export function createConfigService ( ) { return { readConfig : async ( filePath , flags ) => { const fileConfig = await explorer . search ( path . dirname ( filePath ) ) . then ( ( result ) => result ?. config ?? { } ) ; const cliConfig = readCliConfig ( flags ) ; return { ... shrinkObject ( fileConfig ) , ... shrinkObject ( cliConfig ) } ; } , } ; } function readCliConfig ( flags ) { const readValues = < T extends string > ( key , validator ? ) : T [ ] | undefined => { const values = flags [ key ] ; return typeof values === 'string' ? values . split ( ',' ) . filter ( ( value ) : value is T => ( validator ? validator ( value ) : true ) ) : undefined ; } ; return { ignoreDeps : readValues ( 'ignoredeps' , isIgnoreDepConfigValue ) , ignorePackages : readValues ( 'ignorepackages' ) , } ; } function isIgnoreDepConfigValue ( value ) { return Object . keys ( IDependencySection ) . includes ( value ) ; }