import path from 'path' ; import { ExplorerBase } from './ExplorerBase' ; import { readFileSync } from './readFile' ; import { cacheWrapperSync } from './cacheWrapper' ; import { getDirectorySync } from './getDirectory' ; import { CosmiconfigResult , ExplorerOptionsSync , LoadedFileContent , } from './types' ; class ExplorerSync extends ExplorerBase < ExplorerOptionsSync > { public constructor ( options ) { super ( options ) ; } public searchSync ( searchFrom = process . cwd ( ) ) { const startDirectory = getDirectorySync ( searchFrom ) ; const result = this . searchFromDirectorySync ( startDirectory ) ; return result ; } private searchFromDirectorySync ( dir ) { const absoluteDir = path . resolve ( process . cwd ( ) , dir ) ; const run = ( ) : CosmiconfigResult => { const result = this . searchDirectorySync ( absoluteDir ) ; const nextDir = this . nextDirectoryToSearch ( absoluteDir , result ) ; if ( nextDir ) { return this . searchFromDirectorySync ( nextDir ) ; } const transformResult = this . config . transform ( result ) ; return transformResult ; } ; if ( this . searchCache ) { return cacheWrapperSync ( this . searchCache , absoluteDir , run ) ; } return run ( ) ; } private searchDirectorySync ( dir ) { for ( const place of this . config . searchPlaces ) { const placeResult = this . loadSearchPlaceSync ( dir , place ) ; if ( this . shouldSearchStopWithResult ( placeResult ) === true ) { return placeResult ; } } return null ; } private loadSearchPlaceSync ( dir , place ) { const filepath = path . join ( dir , place ) ; const content = readFileSync ( filepath ) ; const result = this . createCosmiconfigResultSync ( filepath , content ) ; return result ; } private loadFileContentSync ( filepath , content ) { if ( content === null ) { return null ; } if ( content . trim ( ) === '' ) { return undefined ; } const loader = this . getLoaderEntryForFile ( filepath ) ; const loaderResult = loader ( filepath , content ) ; return loaderResult ; } private createCosmiconfigResultSync ( filepath , content ) { const fileContent = this . loadFileContentSync ( filepath , content ) ; const result = this . loadedContentToCosmiconfigResult ( filepath , fileContent ) ; return result ; } public loadSync ( filepath ) { this . validateFilePath ( filepath ) ; const absoluteFilePath = path . resolve ( process . cwd ( ) , filepath ) ; const runLoadSync = ( ) : CosmiconfigResult => { const content = readFileSync ( absoluteFilePath , { throwNotFound : true } ) ; const cosmiconfigResult = this . createCosmiconfigResultSync ( absoluteFilePath , content ) ; const transformResult = this . config . transform ( cosmiconfigResult ) ; return transformResult ; } ; if ( this . loadCache ) { return cacheWrapperSync ( this . loadCache , absoluteFilePath , runLoadSync ) ; } return runLoadSync ( ) ; } } export { ExplorerSync } ;