1
- import { readFileSync } from 'fs' ;
1
+ import { readdirSync , readFileSync , existsSync } from 'fs' ;
2
2
import * as path from 'path' ;
3
3
import { Match , Template } from '@aws-cdk/assertions' ;
4
4
import * as cloudfront from '@aws-cdk/aws-cloudfront' ;
@@ -1099,7 +1099,7 @@ test('Source.data() can be used to create a file with string contents', () => {
1099
1099
} ) ;
1100
1100
1101
1101
const result = app . synth ( ) ;
1102
- const content = readDataFile ( result , 'c5b1c01fc092abf1da35f6772e7c507e566aaa69404025c080ba074c69741755' , ' my/path.txt') ;
1102
+ const content = readDataFile ( result , 'my/path.txt' ) ;
1103
1103
expect ( content ) . toStrictEqual ( 'hello, world' ) ;
1104
1104
} ) ;
1105
1105
@@ -1121,7 +1121,7 @@ test('Source.jsonData() can be used to create a file with a JSON object', () =>
1121
1121
} ) ;
1122
1122
1123
1123
const result = app . synth ( ) ;
1124
- const obj = JSON . parse ( readDataFile ( result , '6a9e1763f42401799363d87d16b238c89bf75a56f2a3f67498a3224573062b0c' , ' app-config.json') ) ;
1124
+ const obj = JSON . parse ( readDataFile ( result , 'app-config.json' ) ) ;
1125
1125
expect ( obj ) . toStrictEqual ( {
1126
1126
foo : 'bar' ,
1127
1127
sub : {
@@ -1138,8 +1138,14 @@ test('Source.jsonData() can be used to create a file with a JSON object', () =>
1138
1138
} ) ;
1139
1139
1140
1140
1141
- function readDataFile ( casm : cxapi . CloudAssembly , assetId : string , filePath : string ) : string {
1142
- const asset = casm . stacks [ 0 ] . assets . find ( a => a . id === assetId ) ;
1143
- if ( ! asset ) { throw new Error ( 'Asset not found' ) ; }
1144
- return readFileSync ( path . join ( casm . directory , asset . path , filePath ) , 'utf-8' ) ;
1141
+ function readDataFile ( casm : cxapi . CloudAssembly , relativePath : string ) : string {
1142
+ const assetDirs = readdirSync ( casm . directory ) . filter ( f => f . startsWith ( 'asset.' ) ) ;
1143
+ for ( const dir of assetDirs ) {
1144
+ const candidate = path . join ( casm . directory , dir , relativePath ) ;
1145
+ if ( existsSync ( candidate ) ) {
1146
+ return readFileSync ( candidate , 'utf8' ) ;
1147
+ }
1148
+ }
1149
+
1150
+ throw new Error ( `File ${ relativePath } not found in any of the assets of the assembly` ) ;
1145
1151
}
0 commit comments