@@ -15,9 +15,6 @@ import { Logger } from '../logger.js';
1515 * Support for AWS SAM framework
1616 */
1717export class SamFramework implements IFramework {
18- protected samConfigFile = 'samconfig.toml' ;
19- protected samTemplateFile = 'template.yaml' ;
20-
2118 /**
2219 * Framework name
2320 */
@@ -29,28 +26,44 @@ export class SamFramework implements IFramework {
2926 * Can this class handle the current project
3027 * @returns
3128 */
32- public async canHandle ( ) : Promise < boolean > {
29+ public async canHandle ( config : LldConfigBase ) : Promise < boolean > {
30+ const { samConfigFile, samTemplateFile } = this . getConfigFiles ( config ) ;
31+
3332 try {
34- await fs . access ( path . resolve ( this . samConfigFile ) , constants . F_OK ) ;
33+ await fs . access ( samConfigFile , constants . F_OK ) ;
3534 } catch {
3635 Logger . verbose (
37- `[SAM] This is not a SAM framework project. ${ path . resolve ( this . samConfigFile ) } not found.` ,
36+ `[SAM] This is not a SAM framework project. ${ samConfigFile } not found.` ,
3837 ) ;
3938 return false ;
4039 }
4140
4241 try {
43- await fs . access ( path . resolve ( this . samTemplateFile ) , constants . F_OK ) ;
42+ await fs . access ( samTemplateFile , constants . F_OK ) ;
4443 } catch {
4544 Logger . verbose (
46- `[SAM] This is not a SAM framework project. ${ path . resolve ( this . samTemplateFile ) } not found.` ,
45+ `[SAM] This is not a SAM framework project. ${ samTemplateFile } not found.` ,
4746 ) ;
4847 return false ;
4948 }
5049
5150 return true ;
5251 }
5352
53+ /**
54+ * Get configuration files
55+ * @param config Configuration
56+ * @returns Configuration files
57+ */
58+ private getConfigFiles ( config : LldConfigBase ) {
59+ const samConfigFile = config . samConfigFile ?? 'samconfig.toml' ;
60+ const samTemplateFile = config . samTemplateFile ?? 'template.yaml' ;
61+ return {
62+ samConfigFile : path . resolve ( samConfigFile ) ,
63+ samTemplateFile : path . resolve ( samTemplateFile ) ,
64+ } ;
65+ }
66+
5467 /**
5568 * Get Lambda functions
5669 * @param config Configuration
@@ -63,24 +76,28 @@ export class SamFramework implements IFramework {
6376 role : config . role ,
6477 } ;
6578
79+ const { samConfigFile, samTemplateFile } = this . getConfigFiles ( config ) ;
80+
6681 const environment = config . configEnv ?? 'default' ;
6782
68- const samConfigContent = await fs . readFile (
69- path . resolve ( this . samConfigFile ) ,
70- 'utf-8' ,
71- ) ;
83+ const samConfigContent = await fs . readFile ( samConfigFile , 'utf-8' ) ;
84+
85+ let samConfig : any ;
86+ // is toml extension
87+ if ( samConfigFile . endsWith ( '.toml' ) ) {
88+ samConfig = toml . parse ( samConfigContent ) ;
89+ } else {
90+ samConfig = yaml . parse ( samConfigContent ) ;
91+ }
7292
73- const samConfig = toml . parse ( samConfigContent ) ;
7493 const stackName = samConfig [ environment ] ?. global ?. parameters ?. stack_name ;
7594
7695 if ( ! stackName ) {
77- throw new Error (
78- `Stack name not found in ${ path . resolve ( this . samConfigFile ) } ` ,
79- ) ;
96+ throw new Error ( `Stack name not found in ${ samConfigFile } ` ) ;
8097 }
8198
8299 const samTemplateContent = await fs . readFile (
83- path . resolve ( this . samTemplateFile ) ,
100+ path . resolve ( samTemplateFile ) ,
84101 'utf-8' ,
85102 ) ;
86103 const template = yaml . parse ( samTemplateContent ) ;
0 commit comments