1- import ts , { ObjectLiteralElementLike } from 'typescript' ;
1+ import ts , { ObjectLiteralElementLike , VariableDeclaration , VariableStatement } from 'typescript' ;
22import { EnvironmentResponse , Runtime } from '@aws-sdk/client-lambda' ;
33import { renderResourceTsFile } from '../resource/resource' ;
44
@@ -15,36 +15,60 @@ export interface FunctionDefinition {
1515
1616const factory = ts . factory ;
1717
18- const createParameter = ( name : string , value : ts . LiteralExpression | ts . ObjectLiteralExpression ) : ts . PropertyAssignment =>
19- factory . createPropertyAssignment ( factory . createIdentifier ( name ) , value ) ;
18+ const amplifyGen1EnvName = 'AMPLIFY_GEN_1_ENV_NAME' ;
19+
20+ const createParameter = (
21+ name : string ,
22+ value : ts . LiteralExpression | ts . ObjectLiteralExpression | ts . TemplateExpression ,
23+ ) : ts . PropertyAssignment => factory . createPropertyAssignment ( factory . createIdentifier ( name ) , value ) ;
24+
25+ const createVariableStatement = ( variableDeclaration : VariableDeclaration ) : VariableStatement => {
26+ return factory . createVariableStatement ( [ ] , factory . createVariableDeclarationList ( [ variableDeclaration ] , ts . NodeFlags . Const ) ) ;
27+ } ;
28+
29+ const createTemplateLiteral = ( templateHead : string , templateSpan : string , templateTail : string ) => {
30+ return factory . createTemplateExpression ( factory . createTemplateHead ( templateHead ) , [
31+ factory . createTemplateSpan ( factory . createIdentifier ( templateSpan ) , factory . createTemplateTail ( templateTail ) ) ,
32+ ] ) ;
33+ } ;
2034
2135export function renderFunctions ( definition : FunctionDefinition , appId ?: string , backendEnvironmentName ?: string | undefined ) {
22- const groupsComment : ( ts . CallExpression | ts . JSDoc ) [ ] = [ ] ;
36+ const postImportStatements = [ ] ;
2337 const namedImports : Record < string , Set < string > > = { '@aws-amplify/backend' : new Set ( ) } ;
2438 namedImports [ '@aws-amplify/backend' ] . add ( 'defineFunction' ) ;
2539
26- groupsComment . push (
40+ postImportStatements . push (
2741 factory . createCallExpression ( factory . createIdentifier ( 'throw new Error' ) , undefined , [
2842 factory . createStringLiteral (
2943 `Source code for this function can be found in your Amplify Gen 1 Directory. See .amplify/migration/amplify/backend/function/${ definition . resourceName } /src` ,
3044 ) ,
3145 ] ) ,
3246 ) ;
3347
34- const defineFunctionProperty = createFunctionDefinition ( definition , groupsComment , namedImports , appId , backendEnvironmentName ) ;
48+ const defineFunctionProperty = createFunctionDefinition ( definition , postImportStatements , namedImports , appId , backendEnvironmentName ) ;
49+
50+ const amplifyGen1EnvStatement = createVariableStatement (
51+ factory . createVariableDeclaration (
52+ amplifyGen1EnvName ,
53+ undefined ,
54+ undefined ,
55+ factory . createIdentifier ( 'process.env.AMPLIFY_GEN_1_ENV_NAME ?? "sandbox"' ) ,
56+ ) ,
57+ ) ;
58+ postImportStatements . push ( amplifyGen1EnvStatement ) ;
3559
3660 return renderResourceTsFile ( {
3761 exportedVariableName : factory . createIdentifier ( definition ?. resourceName || 'sayHello' ) ,
3862 functionCallParameter : factory . createObjectLiteralExpression ( defineFunctionProperty , true ) ,
3963 backendFunctionConstruct : 'defineFunction' ,
4064 additionalImportedBackendIdentifiers : namedImports ,
41- postImportStatements : groupsComment ,
65+ postImportStatements,
4266 } ) ;
4367}
4468
4569export function createFunctionDefinition (
4670 definition ?: FunctionDefinition ,
47- groupsComment ?: ( ts . CallExpression | ts . JSDoc ) [ ] ,
71+ postImportStatements ?: ( ts . CallExpression | ts . JSDoc ) [ ] ,
4872 namedImports ?: Record < string , Set < string > > ,
4973 appId ?: string ,
5074 backendEnvironmentName ?: string ,
@@ -55,7 +79,12 @@ export function createFunctionDefinition(
5579 defineFunctionProperties . push ( createParameter ( 'entry' , factory . createStringLiteral ( './handler.ts' ) ) ) ;
5680 }
5781 if ( definition ?. name ) {
58- defineFunctionProperties . push ( createParameter ( 'name' , factory . createStringLiteral ( definition . name ) ) ) ;
82+ const splitFuncName = definition . name . split ( '-' ) ;
83+ const funcNameWithoutBackendEnvName = splitFuncName . slice ( 0 , - 1 ) . join ( '-' ) ;
84+
85+ const funcNameAssignment = createTemplateLiteral ( `${ funcNameWithoutBackendEnvName } -` , amplifyGen1EnvName , '' ) ;
86+
87+ defineFunctionProperties . push ( createParameter ( 'name' , funcNameAssignment ) ) ;
5988 }
6089 if ( definition ?. timeoutSeconds ) {
6190 defineFunctionProperties . push ( createParameter ( 'timeoutSeconds' , factory . createNumericLiteral ( definition . timeoutSeconds ) ) ) ;
@@ -71,7 +100,7 @@ export function createFunctionDefinition(
71100 factory . createObjectLiteralExpression (
72101 Object . entries ( definition . environment . Variables ) . map ( ( [ key , value ] ) => {
73102 if ( key == 'API_KEY' && value . startsWith ( `/amplify/${ appId } /${ backendEnvironmentName } ` ) ) {
74- groupsComment ?. push (
103+ postImportStatements ?. push (
75104 factory . createCallExpression ( factory . createIdentifier ( 'throw new Error' ) , undefined , [
76105 // eslint-disable-next-line spellcheck/spell-checker
77106 factory . createStringLiteral ( 'Secrets need to be reset, use `npx ampx sandbox secret set API_KEY` to set the value' ) ,
@@ -87,6 +116,9 @@ export function createFunctionDefinition(
87116 key ,
88117 factory . createCallExpression ( factory . createIdentifier ( 'secret' ) , undefined , [ factory . createStringLiteral ( 'API_KEY' ) ] ) ,
89118 ) ;
119+ } else if ( key == 'ENV' ) {
120+ const envNameAssignment = createTemplateLiteral ( '' , amplifyGen1EnvName , '' ) ;
121+ return createParameter ( key , envNameAssignment ) ;
90122 }
91123
92124 return createParameter ( key , factory . createStringLiteral ( value ) ) ;
0 commit comments