1- import { readFileSync } from 'fs' ;
21import { resolve , dirname , isAbsolute } from 'path' ;
3- import { fileURLToPath , pathToFileURL } from 'url' ;
4- import { load } from 'js-yaml ' ;
5- import { TopLevelSection , IntrinsicFunction } from '../context/ContextType ' ;
6- import { Document , DocumentType } from '../document/Document' ;
7- import { detectDocumentType } from '../document/DocumentUtils' ;
2+ import { fileURLToPath } from 'url' ;
3+ import { TopLevelSection } from '../context/ContextType ' ;
4+ import { normalizeIntrinsicFunctionAndCondition } from '../context/semantic/Intrinsics ' ;
5+ import { DocumentType } from '../document/Document' ;
6+ import { parseDocumentContent } from '../document/DocumentUtils' ;
87import { S3Service } from '../services/S3Service' ;
98import { Artifact } from '../stacks/actions/StackActionRequestType' ;
109import { isS3Url , RESOURCE_EXPORTER_MAP } from './ResourceExporters' ;
1110
12- const INTRINSIC_FUNCTION_MAP = new Map < string , string > ( [
13- [ '!Ref' , IntrinsicFunction . Ref ] ,
14- [ '!GetAtt' , IntrinsicFunction . GetAtt ] ,
15- [ '!Join' , IntrinsicFunction . Join ] ,
16- [ '!Sub' , IntrinsicFunction . Sub ] ,
17- [ '!Base64' , IntrinsicFunction . Base64 ] ,
18- [ '!GetAZs' , IntrinsicFunction . GetAZs ] ,
19- [ '!ImportValue' , IntrinsicFunction . ImportValue ] ,
20- [ '!Select' , IntrinsicFunction . Select ] ,
21- [ '!Split' , IntrinsicFunction . Split ] ,
22- [ '!FindInMap' , IntrinsicFunction . FindInMap ] ,
23- [ '!Equals' , IntrinsicFunction . Equals ] ,
24- [ '!If' , IntrinsicFunction . If ] ,
25- [ '!Not' , IntrinsicFunction . Not ] ,
26- [ '!And' , IntrinsicFunction . And ] ,
27- [ '!Or' , IntrinsicFunction . Or ] ,
28- [ '!Cidr' , IntrinsicFunction . Cidr ] ,
29- [ '!Transform' , IntrinsicFunction . Transform ] ,
30- [ '!Condition' , 'Condition' ] ,
31- ] ) ;
32-
3311export type ArtifactWithProperty = {
3412 resourceType : string ;
3513 resourcePropertyDict : Record < string , unknown > ;
@@ -39,30 +17,14 @@ export type ArtifactWithProperty = {
3917
4018export class ArtifactExporter {
4119 private readonly templateDict : unknown ;
42- private readonly templateUri : string ;
43- private readonly templateType : DocumentType ;
4420
4521 constructor (
4622 private readonly s3Service : S3Service ,
47- private readonly document ?: Document ,
48- private readonly templateAbsPath ?: string ,
23+ private readonly templateType : DocumentType ,
24+ private readonly templateUri : string ,
25+ templateContent : string ,
4926 ) {
50- if ( this . document ) {
51- this . templateDict = this . document . getParsedDocumentContent ( ) ;
52- this . templateUri = this . document . uri ;
53- this . templateType = this . document . documentType ;
54- } else if ( this . templateAbsPath ) {
55- const content = readFileSync ( this . templateAbsPath , 'utf8' ) ;
56- this . templateUri = pathToFileURL ( this . templateAbsPath ) . href ;
57- this . templateType = detectDocumentType ( this . templateUri , content ) . type ;
58- if ( this . templateType === DocumentType . YAML ) {
59- this . templateDict = load ( content ) ;
60- } else {
61- this . templateDict = JSON . parse ( content ) ;
62- }
63- } else {
64- throw new Error ( 'Either document or absolutePath must be provided' ) ;
65- }
27+ this . templateDict = parseDocumentContent ( templateUri , templateContent ) ;
6628 }
6729
6830 private getResourceMapWithArtifact ( ) : Record < string , ArtifactWithProperty [ ] > {
@@ -148,7 +110,7 @@ export class ArtifactExporter {
148110 const objDict = obj as Record < string , unknown > ;
149111
150112 for ( const [ key , value ] of Object . entries ( objDict ) ) {
151- const newKey = INTRINSIC_FUNCTION_MAP . get ( key ) ?? key ;
113+ const newKey = normalizeIntrinsicFunctionAndCondition ( key ) ;
152114 result [ newKey ] = this . convertIntrinsicFunctionKeys ( value ) ;
153115 }
154116
0 commit comments