import { readFileSync } from 'fs' ; import path from 'path' ; import { ICfnFunctionContext } from './cfn-functions' ; import { OrgFormationError } from '~org-formation-error' ; export class CfnReadFile { static resolve ( context , resource , resourceParent , resourceKey , key , val ) { if ( key === 'Fn::ReadFile' ) { if ( typeof val !== 'string' ) { if ( ! context . finalPass ) { return ; } throw new OrgFormationError ( `Fn::ReadFile expression expects a string as value. Found ${ typeof val }` ) ; } const resolved = CfnReadFile . readFile ( context . filePath , val ) ; resourceParent [ resourceKey ] = resolved ; } } static readFile ( contextPath , filePath ) { const dir = path . dirname ( contextPath ) ; const resolvedFilePath = path . resolve ( dir , filePath ) ; return readFileSync ( resolvedFilePath ) . toString ( 'utf-8' ) ; } }