import os from 'os' ; import { Explorer } from './Explorer' ; import { ExplorerSync } from './ExplorerSync' ; import { loaders } from './loaders' ; import { Config , CosmiconfigResult , ExplorerOptions , ExplorerOptionsSync , Loaders , LoadersSync , } from './types' ; type LoaderResult = Config | null ; export type Loader = ( ( filepath : string , content : string ) => Promise < LoaderResult > ) | LoaderSync ; export type LoaderSync = ( filepath : string , content : string ) => LoaderResult ; export type Transform = ( ( CosmiconfigResult : CosmiconfigResult ) => Promise < CosmiconfigResult > ) | TransformSync ; export type TransformSync = ( CosmiconfigResult : CosmiconfigResult ) => CosmiconfigResult ; interface OptionsBase { packageProp ? : string | Array < string > ; searchPlaces ? : Array < string > ; ignoreEmptySearchPlaces ? : boolean ; stopDir ? : string ; cache ? : boolean ; } export interface Options extends OptionsBase { loaders ? : Loaders ; transform ? : Transform ; } export interface OptionsSync extends OptionsBase { loaders ? : LoadersSync ; transform ? : TransformSync ; } function cosmiconfig ( moduleName , options = { } ) { const normalizedOptions = normalizeOptions ( moduleName , options ) ; const explorer = new Explorer ( normalizedOptions ) ; return { search : explorer . search . bind ( explorer ) , load : explorer . load . bind ( explorer ) , clearLoadCache : explorer . clearLoadCache . bind ( explorer ) , clearSearchCache : explorer . clearSearchCache . bind ( explorer ) , clearCaches : explorer . clearCaches . bind ( explorer ) , } as const ; } function cosmiconfigSync ( moduleName , options = { } ) { const normalizedOptions = normalizeOptions ( moduleName , options ) ; const explorerSync = new ExplorerSync ( normalizedOptions ) ; return { search : explorerSync . searchSync . bind ( explorerSync ) , load : explorerSync . loadSync . bind ( explorerSync ) , clearLoadCache : explorerSync . clearLoadCache . bind ( explorerSync ) , clearSearchCache : explorerSync . clearSearchCache . bind ( explorerSync ) , clearCaches : explorerSync . clearCaches . bind ( explorerSync ) , } as const ; } const defaultLoaders = Object . freeze ( { '.cjs' : loaders . loadJs , '.js' : loaders . loadJs , '.json' : loaders . loadJson , '.yaml' : loaders . loadYaml , '.yml' : loaders . loadYaml , noExt : loaders . loadYaml , } as const ) ; const identity = function identity ( x ) { return x ; } ; function normalizeOptions ( moduleName , options ) ; function normalizeOptions ( moduleName , options ) ; function normalizeOptions ( moduleName , options ) { const defaults = { packageProp : moduleName , searchPlaces : [ 'package.json' , `.${ moduleName }rc` , `.${ moduleName }rc.json` , `.${ moduleName }rc.yaml` , `.${ moduleName }rc.yml` , `.${ moduleName }rc.js` , `.${ moduleName }rc.cjs` , `${ moduleName }.config.js` , `${ moduleName }.config.cjs` , ] , ignoreEmptySearchPlaces : true , stopDir : os . homedir ( ) , cache : true , transform : identity , loaders : defaultLoaders , } ; const normalizedOptions = { ... defaults , ... options , loaders : { ... defaults . loaders , ... options . loaders , } , } ; return normalizedOptions ; } export { cosmiconfig , cosmiconfigSync , defaultLoaders } ;