import { path , existsSync , readFileSync } from './file-methods' ; const _BOM = /^\uFEFF/ ; import EtaErr from './err' ; import type { EtaConfig } from './config' ; param name param parentfile param isDirectory return function getWholeFilePath ( name , parentfile , isDirectory ? ) { const includePath = path . resolve ( isDirectory ? parentfile : path . dirname ( parentfile ) , name ) + ( path . extname ( name ) ? '' : '.eta' ) ; return includePath ; } param path param options return function getPath ( path , options ) { let includePath = false ; const views = options . views ; let searchedPaths = [ ] ; const pathOptions = JSON . stringify ( { filename : options . filename , path : path , root : options . root , views : options . views } ) ; if ( options . cache && options . filepathCache && options . filepathCache [ pathOptions ] ) { return options . filepathCache [ pathOptions ] ; } /** Add a filepath to the list of paths we've checked for a template */ function addPathToSearched ( pathSearched ) { if ( ! searchedPaths . includes ( pathSearched ) ) { searchedPaths . push ( pathSearched ) ; } } param views param path function searchViews ( views , path ) { let filePath ; if ( Array . isArray ( views ) && views . some ( function ( v ) { filePath = getWholeFilePath ( path , v , true ) ; addPathToSearched ( filePath ) ; return existsSync ( filePath ) ; } ) ) { return ( filePath as unknown ) as string ; } else if ( typeof views === 'string' ) { filePath = getWholeFilePath ( path , views , true ) ; addPathToSearched ( filePath ) ; if ( existsSync ( filePath ) ) { return filePath ; } } return false ; } const match = /^[A-Za-z]+:\\|^\// . exec ( path ) ; if ( match && match . length ) { const formattedPath = path . replace ( /^\/*/ , '' ) ; includePath = searchViews ( views , formattedPath ) ; if ( ! includePath ) { const pathFromRoot = getWholeFilePath ( formattedPath , options . root || '/' , true ) ; addPathToSearched ( pathFromRoot ) ; includePath = pathFromRoot ; } } else { if ( options . filename ) { const filePath = getWholeFilePath ( path , options . filename ) ; addPathToSearched ( filePath ) ; if ( existsSync ( filePath ) ) { includePath = filePath ; } } if ( ! includePath ) { includePath = searchViews ( views , path ) ; } if ( ! includePath ) { throw EtaErr ( 'Could not find the template "' + path + '". Paths tried: ' + searchedPaths ) ; } } if ( options . cache && options . filepathCache ) { options . filepathCache [ pathOptions ] = includePath ; } return includePath ; } /**
 * Reads a file synchronously
 */ function readFile ( filePath ) { try { return readFileSync ( filePath ) . toString ( ) . replace ( _BOM , '' ) ; } catch { throw EtaErr ( "Failed to read template at '" + filePath + "'" ) ; } } export { getPath , readFile } ;