import EtaErr from './err' ; import compile from './compile' ; import { getConfig } from './config' ; import { getPath , readFile } from './file-utils' ; import { copyProps } from './utils' ; import { promiseImpl } from './polyfills' ; import type { EtaConfig , PartialConfig , EtaConfigWithFilename } from './config' ; import type { TemplateFunction } from './compile' ; export type CallbackFn = ( err : Error | null , str ? : string ) => void ; interface DataObj { /** Express.js settings may be stored here */ settings ? : { [ key ] : any ; } ; [ key ] : any ; } interface PartialConfigWithFilename extends Partial < EtaConfig > { filename : string ; } param filePath param options param noCache export function loadFile ( filePath , options , noCache ? ) { const config = getConfig ( options ) ; const template = readFile ( filePath ) ; try { const compiledTemplate = compile ( template , config ) ; if ( ! noCache ) { config . templates . define ( ( config as EtaConfigWithFilename ) . filename , compiledTemplate ) ; } return compiledTemplate ; } catch ( e ) { throw EtaErr ( 'Loading file: ' + filePath + ' failed:\n\n' + e . message ) ; } } param options return function handleCache ( options ) { const filename = options . filename ; if ( options . cache ) { const func = options . templates . get ( filename ) ; if ( func ) { return func ; } return loadFile ( filename , options ) ; } return loadFile ( filename , options , true ) ; } param data param options param cb function tryHandleCache ( data , options , cb ) { if ( cb ) { try { const templateFn = handleCache ( options ) ; templateFn ( data , options , cb ) ; } catch ( err ) { return cb ( err ) ; } } else { if ( typeof promiseImpl === 'function' ) { return new promiseImpl < string > ( function ( resolve , reject ) { try { const templateFn = handleCache ( options ) ; const result = templateFn ( data , options ) ; resolve ( result ) ; } catch ( err ) { reject ( err ) ; } } ) ; } else { throw EtaErr ( "Please provide a callback function, this env doesn't support Promises" ) ; } } } remarks param path param options return function includeFile ( path , options ) { const newFileOptions = getConfig ( { filename : getPath ( path , options ) } , options ) ; return [ handleCache ( newFileOptions as EtaConfigWithFilename ) , newFileOptions ] ; } param filepath function renderFile ( filename , data , config ? , cb ? ) ; function renderFile ( filename , data , cb ? ) ; function renderFile ( filename , data , config ? , cb ? ) { let renderConfig ; let callback ; data = data || { } ; if ( typeof cb === 'function' ) { callback = cb ; } else if ( typeof config === 'function' ) { callback = config ; } if ( typeof config === 'object' ) { renderConfig = getConfig ( ( config as PartialConfig ) || { } ) as EtaConfigWithFilename ; } else { renderConfig = getConfig ( data as PartialConfig ) as EtaConfigWithFilename ; if ( data . settings ) { if ( data . settings . views ) { renderConfig . views = data . settings . views ; } if ( data . settings [ 'view cache' ] ) { renderConfig . cache = true ; } const viewOpts = data . settings [ 'view options' ] ; if ( viewOpts ) { copyProps ( renderConfig , viewOpts ) ; } } } renderConfig . filename = getPath ( filename , renderConfig ) ; return tryHandleCache ( data , renderConfig , callback ) ; } param filepath function renderFileAsync ( filename , data , config ? , cb ? ) ; function renderFileAsync ( filename , data , cb ? ) ; function renderFileAsync ( filename , data , config ? , cb ? ) { return renderFile ( filename , typeof config === 'function' ? { ... data , async : true } : data , typeof config === 'object' ? { ... config , async : true } : config , cb ) ; } export { includeFile , renderFile , renderFileAsync } ;